1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the Clear BSD license. |
---|
3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
4 | * full text of the license. */ |
---|
5 | |
---|
6 | /** |
---|
7 | * @requires OpenLayers/Format.js |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * Class: OpenLayers.Format.XML |
---|
12 | * Read and write XML. For cross-browser XML generation, use methods on an |
---|
13 | * instance of the XML format class instead of on <code>document<end>. |
---|
14 | * The DOM creation and traversing methods exposed here all mimic the |
---|
15 | * W3C XML DOM methods. Create a new parser with the |
---|
16 | * <OpenLayers.Format.XML> constructor. |
---|
17 | * |
---|
18 | * Inherits from: |
---|
19 | * - <OpenLayers.Format> |
---|
20 | */ |
---|
21 | OpenLayers.Format.XML = OpenLayers.Class(OpenLayers.Format, { |
---|
22 | |
---|
23 | /** |
---|
24 | * Property: namespaces |
---|
25 | * {Object} Mapping of namespace aliases to namespace URIs. Properties |
---|
26 | * of this object should not be set individually. Read-only. All |
---|
27 | * XML subclasses should have their own namespaces object. Use |
---|
28 | * <setNamespace> to add or set a namespace alias after construction. |
---|
29 | */ |
---|
30 | namespaces: null, |
---|
31 | |
---|
32 | /** |
---|
33 | * Property: namespaceAlias |
---|
34 | * {Object} Mapping of namespace URI to namespace alias. This object |
---|
35 | * is read-only. Use <setNamespace> to add or set a namespace alias. |
---|
36 | */ |
---|
37 | namespaceAlias: null, |
---|
38 | |
---|
39 | /** |
---|
40 | * Property: defaultPrefix |
---|
41 | * {String} The default namespace alias for creating element nodes. |
---|
42 | */ |
---|
43 | defaultPrefix: null, |
---|
44 | |
---|
45 | /** |
---|
46 | * Property: readers |
---|
47 | * Contains public functions, grouped by namespace prefix, that will |
---|
48 | * be applied when a namespaced node is found matching the function |
---|
49 | * name. The function will be applied in the scope of this parser |
---|
50 | * with two arguments: the node being read and a context object passed |
---|
51 | * from the parent. |
---|
52 | */ |
---|
53 | readers: {}, |
---|
54 | |
---|
55 | /** |
---|
56 | * Property: writers |
---|
57 | * As a compliment to the <readers> property, this structure contains public |
---|
58 | * writing functions grouped by namespace alias and named like the |
---|
59 | * node names they produce. |
---|
60 | */ |
---|
61 | writers: {}, |
---|
62 | |
---|
63 | /** |
---|
64 | * Property: xmldom |
---|
65 | * {XMLDom} If this browser uses ActiveX, this will be set to a XMLDOM |
---|
66 | * object. It is not intended to be a browser sniffing property. |
---|
67 | * Instead, the xmldom property is used instead of <code>document<end> |
---|
68 | * where namespaced node creation methods are not supported. In all |
---|
69 | * other browsers, this remains null. |
---|
70 | */ |
---|
71 | xmldom: null, |
---|
72 | |
---|
73 | /** |
---|
74 | * Constructor: OpenLayers.Format.XML |
---|
75 | * Construct an XML parser. The parser is used to read and write XML. |
---|
76 | * Reading XML from a string returns a DOM element. Writing XML from |
---|
77 | * a DOM element returns a string. |
---|
78 | * |
---|
79 | * Parameters: |
---|
80 | * options - {Object} Optional object whose properties will be set on |
---|
81 | * the object. |
---|
82 | */ |
---|
83 | initialize: function(options) { |
---|
84 | if(window.ActiveXObject) { |
---|
85 | this.xmldom = new ActiveXObject("Microsoft.XMLDOM"); |
---|
86 | } |
---|
87 | OpenLayers.Format.prototype.initialize.apply(this, [options]); |
---|
88 | // clone the namespace object and set all namespace aliases |
---|
89 | this.namespaces = OpenLayers.Util.extend({}, this.namespaces); |
---|
90 | this.namespaceAlias = {}; |
---|
91 | for(var alias in this.namespaces) { |
---|
92 | this.namespaceAlias[this.namespaces[alias]] = alias; |
---|
93 | } |
---|
94 | }, |
---|
95 | |
---|
96 | /** |
---|
97 | * APIMethod: destroy |
---|
98 | * Clean up. |
---|
99 | */ |
---|
100 | destroy: function() { |
---|
101 | this.xmldom = null; |
---|
102 | OpenLayers.Format.prototype.destroy.apply(this, arguments); |
---|
103 | }, |
---|
104 | |
---|
105 | /** |
---|
106 | * Method: setNamespace |
---|
107 | * Set a namespace alias and URI for the format. |
---|
108 | * |
---|
109 | * Parameters: |
---|
110 | * alias - {String} The namespace alias (prefix). |
---|
111 | * uri - {String} The namespace URI. |
---|
112 | */ |
---|
113 | setNamespace: function(alias, uri) { |
---|
114 | this.namespaces[alias] = uri; |
---|
115 | this.namespaceAlias[uri] = alias; |
---|
116 | }, |
---|
117 | |
---|
118 | /** |
---|
119 | * APIMethod: read |
---|
120 | * Deserialize a XML string and return a DOM node. |
---|
121 | * |
---|
122 | * Parameters: |
---|
123 | * text - {String} A XML string |
---|
124 | |
---|
125 | * Returns: |
---|
126 | * {DOMElement} A DOM node |
---|
127 | */ |
---|
128 | read: function(text) { |
---|
129 | var index = text.indexOf('<'); |
---|
130 | if(index > 0) { |
---|
131 | text = text.substring(index); |
---|
132 | } |
---|
133 | var node = OpenLayers.Util.Try( |
---|
134 | OpenLayers.Function.bind(( |
---|
135 | function() { |
---|
136 | var xmldom; |
---|
137 | /** |
---|
138 | * Since we want to be able to call this method on the prototype |
---|
139 | * itself, this.xmldom may not exist even if in IE. |
---|
140 | */ |
---|
141 | if(window.ActiveXObject && !this.xmldom) { |
---|
142 | xmldom = new ActiveXObject("Microsoft.XMLDOM"); |
---|
143 | } else { |
---|
144 | xmldom = this.xmldom; |
---|
145 | |
---|
146 | } |
---|
147 | xmldom.loadXML(text); |
---|
148 | return xmldom; |
---|
149 | } |
---|
150 | ), this), |
---|
151 | function() { |
---|
152 | return new DOMParser().parseFromString(text, 'text/xml'); |
---|
153 | }, |
---|
154 | function() { |
---|
155 | var req = new XMLHttpRequest(); |
---|
156 | req.open("GET", "data:" + "text/xml" + |
---|
157 | ";charset=utf-8," + encodeURIComponent(text), false); |
---|
158 | if(req.overrideMimeType) { |
---|
159 | req.overrideMimeType("text/xml"); |
---|
160 | } |
---|
161 | req.send(null); |
---|
162 | return req.responseXML; |
---|
163 | } |
---|
164 | ); |
---|
165 | |
---|
166 | if(this.keepData) { |
---|
167 | this.data = node; |
---|
168 | } |
---|
169 | |
---|
170 | return node; |
---|
171 | }, |
---|
172 | |
---|
173 | /** |
---|
174 | * APIMethod: write |
---|
175 | * Serialize a DOM node into a XML string. |
---|
176 | * |
---|
177 | * Parameters: |
---|
178 | * node - {DOMElement} A DOM node. |
---|
179 | * |
---|
180 | * Returns: |
---|
181 | * {String} The XML string representation of the input node. |
---|
182 | */ |
---|
183 | write: function(node) { |
---|
184 | var data; |
---|
185 | if(this.xmldom) { |
---|
186 | data = node.xml; |
---|
187 | } else { |
---|
188 | var serializer = new XMLSerializer(); |
---|
189 | if (node.nodeType == 1) { |
---|
190 | // Add nodes to a document before serializing. Everything else |
---|
191 | // is serialized as is. This may need more work. See #1218 . |
---|
192 | var doc = document.implementation.createDocument("", "", null); |
---|
193 | if (doc.importNode) { |
---|
194 | node = doc.importNode(node, true); |
---|
195 | } |
---|
196 | doc.appendChild(node); |
---|
197 | data = serializer.serializeToString(doc); |
---|
198 | } else { |
---|
199 | data = serializer.serializeToString(node); |
---|
200 | } |
---|
201 | } |
---|
202 | return data; |
---|
203 | }, |
---|
204 | |
---|
205 | /** |
---|
206 | * APIMethod: createElementNS |
---|
207 | * Create a new element with namespace. This node can be appended to |
---|
208 | * another node with the standard node.appendChild method. For |
---|
209 | * cross-browser support, this method must be used instead of |
---|
210 | * document.createElementNS. |
---|
211 | * |
---|
212 | * Parameters: |
---|
213 | * uri - {String} Namespace URI for the element. |
---|
214 | * name - {String} The qualified name of the element (prefix:localname). |
---|
215 | * |
---|
216 | * Returns: |
---|
217 | * {Element} A DOM element with namespace. |
---|
218 | */ |
---|
219 | createElementNS: function(uri, name) { |
---|
220 | var element; |
---|
221 | if(this.xmldom) { |
---|
222 | if(typeof uri == "string") { |
---|
223 | element = this.xmldom.createNode(1, name, uri); |
---|
224 | } else { |
---|
225 | element = this.xmldom.createNode(1, name, ""); |
---|
226 | } |
---|
227 | } else { |
---|
228 | element = document.createElementNS(uri, name); |
---|
229 | } |
---|
230 | return element; |
---|
231 | }, |
---|
232 | |
---|
233 | /** |
---|
234 | * APIMethod: createTextNode |
---|
235 | * Create a text node. This node can be appended to another node with |
---|
236 | * the standard node.appendChild method. For cross-browser support, |
---|
237 | * this method must be used instead of document.createTextNode. |
---|
238 | * |
---|
239 | * Parameters: |
---|
240 | * text - {String} The text of the node. |
---|
241 | * |
---|
242 | * Returns: |
---|
243 | * {DOMElement} A DOM text node. |
---|
244 | */ |
---|
245 | createTextNode: function(text) { |
---|
246 | var node; |
---|
247 | if (typeof text !== "string") { |
---|
248 | text = String(text); |
---|
249 | } |
---|
250 | if(this.xmldom) { |
---|
251 | node = this.xmldom.createTextNode(text); |
---|
252 | } else { |
---|
253 | node = document.createTextNode(text); |
---|
254 | } |
---|
255 | return node; |
---|
256 | }, |
---|
257 | |
---|
258 | /** |
---|
259 | * APIMethod: getElementsByTagNameNS |
---|
260 | * Get a list of elements on a node given the namespace URI and local name. |
---|
261 | * To return all nodes in a given namespace, use '*' for the name |
---|
262 | * argument. To return all nodes of a given (local) name, regardless |
---|
263 | * of namespace, use '*' for the uri argument. |
---|
264 | * |
---|
265 | * Parameters: |
---|
266 | * node - {Element} Node on which to search for other nodes. |
---|
267 | * uri - {String} Namespace URI. |
---|
268 | * name - {String} Local name of the tag (without the prefix). |
---|
269 | * |
---|
270 | * Returns: |
---|
271 | * {NodeList} A node list or array of elements. |
---|
272 | */ |
---|
273 | getElementsByTagNameNS: function(node, uri, name) { |
---|
274 | var elements = []; |
---|
275 | if(node.getElementsByTagNameNS) { |
---|
276 | elements = node.getElementsByTagNameNS(uri, name); |
---|
277 | } else { |
---|
278 | // brute force method |
---|
279 | var allNodes = node.getElementsByTagName("*"); |
---|
280 | var potentialNode, fullName; |
---|
281 | for(var i=0, len=allNodes.length; i<len; ++i) { |
---|
282 | potentialNode = allNodes[i]; |
---|
283 | fullName = (potentialNode.prefix) ? |
---|
284 | (potentialNode.prefix + ":" + name) : name; |
---|
285 | if((name == "*") || (fullName == potentialNode.nodeName)) { |
---|
286 | if((uri == "*") || (uri == potentialNode.namespaceURI)) { |
---|
287 | elements.push(potentialNode); |
---|
288 | } |
---|
289 | } |
---|
290 | } |
---|
291 | } |
---|
292 | return elements; |
---|
293 | }, |
---|
294 | |
---|
295 | /** |
---|
296 | * APIMethod: getAttributeNodeNS |
---|
297 | * Get an attribute node given the namespace URI and local name. |
---|
298 | * |
---|
299 | * Parameters: |
---|
300 | * node - {Element} Node on which to search for attribute nodes. |
---|
301 | * uri - {String} Namespace URI. |
---|
302 | * name - {String} Local name of the attribute (without the prefix). |
---|
303 | * |
---|
304 | * Returns: |
---|
305 | * {DOMElement} An attribute node or null if none found. |
---|
306 | */ |
---|
307 | getAttributeNodeNS: function(node, uri, name) { |
---|
308 | var attributeNode = null; |
---|
309 | if(node.getAttributeNodeNS) { |
---|
310 | attributeNode = node.getAttributeNodeNS(uri, name); |
---|
311 | } else { |
---|
312 | var attributes = node.attributes; |
---|
313 | var potentialNode, fullName; |
---|
314 | for(var i=0, len=attributes.length; i<len; ++i) { |
---|
315 | potentialNode = attributes[i]; |
---|
316 | if(potentialNode.namespaceURI == uri) { |
---|
317 | fullName = (potentialNode.prefix) ? |
---|
318 | (potentialNode.prefix + ":" + name) : name; |
---|
319 | if(fullName == potentialNode.nodeName) { |
---|
320 | attributeNode = potentialNode; |
---|
321 | break; |
---|
322 | } |
---|
323 | } |
---|
324 | } |
---|
325 | } |
---|
326 | return attributeNode; |
---|
327 | }, |
---|
328 | |
---|
329 | /** |
---|
330 | * APIMethod: getAttributeNS |
---|
331 | * Get an attribute value given the namespace URI and local name. |
---|
332 | * |
---|
333 | * Parameters: |
---|
334 | * node - {Element} Node on which to search for an attribute. |
---|
335 | * uri - {String} Namespace URI. |
---|
336 | * name - {String} Local name of the attribute (without the prefix). |
---|
337 | * |
---|
338 | * Returns: |
---|
339 | * {String} An attribute value or and empty string if none found. |
---|
340 | */ |
---|
341 | getAttributeNS: function(node, uri, name) { |
---|
342 | var attributeValue = ""; |
---|
343 | if(node.getAttributeNS) { |
---|
344 | attributeValue = node.getAttributeNS(uri, name) || ""; |
---|
345 | } else { |
---|
346 | var attributeNode = this.getAttributeNodeNS(node, uri, name); |
---|
347 | if(attributeNode) { |
---|
348 | attributeValue = attributeNode.nodeValue; |
---|
349 | } |
---|
350 | } |
---|
351 | return attributeValue; |
---|
352 | }, |
---|
353 | |
---|
354 | /** |
---|
355 | * APIMethod: getChildValue |
---|
356 | * Get the textual value of the node if it exists, or return an |
---|
357 | * optional default string. Returns an empty string if no first child |
---|
358 | * exists and no default value is supplied. |
---|
359 | * |
---|
360 | * Parameters: |
---|
361 | * node - {DOMElement} The element used to look for a first child value. |
---|
362 | * def - {String} Optional string to return in the event that no |
---|
363 | * first child value exists. |
---|
364 | * |
---|
365 | * Returns: |
---|
366 | * {String} The value of the first child of the given node. |
---|
367 | */ |
---|
368 | getChildValue: function(node, def) { |
---|
369 | var value = def || ""; |
---|
370 | if(node) { |
---|
371 | for(var child=node.firstChild; child; child=child.nextSibling) { |
---|
372 | switch(child.nodeType) { |
---|
373 | case 3: // text node |
---|
374 | case 4: // cdata section |
---|
375 | value += child.nodeValue; |
---|
376 | } |
---|
377 | } |
---|
378 | } |
---|
379 | return value; |
---|
380 | }, |
---|
381 | |
---|
382 | /** |
---|
383 | * APIMethod: concatChildValues |
---|
384 | * *Deprecated*. Use <getChildValue> instead. |
---|
385 | * |
---|
386 | * Concatenate the value of all child nodes if any exist, or return an |
---|
387 | * optional default string. Returns an empty string if no children |
---|
388 | * exist and no default value is supplied. Not optimized for large |
---|
389 | * numbers of child nodes. |
---|
390 | * |
---|
391 | * Parameters: |
---|
392 | * node - {DOMElement} The element used to look for child values. |
---|
393 | * def - {String} Optional string to return in the event that no |
---|
394 | * child exist. |
---|
395 | * |
---|
396 | * Returns: |
---|
397 | * {String} The concatenated value of all child nodes of the given node. |
---|
398 | */ |
---|
399 | concatChildValues: function(node, def) { |
---|
400 | var value = ""; |
---|
401 | var child = node.firstChild; |
---|
402 | var childValue; |
---|
403 | while(child) { |
---|
404 | childValue = child.nodeValue; |
---|
405 | if(childValue) { |
---|
406 | value += childValue; |
---|
407 | } |
---|
408 | child = child.nextSibling; |
---|
409 | } |
---|
410 | if(value == "" && def != undefined) { |
---|
411 | value = def; |
---|
412 | } |
---|
413 | return value; |
---|
414 | }, |
---|
415 | |
---|
416 | /** |
---|
417 | * APIMethod: isSimpleContent |
---|
418 | * Test if the given node has only simple content (i.e. no child element |
---|
419 | * nodes). |
---|
420 | * |
---|
421 | * Parameters: |
---|
422 | * node - {DOMElement} An element node. |
---|
423 | * |
---|
424 | * Returns: |
---|
425 | * {Boolean} The node has no child element nodes (nodes of type 1). |
---|
426 | */ |
---|
427 | isSimpleContent: function(node) { |
---|
428 | var simple = true; |
---|
429 | for(var child=node.firstChild; child; child=child.nextSibling) { |
---|
430 | if(child.nodeType === 1) { |
---|
431 | simple = false; |
---|
432 | break; |
---|
433 | } |
---|
434 | } |
---|
435 | return simple; |
---|
436 | }, |
---|
437 | |
---|
438 | /** |
---|
439 | * APIMethod: contentType |
---|
440 | * Determine the content type for a given node. |
---|
441 | * |
---|
442 | * Parameters: |
---|
443 | * node - {DOMElement} |
---|
444 | * |
---|
445 | * Returns: |
---|
446 | * {Integer} One of OpenLayers.Format.XML.CONTENT_TYPE.{EMPTY,SIMPLE,COMPLEX,MIXED} |
---|
447 | * if the node has no, simple, complex, or mixed content. |
---|
448 | */ |
---|
449 | contentType: function(node) { |
---|
450 | var simple = false, |
---|
451 | complex = false; |
---|
452 | |
---|
453 | var type = OpenLayers.Format.XML.CONTENT_TYPE.EMPTY; |
---|
454 | |
---|
455 | for(var child=node.firstChild; child; child=child.nextSibling) { |
---|
456 | switch(child.nodeType) { |
---|
457 | case 1: // element |
---|
458 | complex = true; |
---|
459 | break; |
---|
460 | case 8: // comment |
---|
461 | break; |
---|
462 | default: |
---|
463 | simple = true; |
---|
464 | } |
---|
465 | if(complex && simple) { |
---|
466 | break; |
---|
467 | } |
---|
468 | } |
---|
469 | |
---|
470 | if(complex && simple) { |
---|
471 | type = OpenLayers.Format.XML.CONTENT_TYPE.MIXED; |
---|
472 | } else if(complex) { |
---|
473 | return OpenLayers.Format.XML.CONTENT_TYPE.COMPLEX; |
---|
474 | } else if(simple) { |
---|
475 | return OpenLayers.Format.XML.CONTENT_TYPE.SIMPLE; |
---|
476 | } |
---|
477 | return type; |
---|
478 | }, |
---|
479 | |
---|
480 | /** |
---|
481 | * APIMethod: hasAttributeNS |
---|
482 | * Determine whether a node has a particular attribute matching the given |
---|
483 | * name and namespace. |
---|
484 | * |
---|
485 | * Parameters: |
---|
486 | * node - {Element} Node on which to search for an attribute. |
---|
487 | * uri - {String} Namespace URI. |
---|
488 | * name - {String} Local name of the attribute (without the prefix). |
---|
489 | * |
---|
490 | * Returns: |
---|
491 | * {Boolean} The node has an attribute matching the name and namespace. |
---|
492 | */ |
---|
493 | hasAttributeNS: function(node, uri, name) { |
---|
494 | var found = false; |
---|
495 | if(node.hasAttributeNS) { |
---|
496 | found = node.hasAttributeNS(uri, name); |
---|
497 | } else { |
---|
498 | found = !!this.getAttributeNodeNS(node, uri, name); |
---|
499 | } |
---|
500 | return found; |
---|
501 | }, |
---|
502 | |
---|
503 | /** |
---|
504 | * APIMethod: setAttributeNS |
---|
505 | * Adds a new attribute or changes the value of an attribute with the given |
---|
506 | * namespace and name. |
---|
507 | * |
---|
508 | * Parameters: |
---|
509 | * node - {Element} Element node on which to set the attribute. |
---|
510 | * uri - {String} Namespace URI for the attribute. |
---|
511 | * name - {String} Qualified name (prefix:localname) for the attribute. |
---|
512 | * value - {String} Attribute value. |
---|
513 | */ |
---|
514 | setAttributeNS: function(node, uri, name, value) { |
---|
515 | if(node.setAttributeNS) { |
---|
516 | node.setAttributeNS(uri, name, value); |
---|
517 | } else { |
---|
518 | if(this.xmldom) { |
---|
519 | if(uri) { |
---|
520 | var attribute = node.ownerDocument.createNode( |
---|
521 | 2, name, uri |
---|
522 | ); |
---|
523 | attribute.nodeValue = value; |
---|
524 | node.setAttributeNode(attribute); |
---|
525 | } else { |
---|
526 | node.setAttribute(name, value); |
---|
527 | } |
---|
528 | } else { |
---|
529 | throw "setAttributeNS not implemented"; |
---|
530 | } |
---|
531 | } |
---|
532 | }, |
---|
533 | |
---|
534 | /** |
---|
535 | * Method: createElementNSPlus |
---|
536 | * Shorthand for creating namespaced elements with optional attributes and |
---|
537 | * child text nodes. |
---|
538 | * |
---|
539 | * Parameters: |
---|
540 | * name - {String} The qualified node name. |
---|
541 | * options - {Object} Optional object for node configuration. |
---|
542 | * |
---|
543 | * Valid options: |
---|
544 | * uri - {String} Optional namespace uri for the element - supply a prefix |
---|
545 | * instead if the namespace uri is a property of the format's namespace |
---|
546 | * object. |
---|
547 | * attributes - {Object} Optional attributes to be set using the |
---|
548 | * <setAttributes> method. |
---|
549 | * value - {String} Optional text to be appended as a text node. |
---|
550 | * |
---|
551 | * Returns: |
---|
552 | * {Element} An element node. |
---|
553 | */ |
---|
554 | createElementNSPlus: function(name, options) { |
---|
555 | options = options || {}; |
---|
556 | // order of prefix preference |
---|
557 | // 1. in the uri option |
---|
558 | // 2. in the prefix option |
---|
559 | // 3. in the qualified name |
---|
560 | // 4. from the defaultPrefix |
---|
561 | var uri = options.uri || this.namespaces[options.prefix]; |
---|
562 | if(!uri) { |
---|
563 | var loc = name.indexOf(":"); |
---|
564 | uri = this.namespaces[name.substring(0, loc)]; |
---|
565 | } |
---|
566 | if(!uri) { |
---|
567 | uri = this.namespaces[this.defaultPrefix]; |
---|
568 | } |
---|
569 | var node = this.createElementNS(uri, name); |
---|
570 | if(options.attributes) { |
---|
571 | this.setAttributes(node, options.attributes); |
---|
572 | } |
---|
573 | var value = options.value; |
---|
574 | if(value != null) { |
---|
575 | node.appendChild(this.createTextNode(value)); |
---|
576 | } |
---|
577 | return node; |
---|
578 | }, |
---|
579 | |
---|
580 | /** |
---|
581 | * Method: setAttributes |
---|
582 | * Set multiple attributes given key value pairs from an object. |
---|
583 | * |
---|
584 | * Parameters: |
---|
585 | * node - {Element} An element node. |
---|
586 | * obj - {Object || Array} An object whose properties represent attribute |
---|
587 | * names and values represent attribute values. If an attribute name |
---|
588 | * is a qualified name ("prefix:local"), the prefix will be looked up |
---|
589 | * in the parsers {namespaces} object. If the prefix is found, |
---|
590 | * setAttributeNS will be used instead of setAttribute. |
---|
591 | */ |
---|
592 | setAttributes: function(node, obj) { |
---|
593 | var value, uri; |
---|
594 | for(var name in obj) { |
---|
595 | if(obj[name] != null && obj[name].toString) { |
---|
596 | value = obj[name].toString(); |
---|
597 | // check for qualified attribute name ("prefix:local") |
---|
598 | uri = this.namespaces[name.substring(0, name.indexOf(":"))] || null; |
---|
599 | this.setAttributeNS(node, uri, name, value); |
---|
600 | } |
---|
601 | } |
---|
602 | }, |
---|
603 | |
---|
604 | /** |
---|
605 | * Method: readNode |
---|
606 | * Shorthand for applying one of the named readers given the node |
---|
607 | * namespace and local name. Readers take two args (node, obj) and |
---|
608 | * generally extend or modify the second. |
---|
609 | * |
---|
610 | * Parameters: |
---|
611 | * node - {DOMElement} The node to be read (required). |
---|
612 | * obj - {Object} The object to be modified (optional). |
---|
613 | * |
---|
614 | * Returns: |
---|
615 | * {Object} The input object, modified (or a new one if none was provided). |
---|
616 | */ |
---|
617 | readNode: function(node, obj) { |
---|
618 | if(!obj) { |
---|
619 | obj = {}; |
---|
620 | } |
---|
621 | var group = this.readers[node.namespaceURI ? this.namespaceAlias[node.namespaceURI]: this.defaultPrefix]; |
---|
622 | if(group) { |
---|
623 | var local = node.localName || node.nodeName.split(":").pop(); |
---|
624 | var reader = group[local] || group["*"]; |
---|
625 | if(reader) { |
---|
626 | reader.apply(this, [node, obj]); |
---|
627 | } |
---|
628 | } |
---|
629 | return obj; |
---|
630 | }, |
---|
631 | |
---|
632 | /** |
---|
633 | * Method: readChildNodes |
---|
634 | * Shorthand for applying the named readers to all children of a node. |
---|
635 | * For each child of type 1 (element), <readSelf> is called. |
---|
636 | * |
---|
637 | * Parameters: |
---|
638 | * node - {DOMElement} The node to be read (required). |
---|
639 | * obj - {Object} The object to be modified (optional). |
---|
640 | * |
---|
641 | * Returns: |
---|
642 | * {Object} The input object, modified. |
---|
643 | */ |
---|
644 | readChildNodes: function(node, obj) { |
---|
645 | if(!obj) { |
---|
646 | obj = {}; |
---|
647 | } |
---|
648 | var children = node.childNodes; |
---|
649 | var child; |
---|
650 | for(var i=0, len=children.length; i<len; ++i) { |
---|
651 | child = children[i]; |
---|
652 | if(child.nodeType == 1) { |
---|
653 | this.readNode(child, obj); |
---|
654 | } |
---|
655 | } |
---|
656 | return obj; |
---|
657 | }, |
---|
658 | |
---|
659 | /** |
---|
660 | * Method: writeNode |
---|
661 | * Shorthand for applying one of the named writers and appending the |
---|
662 | * results to a node. If a qualified name is not provided for the |
---|
663 | * second argument (and a local name is used instead), the namespace |
---|
664 | * of the parent node will be assumed. |
---|
665 | * |
---|
666 | * Parameters: |
---|
667 | * name - {String} The name of a node to generate. If a qualified name |
---|
668 | * (e.g. "pre:Name") is used, the namespace prefix is assumed to be |
---|
669 | * in the <writers> group. If a local name is used (e.g. "Name") then |
---|
670 | * the namespace of the parent is assumed. If a local name is used |
---|
671 | * and no parent is supplied, then the default namespace is assumed. |
---|
672 | * obj - {Object} Structure containing data for the writer. |
---|
673 | * parent - {DOMElement} Result will be appended to this node. If no parent |
---|
674 | * is supplied, the node will not be appended to anything. |
---|
675 | * |
---|
676 | * Returns: |
---|
677 | * {DOMElement} The child node. |
---|
678 | */ |
---|
679 | writeNode: function(name, obj, parent) { |
---|
680 | var prefix, local; |
---|
681 | var split = name.indexOf(":"); |
---|
682 | if(split > 0) { |
---|
683 | prefix = name.substring(0, split); |
---|
684 | local = name.substring(split + 1); |
---|
685 | } else { |
---|
686 | if(parent) { |
---|
687 | prefix = this.namespaceAlias[parent.namespaceURI]; |
---|
688 | } else { |
---|
689 | prefix = this.defaultPrefix; |
---|
690 | } |
---|
691 | local = name; |
---|
692 | } |
---|
693 | var child = this.writers[prefix][local].apply(this, [obj]); |
---|
694 | if(parent) { |
---|
695 | parent.appendChild(child); |
---|
696 | } |
---|
697 | return child; |
---|
698 | }, |
---|
699 | |
---|
700 | /** |
---|
701 | * APIMethod: getChildEl |
---|
702 | * Get the first child element. Optionally only return the first child |
---|
703 | * if it matches the given name and namespace URI. |
---|
704 | * |
---|
705 | * Parameters: |
---|
706 | * node - {DOMElement} The parent node. |
---|
707 | * name - {String} Optional node name (local) to search for. |
---|
708 | * uri - {String} Optional namespace URI to search for. |
---|
709 | * |
---|
710 | * Returns: |
---|
711 | * {DOMElement} The first child. Returns null if no element is found, if |
---|
712 | * something significant besides an element is found, or if the element |
---|
713 | * found does not match the optional name and uri. |
---|
714 | */ |
---|
715 | getChildEl: function(node, name, uri) { |
---|
716 | return node && this.getThisOrNextEl(node.firstChild, name, uri); |
---|
717 | }, |
---|
718 | |
---|
719 | /** |
---|
720 | * APIMethod: getNextEl |
---|
721 | * Get the next sibling element. Optionally get the first sibling only |
---|
722 | * if it matches the given local name and namespace URI. |
---|
723 | * |
---|
724 | * Parameters: |
---|
725 | * node - {DOMElement} The node. |
---|
726 | * name - {String} Optional local name of the sibling to search for. |
---|
727 | * uri - {String} Optional namespace URI of the sibling to search for. |
---|
728 | * |
---|
729 | * Returns: |
---|
730 | * {DOMElement} The next sibling element. Returns null if no element is |
---|
731 | * found, something significant besides an element is found, or the |
---|
732 | * found element does not match the optional name and uri. |
---|
733 | */ |
---|
734 | getNextEl: function(node, name, uri) { |
---|
735 | return node && this.getThisOrNextEl(node.nextSibling, name, uri); |
---|
736 | }, |
---|
737 | |
---|
738 | /** |
---|
739 | * Method: getThisOrNextEl |
---|
740 | * Return this node or the next element node. Optionally get the first |
---|
741 | * sibling with the given local name or namespace URI. |
---|
742 | * |
---|
743 | * Parameters: |
---|
744 | * node - {DOMElement} The node. |
---|
745 | * name - {String} Optional local name of the sibling to search for. |
---|
746 | * uri - {String} Optional namespace URI of the sibling to search for. |
---|
747 | * |
---|
748 | * Returns: |
---|
749 | * {DOMElement} The next sibling element. Returns null if no element is |
---|
750 | * found, something significant besides an element is found, or the |
---|
751 | * found element does not match the query. |
---|
752 | */ |
---|
753 | getThisOrNextEl: function(node, name, uri) { |
---|
754 | outer: for(var sibling=node; sibling; sibling=sibling.nextSibling) { |
---|
755 | switch(sibling.nodeType) { |
---|
756 | case 1: // Element |
---|
757 | if((!name || name === (sibling.localName || sibling.nodeName.split(":").pop())) && |
---|
758 | (!uri || uri === sibling.namespaceURI)) { |
---|
759 | // matches |
---|
760 | break outer; |
---|
761 | } |
---|
762 | sibling = null; |
---|
763 | break outer; |
---|
764 | case 3: // Text |
---|
765 | if(/^\s*$/.test(sibling.nodeValue)) { |
---|
766 | break; |
---|
767 | } |
---|
768 | case 4: // CDATA |
---|
769 | case 6: // ENTITY_NODE |
---|
770 | case 12: // NOTATION_NODE |
---|
771 | case 10: // DOCUMENT_TYPE_NODE |
---|
772 | case 11: // DOCUMENT_FRAGMENT_NODE |
---|
773 | sibling = null; |
---|
774 | break outer; |
---|
775 | } // ignore comments and processing instructions |
---|
776 | } |
---|
777 | return sibling || null; |
---|
778 | }, |
---|
779 | |
---|
780 | /** |
---|
781 | * APIMethod: lookupNamespaceURI |
---|
782 | * Takes a prefix and returns the namespace URI associated with it on the given |
---|
783 | * node if found (and null if not). Supplying null for the prefix will |
---|
784 | * return the default namespace. |
---|
785 | * |
---|
786 | * For browsers that support it, this calls the native lookupNamesapceURI |
---|
787 | * function. In other browsers, this is an implementation of |
---|
788 | * http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-lookupNamespaceURI. |
---|
789 | * |
---|
790 | * For browsers that don't support the attribute.ownerElement property, this |
---|
791 | * method cannot be called on attribute nodes. |
---|
792 | * |
---|
793 | * Parameters: |
---|
794 | * node - {DOMElement} The node from which to start looking. |
---|
795 | * prefix - {String} The prefix to lookup or null to lookup the default namespace. |
---|
796 | * |
---|
797 | * Returns: |
---|
798 | * {String} The namespace URI for the given prefix. Returns null if the prefix |
---|
799 | * cannot be found or the node is the wrong type. |
---|
800 | */ |
---|
801 | lookupNamespaceURI: function(node, prefix) { |
---|
802 | var uri = null; |
---|
803 | if(node) { |
---|
804 | if(node.lookupNamespaceURI) { |
---|
805 | uri = node.lookupNamespaceURI(prefix); |
---|
806 | } else { |
---|
807 | outer: switch(node.nodeType) { |
---|
808 | case 1: // ELEMENT_NODE |
---|
809 | if(node.namespaceURI !== null && node.prefix === prefix) { |
---|
810 | uri = node.namespaceURI; |
---|
811 | break outer; |
---|
812 | } |
---|
813 | var len = node.attributes.length; |
---|
814 | if(len) { |
---|
815 | var attr; |
---|
816 | for(var i=0; i<len; ++i) { |
---|
817 | attr = node.attributes[i]; |
---|
818 | if(attr.prefix === "xmlns" && attr.name === "xmlns:" + prefix) { |
---|
819 | uri = attr.value || null; |
---|
820 | break outer; |
---|
821 | } else if(attr.name === "xmlns" && prefix === null) { |
---|
822 | uri = attr.value || null; |
---|
823 | break outer; |
---|
824 | } |
---|
825 | } |
---|
826 | } |
---|
827 | uri = this.lookupNamespaceURI(node.parentNode, prefix); |
---|
828 | break outer; |
---|
829 | case 2: // ATTRIBUTE_NODE |
---|
830 | uri = this.lookupNamespaceURI(node.ownerElement, prefix); |
---|
831 | break outer; |
---|
832 | case 9: // DOCUMENT_NODE |
---|
833 | uri = this.lookupNamespaceURI(node.documentElement, prefix); |
---|
834 | break outer; |
---|
835 | case 6: // ENTITY_NODE |
---|
836 | case 12: // NOTATION_NODE |
---|
837 | case 10: // DOCUMENT_TYPE_NODE |
---|
838 | case 11: // DOCUMENT_FRAGMENT_NODE |
---|
839 | break outer; |
---|
840 | default: |
---|
841 | // TEXT_NODE (3), CDATA_SECTION_NODE (4), ENTITY_REFERENCE_NODE (5), |
---|
842 | // PROCESSING_INSTRUCTION_NODE (7), COMMENT_NODE (8) |
---|
843 | uri = this.lookupNamespaceURI(node.parentNode, prefix); |
---|
844 | break outer; |
---|
845 | } |
---|
846 | } |
---|
847 | } |
---|
848 | return uri; |
---|
849 | }, |
---|
850 | |
---|
851 | CLASS_NAME: "OpenLayers.Format.XML" |
---|
852 | |
---|
853 | }); |
---|
854 | |
---|
855 | OpenLayers.Format.XML.CONTENT_TYPE = {EMPTY: 0, SIMPLE: 1, COMPLEX: 2, MIXED: 3}; |
---|
856 | |
---|
857 | /** |
---|
858 | * APIFunction: OpenLayers.Format.XML.lookupNamespaceURI |
---|
859 | * Takes a prefix and returns the namespace URI associated with it on the given |
---|
860 | * node if found (and null if not). Supplying null for the prefix will |
---|
861 | * return the default namespace. |
---|
862 | * |
---|
863 | * For browsers that support it, this calls the native lookupNamesapceURI |
---|
864 | * function. In other browsers, this is an implementation of |
---|
865 | * http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-lookupNamespaceURI. |
---|
866 | * |
---|
867 | * For browsers that don't support the attribute.ownerElement property, this |
---|
868 | * method cannot be called on attribute nodes. |
---|
869 | * |
---|
870 | * Parameters: |
---|
871 | * node - {DOMElement} The node from which to start looking. |
---|
872 | * prefix - {String} The prefix to lookup or null to lookup the default namespace. |
---|
873 | * |
---|
874 | * Returns: |
---|
875 | * {String} The namespace URI for the given prefix. Returns null if the prefix |
---|
876 | * cannot be found or the node is the wrong type. |
---|
877 | */ |
---|
878 | OpenLayers.Format.XML.lookupNamespaceURI = OpenLayers.Function.bind( |
---|
879 | OpenLayers.Format.XML.prototype.lookupNamespaceURI, |
---|
880 | OpenLayers.Format.XML.prototype |
---|
881 | ); |
---|