root / trunk / web / dojo / dojox / xml / DomParser.js @ 12
History | View | Annotate | Download (6.72 KB)
1 |
/*
|
---|---|
2 |
Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
3 |
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
4 |
see: http://dojotoolkit.org/license for details
|
5 |
*/
|
6 |
|
7 |
|
8 |
if(!dojo._hasResource["dojox.xml.DomParser"]){ |
9 |
dojo._hasResource["dojox.xml.DomParser"]=true; |
10 |
dojo.provide("dojox.xml.DomParser");
|
11 |
dojox.xml.DomParser=new (function(){ |
12 |
var _1={ELEMENT:1,ATTRIBUTE:2,TEXT:3,CDATA_SECTION:4,PROCESSING_INSTRUCTION:7,COMMENT:8,DOCUMENT:9}; |
13 |
var _2=/<([^>\/\s+]*)([^>]*)>([^<]*)/g; |
14 |
var _3=/([^=]*)=(("([^"]*)")|('([^']*)'))/g; |
15 |
var _4=/<!ENTITY\s+([^"]*)\s+"([^"]*)">/g; |
16 |
var _5=/<!\[CDATA\[([\u0001-\uFFFF]*?)\]\]>/g; |
17 |
var _6=/<!--([\u0001-\uFFFF]*?)-->/g; |
18 |
var _7=/^\s+|\s+$/g; |
19 |
var _8=/\s+/g; |
20 |
var _9=/\>/g; |
21 |
var _a=/\</g; |
22 |
var _b=/\"/g; |
23 |
var _c=/\'/g; |
24 |
var _d=/\&/g; |
25 |
var _e="_def_"; |
26 |
function _f(){ |
27 |
return new (function(){ |
28 |
var all={};
|
29 |
this.nodeType=_1.DOCUMENT;
|
30 |
this.nodeName="#document"; |
31 |
this.namespaces={};
|
32 |
this._nsPaths={};
|
33 |
this.childNodes=[];
|
34 |
this.documentElement=null; |
35 |
this._add=function(obj){ |
36 |
if(typeof (obj.id)!="undefined"){ |
37 |
all[obj.id]=obj; |
38 |
} |
39 |
}; |
40 |
this._remove=function(id){ |
41 |
if(all[id]){
|
42 |
delete all[id];
|
43 |
} |
44 |
}; |
45 |
this.byId=this.getElementById=function(id){ |
46 |
return all[id];
|
47 |
}; |
48 |
this.byName=this.getElementsByTagName=_10; |
49 |
this.byNameNS=this.getElementsByTagNameNS=_11; |
50 |
this.childrenByName=_12;
|
51 |
this.childrenByNameNS=_13;
|
52 |
})(); |
53 |
}; |
54 |
function _10(_14){ |
55 |
function _15(_16,_17,arr){ |
56 |
dojo.forEach(_16.childNodes,function(c){
|
57 |
if(c.nodeType==_1.ELEMENT){
|
58 |
if(_17=="*"){ |
59 |
arr.push(c); |
60 |
}else{
|
61 |
if(c.nodeName==_17){
|
62 |
arr.push(c); |
63 |
} |
64 |
} |
65 |
_15(c,_17,arr); |
66 |
} |
67 |
}); |
68 |
}; |
69 |
var a=[];
|
70 |
_15(this,_14,a);
|
71 |
return a;
|
72 |
}; |
73 |
function _11(_18,ns){ |
74 |
function _19(_1a,_1b,ns,arr){ |
75 |
dojo.forEach(_1a.childNodes,function(c){
|
76 |
if(c.nodeType==_1.ELEMENT){
|
77 |
if(_1b=="*"&&c.ownerDocument._nsPaths[ns]==c.namespace){ |
78 |
arr.push(c); |
79 |
}else{
|
80 |
if(c.localName==_1b&&c.ownerDocument._nsPaths[ns]==c.namespace){
|
81 |
arr.push(c); |
82 |
} |
83 |
} |
84 |
_19(c,_1b,ns,arr); |
85 |
} |
86 |
}); |
87 |
}; |
88 |
if(!ns){
|
89 |
ns=_e; |
90 |
} |
91 |
var a=[];
|
92 |
_19(this,_18,ns,a);
|
93 |
return a;
|
94 |
}; |
95 |
function _12(_1c){ |
96 |
var a=[];
|
97 |
dojo.forEach(this.childNodes,function(c){ |
98 |
if(c.nodeType==_1.ELEMENT){
|
99 |
if(_1c=="*"){ |
100 |
a.push(c); |
101 |
}else{
|
102 |
if(c.nodeName==_1c){
|
103 |
a.push(c); |
104 |
} |
105 |
} |
106 |
} |
107 |
}); |
108 |
return a;
|
109 |
}; |
110 |
function _13(_1d,ns){ |
111 |
var a=[];
|
112 |
dojo.forEach(this.childNodes,function(c){ |
113 |
if(c.nodeType==_1.ELEMENT){
|
114 |
if(_1d=="*"&&c.ownerDocument._nsPaths[ns]==c.namespace){ |
115 |
a.push(c); |
116 |
}else{
|
117 |
if(c.localName==_1d&&c.ownerDocument._nsPaths[ns]==c.namespace){
|
118 |
a.push(c); |
119 |
} |
120 |
} |
121 |
} |
122 |
}); |
123 |
return a;
|
124 |
}; |
125 |
function _1e(v){ |
126 |
return {nodeType:_1.TEXT,nodeName:"#text",nodeValue:v.replace(_8," ").replace(_9,">").replace(_a,"<").replace(_c,"'").replace(_b,"\"").replace(_d,"&")}; |
127 |
}; |
128 |
function _1f(_20){ |
129 |
for(var i=0;i<this.attributes.length;i++){ |
130 |
if(this.attributes[i].nodeName==_20){ |
131 |
return this.attributes[i].nodeValue; |
132 |
} |
133 |
} |
134 |
return null; |
135 |
}; |
136 |
function _21(_22,ns){ |
137 |
for(var i=0;i<this.attributes.length;i++){ |
138 |
if(this.ownerDocument._nsPaths[ns]==this.attributes[i].namespace&&this.attributes[i].localName==_22){ |
139 |
return this.attributes[i].nodeValue; |
140 |
} |
141 |
} |
142 |
return null; |
143 |
}; |
144 |
function _23(_24,val){ |
145 |
var old=null; |
146 |
for(var i=0;i<this.attributes.length;i++){ |
147 |
if(this.attributes[i].nodeName==_24){ |
148 |
old=this.attributes[i].nodeValue;
|
149 |
this.attributes[i].nodeValue=val;
|
150 |
break;
|
151 |
} |
152 |
} |
153 |
if(_24=="id"){ |
154 |
if(old!=null){ |
155 |
this.ownerDocument._remove(old);
|
156 |
} |
157 |
this.ownerDocument._add(this); |
158 |
} |
159 |
}; |
160 |
function _25(_26,val,ns){ |
161 |
for(var i=0;i<this.attributes.length;i++){ |
162 |
if(this.ownerDocument._nsPaths[ns]==this.attributes[i].namespace&&this.attributes[i].localName==_26){ |
163 |
this.attributes[i].nodeValue=val;
|
164 |
return;
|
165 |
} |
166 |
} |
167 |
}; |
168 |
function _27(){ |
169 |
var p=this.parentNode; |
170 |
if(p){
|
171 |
for(var i=0;i<p.childNodes.length;i++){ |
172 |
if(p.childNodes[i]==this&&i>0){ |
173 |
return p.childNodes[i-1]; |
174 |
} |
175 |
} |
176 |
} |
177 |
return null; |
178 |
}; |
179 |
function _28(){ |
180 |
var p=this.parentNode; |
181 |
if(p){
|
182 |
for(var i=0;i<p.childNodes.length;i++){ |
183 |
if(p.childNodes[i]==this&&(i+1)<p.childNodes.length){ |
184 |
return p.childNodes[i+1]; |
185 |
} |
186 |
} |
187 |
} |
188 |
return null; |
189 |
}; |
190 |
this.parse=function(str){ |
191 |
var _29=_f();
|
192 |
if(str==null){ |
193 |
return _29;
|
194 |
} |
195 |
if(str.length==0){ |
196 |
return _29;
|
197 |
} |
198 |
if(str.indexOf("<!ENTITY")>0){ |
199 |
var _2a,eRe=[];
|
200 |
if(_4.test(str)){
|
201 |
_4.lastIndex=0;
|
202 |
while((_2a=_4.exec(str))!=null){ |
203 |
eRe.push({entity:"&"+_2a[1].replace(_7,"")+";",expression:_2a[2]}); |
204 |
} |
205 |
for(var i=0;i<eRe.length;i++){ |
206 |
str=str.replace(new RegExp(eRe[i].entity,"g"),eRe[i].expression); |
207 |
} |
208 |
} |
209 |
} |
210 |
var _2b=[],_2c;
|
211 |
while((_2c=_5.exec(str))!=null){ |
212 |
_2b.push(_2c[1]);
|
213 |
} |
214 |
for(var i=0;i<_2b.length;i++){ |
215 |
str=str.replace(_2b[i],i); |
216 |
} |
217 |
var _2d=[],_2e;
|
218 |
while((_2e=_6.exec(str))!=null){ |
219 |
_2d.push(_2e[1]);
|
220 |
} |
221 |
for(i=0;i<_2d.length;i++){ |
222 |
str=str.replace(_2d[i],i); |
223 |
} |
224 |
var res,obj=_29;
|
225 |
while((res=_2.exec(str))!=null){ |
226 |
if(res[2].charAt(0)=="/"&&res[2].replace(_7,"").length>1){ |
227 |
if(obj.parentNode){
|
228 |
obj=obj.parentNode; |
229 |
} |
230 |
var _2f=(res[3]||"").replace(_7,""); |
231 |
if(_2f.length>0){ |
232 |
obj.childNodes.push(_1e(_2f)); |
233 |
} |
234 |
}else{
|
235 |
if(res[1].length>0){ |
236 |
if(res[1].charAt(0)=="?"){ |
237 |
var _30=res[1].substr(1); |
238 |
var _31=res[2].substr(0,res[2].length-2); |
239 |
obj.childNodes.push({nodeType:_1.PROCESSING_INSTRUCTION,nodeName:_30,nodeValue:_31}); |
240 |
}else{
|
241 |
if(res[1].charAt(0)=="!"){ |
242 |
if(res[1].indexOf("![CDATA[")==0){ |
243 |
var val=parseInt(res[1].replace("![CDATA[","").replace("]]","")); |
244 |
obj.childNodes.push({nodeType:_1.CDATA_SECTION,nodeName:"#cdata-section",nodeValue:_2b[val]}); |
245 |
}else{
|
246 |
if(res[1].substr(0,3)=="!--"){ |
247 |
var val=parseInt(res[1].replace("!--","").replace("--","")); |
248 |
obj.childNodes.push({nodeType:_1.COMMENT,nodeName:"#comment",nodeValue:_2d[val]}); |
249 |
} |
250 |
} |
251 |
}else{
|
252 |
var _30=res[1].replace(_7,""); |
253 |
var o={nodeType:_1.ELEMENT,nodeName:_30,localName:_30,namespace:_e,ownerDocument:_29,attributes:[],parentNode:null,childNodes:[]}; |
254 |
if(_30.indexOf(":")>-1){ |
255 |
var t=_30.split(":"); |
256 |
o.namespace=t[0];
|
257 |
o.localName=t[1];
|
258 |
} |
259 |
o.byName=o.getElementsByTagName=_10; |
260 |
o.byNameNS=o.getElementsByTagNameNS=_11; |
261 |
o.childrenByName=_12; |
262 |
o.childrenByNameNS=_13; |
263 |
o.getAttribute=_1f; |
264 |
o.getAttributeNS=_21; |
265 |
o.setAttribute=_23; |
266 |
o.setAttributeNS=_25; |
267 |
o.previous=o.previousSibling=_27; |
268 |
o.next=o.nextSibling=_28; |
269 |
var _32;
|
270 |
while((_32=_3.exec(res[2]))!=null){ |
271 |
if(_32.length>0){ |
272 |
var _30=_32[1].replace(_7,""); |
273 |
var val=(_32[4]||_32[6]||"").replace(_8," ").replace(_9,">").replace(_a,"<").replace(_c,"'").replace(_b,"\"").replace(_d,"&"); |
274 |
if(_30.indexOf("xmlns")==0){ |
275 |
if(_30.indexOf(":")>0){ |
276 |
var ns=_30.split(":"); |
277 |
_29.namespaces[ns[1]]=val;
|
278 |
_29._nsPaths[val]=ns[1];
|
279 |
}else{
|
280 |
_29.namespaces[_e]=val; |
281 |
_29._nsPaths[val]=_e; |
282 |
} |
283 |
}else{
|
284 |
var ln=_30;
|
285 |
var ns=_e;
|
286 |
if(_30.indexOf(":")>0){ |
287 |
var t=_30.split(":"); |
288 |
ln=t[1];
|
289 |
ns=t[0];
|
290 |
} |
291 |
o.attributes.push({nodeType:_1.ATTRIBUTE,nodeName:_30,localName:ln,namespace:ns,nodeValue:val}); |
292 |
if(ln=="id"){ |
293 |
o.id=val; |
294 |
} |
295 |
} |
296 |
} |
297 |
} |
298 |
_29._add(o); |
299 |
if(obj){
|
300 |
obj.childNodes.push(o); |
301 |
o.parentNode=obj; |
302 |
if(res[2].charAt(res[2].length-1)!="/"){ |
303 |
obj=o; |
304 |
} |
305 |
} |
306 |
var _2f=res[3]; |
307 |
if(_2f.length>0){ |
308 |
obj.childNodes.push(_1e(_2f)); |
309 |
} |
310 |
} |
311 |
} |
312 |
} |
313 |
} |
314 |
} |
315 |
for(var i=0;i<_29.childNodes.length;i++){ |
316 |
var e=_29.childNodes[i];
|
317 |
if(e.nodeType==_1.ELEMENT){
|
318 |
_29.documentElement=e; |
319 |
break;
|
320 |
} |
321 |
} |
322 |
return _29;
|
323 |
}; |
324 |
})(); |
325 |
} |