Atom.js
23 KB
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
/* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
* full list of contributors). Published under the Clear BSD license.
* See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/Format/XML.js
* @requires OpenLayers/Format/GML/v3.js
* @requires OpenLayers/Feature/Vector.js
*/
/**
* Class: OpenLayers.Format.Atom
* Read/write Atom feeds. Create a new instance with the
* <OpenLayers.Format.AtomFeed> constructor.
*
* Inherits from:
* - <OpenLayers.Format.XML>
*/
OpenLayers.Format.Atom = OpenLayers.Class(OpenLayers.Format.XML, {
/**
* Property: namespaces
* {Object} Mapping of namespace aliases to namespace URIs. Properties
* of this object should not be set individually. Read-only. All
* XML subclasses should have their own namespaces object. Use
* <setNamespace> to add or set a namespace alias after construction.
*/
namespaces: {
atom: "http://www.w3.org/2005/Atom",
georss: "http://www.georss.org/georss"
},
/**
* APIProperty: feedTitle
* {String} Atom feed elements require a title. Default is "untitled".
*/
feedTitle: "untitled",
/**
* APIProperty: defaultEntryTitle
* {String} Atom entry elements require a title. In cases where one is
* not provided in the feature attributes, this will be used. Default
* is "untitled".
*/
defaultEntryTitle: "untitled",
/**
* Property: gmlParse
* {Object} GML Format object for parsing features
* Non-API and only created if necessary
*/
gmlParser: null,
/**
* APIProperty: xy
* {Boolean} Order of the GML coordinate: true:(x,y) or false:(y,x)
* For GeoRSS the default is (y,x), therefore: false
*/
xy: false,
/**
* Constructor: OpenLayers.Format.AtomEntry
* Create a new parser for Atom.
*
* Parameters:
* options - {Object} An optional object whose properties will be set on
* this instance.
*/
/**
* APIMethod: read
* Return a list of features from an Atom feed or entry document.
* Parameters:
* doc - {Element} or {String}
*
* Returns:
* An Array of <OpenLayers.Feature.Vector>s
*/
read: function(doc) {
if (typeof doc == "string") {
doc = OpenLayers.Format.XML.prototype.read.apply(this, [doc]);
}
return this.parseFeatures(doc);
},
/**
* APIMethod: write
* Serialize or more feature nodes to Atom documents.
*
* Parameters:
* features - a single {<OpenLayers.Feature.Vector>} or an
* Array({<OpenLayers.Feature.Vector>}).
*
* Returns:
* {String} an Atom entry document if passed one feature node, or a feed
* document if passed an array of feature nodes.
*/
write: function(features) {
var doc;
if (OpenLayers.Util.isArray(features)) {
doc = this.createElementNSPlus("atom:feed");
doc.appendChild(
this.createElementNSPlus("atom:title", {
value: this.feedTitle
})
);
for (var i=0, ii=features.length; i<ii; i++) {
doc.appendChild(this.buildEntryNode(features[i]));
}
}
else {
doc = this.buildEntryNode(features);
}
return OpenLayers.Format.XML.prototype.write.apply(this, [doc]);
},
/**
* Method: buildContentNode
*
* Parameters:
* content - {Object}
*
* Returns:
* {DOMElement} an Atom content node.
*
* TODO: types other than text.
*/
buildContentNode: function(content) {
var node = this.createElementNSPlus("atom:content", {
attributes: {
type: content.type || null
}
});
if (content.src) {
node.setAttribute("src", content.src);
} else {
if (content.type == "text" || content.type == null) {
node.appendChild(
this.createTextNode(content.value)
);
} else if (content.type == "html") {
if (typeof content.value != "string") {
throw "HTML content must be in form of an escaped string";
}
node.appendChild(
this.createTextNode(content.value)
);
} else if (content.type == "xhtml") {
node.appendChild(content.value);
} else if (content.type == "xhtml" ||
content.type.match(/(\+|\/)xml$/)) {
node.appendChild(content.value);
}
else { // MUST be a valid Base64 encoding
node.appendChild(
this.createTextNode(content.value)
);
}
}
return node;
},
/**
* Method: buildEntryNode
* Build an Atom entry node from a feature object.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>}
*
* Returns:
* {DOMElement} an Atom entry node.
*
* These entries are geared for publication using AtomPub.
*
* TODO: support extension elements
*/
buildEntryNode: function(feature) {
var attrib = feature.attributes;
var atomAttrib = attrib.atom || {};
var entryNode = this.createElementNSPlus("atom:entry");
// atom:author
if (atomAttrib.authors) {
var authors = OpenLayers.Util.isArray(atomAttrib.authors) ?
atomAttrib.authors : [atomAttrib.authors];
for (var i=0, ii=authors.length; i<ii; i++) {
entryNode.appendChild(
this.buildPersonConstructNode(
"author", authors[i]
)
);
}
}
// atom:category
if (atomAttrib.categories) {
var categories = OpenLayers.Util.isArray(atomAttrib.categories) ?
atomAttrib.categories : [atomAttrib.categories];
var category;
for (var i=0, ii=categories.length; i<ii; i++) {
category = categories[i];
entryNode.appendChild(
this.createElementNSPlus("atom:category", {
attributes: {
term: category.term,
scheme: category.scheme || null,
label: category.label || null
}
})
);
}
}
// atom:content
if (atomAttrib.content) {
entryNode.appendChild(this.buildContentNode(atomAttrib.content));
}
// atom:contributor
if (atomAttrib.contributors) {
var contributors = OpenLayers.Util.isArray(atomAttrib.contributors) ?
atomAttrib.contributors : [atomAttrib.contributors];
for (var i=0, ii=contributors.length; i<ii; i++) {
entryNode.appendChild(
this.buildPersonConstructNode(
"contributor",
contributors[i]
)
);
}
}
// atom:id
if (feature.fid) {
entryNode.appendChild(
this.createElementNSPlus("atom:id", {
value: feature.fid
})
);
}
// atom:link
if (atomAttrib.links) {
var links = OpenLayers.Util.isArray(atomAttrib.links) ?
atomAttrib.links : [atomAttrib.links];
var link;
for (var i=0, ii=links.length; i<ii; i++) {
link = links[i];
entryNode.appendChild(
this.createElementNSPlus("atom:link", {
attributes: {
href: link.href,
rel: link.rel || null,
type: link.type || null,
hreflang: link.hreflang || null,
title: link.title || null,
length: link.length || null
}
})
);
}
}
// atom:published
if (atomAttrib.published) {
entryNode.appendChild(
this.createElementNSPlus("atom:published", {
value: atomAttrib.published
})
);
}
// atom:rights
if (atomAttrib.rights) {
entryNode.appendChild(
this.createElementNSPlus("atom:rights", {
value: atomAttrib.rights
})
);
}
// atom:source not implemented
// atom:summary
if (atomAttrib.summary || attrib.description) {
entryNode.appendChild(
this.createElementNSPlus("atom:summary", {
value: atomAttrib.summary || attrib.description
})
);
}
// atom:title
entryNode.appendChild(
this.createElementNSPlus("atom:title", {
value: atomAttrib.title || attrib.title || this.defaultEntryTitle
})
);
// atom:updated
if (atomAttrib.updated) {
entryNode.appendChild(
this.createElementNSPlus("atom:updated", {
value: atomAttrib.updated
})
);
}
// georss:where
if (feature.geometry) {
var whereNode = this.createElementNSPlus("georss:where");
whereNode.appendChild(
this.buildGeometryNode(feature.geometry)
);
entryNode.appendChild(whereNode);
}
return entryNode;
},
/**
* Method: initGmlParser
* Creates a GML parser.
*/
initGmlParser: function() {
this.gmlParser = new OpenLayers.Format.GML.v3({
xy: this.xy,
featureNS: "http://example.com#feature",
internalProjection: this.internalProjection,
externalProjection: this.externalProjection
});
},
/**
* Method: buildGeometryNode
* builds a GeoRSS node with a given geometry
*
* Parameters:
* geometry - {<OpenLayers.Geometry>}
*
* Returns:
* {DOMElement} A gml node.
*/
buildGeometryNode: function(geometry) {
if (!this.gmlParser) {
this.initGmlParser();
}
var node = this.gmlParser.writeNode("feature:_geometry", geometry);
return node.firstChild;
},
/**
* Method: buildPersonConstructNode
*
* Parameters:
* name - {String}
* value - {Object}
*
* Returns:
* {DOMElement} an Atom person construct node.
*
* Example:
* >>> buildPersonConstructNode("author", {name: "John Smith"})
* {<author><name>John Smith</name></author>}
*
* TODO: how to specify extension elements? Add to the oNames array?
*/
buildPersonConstructNode: function(name, value) {
var oNames = ["uri", "email"];
var personNode = this.createElementNSPlus("atom:" + name);
personNode.appendChild(
this.createElementNSPlus("atom:name", {
value: value.name
})
);
for (var i=0, ii=oNames.length; i<ii; i++) {
if (value[oNames[i]]) {
personNode.appendChild(
this.createElementNSPlus("atom:" + oNames[i], {
value: value[oNames[i]]
})
);
}
}
return personNode;
},
/**
* Method: getFirstChildValue
*
* Parameters:
* node - {DOMElement}
* nsuri - {String} Child node namespace uri ("*" for any).
* name - {String} Child node name.
* def - {String} Optional string default to return if no child found.
*
* Returns:
* {String} The value of the first child with the given tag name. Returns
* default value or empty string if none found.
*/
getFirstChildValue: function(node, nsuri, name, def) {
var value;
var nodes = this.getElementsByTagNameNS(node, nsuri, name);
if (nodes && nodes.length > 0) {
value = this.getChildValue(nodes[0], def);
} else {
value = def;
}
return value;
},
/**
* Method: parseFeature
* Parse feature from an Atom entry node..
*
* Parameters:
* node - {DOMElement} An Atom entry or feed node.
*
* Returns:
* An <OpenLayers.Feature.Vector>.
*/
parseFeature: function(node) {
var atomAttrib = {};
var value = null;
var nodes = null;
var attval = null;
var atomns = this.namespaces.atom;
// atomAuthor*
this.parsePersonConstructs(node, "author", atomAttrib);
// atomCategory*
nodes = this.getElementsByTagNameNS(node, atomns, "category");
if (nodes.length > 0) {
atomAttrib.categories = [];
}
for (var i=0, ii=nodes.length; i<ii; i++) {
value = {};
value.term = nodes[i].getAttribute("term");
attval = nodes[i].getAttribute("scheme");
if (attval) { value.scheme = attval; }
attval = nodes[i].getAttribute("label");
if (attval) { value.label = attval; }
atomAttrib.categories.push(value);
}
// atomContent?
nodes = this.getElementsByTagNameNS(node, atomns, "content");
if (nodes.length > 0) {
value = {};
attval = nodes[0].getAttribute("type");
if (attval) {
value.type = attval;
}
attval = nodes[0].getAttribute("src");
if (attval) {
value.src = attval;
} else {
if (value.type == "text" ||
value.type == "html" ||
value.type == null ) {
value.value = this.getFirstChildValue(
node,
atomns,
"content",
null
);
} else if (value.type == "xhtml" ||
value.type.match(/(\+|\/)xml$/)) {
value.value = this.getChildEl(nodes[0]);
} else { // MUST be base64 encoded
value.value = this.getFirstChildValue(
node,
atomns,
"content",
null
);
}
atomAttrib.content = value;
}
}
// atomContributor*
this.parsePersonConstructs(node, "contributor", atomAttrib);
// atomId
atomAttrib.id = this.getFirstChildValue(node, atomns, "id", null);
// atomLink*
nodes = this.getElementsByTagNameNS(node, atomns, "link");
if (nodes.length > 0) {
atomAttrib.links = new Array(nodes.length);
}
var oAtts = ["rel", "type", "hreflang", "title", "length"];
for (var i=0, ii=nodes.length; i<ii; i++) {
value = {};
value.href = nodes[i].getAttribute("href");
for (var j=0, jj=oAtts.length; j<jj; j++) {
attval = nodes[i].getAttribute(oAtts[j]);
if (attval) {
value[oAtts[j]] = attval;
}
}
atomAttrib.links[i] = value;
}
// atomPublished?
value = this.getFirstChildValue(node, atomns, "published", null);
if (value) {
atomAttrib.published = value;
}
// atomRights?
value = this.getFirstChildValue(node, atomns, "rights", null);
if (value) {
atomAttrib.rights = value;
}
// atomSource? -- not implemented
// atomSummary?
value = this.getFirstChildValue(node, atomns, "summary", null);
if (value) {
atomAttrib.summary = value;
}
// atomTitle
atomAttrib.title = this.getFirstChildValue(
node, atomns, "title", null
);
// atomUpdated
atomAttrib.updated = this.getFirstChildValue(
node, atomns, "updated", null
);
var featureAttrib = {
title: atomAttrib.title,
description: atomAttrib.summary,
atom: atomAttrib
};
var geometry = this.parseLocations(node)[0];
var feature = new OpenLayers.Feature.Vector(geometry, featureAttrib);
feature.fid = atomAttrib.id;
return feature;
},
/**
* Method: parseFeatures
* Return features from an Atom entry or feed.
*
* Parameters:
* node - {DOMElement} An Atom entry or feed node.
*
* Returns:
* An Array of <OpenLayers.Feature.Vector>s.
*/
parseFeatures: function(node) {
var features = [];
var entries = this.getElementsByTagNameNS(
node, this.namespaces.atom, "entry"
);
if (entries.length == 0) {
entries = [node];
}
for (var i=0, ii=entries.length; i<ii; i++) {
features.push(this.parseFeature(entries[i]));
}
return features;
},
/**
* Method: parseLocations
* Parse the locations from an Atom entry or feed.
*
* Parameters:
* node - {DOMElement} An Atom entry or feed node.
*
* Returns:
* An Array of <OpenLayers.Geometry>s.
*/
parseLocations: function(node) {
var georssns = this.namespaces.georss;
var locations = {components: []};
var where = this.getElementsByTagNameNS(node, georssns, "where");
if (where && where.length > 0) {
if (!this.gmlParser) {
this.initGmlParser();
}
for (var i=0, ii=where.length; i<ii; i++) {
this.gmlParser.readChildNodes(where[i], locations);
}
}
var components = locations.components;
var point = this.getElementsByTagNameNS(node, georssns, "point");
if (point && point.length > 0) {
for (var i=0, ii=point.length; i<ii; i++) {
var xy = OpenLayers.String.trim(
point[i].firstChild.nodeValue
).split(/\s+/);
if (xy.length !=2) {
xy = OpenLayers.String.trim(
point[i].firstChild.nodeValue
).split(/\s*,\s*/);
}
components.push(
new OpenLayers.Geometry.Point(
parseFloat(xy[1]),
parseFloat(xy[0])
)
);
}
}
var line = this.getElementsByTagNameNS(node, georssns, "line");
if (line && line.length > 0) {
var coords;
var p;
var points;
for (var i=0, ii=line.length; i<ii; i++) {
coords = OpenLayers.String.trim(
line[i].firstChild.nodeValue
).split(/\s+/);
points = [];
for (var j=0, jj=coords.length; j<jj; j+=2) {
p = new OpenLayers.Geometry.Point(
parseFloat(coords[j+1]),
parseFloat(coords[j])
);
points.push(p);
}
components.push(
new OpenLayers.Geometry.LineString(points)
);
}
}
var polygon = this.getElementsByTagNameNS(node, georssns, "polygon");
if (polygon && polygon.length > 0) {
var coords;
var p;
var points;
for (var i=0, ii=polygon.length; i<ii; i++) {
coords = OpenLayers.String.trim(
polygon[i].firstChild.nodeValue
).split(/\s+/);
points = [];
for (var j=0, jj=coords.length; j<jj; j+=2) {
p = new OpenLayers.Geometry.Point(
parseFloat(coords[j+1]),
parseFloat(coords[j])
);
points.push(p);
}
components.push(
new OpenLayers.Geometry.Polygon(
[new OpenLayers.Geometry.LinearRing(components)]
)
);
}
}
if (this.internalProjection && this.externalProjection) {
for (var i=0, ii=components.length; i<ii; i++) {
if (components[i]) {
components[i].transform(
this.externalProjection,
this.internalProjection
);
}
}
}
return components;
},
/**
* Method: parsePersonConstruct
* Parse Atom person constructs from an Atom entry node.
*
* Parameters:
* node - {DOMElement} An Atom entry or feed node.
* name - {String} Construcy name ("author" or "contributor")
* data = {Object} Object in which to put parsed persons.
*
* Returns:
* An {Object}.
*/
parsePersonConstructs: function(node, name, data) {
var persons = [];
var atomns = this.namespaces.atom;
var nodes = this.getElementsByTagNameNS(node, atomns, name);
var oAtts = ["uri", "email"];
for (var i=0, ii=nodes.length; i<ii; i++) {
var value = {};
value.name = this.getFirstChildValue(
nodes[i],
atomns,
"name",
null
);
for (var j=0, jj=oAtts.length; j<jj; j++) {
var attval = this.getFirstChildValue(
nodes[i],
atomns,
oAtts[j],
null);
if (attval) {
value[oAtts[j]] = attval;
}
}
persons.push(value);
}
if (persons.length > 0) {
data[name + "s"] = persons;
}
},
CLASS_NAME: "OpenLayers.Format.Atom"
});