| 1 | // SpryXML.js - version 0.4 - Spry Pre-Release 1.6 |
|---|
| 2 | // |
|---|
| 3 | // Copyright (c) 2007. Adobe Systems Incorporated. |
|---|
| 4 | // All rights reserved. |
|---|
| 5 | // |
|---|
| 6 | // Redistribution and use in source and binary forms, with or without |
|---|
| 7 | // modification, are permitted provided that the following conditions are met: |
|---|
| 8 | // |
|---|
| 9 | // * Redistributions of source code must retain the above copyright notice, |
|---|
| 10 | // this list of conditions and the following disclaimer. |
|---|
| 11 | // * Redistributions in binary form must reproduce the above copyright notice, |
|---|
| 12 | // this list of conditions and the following disclaimer in the documentation |
|---|
| 13 | // and/or other materials provided with the distribution. |
|---|
| 14 | // * Neither the name of Adobe Systems Incorporated nor the names of its |
|---|
| 15 | // contributors may be used to endorse or promote products derived from this |
|---|
| 16 | // software without specific prior written permission. |
|---|
| 17 | // |
|---|
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|---|
| 19 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 20 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|---|
| 21 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
|---|
| 22 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 23 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 24 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|---|
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|---|
| 26 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|---|
| 27 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|---|
| 28 | // POSSIBILITY OF SUCH DAMAGE. |
|---|
| 29 | |
|---|
| 30 | var Spry;if(!Spry)Spry={};if(!Spry.XML)Spry.XML={};if(!Spry.XML.Schema)Spry.XML.Schema={};Spry.XML.Schema.Node=function(nodeName) |
|---|
| 31 | {this.nodeName=nodeName;this.isAttribute=false;this.appearsMoreThanOnce=false;this.children=new Array;};Spry.XML.Schema.Node.prototype.toString=function(indentStr) |
|---|
| 32 | {if(!indentStr) |
|---|
| 33 | indentStr="";var str=indentStr+this.nodeName;if(this.appearsMoreThanOnce) |
|---|
| 34 | str+=" (+)";str+="\n";var newIndentStr=indentStr+" ";for(var $childName in this.children) |
|---|
| 35 | {var child=this.children[$childName];if(child.isAttribute) |
|---|
| 36 | str+=newIndentStr+child.nodeName+"\n";else |
|---|
| 37 | str+=child.toString(newIndentStr);} |
|---|
| 38 | return str;};Spry.XML.Schema.mapElementIntoSchemaNode=function(ele,schemaNode) |
|---|
| 39 | {if(!ele||!schemaNode) |
|---|
| 40 | return;var i=0;for(i=0;i<ele.attributes.length;i++) |
|---|
| 41 | {var attr=ele.attributes.item(i);if(attr&&attr.nodeType==2) |
|---|
| 42 | {var attrName="@"+attr.name;if(!schemaNode.children[attrName]) |
|---|
| 43 | {var attrObj=new Spry.XML.Schema.Node(attrName);attrObj.isAttribute=true;schemaNode.children[attrName]=attrObj;}}} |
|---|
| 44 | var child=ele.firstChild;var namesSeenSoFar=new Array;while(child) |
|---|
| 45 | {if(child.nodeType==1) |
|---|
| 46 | {var childSchemaNode=schemaNode.children[child.nodeName];if(!childSchemaNode) |
|---|
| 47 | {childSchemaNode=new Spry.XML.Schema.Node(child.nodeName);if(childSchemaNode) |
|---|
| 48 | schemaNode.children[child.nodeName]=childSchemaNode;} |
|---|
| 49 | if(childSchemaNode) |
|---|
| 50 | {if(namesSeenSoFar[childSchemaNode.nodeName]) |
|---|
| 51 | childSchemaNode.appearsMoreThanOnce=true;else |
|---|
| 52 | namesSeenSoFar[childSchemaNode.nodeName]=true;} |
|---|
| 53 | Spry.XML.Schema.mapElementIntoSchemaNode(child,childSchemaNode);} |
|---|
| 54 | child=child.nextSibling;}};Spry.XML.getSchemaForElement=function(ele) |
|---|
| 55 | {if(!ele) |
|---|
| 56 | return null;schemaNode=new Spry.XML.Schema.Node(ele.nodeName);Spry.XML.Schema.mapElementIntoSchemaNode(ele,schemaNode);return schemaNode;};Spry.XML.getSchema=function(xmlDoc) |
|---|
| 57 | {if(!xmlDoc) |
|---|
| 58 | return null;var node=xmlDoc.firstChild;while(node) |
|---|
| 59 | {if(node.nodeType==1) |
|---|
| 60 | break;node=node.nextSibling;} |
|---|
| 61 | return Spry.XML.getSchemaForElement(node);};Spry.XML.nodeHasValue=function(node) |
|---|
| 62 | {if(node) |
|---|
| 63 | {var child=node.firstChild;if(child&&child.nextSibling==null&&(child.nodeType==3||child.nodeType==4)) |
|---|
| 64 | return true;} |
|---|
| 65 | return false;};Spry.XML.XObject=function() |
|---|
| 66 | {};Spry.XML.XObject.prototype._value=function() |
|---|
| 67 | {var val=this["#text"];if(val!=undefined) |
|---|
| 68 | return val;return this["#cdata-section"];};Spry.XML.XObject.prototype._hasValue=function() |
|---|
| 69 | {return this._value()!=undefined;};Spry.XML.XObject.prototype._valueIsText=function() |
|---|
| 70 | {return this["#text"]!=undefined;};Spry.XML.XObject.prototype._valueIsCData=function() |
|---|
| 71 | {return this["#cdata-section"]!=undefined;};Spry.XML.XObject.prototype._propertyIsArray=function(prop) |
|---|
| 72 | {var val=this[prop];if(val==undefined) |
|---|
| 73 | return false;return(typeof val=="object"&&val.constructor==Array);};Spry.XML.XObject.prototype._getPropertyAsArray=function(prop) |
|---|
| 74 | {var arr=[];var val=this[prop];if(val!=undefined) |
|---|
| 75 | {if(typeof val=="object"&&val.constructor==Array) |
|---|
| 76 | return val;arr.push(val);} |
|---|
| 77 | return arr;};Spry.XML.XObject.prototype._getProperties=function() |
|---|
| 78 | {var props=[];for(var p in this) |
|---|
| 79 | {if(!/^_/.test(p)) |
|---|
| 80 | props.push(p);} |
|---|
| 81 | return props;};Spry.XML.nodeToObject=function(node) |
|---|
| 82 | {if(!node) |
|---|
| 83 | return null;var obj=new Spry.XML.XObject();for(var i=0;i<node.attributes.length;i++) |
|---|
| 84 | {var attr=node.attributes[i];var attrName="@"+attr.name;obj[attrName]=attr.value;} |
|---|
| 85 | var child;if(Spry.XML.nodeHasValue(node)) |
|---|
| 86 | {try |
|---|
| 87 | {child=node.firstChild;if(child.nodeType==3) |
|---|
| 88 | obj[child.nodeName]=Spry.Utils.encodeEntities(child.data);else if(child.nodeType==4) |
|---|
| 89 | obj[child.nodeName]=child.data;}catch(e){Spry.Debug.reportError("Spry.XML.nodeToObject() exception caught: "+e+"\n");}} |
|---|
| 90 | else |
|---|
| 91 | {child=node.firstChild;while(child) |
|---|
| 92 | {if(child.nodeType==1) |
|---|
| 93 | {var isArray=false;var tagName=child.nodeName;if(obj[tagName]) |
|---|
| 94 | {if(obj[tagName].constructor!=Array) |
|---|
| 95 | {var curValue=obj[tagName];obj[tagName]=new Array;obj[tagName].push(curValue);} |
|---|
| 96 | isArray=true;} |
|---|
| 97 | var childObj=Spry.XML.nodeToObject(child);if(isArray) |
|---|
| 98 | obj[tagName].push(childObj);else |
|---|
| 99 | obj[tagName]=childObj;} |
|---|
| 100 | child=child.nextSibling;}} |
|---|
| 101 | return obj;};Spry.XML.documentToObject=function(xmlDoc) |
|---|
| 102 | {var obj=null;if(xmlDoc&&xmlDoc.firstChild) |
|---|
| 103 | {var child=xmlDoc.firstChild;while(child) |
|---|
| 104 | {if(child.nodeType==1) |
|---|
| 105 | {obj=new Spry.XML.XObject();obj[child.nodeName]=Spry.XML.nodeToObject(child);break;} |
|---|
| 106 | child=child.nextSibling;}} |
|---|
| 107 | return obj;}; |
|---|