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. */
7
* @requires OpenLayers/Format/WMC.js
8
* @requires OpenLayers/Format/XML.js
12
* Class: OpenLayers.Format.WMC.v1
13
* Superclass for WMC version 1 parsers.
16
* - <OpenLayers.Format.XML>
18
OpenLayers.Format.WMC.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
21
* Property: namespaces
22
* {Object} Mapping of namespace aliases to namespace URIs.
25
ol: "http://openlayers.org/context",
26
wmc: "http://www.opengis.net/context",
27
sld: "http://www.opengis.net/sld",
28
xlink: "http://www.w3.org/1999/xlink",
29
xsi: "http://www.w3.org/2001/XMLSchema-instance"
33
* Property: schemaLocation
34
* {String} Schema location for a particular minor version.
39
* Method: getNamespacePrefix
40
* Get the namespace prefix for a given uri from the <namespaces> object.
43
* {String} A namespace prefix or null if none found.
45
getNamespacePrefix: function(uri) {
48
prefix = this.namespaces[this.defaultPrefix];
50
for(prefix in this.namespaces) {
51
if(this.namespaces[prefix] == uri) {
60
* Property: defaultPrefix
65
* Property: rootPrefix
66
* {String} Prefix on the root node that maps to the context namespace URI.
71
* Property: defaultStyleName
72
* {String} Style name used if layer has no style param. Default is "".
77
* Property: defaultStyleTitle
78
* {String} Default style title. Default is "Default".
80
defaultStyleTitle: "Default",
83
* Constructor: OpenLayers.Format.WMC.v1
84
* Instances of this class are not created directly. Use the
85
* <OpenLayers.Format.WMC> constructor instead.
88
* options - {Object} An optional object whose properties will be set on
91
initialize: function(options) {
92
OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
97
* Read capabilities data from a string, and return a list of layers.
100
* data - {String} or {DOMElement} data to read/parse.
103
* {Array} List of named layers.
105
read: function(data) {
106
if(typeof data == "string") {
107
data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
109
var root = data.documentElement;
110
this.rootPrefix = root.prefix;
112
version: root.getAttribute("version")
114
this.runChildNodes(context, root);
119
* Method: runChildNodes
121
runChildNodes: function(obj, node) {
122
var children = node.childNodes;
123
var childNode, processor, prefix, local;
124
for(var i=0, len=children.length; i<len; ++i) {
125
childNode = children[i];
126
if(childNode.nodeType == 1) {
127
prefix = this.getNamespacePrefix(childNode.namespaceURI);
128
local = childNode.nodeName.split(":").pop();
129
processor = this["read_" + prefix + "_" + local];
131
processor.apply(this, [obj, childNode]);
138
* Method: read_wmc_General
140
read_wmc_General: function(context, node) {
141
this.runChildNodes(context, node);
145
* Method: read_wmc_BoundingBox
147
read_wmc_BoundingBox: function(context, node) {
148
context.projection = node.getAttribute("SRS");
149
context.bounds = new OpenLayers.Bounds(
150
parseFloat(node.getAttribute("minx")),
151
parseFloat(node.getAttribute("miny")),
152
parseFloat(node.getAttribute("maxx")),
153
parseFloat(node.getAttribute("maxy"))
158
* Method: read_wmc_LayerList
160
read_wmc_LayerList: function(context, node) {
161
// layersContext is an array containing info for each layer
162
context.layersContext = [];
163
this.runChildNodes(context, node);
167
* Method: read_wmc_Layer
169
read_wmc_Layer: function(context, node) {
171
visibility: (node.getAttribute("hidden") != "1"),
172
queryable: (node.getAttribute("queryable") == "1"),
177
this.runChildNodes(layerContext, node);
178
// set properties common to multiple objects on layer options/params
179
context.layersContext.push(layerContext);
183
* Method: read_wmc_Extension
185
read_wmc_Extension: function(obj, node) {
186
this.runChildNodes(obj, node);
190
* Method: read_ol_units
192
read_ol_units: function(layerContext, node) {
193
layerContext.units = this.getChildValue(node);
197
* Method: read_ol_maxExtent
199
read_ol_maxExtent: function(obj, node) {
200
var bounds = new OpenLayers.Bounds(
201
node.getAttribute("minx"), node.getAttribute("miny"),
202
node.getAttribute("maxx"), node.getAttribute("maxy")
204
obj.maxExtent = bounds;
208
* Method: read_ol_transparent
210
read_ol_transparent: function(layerContext, node) {
211
layerContext.transparent = this.getChildValue(node);
215
* Method: read_ol_numZoomLevels
217
read_ol_numZoomLevels: function(layerContext, node) {
218
layerContext.numZoomLevels = parseInt(this.getChildValue(node));
222
* Method: read_ol_opacity
224
read_ol_opacity: function(layerContext, node) {
225
layerContext.opacity = parseFloat(this.getChildValue(node));
229
* Method: read_ol_singleTile
231
read_ol_singleTile: function(layerContext, node) {
232
layerContext.singleTile = (this.getChildValue(node) == "true");
236
* Method: read_ol_tileSize
238
read_ol_tileSize: function(layerContext, node) {
239
var obj = {"width": node.getAttribute("width"), "height": node.getAttribute("height")};
240
layerContext.tileSize = obj;
244
* Method: read_ol_isBaseLayer
246
read_ol_isBaseLayer: function(layerContext, node) {
247
layerContext.isBaseLayer = (this.getChildValue(node) == "true");
251
* Method: read_ol_displayInLayerSwitcher
253
read_ol_displayInLayerSwitcher: function(layerContext, node) {
254
layerContext.displayInLayerSwitcher = (this.getChildValue(node) == "true");
258
* Method: read_wmc_Server
260
read_wmc_Server: function(layerContext, node) {
261
layerContext.version = node.getAttribute("version");
263
var links = node.getElementsByTagName("OnlineResource");
264
if(links.length > 0) {
265
this.read_wmc_OnlineResource(server, links[0]);
267
layerContext.url = server.href;
271
* Method: read_wmc_FormatList
273
read_wmc_FormatList: function(layerContext, node) {
274
this.runChildNodes(layerContext, node);
278
* Method: read_wmc_Format
280
read_wmc_Format: function(layerContext, node) {
282
value: this.getChildValue(node)
284
if(node.getAttribute("current") == "1") {
285
format.current = true;
287
layerContext.formats.push(format);
291
* Method: read_wmc_StyleList
293
read_wmc_StyleList: function(layerContext, node) {
294
this.runChildNodes(layerContext, node);
298
* Method: read_wmc_Style
300
read_wmc_Style: function(layerContext, node) {
302
this.runChildNodes(style, node);
303
if(node.getAttribute("current") == "1") {
304
style.current = true;
306
layerContext.styles.push(style);
310
* Method: read_wmc_SLD
312
read_wmc_SLD: function(style, node) {
313
this.runChildNodes(style, node);
314
// style either comes back with an href or a body property
318
* Method: read_sld_StyledLayerDescriptor
320
read_sld_StyledLayerDescriptor: function(sld, node) {
321
var xml = OpenLayers.Format.XML.prototype.write.apply(this, [node]);
326
* Method: read_wmc_OnlineResource
328
read_wmc_OnlineResource: function(obj, node) {
329
obj.href = this.getAttributeNS(
330
node, this.namespaces.xlink, "href"
335
* Method: read_wmc_Name
337
read_wmc_Name: function(obj, node) {
338
var name = this.getChildValue(node);
345
* Method: read_wmc_Title
347
read_wmc_Title: function(obj, node) {
348
var title = this.getChildValue(node);
355
* Method: read_wmc_MetadataURL
357
read_wmc_MetadataURL: function(layerContext, node) {
358
var metadataURL = {};
359
var links = node.getElementsByTagName("OnlineResource");
360
if(links.length > 0) {
361
this.read_wmc_OnlineResource(metadataURL, links[0]);
363
layerContext.metadataURL = metadataURL.href;
368
* Method: read_wmc_Abstract
370
read_wmc_Abstract: function(obj, node) {
371
var abst = this.getChildValue(node);
373
obj["abstract"] = abst;
378
* Method: read_wmc_LegendURL
380
read_wmc_LegendURL: function(style, node) {
382
width: node.getAttribute('width'),
383
height: node.getAttribute('height')
385
var links = node.getElementsByTagName("OnlineResource");
386
if(links.length > 0) {
387
this.read_wmc_OnlineResource(legend, links[0]);
389
style.legend = legend;
396
* context - {Object} An object representing the map context.
397
* options - {Object} Optional object.
400
* {String} A WMC document string.
402
write: function(context, options) {
403
var root = this.createElementDefaultNS("ViewContext");
404
this.setAttributes(root, {
405
version: this.VERSION,
406
id: (options && typeof options.id == "string") ?
408
OpenLayers.Util.createUniqueID("OpenLayers_Context_")
411
// add schemaLocation attribute
413
root, this.namespaces.xsi,
414
"xsi:schemaLocation", this.schemaLocation
417
// required General element
418
root.appendChild(this.write_wmc_General(context));
420
// required LayerList element
421
root.appendChild(this.write_wmc_LayerList(context));
423
return OpenLayers.Format.XML.prototype.write.apply(this, [root]);
427
* Method: createElementDefaultNS
428
* Shorthand for createElementNS with namespace from <defaultPrefix>.
429
* Can optionally be used to set attributes and a text child value.
432
* name - {String} The qualified node name.
433
* childValue - {String} Optional value for text child node.
434
* attributes - {Object} Optional object representing attributes.
437
* {Element} An element node.
439
createElementDefaultNS: function(name, childValue, attributes) {
440
var node = this.createElementNS(
441
this.namespaces[this.defaultPrefix],
445
node.appendChild(this.createTextNode(childValue));
448
this.setAttributes(node, attributes);
454
* Method: setAttributes
455
* Set multiple attributes given key value pairs from an object.
458
* node - {Element} An element node.
459
* obj - {Object} An object whose properties represent attribute names and
460
* values represent attribute values.
462
setAttributes: function(node, obj) {
464
for(var name in obj) {
465
value = obj[name].toString();
466
if(value.match(/[A-Z]/)) {
467
// safari lowercases attributes with setAttribute
468
this.setAttributeNS(node, null, name, value);
470
node.setAttribute(name, value);
476
* Method: write_wmc_General
477
* Create a General node given an context object.
480
* context - {Object} Context object.
483
* {Element} A WMC General element node.
485
write_wmc_General: function(context) {
486
var node = this.createElementDefaultNS("General");
488
// optional Window element
490
node.appendChild(this.createElementDefaultNS(
493
width: context.size.w,
494
height: context.size.h
499
// required BoundingBox element
500
var bounds = context.bounds;
501
node.appendChild(this.createElementDefaultNS(
504
minx: bounds.left.toPrecision(18),
505
miny: bounds.bottom.toPrecision(18),
506
maxx: bounds.right.toPrecision(18),
507
maxy: bounds.top.toPrecision(18),
508
SRS: context.projection
512
// required Title element
513
node.appendChild(this.createElementDefaultNS(
514
"Title", context.title
517
// OpenLayers specific map properties
518
node.appendChild(this.write_ol_MapExtension(context));
524
* Method: write_ol_MapExtension
526
write_ol_MapExtension: function(context) {
527
var node = this.createElementDefaultNS("Extension");
529
var bounds = context.maxExtent;
531
var maxExtent = this.createElementNS(
532
this.namespaces.ol, "ol:maxExtent"
534
this.setAttributes(maxExtent, {
535
minx: bounds.left.toPrecision(18),
536
miny: bounds.bottom.toPrecision(18),
537
maxx: bounds.right.toPrecision(18),
538
maxy: bounds.top.toPrecision(18)
540
node.appendChild(maxExtent);
547
* Method: write_wmc_LayerList
548
* Create a LayerList node given an context object.
551
* context - {Object} Context object.
554
* {Element} A WMC LayerList element node.
556
write_wmc_LayerList: function(context) {
557
var list = this.createElementDefaultNS("LayerList");
559
for(var i=0, len=context.layersContext.length; i<len; ++i) {
560
list.appendChild(this.write_wmc_Layer(context.layersContext[i]));
567
* Method: write_wmc_Layer
568
* Create a Layer node given a layer context object.
571
* context - {Object} A layer context object.}
574
* {Element} A WMC Layer element node.
576
write_wmc_Layer: function(context) {
577
var node = this.createElementDefaultNS(
579
queryable: context.queryable ? "1" : "0",
580
hidden: context.visibility ? "0" : "1"
584
// required Server element
585
node.appendChild(this.write_wmc_Server(context));
587
// required Name element
588
node.appendChild(this.createElementDefaultNS(
592
// required Title element
593
node.appendChild(this.createElementDefaultNS(
594
"Title", context.title
597
// optional MetadataURL element
598
if (context.metadataURL) {
599
node.appendChild(this.write_wmc_MetadataURL(context.metadataURL));
606
* Method: write_wmc_LayerExtension
607
* Add OpenLayers specific layer parameters to an Extension element.
610
* context - {Object} A layer context object.
613
* {Element} A WMC Extension element (for a layer).
615
write_wmc_LayerExtension: function(context) {
616
var node = this.createElementDefaultNS("Extension");
618
var bounds = context.maxExtent;
619
var maxExtent = this.createElementNS(
620
this.namespaces.ol, "ol:maxExtent"
622
this.setAttributes(maxExtent, {
623
minx: bounds.left.toPrecision(18),
624
miny: bounds.bottom.toPrecision(18),
625
maxx: bounds.right.toPrecision(18),
626
maxy: bounds.top.toPrecision(18)
628
node.appendChild(maxExtent);
630
if (context.tileSize && !context.singleTile) {
631
var size = this.createElementNS(
632
this.namespaces.ol, "ol:tileSize"
634
this.setAttributes(size, context.tileSize);
635
node.appendChild(size);
639
"transparent", "numZoomLevels", "units", "isBaseLayer",
640
"opacity", "displayInLayerSwitcher", "singleTile"
643
for(var i=0, len=properties.length; i<len; ++i) {
644
child = this.createOLPropertyNode(context, properties[i]);
646
node.appendChild(child);
654
* Method: createOLPropertyNode
655
* Create a node representing an OpenLayers property. If the property is
656
* null or undefined, null will be returned.
659
* object - {Object} An object.
660
* prop - {String} A property.
663
* {Element} A property node.
665
createOLPropertyNode: function(obj, prop) {
667
if(obj[prop] != null) {
668
node = this.createElementNS(this.namespaces.ol, "ol:" + prop);
669
node.appendChild(this.createTextNode(obj[prop].toString()));
675
* Method: write_wmc_Server
676
* Create a Server node given a layer context object.
679
* context - {Object} Layer context object.
682
* {Element} A WMC Server element node.
684
write_wmc_Server: function(context) {
685
var node = this.createElementDefaultNS("Server");
686
this.setAttributes(node, {
688
version: context.version
691
// required OnlineResource element
692
node.appendChild(this.write_wmc_OnlineResource(context.url));
698
* Method: write_wmc_MetadataURL
699
* Create a MetadataURL node given a metadataURL string.
702
* metadataURL - {String} MetadataURL string value.
705
* {Element} A WMC metadataURL element node.
707
write_wmc_MetadataURL: function(metadataURL) {
708
var node = this.createElementDefaultNS("MetadataURL");
710
// required OnlineResource element
711
node.appendChild(this.write_wmc_OnlineResource(metadataURL));
717
* Method: write_wmc_FormatList
718
* Create a FormatList node given a layer context.
721
* context - {Object} Layer context object.
724
* {Element} A WMC FormatList element node.
726
write_wmc_FormatList: function(context) {
727
var node = this.createElementDefaultNS("FormatList");
728
for (var i=0, len=context.formats.length; i<len; i++) {
729
var format = context.formats[i];
730
node.appendChild(this.createElementDefaultNS(
733
(format.current && format.current == true) ?
734
{current: "1"} : null
742
* Method: write_wmc_StyleList
743
* Create a StyleList node given a layer context.
746
* context - {Object} Layer context object.
749
* {Element} A WMC StyleList element node.
751
write_wmc_StyleList: function(layer) {
752
var node = this.createElementDefaultNS("StyleList");
754
var styles = layer.styles;
755
if (styles && styles instanceof Array) {
757
for (var i=0, len=styles.length; i<len; i++) {
759
// three style types to consider
763
// running child nodes always gets name, optionally gets href or body
764
var style = this.createElementDefaultNS(
767
(s.current && s.current == true) ?
768
{current: "1"} : null
771
sld = this.createElementDefaultNS("SLD");
772
var link = this.write_wmc_OnlineResource(s.href);
773
sld.appendChild(link);
775
sld.appendChild(this.createElementDefaultNS("Name", s.name));
776
// Title is optional.
778
sld.appendChild(this.createElementDefaultNS("Title", s.title));
780
style.appendChild(sld);
781
} else if(s.body) { // [2]
782
sld = this.createElementDefaultNS("SLD");
783
// read in body as xml doc - assume proper namespace declarations
784
var doc = OpenLayers.Format.XML.prototype.read.apply(this, [s.body]);
785
// append to StyledLayerDescriptor node
786
var imported = doc.documentElement;
787
if(sld.ownerDocument && sld.ownerDocument.importNode) {
788
imported = sld.ownerDocument.importNode(imported, true);
790
sld.appendChild(imported);
792
sld.appendChild(this.createElementDefaultNS("Name", s.name));
793
// Title is optional.
795
sld.appendChild(this.createElementDefaultNS("Title", s.title));
797
style.appendChild(sld);
799
// both Name and Title are required.
800
style.appendChild(this.createElementDefaultNS("Name", s.name));
801
style.appendChild(this.createElementDefaultNS("Title", s.title));
802
// Abstract is optional
803
if (s['abstract']) { // abstract is a js keyword
804
style.appendChild(this.createElementDefaultNS(
805
"Abstract", s['abstract']
809
node.appendChild(style);
817
* Method: write_wmc_OnlineResource
818
* Create an OnlineResource node given a URL.
821
* href - {String} URL for the resource.
824
* {Element} A WMC OnlineResource element node.
826
write_wmc_OnlineResource: function(href) {
827
var node = this.createElementDefaultNS("OnlineResource");
828
this.setAttributeNS(node, this.namespaces.xlink, "xlink:type", "simple");
829
this.setAttributeNS(node, this.namespaces.xlink, "xlink:href", href);
833
CLASS_NAME: "OpenLayers.Format.WMC.v1"