[ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 /* 2 Copyright (c) 2003 Jan-Klaas Kollhof 3 4 This file is part of the JavaScript o lait library(jsolait). 5 6 jsolait is free software; you can redistribute it and/or modify 7 it under the terms of the GNU Lesser General Public License as published by 8 the Free Software Foundation; either version 2.1 of the License, or 9 (at your option) any later version. 10 11 This software is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Lesser General Public License for more details. 15 16 You should have received a copy of the GNU Lesser General Public License 17 along with this software; if not, write to the Free Software 18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 */ 20 21 22 /** 23 Provides parseXML and an importNode implementation. 24 */ 25 Module("xml","1.1.2", function(mod){ 26 /** 27 Thrown if no parser could be instanciated. 28 */ 29 mod.NoXMLParser=Class("NoXMLParser", mod.Exception, function(publ, supr){ 30 /** 31 Initializes the Exception. 32 @param trace The error causing the Exception. 33 */ 34 publ.init=function(trace){ 35 supr(this).init("Could not create an XML parser.", trace); 36 } 37 }) 38 /** 39 Thrown if a document could not be parsed. 40 */ 41 mod.ParsingFailed=Class("ParsingFailed", mod.Exception, function(publ, supr){ 42 /** 43 Initializes the Exception. 44 @param xml The xml source which could not be parsed. 45 @param trace The error causing this Exception. 46 */ 47 publ.init=function(xml,trace){ 48 supr(this).init("Failed parsing XML document.",trace); 49 this.xml = xml; 50 } 51 ///The xml source which could not be parsed. 52 publ.xml; 53 }) 54 /** 55 Parses an xml document. 56 @param xml The xml text. 57 @return A DOM of the xml document. 58 */ 59 mod.parseXML=function(xml){ 60 var obj=null; 61 var isMoz=false; 62 var isIE=false; 63 var isASV=false; 64 65 try{//to get Adobe's SVG parseXML 66 var p=window.parseXML; 67 if(p==null){ 68 throw "No ASV paseXML"; 69 } 70 isASV=true; 71 }catch(e){ 72 try{//to get the mozilla parser 73 obj = new DOMParser(); 74 isMoz=true; 75 }catch(e){ 76 try{//to get the MS XML parser 77 obj = new ActiveXObject("Msxml2.DomDocument.4.0"); 78 isIE=true; 79 }catch(e){ 80 try{//to get the MS XML parser 81 obj = new ActiveXObject("Msxml2.DomDocument"); 82 isIE=true; 83 }catch(e){ 84 try{//to get the old MS XML parser 85 obj = new ActiveXObject("microsoft.XMLDOM"); 86 isIE=true; 87 }catch(e){ 88 throw new mod.NoXMLParser(e); 89 } 90 } 91 } 92 } 93 } 94 try{ 95 if(isMoz){ 96 obj = obj.parseFromString(xml, "text/xml"); 97 return obj; 98 }else if(isIE){ 99 obj.loadXML(xml); 100 return obj; 101 }else if(isASV){ 102 return window.parseXML(xml, null); 103 } 104 }catch(e){ 105 throw new mod.ParsingFailed(xml,e); 106 } 107 } 108 /** 109 DOM2 implimentation of document.importNode(). 110 This will import into the current document. In SVG it will create SVG nodes in HTML it will create HTML nodes.... 111 This might become customizable in the future. 112 @param importedNode The node to import. 113 @param deep=true Import all childNodes recursively. 114 @return The imported Node. 115 */ 116 mod.importNode=function(importedNode, deep){ 117 deep = (deep==null) ? true : deep; 118 //constants from doom2 119 var ELEMENT_NODE = 1; 120 var ATTRIBUTE_NODE = 2; 121 var TEXT_NODE = 3; 122 var CDATA_SECTION_NODE = 4; 123 var ENTITY_REFERENCE_NODE = 5; 124 var ENTITY_NODE = 6; 125 var PROCESSING_INSTRUCTION_NODE = 7; 126 var COMMENT_NODE = 8; 127 var DOCUMENT_NODE = 9; 128 var DOCUMENT_TYPE_NODE = 10; 129 var DOCUMENT_FRAGMENT_NODE = 11; 130 var NOTATION_NODE = 12; 131 var importChildren=function(srcNode, parent){ 132 if(deep){ 133 for(var i=0; i<srcNode.childNodes.length; i++){ 134 var n=mod.importNode(srcNode.childNodes.item(i), true); 135 parent.appendChild(n); 136 } 137 } 138 } 139 var node=null; 140 switch(importedNode.nodeType){ 141 case ATTRIBUTE_NODE: 142 node=document.createAttributeNS(importedNode.namespaceURI, importedNode.nodeName); 143 node.value=importedNode.value; 144 break; 145 case DOCUMENT_FRAGMENT_NODE: 146 node=document.createDocumentFragment(); 147 importChildren(importedNode,node); 148 break; 149 case ELEMENT_NODE: 150 node=document.createElementNS(importedNode.namespaceURI, importedNode.tagName); 151 //import all attributes 152 for(var i=0; i<importedNode.attributes.length; i++){ 153 var attr=this.importNode(importedNode.attributes.item(i), deep); 154 node.setAttributeNodeNS(attr); 155 } 156 importChildren(importedNode,node); 157 break; 158 case ENTITY_REFERENCE_NODE: 159 node=importedNode; 160 break; 161 case PROCESSING_INSTRUCTION_NODE: 162 node=document.createProcessingInstruction(importedNode.target, importedNode.data); 163 break; 164 case TEXT_NODE: 165 case CDATA_SECTION_NODE: 166 case COMMENT_NODE: 167 node=document.createTextNode(importedNode.nodeValue); 168 break; 169 case DOCUMENT_NODE: 170 //Document nodes cannot be imported. 171 case DOCUMENT_TYPE_NODE: 172 //DocumentType nodes cannot be imported. 173 case NOTATION_NODE: 174 //readonly in DOM2 175 case ENTITY_NODE: 176 //readonly in DOM2 177 throw "not supported in DOM2"; 178 break; 179 } 180 return node; 181 } 182 /** 183 Turns an XML document into a String. 184 @param node The node to print. 185 @return A string containing the text for the XML. 186 */ 187 mod.node2XML = function(node){ 188 var ELEMENT_NODE = 1; 189 var ATTRIBUTE_NODE = 2; 190 var TEXT_NODE = 3; 191 var CDATA_SECTION_NODE = 4; 192 var ENTITY_REFERENCE_NODE = 5; 193 var ENTITY_NODE = 6; 194 var PROCESSING_INSTRUCTION_NODE = 7; 195 var COMMENT_NODE = 8; 196 var DOCUMENT_NODE = 9; 197 var DOCUMENT_TYPE_NODE = 10; 198 var DOCUMENT_FRAGMENT_NODE = 11; 199 var NOTATION_NODE = 12; 200 var s=""; 201 switch(node.nodeType){ 202 case ATTRIBUTE_NODE: 203 s+=node.nodeName+'="' + node.value + '"'; 204 break; 205 case DOCUMENT_NODE: 206 s+=this.node2XML(node.documentElement); 207 break; 208 case ELEMENT_NODE: 209 s+="<" + node.tagName; 210 //attributes 211 for(var i=0; i<node.attributes.length; i++){ 212 s+=" " + this.node2XML(node.attributes.item(i)); 213 } 214 //children 215 if(node.childNodes.length==0){ 216 s+="/>\n"; 217 }else{ 218 s+=">"; 219 for(var i=0; i<node.childNodes.length; i++){ 220 s+=this.node2XML(node.childNodes.item(i)); 221 } 222 s+="</" + node.tagName+ ">\n"; 223 } 224 break; 225 case PROCESSING_INSTRUCTION_NODE: 226 s+="<?" + node.target + " " + node.data + " ?>"; 227 break; 228 case TEXT_NODE: 229 s+=node.nodeValue; 230 break; 231 case CDATA_SECTION_NODE: 232 s+="<" +"![CDATA[" + node.nodeValue + "]" + "]>"; 233 break; 234 case COMMENT_NODE: 235 s+="<!--" + node.nodeValue + "-->"; 236 break; 237 case ENTITY_REFERENCE_NODE: 238 case DOCUMENT_FRAGMENT_NODE: 239 case DOCUMENT_TYPE_NODE: 240 case NOTATION_NODE: 241 case ENTITY_NODE: 242 throw new mod.Exception("Nodetype(%s) not supported.".format(node.nodeType)); 243 break; 244 } 245 return s; 246 } 247 })
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |