]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/jsolait/lib/xml.js
Release 6.2.1
[Github/sugarcrm.git] / jssource / src_files / include / jsolait / lib / xml.js
1 /*
2
3 Modification information for LGPL compliance
4
5 r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
6
7 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
8
9 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
10
11 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3  tags and updated the build system 
12
13 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
14
15 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
16
17 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
18
19 r4085 - 2005-04-13 17:30:42 -0700 (Wed, 13 Apr 2005) - robert - adding meeting scheduler and accept/decline
20
21
22 */
23
24 /*
25   Copyright (c) 2003 Jan-Klaas Kollhof
26   
27   This file is part of the JavaScript o lait library(jsolait).
28   
29   jsolait is free software; you can redistribute it and/or modify
30   it under the terms of the GNU Lesser General Public License as published by
31   the Free Software Foundation; either version 2.1 of the License, or
32   (at your option) any later version.
33  
34   This software is distributed in the hope that it will be useful,
35   but WITHOUT ANY WARRANTY; without even the implied warranty of
36   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37   GNU Lesser General Public License for more details.
38  
39   You should have received a copy of the GNU Lesser General Public License
40   along with this software; if not, write to the Free Software
41   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
42 */
43
44
45 /**
46    Provides parseXML and an importNode implementation.
47 */
48 Module("xml","1.1.2", function(mod){
49     /**
50         Thrown if no parser could be instanciated.
51     */
52     mod.NoXMLParser=Class("NoXMLParser", mod.Exception, function(publ, supr){
53         /**
54             Initializes the Exception.
55             @param trace  The error causing the Exception.
56         */
57         publ.init=function(trace){
58             supr(this).init("Could not create an XML parser.", trace);
59         }
60     })
61     /**
62         Thrown if a document could not be parsed.
63     */
64     mod.ParsingFailed=Class("ParsingFailed", mod.Exception, function(publ, supr){
65         /**
66             Initializes the Exception.
67             @param xml    The xml source which could not be parsed.
68             @param trace The error causing this Exception.
69         */
70         publ.init=function(xml,trace){
71              supr(this).init("Failed parsing XML document.",trace);
72             this.xml = xml;
73         }
74         ///The xml source which could not be parsed.
75         publ.xml;
76     })
77     /**
78         Parses an xml document.
79         @param xml     The xml text.
80         @return          A DOM of the xml document.
81     */
82     mod.parseXML=function(xml){
83         var obj=null;
84         var isMoz=false;
85         var isIE=false;
86         var isASV=false;
87         
88         try{//to get Adobe's SVG parseXML
89             var p=window.parseXML;
90             if(p==null){
91                 throw "No ASV paseXML";
92             }
93             isASV=true;
94         }catch(e){
95             try{//to get the mozilla parser
96                 obj = new DOMParser();
97                 isMoz=true;
98             }catch(e){
99                 try{//to get the MS XML parser
100                     obj = new ActiveXObject("Msxml2.DomDocument.4.0"); 
101                     isIE=true;
102                 }catch(e){
103                     try{//to get the MS XML parser
104                         obj = new ActiveXObject("Msxml2.DomDocument"); 
105                         isIE=true;
106                     }catch(e){
107                         try{//to get the old MS XML parser
108                             obj = new ActiveXObject("microsoft.XMLDOM"); 
109                             isIE=true;
110                         }catch(e){
111                             throw new mod.NoXMLParser(e);
112                         }
113                     }
114                 }
115             }
116         }
117         try{
118             if(isMoz){
119                 obj = obj.parseFromString(xml, "text/xml");
120                 return obj;
121             }else if(isIE){
122                 obj.loadXML(xml);
123                 return obj;
124             }else if(isASV){
125                 return window.parseXML(xml, null);
126             }
127         }catch(e){
128             throw new mod.ParsingFailed(xml,e);
129         }
130     }
131     /**
132         DOM2 implimentation of document.importNode().
133         This will import into the current document. In SVG it will create SVG nodes in HTML it will create HTML nodes....
134         This might become customizable in the future.
135         @param importedNode   The node to import.
136         @param deep=true        Import all childNodes recursively.
137         @return                      The imported Node.
138     */
139     mod.importNode=function(importedNode, deep){
140         deep = (deep==null) ? true : deep;
141         //constants from doom2
142         var ELEMENT_NODE = 1;
143         var ATTRIBUTE_NODE = 2;
144         var TEXT_NODE = 3;
145         var CDATA_SECTION_NODE = 4;
146         var ENTITY_REFERENCE_NODE = 5;
147         var ENTITY_NODE = 6;
148         var PROCESSING_INSTRUCTION_NODE = 7;
149         var COMMENT_NODE = 8;
150         var DOCUMENT_NODE = 9;
151         var DOCUMENT_TYPE_NODE = 10;
152         var DOCUMENT_FRAGMENT_NODE = 11;
153         var NOTATION_NODE = 12;
154         var importChildren=function(srcNode, parent){
155             if(deep){
156                  for(var i=0; i<srcNode.childNodes.length; i++){
157                     var n=mod.importNode(srcNode.childNodes.item(i), true);
158                     parent.appendChild(n);
159                 }
160             }
161         }
162         var node=null;
163         switch(importedNode.nodeType){
164             case ATTRIBUTE_NODE:
165                 node=document.createAttributeNS(importedNode.namespaceURI, importedNode.nodeName);
166                 node.value=importedNode.value;
167                 break;
168             case DOCUMENT_FRAGMENT_NODE:
169                 node=document.createDocumentFragment();
170                 importChildren(importedNode,node);
171                 break;
172             case ELEMENT_NODE:
173                 node=document.createElementNS(importedNode.namespaceURI, importedNode.tagName);
174                 //import all attributes
175                 for(var i=0; i<importedNode.attributes.length; i++){
176                     var attr=this.importNode(importedNode.attributes.item(i), deep);
177                     node.setAttributeNodeNS(attr);
178                 }
179                 importChildren(importedNode,node);
180                 break;
181             case ENTITY_REFERENCE_NODE:
182                 node=importedNode;
183                 break;
184             case PROCESSING_INSTRUCTION_NODE:
185                 node=document.createProcessingInstruction(importedNode.target, importedNode.data);
186                 break;
187             case TEXT_NODE:
188             case CDATA_SECTION_NODE:
189             case COMMENT_NODE:
190                 node=document.createTextNode(importedNode.nodeValue);
191                 break;
192             case DOCUMENT_NODE:
193                 //Document nodes cannot be imported.
194             case DOCUMENT_TYPE_NODE:
195                 //DocumentType nodes cannot be imported.
196             case NOTATION_NODE:
197                 //readonly in DOM2
198             case ENTITY_NODE:
199                 //readonly in DOM2
200                 throw "not supported in DOM2";
201                 break;
202         }
203         return node;
204     }
205     /**
206         Turns an XML document into a String.
207         @param node   The node to print.
208         @return           A string containing the text for the XML.
209     */
210     mod.node2XML = function(node){
211         var ELEMENT_NODE = 1;
212         var ATTRIBUTE_NODE = 2;
213         var TEXT_NODE = 3;
214         var CDATA_SECTION_NODE = 4;
215         var ENTITY_REFERENCE_NODE = 5;
216         var ENTITY_NODE = 6;
217         var PROCESSING_INSTRUCTION_NODE = 7;
218         var COMMENT_NODE = 8;
219         var DOCUMENT_NODE = 9;
220         var DOCUMENT_TYPE_NODE = 10;
221         var DOCUMENT_FRAGMENT_NODE = 11;
222         var NOTATION_NODE = 12;
223         var s="";
224         switch(node.nodeType){
225             case ATTRIBUTE_NODE:
226                 s+=node.nodeName+'="' + node.value + '"';
227                 break;
228             case DOCUMENT_NODE:
229                 s+=this.node2XML(node.documentElement);
230                 break;
231             case ELEMENT_NODE:
232                 s+="<" + node.tagName;
233                 //attributes
234                 for(var i=0; i<node.attributes.length; i++){
235                     s+=" " + this.node2XML(node.attributes.item(i));
236                 }
237                 //children
238                 if(node.childNodes.length==0){
239                     s+="/>\n";
240                 }else{
241                     s+=">";
242                     for(var i=0; i<node.childNodes.length; i++){
243                         s+=this.node2XML(node.childNodes.item(i));
244                     }
245                     s+="</" + node.tagName+ ">\n";
246                 }
247                 break;
248             case PROCESSING_INSTRUCTION_NODE:
249                 s+="<?" + node.target + " " + node.data + " ?>";
250                 break;
251             case TEXT_NODE:
252                 s+=node.nodeValue;
253                 break;
254             case CDATA_SECTION_NODE:
255                 s+="<" +"![CDATA[" + node.nodeValue + "]" + "]>";
256                 break;
257             case COMMENT_NODE:
258                 s+="<!--" + node.nodeValue + "-->";
259                 break;
260             case ENTITY_REFERENCE_NODE:
261             case DOCUMENT_FRAGMENT_NODE:
262             case DOCUMENT_TYPE_NODE:
263             case NOTATION_NODE:
264             case ENTITY_NODE:
265                 throw new mod.Exception("Nodetype(%s) not supported.".format(node.nodeType));
266                 break;
267         }
268         return s;
269     }
270 })