1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/examples/Common/PEP241/Resources/scripts/sarissa.js Wed Oct 12 17:15:40 2005 +0000
1.3 @@ -0,0 +1,616 @@
1.4 +/**
1.5 + * ====================================================================
1.6 + * About
1.7 + * ====================================================================
1.8 + * Sarissa cross browser XML library
1.9 + * @version 0.9.6
1.10 + * @author: Manos Batsis, mailto: mbatsis at users full stop sourceforge full stop net
1.11 + *
1.12 + * Sarissa is an ECMAScript library acting as a cross-browser wrapper for native XML APIs.
1.13 + * The library supports Gecko based browsers like Mozilla and Firefox,
1.14 + * Internet Explorer (5.5+ with MSXML3.0+) and, last but not least, KHTML based browsers like
1.15 + * Konqueror and Safari.
1.16 + *
1.17 + * ====================================================================
1.18 + * Licence
1.19 + * ====================================================================
1.20 + * This program is free software; you can redistribute it and/or modify
1.21 + * it under the terms of the GNU General Public License version 2 or
1.22 + * the GNU Lesser General Public License version 2.1 as published by
1.23 + * the Free Software Foundation (your choice of the two).
1.24 + *
1.25 + * This program is distributed in the hope that it will be useful,
1.26 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.27 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.28 + * GNU General Public License or GNU Lesser General Public License for more details.
1.29 + *
1.30 + * You should have received a copy of the GNU General Public License
1.31 + * or GNU Lesser General Public License along with this program; if not,
1.32 + * write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1.33 + * or visit http://www.gnu.org
1.34 + *
1.35 + */
1.36 +/**
1.37 + * <p>Sarissa is a utility class. Provides static methods for DOMDocument and
1.38 + * XMLHTTP objects, DOM Node serializatrion to XML strings and other goodies.</p>
1.39 + * @constructor
1.40 + */
1.41 +function Sarissa(){};
1.42 +/** @private */
1.43 +Sarissa.PARSED_OK = "Document contains no parsing errors";
1.44 +/**
1.45 + * Tells you whether transformNode and transformNodeToObject are available. This functionality
1.46 + * is contained in sarissa_ieemu_xslt.js and is deprecated. If you want to control XSLT transformations
1.47 + * use the XSLTProcessor
1.48 + * @deprecated
1.49 + * @type boolean
1.50 + */
1.51 +Sarissa.IS_ENABLED_TRANSFORM_NODE = false;
1.52 +/**
1.53 + * tells you whether XMLHttpRequest (or equivalent) is available
1.54 + * @type boolean
1.55 + */
1.56 +Sarissa.IS_ENABLED_XMLHTTP = false;
1.57 +/**
1.58 + * tells you whether selectNodes/selectSingleNode is available
1.59 + * @type boolean
1.60 + */
1.61 +Sarissa.IS_ENABLED_SELECT_NODES = false;
1.62 +var _sarissa_iNsCounter = 0;
1.63 +var _SARISSA_IEPREFIX4XSLPARAM = "";
1.64 +var _SARISSA_HAS_DOM_IMPLEMENTATION = document.implementation && true;
1.65 +var _SARISSA_HAS_DOM_CREATE_DOCUMENT = _SARISSA_HAS_DOM_IMPLEMENTATION && document.implementation.createDocument;
1.66 +var _SARISSA_HAS_DOM_FEATURE = _SARISSA_HAS_DOM_IMPLEMENTATION && document.implementation.hasFeature;
1.67 +var _SARISSA_IS_MOZ = _SARISSA_HAS_DOM_CREATE_DOCUMENT && _SARISSA_HAS_DOM_FEATURE;
1.68 +var _SARISSA_IS_SAFARI = navigator.userAgent.toLowerCase().indexOf("applewebkit") != -1;
1.69 +var _SARISSA_IS_IE = document.all && window.ActiveXObject && navigator.userAgent.toLowerCase().indexOf("msie") > -1 && navigator.userAgent.toLowerCase().indexOf("opera") == -1;
1.70 +
1.71 +if(!window.Node || !window.Node.ELEMENT_NODE){
1.72 + var Node = {ELEMENT_NODE: 1, ATTRIBUTE_NODE: 2, TEXT_NODE: 3, CDATA_SECTION_NODE: 4, ENTITY_REFERENCE_NODE: 5, ENTITY_NODE: 6, PROCESSING_INSTRUCTION_NODE: 7, COMMENT_NODE: 8, DOCUMENT_NODE: 9, DOCUMENT_TYPE_NODE: 10, DOCUMENT_FRAGMENT_NODE: 11, NOTATION_NODE: 12};
1.73 +};
1.74 +
1.75 +// IE initialization
1.76 +if(_SARISSA_IS_IE){
1.77 + // for XSLT parameter names, prefix needed by IE
1.78 + _SARISSA_IEPREFIX4XSLPARAM = "xsl:";
1.79 + // used to store the most recent ProgID available out of the above
1.80 + var _SARISSA_DOM_PROGID = "";
1.81 + var _SARISSA_XMLHTTP_PROGID = "";
1.82 + /**
1.83 + * Called when the Sarissa_xx.js file is parsed, to pick most recent
1.84 + * ProgIDs for IE, then gets destroyed.
1.85 + * @param idList an array of MSXML PROGIDs from which the most recent will be picked for a given object
1.86 + * @param enabledList an array of arrays where each array has two items; the index of the PROGID for which a certain feature is enabled
1.87 + */
1.88 + pickRecentProgID = function (idList, enabledList){
1.89 + // found progID flag
1.90 + var bFound = false;
1.91 + for(var i=0; i < idList.length && !bFound; i++){
1.92 + try{
1.93 + var oDoc = new ActiveXObject(idList[i]);
1.94 + o2Store = idList[i];
1.95 + bFound = true;
1.96 + for(var j=0;j<enabledList.length;j++)
1.97 + if(i <= enabledList[j][1])
1.98 + Sarissa["IS_ENABLED_"+enabledList[j][0]] = true;
1.99 + }catch (objException){
1.100 + // trap; try next progID
1.101 + };
1.102 + };
1.103 + if (!bFound)
1.104 + throw "Could not retreive a valid progID of Class: " + idList[idList.length-1]+". (original exception: "+e+")";
1.105 + idList = null;
1.106 + return o2Store;
1.107 + };
1.108 + // pick best available MSXML progIDs
1.109 + _SARISSA_DOM_PROGID = pickRecentProgID(["Msxml2.DOMDocument.5.0", "Msxml2.DOMDocument.4.0", "Msxml2.DOMDocument.3.0", "MSXML2.DOMDocument", "MSXML.DOMDocument", "Microsoft.XMLDOM"], [["SELECT_NODES", 2],["TRANSFORM_NODE", 2]]);
1.110 + _SARISSA_XMLHTTP_PROGID = pickRecentProgID(["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"], [["XMLHTTP", 4]]);
1.111 + _SARISSA_THREADEDDOM_PROGID = pickRecentProgID(["Msxml2.FreeThreadedDOMDocument.5.0", "MSXML2.FreeThreadedDOMDocument.4.0", "MSXML2.FreeThreadedDOMDocument.3.0"]);
1.112 + _SARISSA_XSLTEMPLATE_PROGID = pickRecentProgID(["Msxml2.XSLTemplate.5.0", "Msxml2.XSLTemplate.4.0", "MSXML2.XSLTemplate.3.0"], [["XSLTPROC", 2]]);
1.113 + // we dont need this anymore
1.114 + pickRecentProgID = null;
1.115 + //============================================
1.116 + // Factory methods (IE)
1.117 + //============================================
1.118 + // see non-IE version
1.119 + Sarissa.getDomDocument = function(sUri, sName){
1.120 + var oDoc = new ActiveXObject(_SARISSA_DOM_PROGID);
1.121 + // if a root tag name was provided, we need to load it in the DOM
1.122 + // object
1.123 + if (sName){
1.124 + // if needed, create an artifical namespace prefix the way Moz
1.125 + // does
1.126 + if (sUri){
1.127 + oDoc.loadXML("<a" + _sarissa_iNsCounter + ":" + sName + " xmlns:a" + _sarissa_iNsCounter + "=\"" + sUri + "\" />");
1.128 + // don't use the same prefix again
1.129 + ++_sarissa_iNsCounter;
1.130 + }
1.131 + else
1.132 + oDoc.loadXML("<" + sName + "/>");
1.133 + };
1.134 + return oDoc;
1.135 + };
1.136 + // see non-IE version
1.137 + Sarissa.getParseErrorText = function (oDoc) {
1.138 + var parseErrorText = Sarissa.PARSED_OK;
1.139 + if(oDoc.parseError != 0){
1.140 + parseErrorText = "XML Parsing Error: " + oDoc.parseError.reason +
1.141 + "\nLocation: " + oDoc.parseError.url +
1.142 + "\nLine Number " + oDoc.parseError.line + ", Column " +
1.143 + oDoc.parseError.linepos +
1.144 + ":\n" + oDoc.parseError.srcText +
1.145 + "\n";
1.146 + for(var i = 0; i < oDoc.parseError.linepos;i++){
1.147 + parseErrorText += "-";
1.148 + };
1.149 + parseErrorText += "^\n";
1.150 + };
1.151 + return parseErrorText;
1.152 + };
1.153 + // see non-IE version
1.154 + Sarissa.setXpathNamespaces = function(oDoc, sNsSet) {
1.155 + oDoc.setProperty("SelectionLanguage", "XPath");
1.156 + oDoc.setProperty("SelectionNamespaces", sNsSet);
1.157 + };
1.158 + /**
1.159 + * Basic implementation of Mozilla's XSLTProcessor for IE.
1.160 + * Reuses the same XSLT stylesheet for multiple transforms
1.161 + * @constructor
1.162 + */
1.163 + XSLTProcessor = function(){
1.164 + this.template = new ActiveXObject(_SARISSA_XSLTEMPLATE_PROGID);
1.165 + this.processor = null;
1.166 + };
1.167 + /**
1.168 + * Impoprts the given XSLT DOM and compiles it to a reusable transform
1.169 + * @argument xslDoc The XSLT DOMDocument to import
1.170 + */
1.171 + XSLTProcessor.prototype.importStylesheet = function(xslDoc){
1.172 + // convert stylesheet to free threaded
1.173 + var converted = new ActiveXObject(_SARISSA_THREADEDDOM_PROGID);
1.174 + converted.loadXML(xslDoc.xml);
1.175 + this.template.stylesheet = converted;
1.176 + this.processor = this.template.createProcessor();
1.177 + // (re)set default param values
1.178 + this.paramsSet = new Array();
1.179 + };
1.180 + /**
1.181 + * Transform the given XML DOM
1.182 + * @argument sourceDoc The XML DOMDocument to transform
1.183 + * @return The transformation result as a DOM Document
1.184 + */
1.185 + XSLTProcessor.prototype.transformToDocument = function(sourceDoc){
1.186 + this.processor.input = sourceDoc;
1.187 + var outDoc = new ActiveXObject(_SARISSA_DOM_PROGID);
1.188 + this.processor.output = outDoc;
1.189 + this.processor.transform();
1.190 + return outDoc;
1.191 + };
1.192 + /**
1.193 + * Not sure if this works in IE. Maybe this will allow non-well-formed
1.194 + * transformation results (i.e. with no single root element)
1.195 + * @argument sourceDoc The XML DOMDocument to transform
1.196 + * @return The transformation result as a DOM Fragment
1.197 + */
1.198 + XSLTProcessor.prototype.transformToFragment = function(sourceDoc, ownerDocument){
1.199 + return this.transformToDocument(sourceDoc);
1.200 + };
1.201 + /**
1.202 + * Set global XSLT parameter of the imported stylesheet
1.203 + * @argument nsURI The parameter namespace URI
1.204 + * @argument name The parameter base name
1.205 + * @argument value The new parameter value
1.206 + */
1.207 + XSLTProcessor.prototype.setParameter = function(nsURI, name, value){
1.208 + /* nsURI is optional but cannot be null */
1.209 + if(nsURI){
1.210 + this.processor.addParameter(name, value, nsURI);
1.211 + }else{
1.212 + this.processor.addParameter(name, value);
1.213 + };
1.214 + /* update updated params for getParameter */
1.215 + if(!this.paramsSet[""+nsURI]){
1.216 + this.paramsSet[""+nsURI] = new Array();
1.217 + };
1.218 + this.paramsSet[""+nsURI][name] = value;
1.219 + };
1.220 + /**
1.221 + * Gets a parameter if previously set by setParameter. Returns null
1.222 + * otherwise
1.223 + * @argument name The parameter base name
1.224 + * @argument value The new parameter value
1.225 + * @return The parameter value if reviously set by setParameter, null otherwise
1.226 + */
1.227 + XSLTProcessor.prototype.getParameter = function(nsURI, name){
1.228 + if(this.paramsSet[""+nsURI] && this.paramsSet[""+nsURI][name])
1.229 + return this.paramsSet[""+nsURI][name];
1.230 + else
1.231 + return null;
1.232 + };
1.233 +}
1.234 +else{ /* end IE initialization, try to deal with real browsers now ;-) */
1.235 + if(_SARISSA_HAS_DOM_CREATE_DOCUMENT){
1.236 + if(window.XMLDocument){
1.237 + /**
1.238 + * <p>Emulate IE's onreadystatechange attribute</p>
1.239 + */
1.240 + XMLDocument.prototype.onreadystatechange = null;
1.241 + /**
1.242 + * <p>Emulates IE's readyState property, which always gives an integer from 0 to 4:</p>
1.243 + * <ul><li>1 == LOADING,</li>
1.244 + * <li>2 == LOADED,</li>
1.245 + * <li>3 == INTERACTIVE,</li>
1.246 + * <li>4 == COMPLETED</li></ul>
1.247 + */
1.248 + XMLDocument.prototype.readyState = 0;
1.249 + /**
1.250 + * <p>Emulate IE's parseError attribute</p>
1.251 + */
1.252 + XMLDocument.prototype.parseError = 0;
1.253 +
1.254 + // NOTE: setting async to false will only work with documents
1.255 + // called over HTTP (meaning a server), not the local file system,
1.256 + // unless you are using Moz 1.4+.
1.257 + // BTW the try>catch block is for 1.4; I haven't found a way to check if
1.258 + // the property is implemented without
1.259 + // causing an error and I dont want to use user agent stuff for that...
1.260 + var _SARISSA_SYNC_NON_IMPLEMENTED = false;
1.261 + try{
1.262 + /**
1.263 + * <p>Emulates IE's async property for Moz versions prior to 1.4.
1.264 + * It controls whether loading of remote XML files works
1.265 + * synchronously or asynchronously.</p>
1.266 + */
1.267 + XMLDocument.prototype.async = true;
1.268 + _SARISSA_SYNC_NON_IMPLEMENTED = true;
1.269 + }catch(e){/* trap */};
1.270 + /**
1.271 + * <p>Keeps a handle to the original load() method. Internal use and only
1.272 + * if Mozilla version is lower than 1.4</p>
1.273 + * @private
1.274 + */
1.275 + XMLDocument.prototype._sarissa_load = XMLDocument.prototype.load;
1.276 +
1.277 + /**
1.278 + * <p>Overrides the original load method to provide synchronous loading for
1.279 + * Mozilla versions prior to 1.4, using an XMLHttpRequest object (if
1.280 + * async is set to false)</p>
1.281 + * @returns the DOM Object as it was before the load() call (may be empty)
1.282 + */
1.283 + XMLDocument.prototype.load = function(sURI) {
1.284 + var oDoc = document.implementation.createDocument("", "", null);
1.285 + Sarissa.copyChildNodes(this, oDoc);
1.286 + this.parseError = 0;
1.287 + Sarissa.__setReadyState__(this, 1);
1.288 + try {
1.289 + if(this.async == false && _SARISSA_SYNC_NON_IMPLEMENTED) {
1.290 + var tmp = new XMLHttpRequest();
1.291 + tmp.open("GET", sURI, false);
1.292 + tmp.send(null);
1.293 + Sarissa.__setReadyState__(this, 2);
1.294 + Sarissa.copyChildNodes(tmp.responseXML, this);
1.295 + Sarissa.__setReadyState__(this, 3);
1.296 + }
1.297 + else {
1.298 + this._sarissa_load(sURI);
1.299 + };
1.300 + }
1.301 + catch (objException) {
1.302 + this.parseError = -1;
1.303 + }
1.304 + finally {
1.305 + if(this.async == false){
1.306 + Sarissa.__handleLoad__(this);
1.307 + };
1.308 + };
1.309 + return oDoc;
1.310 + };
1.311 + };//if(window.XMLDocument)
1.312 +
1.313 + /**
1.314 + * <p>Ensures the document was loaded correctly, otherwise sets the
1.315 + * parseError to -1 to indicate something went wrong. Internal use</p>
1.316 + * @private
1.317 + */
1.318 + Sarissa.__handleLoad__ = function(oDoc){
1.319 + if (!oDoc.documentElement || oDoc.documentElement.tagName == "parsererror")
1.320 + oDoc.parseError = -1;
1.321 + Sarissa.__setReadyState__(oDoc, 4);
1.322 + };
1.323 +
1.324 + /**
1.325 + * <p>Attached by an event handler to the load event. Internal use.</p>
1.326 + * @private
1.327 + */
1.328 + _sarissa_XMLDocument_onload = function(){
1.329 + Sarissa.__handleLoad__(this);
1.330 + };
1.331 +
1.332 + /**
1.333 + * <p>Sets the readyState property of the given DOM Document object.
1.334 + * Internal use.</p>
1.335 + * @private
1.336 + * @argument oDoc the DOM Document object to fire the
1.337 + * readystatechange event
1.338 + * @argument iReadyState the number to change the readystate property to
1.339 + */
1.340 + Sarissa.__setReadyState__ = function(oDoc, iReadyState){
1.341 + oDoc.readyState = iReadyState;
1.342 + if (oDoc.onreadystatechange != null && typeof oDoc.onreadystatechange == "function")
1.343 + oDoc.onreadystatechange();
1.344 + };
1.345 + /**
1.346 + * <p>Factory method to obtain a new DOM Document object</p>
1.347 + * @argument sUri the namespace of the root node (if any)
1.348 + * @argument sUri the local name of the root node (if any)
1.349 + * @returns a new DOM Document
1.350 + */
1.351 + Sarissa.getDomDocument = function(sUri, sName){
1.352 + var oDoc = document.implementation.createDocument(sUri?sUri:"", sName?sName:"", null);
1.353 + oDoc.addEventListener("load", _sarissa_XMLDocument_onload, false);
1.354 + return oDoc;
1.355 + };
1.356 + };//if(_SARISSA_HAS_DOM_CREATE_DOCUMENT)
1.357 +};
1.358 +//==========================================
1.359 +// Common stuff
1.360 +//==========================================
1.361 +if(!window.DOMParser){
1.362 + /**
1.363 + * DOMParser is a utility class, used to construct DOMDocuments from XML strings
1.364 + * @constructor
1.365 + */
1.366 + DOMParser = function() {
1.367 + };
1.368 + /**
1.369 + * Construct a new DOM Document from the given XMLstring
1.370 + * @param sXml the given XML string
1.371 + * @param contentType the content type of the document the given string represents (one of text/xml, application/xml, application/xhtml+xml).
1.372 + * @return a new DOM Document from the given XML string
1.373 + */
1.374 + DOMParser.prototype.parseFromString = function(sXml, contentType){
1.375 + var doc = Sarissa.getDomDocument();
1.376 + doc.loadXML(sXml);
1.377 + return doc;
1.378 + };
1.379 +
1.380 +};
1.381 +
1.382 +if(window.XMLHttpRequest){
1.383 + Sarissa.IS_ENABLED_XMLHTTP = true;
1.384 +}
1.385 +else if(_SARISSA_IS_IE){
1.386 + /**
1.387 + * Emulate XMLHttpRequest
1.388 + * @constructor
1.389 + */
1.390 + XMLHttpRequest = function() {
1.391 + return new ActiveXObject(_SARISSA_XMLHTTP_PROGID);
1.392 + };
1.393 + Sarissa.IS_ENABLED_XMLHTTP = true;
1.394 +};
1.395 +
1.396 +if(!window.document.importNode && _SARISSA_IS_IE){
1.397 + try{
1.398 + /**
1.399 + * Implements importNode for the current window document in IE using innerHTML.
1.400 + * Testing showed that DOM was multiple times slower than innerHTML for this,
1.401 + * sorry folks. If you encounter trouble (who knows what IE does behind innerHTML)
1.402 + * please gimme a call.
1.403 + * @param oNode the Node to import
1.404 + * @param bChildren whether to include the children of oNode
1.405 + * @returns the imported node for further use
1.406 + */
1.407 + window.document.importNode = function(oNode, bChildren){
1.408 + var importNode = document.createElement("div");
1.409 + if(bChildren)
1.410 + importNode.innerHTML = Sarissa.serialize(oNode);
1.411 + else
1.412 + importNode.innerHTML = Sarissa.serialize(oNode.cloneNode(false));
1.413 + return importNode.firstChild;
1.414 + };
1.415 + }catch(e){};
1.416 +};
1.417 +if(!Sarissa.getParseErrorText){
1.418 + /**
1.419 + * <p>Returns a human readable description of the parsing error. Usefull
1.420 + * for debugging. Tip: append the returned error string in a <pre>
1.421 + * element if you want to render it.</p>
1.422 + * <p>Many thanks to Christian Stocker for the initial patch.</p>
1.423 + * @argument oDoc The target DOM document
1.424 + * @returns The parsing error description of the target Document in
1.425 + * human readable form (preformated text)
1.426 + */
1.427 + Sarissa.getParseErrorText = function (oDoc){
1.428 + var parseErrorText = Sarissa.PARSED_OK;
1.429 + if(oDoc.parseError != 0){
1.430 + /*moz*/
1.431 + if(oDoc.documentElement.tagName == "parsererror"){
1.432 + parseErrorText = oDoc.documentElement.firstChild.data;
1.433 + parseErrorText += "\n" + oDoc.documentElement.firstChild.nextSibling.firstChild.data;
1.434 + }/*konq*/
1.435 + else if(oDoc.documentElement.tagName == "html"){
1.436 + parseErrorText = Sarissa.getText(oDoc.documentElement.getElementsByTagName("h1")[0], false) + "\n";
1.437 + parseErrorText += Sarissa.getText(oDoc.documentElement.getElementsByTagName("body")[0], false) + "\n";
1.438 + parseErrorText += Sarissa.getText(oDoc.documentElement.getElementsByTagName("pre")[0], false);
1.439 + };
1.440 + };
1.441 + return parseErrorText;
1.442 + };
1.443 +};
1.444 +Sarissa.getText = function(oNode, deep){
1.445 + var s = "";
1.446 + var nodes = oNode.childNodes;
1.447 + for(var i=0; i < nodes.length; i++){
1.448 + var node = nodes[i];
1.449 + var nodeType = node.nodeType;
1.450 + if(nodeType == Node.TEXT_NODE || nodeType == Node.CDATA_SECTION_NODE){
1.451 + s += node.data;
1.452 + }
1.453 + else if(deep == true
1.454 + && (nodeType == Node.ELEMENT_NODE
1.455 + || nodeType == Node.DOCUMENT_NODE
1.456 + || nodeType == Node.DOCUMENT_FRAGMENT_NODE)){
1.457 + s += Sarissa.getText(node, true);
1.458 + };
1.459 + };
1.460 + return s;
1.461 +};
1.462 +if(window.XMLSerializer){
1.463 + /**
1.464 + * <p>Factory method to obtain the serialization of a DOM Node</p>
1.465 + * @returns the serialized Node as an XML string
1.466 + */
1.467 + Sarissa.serialize = function(oDoc){
1.468 + return (new XMLSerializer()).serializeToString(oDoc);
1.469 + };
1.470 +}else{
1.471 + if((Sarissa.getDomDocument("","foo", null)).xml){
1.472 + // see non-IE version
1.473 + Sarissa.serialize = function(oDoc) {
1.474 + // TODO: check for HTML document and return innerHTML instead
1.475 + return oDoc.xml;
1.476 + };
1.477 + /**
1.478 + * Utility class to serialize DOM Node objects to XML strings
1.479 + * @constructor
1.480 + */
1.481 + XMLSerializer = function(){};
1.482 + /**
1.483 + * Serialize the given DOM Node to an XML string
1.484 + * @param oNode the DOM Node to serialize
1.485 + */
1.486 + XMLSerializer.prototype.serializeToString = function(oNode) {
1.487 + return oNode.xml;
1.488 + };
1.489 + };
1.490 +};
1.491 +
1.492 +/**
1.493 + * strips tags from a markup string
1.494 + */
1.495 +Sarissa.stripTags = function (s) {
1.496 + return s.replace(/<[^>]+>/g,"");
1.497 +};
1.498 +/**
1.499 + * <p>Deletes all child nodes of the given node</p>
1.500 + * @argument oNode the Node to empty
1.501 + */
1.502 +Sarissa.clearChildNodes = function(oNode) {
1.503 + while(oNode.hasChildNodes()){
1.504 + oNode.removeChild(oNode.firstChild);
1.505 + };
1.506 +};
1.507 +/**
1.508 + * <p> Copies the childNodes of nodeFrom to nodeTo</p>
1.509 + * <p> <b>Note:</b> The second object's original content is deleted before
1.510 + * the copy operation, unless you supply a true third parameter</p>
1.511 + * @argument nodeFrom the Node to copy the childNodes from
1.512 + * @argument nodeTo the Node to copy the childNodes to
1.513 + * @argument bPreserveExisting whether to preserve the original content of nodeTo, default is false
1.514 + */
1.515 +Sarissa.copyChildNodes = function(nodeFrom, nodeTo, bPreserveExisting) {
1.516 + if(!bPreserveExisting){
1.517 + Sarissa.clearChildNodes(nodeTo);
1.518 + };
1.519 + var ownerDoc = nodeTo.nodeType == Node.DOCUMENT_NODE ? nodeTo : nodeTo.ownerDocument;
1.520 + var nodes = nodeFrom.childNodes;
1.521 + if(ownerDoc.importNode && (!_SARISSA_IS_IE)) {
1.522 + for(var i=0;i < nodes.length;i++) {
1.523 + nodeTo.appendChild(ownerDoc.importNode(nodes[i], true));
1.524 + };
1.525 + }
1.526 + else{
1.527 + for(var i=0;i < nodes.length;i++) {
1.528 + nodeTo.appendChild(nodes[i].cloneNode(true));
1.529 + };
1.530 + };
1.531 +};
1.532 +
1.533 +/**
1.534 + * <p> Moves the childNodes of nodeFrom to nodeTo</p>
1.535 + * <p> <b>Note:</b> The second object's original content is deleted before
1.536 + * the move operation, unless you supply a true third parameter</p>
1.537 + * @argument nodeFrom the Node to copy the childNodes from
1.538 + * @argument nodeTo the Node to copy the childNodes to
1.539 + * @argument bPreserveExisting whether to preserve the original content of nodeTo, default is false
1.540 + */
1.541 +Sarissa.moveChildNodes = function(nodeFrom, nodeTo, bPreserveExisting) {
1.542 + if(!bPreserveExisting){
1.543 + Sarissa.clearChildNodes(nodeTo);
1.544 + };
1.545 +
1.546 + var nodes = nodeFrom.childNodes;
1.547 + // if within the same doc, just move, else copy and delete
1.548 + if(nodeFrom.ownerDocument == nodeTo.ownerDocument){
1.549 + nodeTo.appendChild(nodes[i]);
1.550 + }else{
1.551 + var ownerDoc = nodeTo.nodeType == Node.DOCUMENT_NODE ? nodeTo : nodeTo.ownerDocument;
1.552 + if(ownerDoc.importNode && (!_SARISSA_IS_IE)) {
1.553 + for(var i=0;i < nodes.length;i++) {
1.554 + nodeTo.appendChild(ownerDoc.importNode(nodes[i], true));
1.555 + };
1.556 + }
1.557 + else{
1.558 + for(var i=0;i < nodes.length;i++) {
1.559 + nodeTo.appendChild(nodes[i].cloneNode(true));
1.560 + };
1.561 + };
1.562 + Sarissa.clearChildNodes(nodeFrom);
1.563 + };
1.564 +
1.565 +};
1.566 +
1.567 +/**
1.568 + * <p>Serialize any object to an XML string. All properties are serialized using the property name
1.569 + * as the XML element name. Array elements are rendered as <code>array-item</code> elements,
1.570 + * using their index/key as the value of the <code>key</code> attribute.</p>
1.571 + * @argument anyObject the object to serialize
1.572 + * @argument objectName a name for that object
1.573 + * @return the XML serializationj of the given object as a string
1.574 + */
1.575 +Sarissa.xmlize = function(anyObject, objectName, indentSpace){
1.576 + indentSpace = indentSpace?indentSpace:'';
1.577 + var s = indentSpace + '<' + objectName + '>';
1.578 + var isLeaf = false;
1.579 + if(!(anyObject instanceof Object) || anyObject instanceof Number || anyObject instanceof String
1.580 + || anyObject instanceof Boolean || anyObject instanceof Date){
1.581 + s += Sarissa.escape(""+anyObject);
1.582 + isLeaf = true;
1.583 + }else{
1.584 + s += "\n";
1.585 + var itemKey = '';
1.586 + var isArrayItem = anyObject instanceof Array;
1.587 + for(var name in anyObject){
1.588 + s += Sarissa.xmlize(anyObject[name], (isArrayItem?"array-item key=\""+name+"\"":name), indentSpace + " ");
1.589 + };
1.590 + s += indentSpace;
1.591 + };
1.592 + return s += (objectName.indexOf(' ')!=-1?"</array-item>\n":"</" + objectName + ">\n");
1.593 +};
1.594 +
1.595 +/**
1.596 + * Escape the given string chacters that correspond to the five predefined XML entities
1.597 + * @param sXml the string to escape
1.598 + */
1.599 +Sarissa.escape = function(sXml){
1.600 + return sXml.replace(/&/g, "&")
1.601 + .replace(/</g, "<")
1.602 + .replace(/>/g, ">")
1.603 + .replace(/"/g, """)
1.604 + .replace(/'/g, "'");
1.605 +};
1.606 +
1.607 +/**
1.608 + * Unescape the given string. This turns the occurences of the predefined XML
1.609 + * entities to become the characters they represent correspond to the five predefined XML entities
1.610 + * @param sXml the string to unescape
1.611 + */
1.612 +Sarissa.unescape = function(sXml){
1.613 + return sXml.replace(/'/g,"'")
1.614 + .replace(/"/g,"\"")
1.615 + .replace(/>/g,">")
1.616 + .replace(/</g,"<")
1.617 + .replace(/&/g,"&");
1.618 +};
1.619 +// EOF