1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760 | 1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
851×
1×
1×
1×
2564×
1×
5121×
2557×
2557×
2564×
2564×
2557×
5121×
1×
1071×
1×
1×
1×
188×
1×
1×
1×
1407×
1×
1×
1×
83×
1×
1×
1×
1×
1×
1×
154×
1×
1×
1×
254×
1×
58×
6×
6×
6×
6×
6×
6×
1×
139×
629×
629×
569×
569×
569×
1×
452×
697×
697×
694×
1×
26×
26×
254×
254×
254×
254×
254×
254×
254×
1×
187×
187×
1536×
1536×
1530×
1530×
1530×
1530×
1×
139×
600×
600×
600×
600×
600×
600×
1×
2×
2×
8×
2×
2×
2×
2×
2×
8×
1×
48×
48×
462×
462×
462×
462×
462×
359×
462×
462×
216×
462×
462×
1×
1×
102×
102×
102×
755×
102×
1×
147×
147×
147×
515×
147×
1×
2001×
2001×
3815×
3815×
3795×
3795×
3474×
1×
1926×
1926×
1926×
1×
341×
341×
341×
1258×
1258×
610×
610×
608×
1×
333×
333×
333×
| goog.provide('ol.xml');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.dom.NodeType');
goog.require('goog.dom.xml');
goog.require('goog.object');
goog.require('goog.userAgent');
/**
* When using {@link ol.xml.makeChildAppender} or
* {@link ol.xml.makeSimpleNodeFactory}, the top `objectStack` item needs to
* have this structure.
* @typedef {{node:Node}}
*/
ol.xml.NodeStackItem;
/**
* @typedef {function(Node, Array.<*>)}
*/
ol.xml.Parser;
/**
* @typedef {function(Node, *, Array.<*>)}
*/
ol.xml.Serializer;
/**
* This document should be used when creating nodes for XML serializations. This
* document is also used by {@link ol.xml.createElementNS} and
* {@link ol.xml.setAttributeNS}
* @const
* @type {Document}
*/
ol.xml.DOCUMENT = goog.dom.xml.createDocument();
/**
* @param {string} namespaceURI Namespace URI.
* @param {string} qualifiedName Qualified name.
* @return {Node} Node.
* @private
*/
ol.xml.createElementNS_ = function(namespaceURI, qualifiedName) {
return ol.xml.DOCUMENT.createElementNS(namespaceURI, qualifiedName);
};
/**
* @param {string} namespaceURI Namespace URI.
* @param {string} qualifiedName Qualified name.
* @return {Node} Node.
* @private
*/
ol.xml.createElementNSActiveX_ = function(namespaceURI, qualifiedName) {
if (!namespaceURI) {
namespaceURI = '';
}
return ol.xml.DOCUMENT.createNode(1, qualifiedName, namespaceURI);
};
/**
* @param {string} namespaceURI Namespace URI.
* @param {string} qualifiedName Qualified name.
* @return {Node} Node.
*/
ol.xml.createElementNS =
(document.implementation && document.implementation.createDocument) ?
ol.xml.createElementNS_ : ol.xml.createElementNSActiveX_;
/**
* Recursively grab all text content of child nodes into a single string.
* @param {Node} node Node.
* @param {boolean} normalizeWhitespace Normalize whitespace: remove all line
* breaks.
* @return {string} All text content.
* @api
*/
ol.xml.getAllTextContent = function(node, normalizeWhitespace) {
return ol.xml.getAllTextContent_(node, normalizeWhitespace, []).join('');
};
/**
* Recursively grab all text content of child nodes into a single string.
* @param {Node} node Node.
* @param {boolean} normalizeWhitespace Normalize whitespace: remove all line
* breaks.
* @param {Array.<String|string>} accumulator Accumulator.
* @private
* @return {Array.<String|string>} Accumulator.
*/
ol.xml.getAllTextContent_ = function(node, normalizeWhitespace, accumulator) {
if (node.nodeType == goog.dom.NodeType.CDATA_SECTION ||
node.nodeType == goog.dom.NodeType.TEXT) {
Iif (normalizeWhitespace) {
// FIXME understand why goog.dom.getTextContent_ uses String here
accumulator.push(String(node.nodeValue).replace(/(\r\n|\r|\n)/g, ''));
} else {
accumulator.push(node.nodeValue);
}
} else {
var n;
for (n = node.firstChild; n; n = n.nextSibling) {
ol.xml.getAllTextContent_(n, normalizeWhitespace, accumulator);
}
}
return accumulator;
};
/**
* @param {Node} node Node.
* @private
* @return {string} Local name.
*/
ol.xml.getLocalName_ = function(node) {
return node.localName;
};
/**
* @param {Node} node Node.
* @private
* @return {string} Local name.
*/
ol.xml.getLocalNameIE_ = function(node) {
var localName = node.localName;
if (localName !== undefined) {
return localName;
}
var baseName = node.baseName;
goog.asserts.assert(baseName,
'Failed to get localName/baseName of node %s', node);
return baseName;
};
/**
* @param {Node} node Node.
* @return {string} Local name.
*/
ol.xml.getLocalName = goog.userAgent.IE ?
ol.xml.getLocalNameIE_ : ol.xml.getLocalName_;
/**
* @param {?} value Value.
* @private
* @return {boolean} Is document.
*/
ol.xml.isDocument_ = function(value) {
return value instanceof Document;
};
/**
* @param {?} value Value.
* @private
* @return {boolean} Is document.
*/
ol.xml.isDocumentIE_ = function(value) {
return goog.isObject(value) && value.nodeType == goog.dom.NodeType.DOCUMENT;
};
/**
* @param {?} value Value.
* @return {boolean} Is document.
*/
ol.xml.isDocument = goog.userAgent.IE ?
ol.xml.isDocumentIE_ : ol.xml.isDocument_;
/**
* @param {?} value Value.
* @private
* @return {boolean} Is node.
*/
ol.xml.isNode_ = function(value) {
return value instanceof Node;
};
/**
* @param {?} value Value.
* @private
* @return {boolean} Is node.
*/
ol.xml.isNodeIE_ = function(value) {
return goog.isObject(value) && value.nodeType !== undefined;
};
/**
* @param {?} value Value.
* @return {boolean} Is node.
*/
ol.xml.isNode = goog.userAgent.IE ? ol.xml.isNodeIE_ : ol.xml.isNode_;
/**
* @param {Node} node Node.
* @param {?string} namespaceURI Namespace URI.
* @param {string} name Attribute name.
* @return {string} Value
* @private
*/
ol.xml.getAttributeNS_ = function(node, namespaceURI, name) {
return node.getAttributeNS(namespaceURI, name) || '';
};
/**
* @param {Node} node Node.
* @param {?string} namespaceURI Namespace URI.
* @param {string} name Attribute name.
* @return {string} Value
* @private
*/
ol.xml.getAttributeNSActiveX_ = function(node, namespaceURI, name) {
var attributeValue = '';
var attributeNode = ol.xml.getAttributeNodeNS(node, namespaceURI, name);
if (attributeNode !== undefined) {
attributeValue = attributeNode.nodeValue;
}
return attributeValue;
};
/**
* @param {Node} node Node.
* @param {?string} namespaceURI Namespace URI.
* @param {string} name Attribute name.
* @return {string} Value
*/
ol.xml.getAttributeNS =
(document.implementation && document.implementation.createDocument) ?
ol.xml.getAttributeNS_ : ol.xml.getAttributeNSActiveX_;
/**
* @param {Node} node Node.
* @param {?string} namespaceURI Namespace URI.
* @param {string} name Attribute name.
* @return {?Node} Attribute node or null if none found.
* @private
*/
ol.xml.getAttributeNodeNS_ = function(node, namespaceURI, name) {
return node.getAttributeNodeNS(namespaceURI, name);
};
/**
* @param {Node} node Node.
* @param {?string} namespaceURI Namespace URI.
* @param {string} name Attribute name.
* @return {?Node} Attribute node or null if none found.
* @private
*/
ol.xml.getAttributeNodeNSActiveX_ = function(node, namespaceURI, name) {
var attributeNode = null;
var attributes = node.attributes;
var potentialNode, fullName;
for (var i = 0, len = attributes.length; i < len; ++i) {
potentialNode = attributes[i];
if (potentialNode.namespaceURI == namespaceURI) {
fullName = (potentialNode.prefix) ?
(potentialNode.prefix + ':' + name) : name;
if (fullName == potentialNode.nodeName) {
attributeNode = potentialNode;
break;
}
}
}
return attributeNode;
};
/**
* @param {Node} node Node.
* @param {?string} namespaceURI Namespace URI.
* @param {string} name Attribute name.
* @return {?Node} Attribute node or null if none found.
*/
ol.xml.getAttributeNodeNS =
(document.implementation && document.implementation.createDocument) ?
ol.xml.getAttributeNodeNS_ : ol.xml.getAttributeNodeNSActiveX_;
/**
* @param {Node} node Node.
* @param {?string} namespaceURI Namespace URI.
* @param {string} name Attribute name.
* @param {string|number} value Value.
* @private
*/
ol.xml.setAttributeNS_ = function(node, namespaceURI, name, value) {
node.setAttributeNS(namespaceURI, name, value);
};
/**
* @param {Node} node Node.
* @param {?string} namespaceURI Namespace URI.
* @param {string} name Attribute name.
* @param {string|number} value Value.
* @private
*/
ol.xml.setAttributeNSActiveX_ = function(node, namespaceURI, name, value) {
if (namespaceURI) {
var attribute = node.ownerDocument.createNode(2, name, namespaceURI);
attribute.nodeValue = value;
node.setAttributeNode(attribute);
} else {
node.setAttribute(name, value);
}
};
/**
* @param {Node} node Node.
* @param {?string} namespaceURI Namespace URI.
* @param {string} name Attribute name.
* @param {string|number} value Value.
*/
ol.xml.setAttributeNS =
(document.implementation && document.implementation.createDocument) ?
ol.xml.setAttributeNS_ : ol.xml.setAttributeNSActiveX_;
/**
* Parse an XML string to an XML Document.
* @param {string} xml XML.
* @return {Document} Document.
* @api
*/
ol.xml.parse = function(xml) {
return new DOMParser().parseFromString(xml, 'application/xml');
};
/**
* Make an array extender function for extending the array at the top of the
* object stack.
* @param {function(this: T, Node, Array.<*>): (Array.<*>|undefined)}
* valueReader Value reader.
* @param {T=} opt_this The object to use as `this` in `valueReader`.
* @return {ol.xml.Parser} Parser.
* @template T
*/
ol.xml.makeArrayExtender = function(valueReader, opt_this) {
return (
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
*/
function(node, objectStack) {
var value = valueReader.call(opt_this, node, objectStack);
Eif (value !== undefined) {
goog.asserts.assert(goog.isArray(value),
'valueReader function is expected to return an array of values');
var array = /** @type {Array.<*>} */
(objectStack[objectStack.length - 1]);
goog.asserts.assert(goog.isArray(array),
'objectStack is supposed to be an array of arrays');
goog.array.extend(array, value);
}
});
};
/**
* Make an array pusher function for pushing to the array at the top of the
* object stack.
* @param {function(this: T, Node, Array.<*>): *} valueReader Value reader.
* @param {T=} opt_this The object to use as `this` in `valueReader`.
* @return {ol.xml.Parser} Parser.
* @template T
*/
ol.xml.makeArrayPusher = function(valueReader, opt_this) {
return (
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
*/
function(node, objectStack) {
var value = valueReader.call(opt_this !== undefined ? opt_this : this,
node, objectStack);
if (value !== undefined) {
var array = objectStack[objectStack.length - 1];
goog.asserts.assert(goog.isArray(array),
'objectStack is supposed to be an array of arrays');
array.push(value);
}
});
};
/**
* Make an object stack replacer function for replacing the object at the
* top of the stack.
* @param {function(this: T, Node, Array.<*>): *} valueReader Value reader.
* @param {T=} opt_this The object to use as `this` in `valueReader`.
* @return {ol.xml.Parser} Parser.
* @template T
*/
ol.xml.makeReplacer = function(valueReader, opt_this) {
return (
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
*/
function(node, objectStack) {
var value = valueReader.call(opt_this !== undefined ? opt_this : this,
node, objectStack);
if (value !== undefined) {
objectStack[objectStack.length - 1] = value;
}
});
};
/**
* Make an object property pusher function for adding a property to the
* object at the top of the stack.
* @param {function(this: T, Node, Array.<*>): *} valueReader Value reader.
* @param {string=} opt_property Property.
* @param {T=} opt_this The object to use as `this` in `valueReader`.
* @return {ol.xml.Parser} Parser.
* @template T
*/
ol.xml.makeObjectPropertyPusher =
function(valueReader, opt_property, opt_this) {
goog.asserts.assert(valueReader !== undefined,
'undefined valueReader, expected function(this: T, Node, Array.<*>)');
return (
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
*/
function(node, objectStack) {
var value = valueReader.call(opt_this !== undefined ? opt_this : this,
node, objectStack);
Eif (value !== undefined) {
var object = /** @type {Object} */
(objectStack[objectStack.length - 1]);
var property = opt_property !== undefined ?
opt_property : node.localName;
goog.asserts.assert(goog.isObject(object),
'entity from stack was not an object');
var array = goog.object.setIfUndefined(object, property, []);
array.push(value);
}
});
};
/**
* Make an object property setter function.
* @param {function(this: T, Node, Array.<*>): *} valueReader Value reader.
* @param {string=} opt_property Property.
* @param {T=} opt_this The object to use as `this` in `valueReader`.
* @return {ol.xml.Parser} Parser.
* @template T
*/
ol.xml.makeObjectPropertySetter =
function(valueReader, opt_property, opt_this) {
goog.asserts.assert(valueReader !== undefined,
'undefined valueReader, expected function(this: T, Node, Array.<*>)');
return (
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
*/
function(node, objectStack) {
var value = valueReader.call(opt_this !== undefined ? opt_this : this,
node, objectStack);
if (value !== undefined) {
var object = /** @type {Object} */
(objectStack[objectStack.length - 1]);
var property = opt_property !== undefined ?
opt_property : node.localName;
goog.asserts.assert(goog.isObject(object),
'entity from stack was not an object');
object[property] = value;
}
});
};
/**
* Create a serializer that appends nodes written by its `nodeWriter` to its
* designated parent. The parent is the `node` of the
* {@link ol.xml.NodeStackItem} at the top of the `objectStack`.
* @param {function(this: T, Node, V, Array.<*>)}
* nodeWriter Node writer.
* @param {T=} opt_this The object to use as `this` in `nodeWriter`.
* @return {ol.xml.Serializer} Serializer.
* @template T, V
*/
ol.xml.makeChildAppender = function(nodeWriter, opt_this) {
return function(node, value, objectStack) {
nodeWriter.call(opt_this !== undefined ? opt_this : this,
node, value, objectStack);
var parent = objectStack[objectStack.length - 1];
goog.asserts.assert(goog.isObject(parent),
'entity from stack was not an object');
var parentNode = parent.node;
goog.asserts.assert(ol.xml.isNode(parentNode) ||
ol.xml.isDocument(parentNode),
'expected parentNode %s to be a Node or a Document', parentNode);
parentNode.appendChild(node);
};
};
/**
* Create a serializer that calls the provided `nodeWriter` from
* {@link ol.xml.serialize}. This can be used by the parent writer to have the
* 'nodeWriter' called with an array of values when the `nodeWriter` was
* designed to serialize a single item. An example would be a LineString
* geometry writer, which could be reused for writing MultiLineString
* geometries.
* @param {function(this: T, Node, V, Array.<*>)}
* nodeWriter Node writer.
* @param {T=} opt_this The object to use as `this` in `nodeWriter`.
* @return {ol.xml.Serializer} Serializer.
* @template T, V
*/
ol.xml.makeArraySerializer = function(nodeWriter, opt_this) {
var serializersNS, nodeFactory;
return function(node, value, objectStack) {
if (serializersNS === undefined) {
serializersNS = {};
var serializers = {};
serializers[node.localName] = nodeWriter;
serializersNS[node.namespaceURI] = serializers;
nodeFactory = ol.xml.makeSimpleNodeFactory(node.localName);
}
ol.xml.serialize(serializersNS, nodeFactory, value, objectStack);
};
};
/**
* Create a node factory which can use the `opt_keys` passed to
* {@link ol.xml.serialize} or {@link ol.xml.pushSerializeAndPop} as node names,
* or a fixed node name. The namespace of the created nodes can either be fixed,
* or the parent namespace will be used.
* @param {string=} opt_nodeName Fixed node name which will be used for all
* created nodes. If not provided, the 3rd argument to the resulting node
* factory needs to be provided and will be the nodeName.
* @param {string=} opt_namespaceURI Fixed namespace URI which will be used for
* all created nodes. If not provided, the namespace of the parent node will
* be used.
* @return {function(*, Array.<*>, string=): (Node|undefined)} Node factory.
*/
ol.xml.makeSimpleNodeFactory = function(opt_nodeName, opt_namespaceURI) {
var fixedNodeName = opt_nodeName;
return (
/**
* @param {*} value Value.
* @param {Array.<*>} objectStack Object stack.
* @param {string=} opt_nodeName Node name.
* @return {Node} Node.
*/
function(value, objectStack, opt_nodeName) {
var context = objectStack[objectStack.length - 1];
var node = context.node;
goog.asserts.assert(ol.xml.isNode(node) || ol.xml.isDocument(node),
'expected node %s to be a Node or a Document', node);
var nodeName = fixedNodeName;
if (nodeName === undefined) {
nodeName = opt_nodeName;
}
var namespaceURI = opt_namespaceURI;
if (opt_namespaceURI === undefined) {
namespaceURI = node.namespaceURI;
}
goog.asserts.assert(nodeName !== undefined, 'nodeName was undefined');
return ol.xml.createElementNS(namespaceURI, nodeName);
}
);
};
/**
* A node factory that creates a node using the parent's `namespaceURI` and the
* `nodeName` passed by {@link ol.xml.serialize} or
* {@link ol.xml.pushSerializeAndPop} to the node factory.
* @const
* @type {function(*, Array.<*>, string=): (Node|undefined)}
*/
ol.xml.OBJECT_PROPERTY_NODE_FACTORY = ol.xml.makeSimpleNodeFactory();
/**
* Create an array of `values` to be used with {@link ol.xml.serialize} or
* {@link ol.xml.pushSerializeAndPop}, where `orderedKeys` has to be provided as
* `opt_key` argument.
* @param {Object.<string, V>} object Key-value pairs for the sequence. Keys can
* be a subset of the `orderedKeys`.
* @param {Array.<string>} orderedKeys Keys in the order of the sequence.
* @return {Array.<V>} Values in the order of the sequence. The resulting array
* has the same length as the `orderedKeys` array. Values that are not
* present in `object` will be `undefined` in the resulting array.
* @template V
*/
ol.xml.makeSequence = function(object, orderedKeys) {
var length = orderedKeys.length;
var sequence = new Array(length);
for (var i = 0; i < length; ++i) {
sequence[i] = object[orderedKeys[i]];
}
return sequence;
};
/**
* Create a namespaced structure, using the same values for each namespace.
* This can be used as a starting point for versioned parsers, when only a few
* values are version specific.
* @param {Array.<string>} namespaceURIs Namespace URIs.
* @param {T} structure Structure.
* @param {Object.<string, T>=} opt_structureNS Namespaced structure to add to.
* @return {Object.<string, T>} Namespaced structure.
* @template T
*/
ol.xml.makeStructureNS = function(namespaceURIs, structure, opt_structureNS) {
/**
* @type {Object.<string, *>}
*/
var structureNS = opt_structureNS !== undefined ? opt_structureNS : {};
var i, ii;
for (i = 0, ii = namespaceURIs.length; i < ii; ++i) {
structureNS[namespaceURIs[i]] = structure;
}
return structureNS;
};
/**
* Parse a node using the parsers and object stack.
* @param {Object.<string, Object.<string, ol.xml.Parser>>} parsersNS
* Parsers by namespace.
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @param {*=} opt_this The object to use as `this`.
*/
ol.xml.parseNode = function(parsersNS, node, objectStack, opt_this) {
var n;
for (n = node.firstElementChild; n; n = n.nextElementSibling) {
var parsers = parsersNS[n.namespaceURI];
if (parsers !== undefined) {
var parser = parsers[n.localName];
if (parser !== undefined) {
parser.call(opt_this, n, objectStack);
}
}
}
};
/**
* Push an object on top of the stack, parse and return the popped object.
* @param {T} object Object.
* @param {Object.<string, Object.<string, ol.xml.Parser>>} parsersNS
* Parsers by namespace.
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @param {*=} opt_this The object to use as `this`.
* @return {T} Object.
* @template T
*/
ol.xml.pushParseAndPop = function(
object, parsersNS, node, objectStack, opt_this) {
objectStack.push(object);
ol.xml.parseNode(parsersNS, node, objectStack, opt_this);
return objectStack.pop();
};
/**
* Walk through an array of `values` and call a serializer for each value.
* @param {Object.<string, Object.<string, ol.xml.Serializer>>} serializersNS
* Namespaced serializers.
* @param {function(this: T, *, Array.<*>, (string|undefined)): (Node|undefined)} nodeFactory
* Node factory. The `nodeFactory` creates the node whose namespace and name
* will be used to choose a node writer from `serializersNS`. This
* separation allows us to decide what kind of node to create, depending on
* the value we want to serialize. An example for this would be different
* geometry writers based on the geometry type.
* @param {Array.<*>} values Values to serialize. An example would be an array
* of {@link ol.Feature} instances.
* @param {Array.<*>} objectStack Node stack.
* @param {Array.<string>=} opt_keys Keys of the `values`. Will be passed to the
* `nodeFactory`. This is used for serializing object literals where the
* node name relates to the property key. The array length of `opt_keys` has
* to match the length of `values`. For serializing a sequence, `opt_keys`
* determines the order of the sequence.
* @param {T=} opt_this The object to use as `this` for the node factory and
* serializers.
* @template T
*/
ol.xml.serialize = function(
serializersNS, nodeFactory, values, objectStack, opt_keys, opt_this) {
var length = (opt_keys !== undefined ? opt_keys : values).length;
var value, node;
for (var i = 0; i < length; ++i) {
value = values[i];
if (value !== undefined) {
node = nodeFactory.call(opt_this, value, objectStack,
opt_keys !== undefined ? opt_keys[i] : undefined);
if (node !== undefined) {
serializersNS[node.namespaceURI][node.localName]
.call(opt_this, node, value, objectStack);
}
}
}
};
/**
* @param {O} object Object.
* @param {Object.<string, Object.<string, ol.xml.Serializer>>} serializersNS
* Namespaced serializers.
* @param {function(this: T, *, Array.<*>, (string|undefined)): (Node|undefined)} nodeFactory
* Node factory. The `nodeFactory` creates the node whose namespace and name
* will be used to choose a node writer from `serializersNS`. This
* separation allows us to decide what kind of node to create, depending on
* the value we want to serialize. An example for this would be different
* geometry writers based on the geometry type.
* @param {Array.<*>} values Values to serialize. An example would be an array
* of {@link ol.Feature} instances.
* @param {Array.<*>} objectStack Node stack.
* @param {Array.<string>=} opt_keys Keys of the `values`. Will be passed to the
* `nodeFactory`. This is used for serializing object literals where the
* node name relates to the property key. The array length of `opt_keys` has
* to match the length of `values`. For serializing a sequence, `opt_keys`
* determines the order of the sequence.
* @param {T=} opt_this The object to use as `this` for the node factory and
* serializers.
* @return {O|undefined} Object.
* @template O, T
*/
ol.xml.pushSerializeAndPop = function(object,
serializersNS, nodeFactory, values, objectStack, opt_keys, opt_this) {
objectStack.push(object);
ol.xml.serialize(
serializersNS, nodeFactory, values, objectStack, opt_keys, opt_this);
return objectStack.pop();
};
|