PageRenderTime 28ms CodeModel.GetById 1ms app.highlight 15ms RepoModel.GetById 3ms app.codeStats 0ms

/examples/jsonrpc/public/services/phpolait/jsolait/lib/xml.js

http://pyjamas.googlecode.com/
JavaScript | 180 lines | 180 code | 0 blank | 0 comment | 10 complexity | 3c0a909ae28f5ebf5dd7d874f8ef7c47 MD5 | raw file
  1Module("xml","1.1.2", function(mod){
  2mod.NoXMLParser=Class("NoXMLParser", mod.Exception, function(publ, supr){
  3publ.init=function(trace){
  4supr(this).init("Could not create an XML parser.", trace);
  5}
  6})
  7mod.ParsingFailed=Class("ParsingFailed", mod.Exception, function(publ, supr){
  8publ.init=function(xml,trace){
  9supr(this).init("Failed parsing XML document.",trace);
 10this.xml = xml;
 11}
 12publ.xml;
 13})
 14mod.parseXML=function(xml){
 15var obj=null;
 16var isMoz=false;
 17var isIE=false;
 18var isASV=false;
 19try{
 20var p=window.parseXML;
 21if(p==null){
 22throw "No ASV paseXML";
 23}
 24isASV=true;
 25}catch(e){
 26try{
 27obj = new DOMParser();
 28isMoz=true;
 29}catch(e){
 30try{
 31obj = new ActiveXObject("Msxml2.DomDocument.4.0"); 
 32isIE=true;
 33}catch(e){
 34try{
 35obj = new ActiveXObject("Msxml2.DomDocument"); 
 36isIE=true;
 37}catch(e){
 38try{
 39obj = new ActiveXObject("microsoft.XMLDOM"); 
 40isIE=true;
 41}catch(e){
 42throw new mod.NoXMLParser(e);
 43}
 44}
 45}
 46}
 47}
 48try{
 49if(isMoz){
 50obj = obj.parseFromString(xml, "text/xml");
 51return obj;
 52}else if(isIE){
 53obj.loadXML(xml);
 54return obj;
 55}else if(isASV){
 56return window.parseXML(xml, null);
 57}
 58}catch(e){
 59throw new mod.ParsingFailed(xml,e);
 60}
 61}
 62mod.importNode=function(importedNode, deep){
 63deep = (deep==null) ? true : deep;
 64var ELEMENT_NODE = 1;
 65var ATTRIBUTE_NODE = 2;
 66var TEXT_NODE = 3;
 67var CDATA_SECTION_NODE = 4;
 68var ENTITY_REFERENCE_NODE = 5;
 69var ENTITY_NODE = 6;
 70var PROCESSING_INSTRUCTION_NODE = 7;
 71var COMMENT_NODE = 8;
 72var DOCUMENT_NODE = 9;
 73var DOCUMENT_TYPE_NODE = 10;
 74var DOCUMENT_FRAGMENT_NODE = 11;
 75var NOTATION_NODE = 12;
 76var importChildren=function(srcNode, parent){
 77if(deep){
 78for(var i=0; i<srcNode.childNodes.length; i++){
 79var n=mod.importNode(srcNode.childNodes.item(i), true);
 80parent.appendChild(n);
 81}
 82}
 83}
 84var node=null;
 85switch(importedNode.nodeType){
 86case ATTRIBUTE_NODE:
 87node=document.createAttributeNS(importedNode.namespaceURI, importedNode.nodeName);
 88node.value=importedNode.value;
 89break;
 90case DOCUMENT_FRAGMENT_NODE:
 91node=document.createDocumentFragment();
 92importChildren(importedNode,node);
 93break;
 94case ELEMENT_NODE:
 95node=document.createElementNS(importedNode.namespaceURI, importedNode.tagName);
 96for(var i=0; i<importedNode.attributes.length; i++){
 97var attr=this.importNode(importedNode.attributes.item(i), deep);
 98node.setAttributeNodeNS(attr);
 99}
100importChildren(importedNode,node);
101break;
102case ENTITY_REFERENCE_NODE:
103node=importedNode;
104break;
105case PROCESSING_INSTRUCTION_NODE:
106node=document.createProcessingInstruction(importedNode.target, importedNode.data);
107break;
108case TEXT_NODE:
109case CDATA_SECTION_NODE:
110case COMMENT_NODE:
111node=document.createTextNode(importedNode.nodeValue);
112break;
113case DOCUMENT_NODE:
114case DOCUMENT_TYPE_NODE:
115case NOTATION_NODE:
116case ENTITY_NODE:
117throw "not supported in DOM2";
118break;
119}
120return node;
121}
122mod.node2XML = function(node){
123var ELEMENT_NODE = 1;
124var ATTRIBUTE_NODE = 2;
125var TEXT_NODE = 3;
126var CDATA_SECTION_NODE = 4;
127var ENTITY_REFERENCE_NODE = 5;
128var ENTITY_NODE = 6;
129var PROCESSING_INSTRUCTION_NODE = 7;
130var COMMENT_NODE = 8;
131var DOCUMENT_NODE = 9;
132var DOCUMENT_TYPE_NODE = 10;
133var DOCUMENT_FRAGMENT_NODE = 11;
134var NOTATION_NODE = 12;
135var s="";
136switch(node.nodeType){
137case ATTRIBUTE_NODE:
138s+=node.nodeName+'="' + node.value + '"';
139break;
140case DOCUMENT_NODE:
141s+=this.node2XML(node.documentElement);
142break;
143case ELEMENT_NODE:
144s+="<" + node.tagName;
145for(var i=0; i<node.attributes.length; i++){
146s+=" " + this.node2XML(node.attributes.item(i));
147}
148if(node.childNodes.length==0){
149s+="/>\n";
150}else{
151s+=">";
152for(var i=0; i<node.childNodes.length; i++){
153s+=this.node2XML(node.childNodes.item(i));
154}
155s+="</" + node.tagName+ ">\n";
156}
157break;
158case PROCESSING_INSTRUCTION_NODE:
159s+="<?" + node.target + " " + node.data + " ?>";
160break;
161case TEXT_NODE:
162s+=node.nodeValue;
163break;
164case CDATA_SECTION_NODE:
165s+="<" +"![CDATA[" + node.nodeValue + "]" + "]>";
166break;
167case COMMENT_NODE:
168s+="<!--" + node.nodeValue + "-->";
169break;
170case ENTITY_REFERENCE_NODE:
171case DOCUMENT_FRAGMENT_NODE:
172case DOCUMENT_TYPE_NODE:
173case NOTATION_NODE:
174case ENTITY_NODE:
175throw new mod.Exception("Nodetype(%s) not supported.".format(node.nodeType));
176break;
177}
178return s;
179}
180})