1 | define(['dojo/_base/kernel', 'dojo/_base/lang', 'dojo/_base/array', 'dojo/_base/window', 'dojo/_base/sniff'], function(dojo){ |
---|
2 | |
---|
3 | dojo.getObject("xml.parser", true, dojox); |
---|
4 | |
---|
5 | //DOM type to int value for reference. |
---|
6 | //Ints make for more compact code than full constant names. |
---|
7 | //ELEMENT_NODE = 1; |
---|
8 | //ATTRIBUTE_NODE = 2; |
---|
9 | //TEXT_NODE = 3; |
---|
10 | //CDATA_SECTION_NODE = 4; |
---|
11 | //ENTITY_REFERENCE_NODE = 5; |
---|
12 | //ENTITY_NODE = 6; |
---|
13 | //PROCESSING_INSTRUCTION_NODE = 7; |
---|
14 | //COMMENT_NODE = 8; |
---|
15 | //DOCUMENT_NODE = 9; |
---|
16 | //DOCUMENT_TYPE_NODE = 10; |
---|
17 | //DOCUMENT_FRAGMENT_NODE = 11; |
---|
18 | //NOTATION_NODE = 12; |
---|
19 | |
---|
20 | dojox.xml.parser.parse = function(/*String?*/ str, /*String?*/ mimetype){ |
---|
21 | // summary: |
---|
22 | // cross-browser implementation of creating an XML document object from null, empty string, and XML text.. |
---|
23 | // |
---|
24 | // str: |
---|
25 | // Optional text to create the document from. If not provided, an empty XML document will be created. |
---|
26 | // If str is empty string "", then a new empty document will be created. |
---|
27 | // mimetype: |
---|
28 | // Optional mimetype of the text. Typically, this is text/xml. Will be defaulted to text/xml if not provided. |
---|
29 | var _document = dojo.doc; |
---|
30 | var doc; |
---|
31 | |
---|
32 | mimetype = mimetype || "text/xml"; |
---|
33 | if(str && dojo.trim(str) && "DOMParser" in dojo.global){ |
---|
34 | //Handle parsing the text on Mozilla based browsers etc.. |
---|
35 | var parser = new DOMParser(); |
---|
36 | doc = parser.parseFromString(str, mimetype); |
---|
37 | var de = doc.documentElement; |
---|
38 | var errorNS = "http://www.mozilla.org/newlayout/xml/parsererror.xml"; |
---|
39 | if(de.nodeName == "parsererror" && de.namespaceURI == errorNS){ |
---|
40 | var sourceText = de.getElementsByTagNameNS(errorNS, 'sourcetext')[0]; |
---|
41 | if(sourceText){ |
---|
42 | sourceText = sourceText.firstChild.data; |
---|
43 | } |
---|
44 | throw new Error("Error parsing text " + de.firstChild.data + " \n" + sourceText); |
---|
45 | } |
---|
46 | return doc; |
---|
47 | |
---|
48 | }else if("ActiveXObject" in dojo.global){ |
---|
49 | //Handle IE. |
---|
50 | var ms = function(n){ return "MSXML" + n + ".DOMDocument"; }; |
---|
51 | var dp = ["Microsoft.XMLDOM", ms(6), ms(4), ms(3), ms(2)]; |
---|
52 | dojo.some(dp, function(p){ |
---|
53 | try{ |
---|
54 | doc = new ActiveXObject(p); |
---|
55 | }catch(e){ return false; } |
---|
56 | return true; |
---|
57 | }); |
---|
58 | if(str && doc){ |
---|
59 | doc.async = false; |
---|
60 | doc.loadXML(str); |
---|
61 | var pe = doc.parseError; |
---|
62 | if(pe.errorCode !== 0){ |
---|
63 | throw new Error("Line: " + pe.line + "\n" + |
---|
64 | "Col: " + pe.linepos + "\n" + |
---|
65 | "Reason: " + pe.reason + "\n" + |
---|
66 | "Error Code: " + pe.errorCode + "\n" + |
---|
67 | "Source: " + pe.srcText); |
---|
68 | } |
---|
69 | } |
---|
70 | if(doc){ |
---|
71 | return doc; //DOMDocument |
---|
72 | } |
---|
73 | }else if(_document.implementation && _document.implementation.createDocument){ |
---|
74 | if(str && dojo.trim(str) && _document.createElement){ |
---|
75 | //Everyone else that we couldn't get to work. Fallback case. |
---|
76 | // FIXME: this may change all tags to uppercase! |
---|
77 | var tmp = _document.createElement("xml"); |
---|
78 | tmp.innerHTML = str; |
---|
79 | var xmlDoc = _document.implementation.createDocument("foo", "", null); |
---|
80 | dojo.forEach(tmp.childNodes, function(child){ |
---|
81 | xmlDoc.importNode(child, true); |
---|
82 | }); |
---|
83 | return xmlDoc; // DOMDocument |
---|
84 | }else{ |
---|
85 | return _document.implementation.createDocument("", "", null); // DOMDocument |
---|
86 | } |
---|
87 | } |
---|
88 | return null; // null |
---|
89 | }; |
---|
90 | |
---|
91 | dojox.xml.parser.textContent = function(/*Node*/node, /*String?*/text){ |
---|
92 | // summary: |
---|
93 | // Implementation of the DOM Level 3 attribute; scan node for text |
---|
94 | // description: |
---|
95 | // Implementation of the DOM Level 3 attribute; scan node for text |
---|
96 | // This function can also update the text of a node by replacing all child |
---|
97 | // content of the node. |
---|
98 | // node: |
---|
99 | // The node to get the text off of or set the text on. |
---|
100 | // text: |
---|
101 | // Optional argument of the text to apply to the node. |
---|
102 | if(arguments.length>1){ |
---|
103 | var _document = node.ownerDocument || dojo.doc; //Preference is to get the node owning doc first or it may fail |
---|
104 | dojox.xml.parser.replaceChildren(node, _document.createTextNode(text)); |
---|
105 | return text; // String |
---|
106 | }else{ |
---|
107 | if(node.textContent !== undefined){ //FF 1.5 -- remove? |
---|
108 | return node.textContent; // String |
---|
109 | } |
---|
110 | var _result = ""; |
---|
111 | if(node){ |
---|
112 | dojo.forEach(node.childNodes, function(child){ |
---|
113 | switch(child.nodeType){ |
---|
114 | case 1: // ELEMENT_NODE |
---|
115 | case 5: // ENTITY_REFERENCE_NODE |
---|
116 | _result += dojox.xml.parser.textContent(child); |
---|
117 | break; |
---|
118 | case 3: // TEXT_NODE |
---|
119 | case 2: // ATTRIBUTE_NODE |
---|
120 | case 4: // CDATA_SECTION_NODE |
---|
121 | _result += child.nodeValue; |
---|
122 | } |
---|
123 | }); |
---|
124 | } |
---|
125 | return _result; // String |
---|
126 | } |
---|
127 | }; |
---|
128 | |
---|
129 | dojox.xml.parser.replaceChildren = function(/*Element*/node, /*Node|Array*/ newChildren){ |
---|
130 | // summary: |
---|
131 | // Removes all children of node and appends newChild. All the existing |
---|
132 | // children will be destroyed. |
---|
133 | // description: |
---|
134 | // Removes all children of node and appends newChild. All the existing |
---|
135 | // children will be destroyed. |
---|
136 | // node: |
---|
137 | // The node to modify the children on |
---|
138 | // newChildren: |
---|
139 | // The children to add to the node. It can either be a single Node or an |
---|
140 | // array of Nodes. |
---|
141 | var nodes = []; |
---|
142 | |
---|
143 | if(dojo.isIE){ |
---|
144 | dojo.forEach(node.childNodes, function(child){ |
---|
145 | nodes.push(child); |
---|
146 | }); |
---|
147 | } |
---|
148 | |
---|
149 | dojox.xml.parser.removeChildren(node); |
---|
150 | dojo.forEach(nodes, dojo.destroy); |
---|
151 | |
---|
152 | if(!dojo.isArray(newChildren)){ |
---|
153 | node.appendChild(newChildren); |
---|
154 | }else{ |
---|
155 | dojo.forEach(newChildren, function(child){ |
---|
156 | node.appendChild(child); |
---|
157 | }); |
---|
158 | } |
---|
159 | }; |
---|
160 | |
---|
161 | dojox.xml.parser.removeChildren = function(/*Element*/node){ |
---|
162 | // summary: |
---|
163 | // removes all children from node and returns the count of children removed. |
---|
164 | // The children nodes are not destroyed. Be sure to call dojo.destroy on them |
---|
165 | // after they are not used anymore. |
---|
166 | // node: |
---|
167 | // The node to remove all the children from. |
---|
168 | var count = node.childNodes.length; |
---|
169 | while(node.hasChildNodes()){ |
---|
170 | node.removeChild(node.firstChild); |
---|
171 | } |
---|
172 | return count; // int |
---|
173 | }; |
---|
174 | |
---|
175 | |
---|
176 | dojox.xml.parser.innerXML = function(/*Node*/node){ |
---|
177 | // summary: |
---|
178 | // Implementation of MS's innerXML function. |
---|
179 | // node: |
---|
180 | // The node from which to generate the XML text representation. |
---|
181 | if(node.innerXML){ |
---|
182 | return node.innerXML; // String |
---|
183 | }else if(node.xml){ |
---|
184 | return node.xml; // String |
---|
185 | }else if(typeof XMLSerializer != "undefined"){ |
---|
186 | return (new XMLSerializer()).serializeToString(node); // String |
---|
187 | } |
---|
188 | return null; |
---|
189 | }; |
---|
190 | |
---|
191 | return dojox.xml.parser; |
---|
192 | |
---|
193 | }); |
---|