From 44f7ebdc22f02d21b534623feb5349c8e9518eb5 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 28 Jan 2008 16:39:05 +0000 Subject: [PATCH] Adding Web Map Context document parsing for versions 1.0.0 and 1.1.0. This also adds a cross browser setAttributeNS to the XML format. Thanks bartvde for supporting this work. r=crschmidt (closes #100) git-svn-id: http://svn.openlayers.org/trunk/openlayers@5919 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- examples/wmc.html | 146 ++++++ lib/OpenLayers.js | 4 + lib/OpenLayers/Format/WMC.js | 193 +++++++ lib/OpenLayers/Format/WMC/v1.js | 764 ++++++++++++++++++++++++++++ lib/OpenLayers/Format/WMC/v1_0_0.js | 49 ++ lib/OpenLayers/Format/WMC/v1_1_0.js | 110 ++++ lib/OpenLayers/Format/XML.js | 31 ++ tests/Format/test_WMC.html | 142 ++++++ tests/list-tests.html | 1 + 9 files changed, 1440 insertions(+) create mode 100644 examples/wmc.html create mode 100644 lib/OpenLayers/Format/WMC.js create mode 100644 lib/OpenLayers/Format/WMC/v1.js create mode 100644 lib/OpenLayers/Format/WMC/v1_0_0.js create mode 100644 lib/OpenLayers/Format/WMC/v1_1_0.js create mode 100644 tests/Format/test_WMC.html diff --git a/examples/wmc.html b/examples/wmc.html new file mode 100644 index 0000000000..6d1c2f78b7 --- /dev/null +++ b/examples/wmc.html @@ -0,0 +1,146 @@ + + + + + + + + +

WMC Example

+ +
+
+

+ Shows parsing of Web Map Context documents. +

+
+ + + + +
+ This is an example of parsing WMC documents. +
+ + + + + + diff --git a/lib/OpenLayers.js b/lib/OpenLayers.js index 31195c9764..6ab5ef0abb 100644 --- a/lib/OpenLayers.js +++ b/lib/OpenLayers.js @@ -185,6 +185,10 @@ "OpenLayers/Format/Text.js", "OpenLayers/Format/JSON.js", "OpenLayers/Format/GeoJSON.js", + "OpenLayers/Format/WMC.js", + "OpenLayers/Format/WMC/v1.js", + "OpenLayers/Format/WMC/v1_0_0.js", + "OpenLayers/Format/WMC/v1_1_0.js", "OpenLayers/Layer/WFS.js", "OpenLayers/Control/MouseToolbar.js", "OpenLayers/Control/NavToolbar.js", diff --git a/lib/OpenLayers/Format/WMC.js b/lib/OpenLayers/Format/WMC.js new file mode 100644 index 0000000000..4ce056f869 --- /dev/null +++ b/lib/OpenLayers/Format/WMC.js @@ -0,0 +1,193 @@ +/* Copyright (c) 2006-2008 MetaCarta, Inc., 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 + */ + +/** + * Class: OpenLayers.Format.WMC + * Read and write Web Map Context documents. + * + * Inherits from: + * - + */ +OpenLayers.Format.WMC = OpenLayers.Class({ + + /** + * APIProperty: defaultVersion + * {String} Version number to assume if none found. Default is "1.1.0". + */ + defaultVersion: "1.1.0", + + /** + * APIProperty: version + * {String} Specify a version string if one is known. + */ + version: null, + + /** + * Property: parser + * {Object} Instance of the versioned parser. Cached for multiple read and + * write calls of the same version. + */ + parser: null, + + /** + * Constructor: OpenLayers.Format.WMC + * Create a new parser for WMC docs. + * + * Parameters: + * options - {Object} An optional object whose properties will be set on + * this instance. + */ + initialize: function(options) { + OpenLayers.Util.extend(this, options); + this.options = options; + }, + + /** + * APIMethod: read + * Read WMC data from a string, and return an object with map properties + * and a list of layers. + * + * Parameters: + * data - {String} or {DOMElement} data to read/parse. + * options - {Object} The options object must contain a map property. If + * the map property is a string, it must be the id of a dom element + * where the new map will be placed. If the map property is an + * , the layers from the context document will be added + * to the map. + * + * Returns: + * {} A map based on the context. + */ + read: function(data, options) { + if(typeof data == "string") { + data = OpenLayers.Format.XML.prototype.read.apply(this, [data]); + } + var root = data.documentElement; + var version = this.version; + if(!version) { + version = root.getAttribute("version"); + if(!version) { + version = this.defaultVersion; + } + } + if(!this.parser || this.parser.VERSION != version) { + var format = OpenLayers.Format.WMC[ + "v" + version.replace(/\./g, "_") + ]; + if(!format) { + throw "Can't find a WMS capabilities parser for version " + + version; + } + this.parser = new format(this.options); + } + var context = this.parser.read(data); + var map; + if(options.map instanceof OpenLayers.Map) { + map = this.mergeContextToMap(context, options.map); + } else { + map = this.contextToMap(context, options.map); + } + return map; + }, + + /** + * Method: contextToMap + * Create a map given a context object. + * + * Parameters: + * context - {Object} The context object. + * id - {String | Element} The dom element or element id that will contain + * the map. + * + * Returns: + * {} A map based on the context object. + */ + contextToMap: function(context, id) { + var map = new OpenLayers.Map(id, { + maxExtent: context.maxExtent, + projection: context.projection + }); + map.addLayers(context.layers); + map.setCenter( + context.bounds.getCenterLonLat(), + map.getZoomForExtent(context.bounds, true) + ); + return map; + }, + + /** + * Method: mergeContextToMap + * Add layers from a context object to a map. + * + * Parameters: + * context - {Object} The context object. + * map - {} The map. + * + * Returns: + * {} The same map with layers added. + */ + mergeContextToMap: function(context, map) { + map.addLayers(context.layers); + return map; + }, + + /** + * APIMethod: write + * Write a WMC document given a map. + * + * Parameters: + * obj - { | Object} A map or context object. + * options - {Object} Optional configuration object. + * + * Returns: + * {String} A WMC document string. + */ + write: function(obj, options) { + if(obj.CLASS_NAME == "OpenLayers.Map") { + obj = this.mapToContext(obj); + } + var version = (options && options.version) || + this.version || this.defaultVersion; + if(!this.parser || this.parser.VERSION != version) { + var format = OpenLayers.Format.WMC[ + "v" + version.replace(/\./g, "_") + ]; + if(!format) { + throw "Can't find a WMS capabilities parser for version " + + version; + } + this.parser = new format(this.options); + } + var wmc = this.parser.write(obj); + return wmc; + }, + + /** + * Method: mapToContext + * Create a context object given a map. + * + * Parameters: + * map - {} The map. + * + * Returns: + * {Object} A context object. + */ + mapToContext: function(map) { + var context = { + bounds: map.getExtent(), + maxExtent: map.maxExtent, + projection: map.projection, + layers: map.layers, + size: map.getSize() + }; + return context; + }, + + CLASS_NAME: "OpenLayers.Format.WMC" + +}); \ No newline at end of file diff --git a/lib/OpenLayers/Format/WMC/v1.js b/lib/OpenLayers/Format/WMC/v1.js new file mode 100644 index 0000000000..d9cd419a6c --- /dev/null +++ b/lib/OpenLayers/Format/WMC/v1.js @@ -0,0 +1,764 @@ +/* Copyright (c) 2006-2008 MetaCarta, Inc., 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 + */ + +/** + * Class: OpenLayers.Format.WMC.v1 + * Superclass for WMC version 1 parsers. + * + * Inherits from: + * - + */ +OpenLayers.Format.WMC.v1 = OpenLayers.Class(OpenLayers.Format.XML, { + + /** + * Property: namespaces + * {Object} Mapping of namespace aliases to namespace URIs. + */ + namespaces: { + ol: "http://openlayers.org/context", + wmc: "http://www.opengis.net/context", + sld: "http://www.opengis.net/sld", + xlink: "http://www.w3.org/1999/xlink", + xsi: "http://www.w3.org/2001/XMLSchema-instance" + }, + + /** + * Property: schemaLocation + * {String} Schema location for a particular minor version. + */ + schemaLocation: "", + + /** + * Method: getNamespacePrefix + * Get the namespace prefix for a given uri from the object. + * + * Returns: + * {String} A namespace prefix or null if none found. + */ + getNamespacePrefix: function(uri) { + var prefix = null; + if(uri == null) { + prefix = this.namespaces[this.defaultPrefix]; + } else { + for(prefix in this.namespaces) { + if(this.namespaces[prefix] == uri) { + break; + } + } + } + return prefix; + }, + + /** + * Property: defaultPrefix + */ + defaultPrefix: "wmc", + + /** + * Property: rootPrefix + * {String} Prefix on the root node that maps to the context namespace URI. + */ + rootPrefix: null, + + /** + * Property: defaultStyleName + * {String} Style name used if layer has no style param. Default is "". + */ + defaultStyleName: "", + + /** + * Property: defaultStyleTitle + * {String} Default style title. Default is "Default". + */ + defaultStyleTitle: "Default", + + /** + * Constructor: OpenLayers.Format.WMC.v1 + * Instances of this class are not created directly. Use the + * constructor instead. + * + * Parameters: + * options - {Object} An optional object whose properties will be set on + * this instance. + */ + initialize: function(options) { + OpenLayers.Format.XML.prototype.initialize.apply(this, [options]); + }, + + /** + * Method: read + * Read capabilities data from a string, and return a list of layers. + * + * Parameters: + * data - {String} or {DOMElement} data to read/parse. + * + * Returns: + * {Array} List of named layers. + */ + read: function(data) { + if(typeof data == "string") { + data = OpenLayers.Format.XML.prototype.read.apply(this, [data]); + } + var root = data.documentElement; + this.rootPrefix = root.prefix; + var context = { + version: root.getAttribute("version") + }; + this.runChildNodes(context, root); + return context; + }, + + /** + * Method: runChildNodes + */ + runChildNodes: function(obj, node) { + var children = node.childNodes; + var childNode, processor, prefix, local; + for(var i=0; i} A WMS layer. + */ + getLayerFromInfo: function(layerInfo) { + var layer = new OpenLayers.Layer.WMS( + layerInfo.title, + layerInfo.href, + layerInfo.params, + layerInfo.options + ); + return layer; + }, + + /** + * Method: read_wmc_Extension + */ + read_wmc_Extension: function(obj, node) { + this.runChildNodes(obj, node); + }, + + /** + * Method: read_ol_units + */ + read_ol_units: function(layerInfo, node) { + layerInfo.options.units = this.getChildValue(node); + }, + + /** + * Method: read_ol_maxExtent + */ + read_ol_maxExtent: function(obj, node) { + var bounds = new OpenLayers.Bounds( + node.getAttribute("minx"), node.getAttribute("miny"), + node.getAttribute("maxx"), node.getAttribute("maxy") + ); + obj.maxExtent = bounds; + }, + + /** + * Method: read_ol_transparent + */ + read_ol_transparent: function(layerInfo, node) { + layerInfo.params.transparent = this.getChildValue(node); + }, + + /** + * Method: read_ol_numZoomLevels + */ + read_ol_numZoomLevels: function(layerInfo, node) { + layerInfo.options.numZoomLevels = parseInt(this.getChildValue(node)); + }, + + /** + * Method: read_ol_opacity + */ + read_ol_opacity: function(layerInfo, node) { + layerInfo.options.opacity = parseFloat(this.getChildValue(node)); + }, + + /** + * Method: read_ol_singleTile + */ + read_ol_singleTile: function(layerInfo, node) { + layerInfo.options.singleTile = (this.getChildValue(node) == "true"); + }, + + /** + * Method: read_ol_isBaseLayer + */ + read_ol_isBaseLayer: function(layerInfo, node) { + layerInfo.options.isBaseLayer = (this.getChildValue(node) == "true"); + }, + + /** + * Method: read_ol_displayInLayerSwitcher + */ + read_ol_displayInLayerSwitcher: function(layerInfo, node) { + layerInfo.options.displayInLayerSwitcher = + (this.getChildValue(node) == "true"); + }, + + /** + * Method: read_wmc_Server + */ + read_wmc_Server: function(layerInfo, node) { + layerInfo.params.version = node.getAttribute("version"); + this.runChildNodes(layerInfo, node); + }, + + /** + * Method: read_wmc_FormatList + */ + read_wmc_FormatList: function(layerInfo, node) { + this.runChildNodes(layerInfo, node); + }, + + /** + * Method: read_wmc_Format + */ + read_wmc_Format: function(layerInfo, node) { + var format = this.getChildValue(node) + layerInfo.formats.push(format); + if(node.getAttribute("current") == "1") { + layerInfo.params.format = format; + } + }, + + /** + * Method: read_wmc_StyleList + */ + read_wmc_StyleList: function(layerInfo, node) { + this.runChildNodes(layerInfo, node); + }, + + /** + * Method: read_wmc_Style + */ + read_wmc_Style: function(layerInfo, node) { + var style = {}; + this.runChildNodes(style, node); + if(node.getAttribute("current") == "1") { + layerInfo.params.style = style.name; + } + layerInfo.styles.push(style); + }, + + /** + * Method: read_wmc_OnlineResource + */ + read_wmc_OnlineResource: function(obj, node) { + obj.href = this.getAttributeNS( + node, this.namespaces.xlink, "href" + ); + }, + + /** + * Method: read_wmc_Name + */ + read_wmc_Name: function(obj, node) { + var name = this.getChildValue(node); + if(name) { + obj.name = name; + } + }, + + /** + * Method: read_wmc_Title + */ + read_wmc_Title: function(obj, node) { + var title = this.getChildValue(node); + if(title) { + obj.title = title; + } + }, + + /** + * Method: read_wmc_Abstract + */ + read_wmc_Abstract: function(obj, node) { + var abst = this.getChildValue(node); + if(abst) { + obj["abstract"] = abst; + } + }, + + /** + * Method: read_wmc_LatLonBoundingBox + */ + read_wmc_LatLonBoundingBox: function(layer, node) { + layer.llbbox = [ + parseFloat(node.getAttribute("minx")), + parseFloat(node.getAttribute("miny")), + parseFloat(node.getAttribute("maxx")), + parseFloat(node.getAttribute("maxy")) + ]; + }, + + /** + * Method: read_wmc_LegendURL + */ + read_wmc_LegendURL: function(style, node) { + var legend = { + width: node.getAttribute('width'), + height: node.getAttribute('height') + }; + var links = node.getElementsByTagName("OnlineResource"); + if(links.length > 0) { + this.read_wmc_OnlineResource(legend, links[0]); + } + style.legend = legend; + }, + + /** + * Method: write + * + * Parameters: + * context - {Object} An object representing the map context. + * options - {Object} Optional object. + * + * Returns: + * {String} A WMC document string. + */ + write: function(context, options) { + var root = this.createElementDefaultNS("ViewContext"); + this.setAttributes(root, { + version: this.VERSION, + id: (options && typeof options.id == "string") ? + options.id : + OpenLayers.Util.createUniqueID("OpenLayers_Context_") + }); + + // add schemaLocation attribute + this.setAttributeNS( + root, this.namespaces.xsi, + "xsi:schemaLocation", this.schemaLocation + ); + + // required General element + root.appendChild(this.write_wmc_General(context)); + + // required LayerList element + root.appendChild(this.write_wmc_LayerList(context)); + + return OpenLayers.Format.XML.prototype.write.apply(this, [root]); + }, + + /** + * Method: createElementDefaultNS + * Shorthand for createElementNS with namespace from . + * Can optionally be used to set attributes and a text child value. + * + * Parameters: + * name - {String} The qualified node name. + * childValue - {String} Optional value for text child node. + * attributes - {Object} Optional object representing attributes. + * + * Returns: + * {Element} An element node. + */ + createElementDefaultNS: function(name, childValue, attributes) { + var node = this.createElementNS( + this.namespaces[this.defaultPrefix], + name + ); + if(childValue) { + node.appendChild(this.createTextNode(childValue)); + } + if(attributes) { + this.setAttributes(node, attributes); + } + return node; + }, + + /** + * Method: setAttributes + * Set multiple attributes given key value pairs from an object. + * + * Parameters: + * node - {Element} An element node. + * obj - {Object} An object whose properties represent attribute names and + * values represent attribute values. + */ + setAttributes: function(node, obj) { + var value; + for(var name in obj) { + value = obj[name].toString(); + if(value.match(/[A-Z]/)) { + // safari lowercases attributes with setAttribute + this.setAttributeNS(node, null, name, value); + } else { + node.setAttribute(name, value); + } + } + }, + + /** + * Method: write_wmc_General + * Create a General node given an context object. + * + * Parameters: + * context - {Object} Context object. + * + * Returns: + * {Element} A WMC General element node. + */ + write_wmc_General: function(context) { + var node = this.createElementDefaultNS("General"); + + // optional Window element + if(context.size) { + node.appendChild(this.createElementDefaultNS( + "Window", null, + { + width: context.size.w, + height: context.size.h + } + )); + } + + // required BoundingBox element + var bounds = context.bounds; + node.appendChild(this.createElementDefaultNS( + "BoundingBox", null, + { + minx: bounds.left.toPrecision(10), + miny: bounds.bottom.toPrecision(10), + maxx: bounds.right.toPrecision(10), + maxy: bounds.top.toPrecision(10), + SRS: context.projection + } + )); + + // required Title element + node.appendChild(this.createElementDefaultNS( + "Title", context.title + )); + + // OpenLayers specific map properties + node.appendChild(this.write_ol_MapExtension(context)); + + return node; + }, + + /** + * Method: write_ol_MapExtension + */ + write_ol_MapExtension: function(context) { + var node = this.createElementDefaultNS("Extension"); + + var bounds = context.maxExtent; + if(bounds) { + var maxExtent = this.createElementNS( + this.namespaces.ol, "ol:maxExtent" + ); + this.setAttributes(maxExtent, { + minx: bounds.left.toPrecision(10), + miny: bounds.bottom.toPrecision(10), + maxx: bounds.right.toPrecision(10), + maxy: bounds.top.toPrecision(10) + }); + node.appendChild(maxExtent); + } + + return node; + }, + + /** + * Method: write_wmc_LayerList + * Create a LayerList node given an context object. + * + * Parameters: + * context - {Object} Context object. + * + * Returns: + * {Element} A WMC LayerList element node. + */ + write_wmc_LayerList: function(context) { + var list = this.createElementDefaultNS("LayerList"); + + var layer; + for(var i=0; i} Layer object. + * + * Returns: + * {Element} A WMC Layer element node. + */ + write_wmc_Layer: function(layer) { + var node = this.createElementDefaultNS( + "Layer", null, { + queryable: "1", + hidden: layer.visibility ? "0" : "1" + } + ); + + // required Server element + node.appendChild(this.write_wmc_Server(layer)); + + // required Name element + node.appendChild(this.createElementDefaultNS( + "Name", layer.params["LAYERS"] + )); + + // required Title element + node.appendChild(this.createElementDefaultNS( + "Title", layer.name + )); + + // optional FormatList element + node.appendChild(this.write_wmc_FormatList(layer)); + + // optional StyleList element + node.appendChild(this.write_wmc_StyleList(layer)); + + // OpenLayers specific properties go in an Extension element + node.appendChild(this.write_wmc_LayerExtension(layer)); + + return node; + }, + + /** + * Method: write_wmc_LayerExtension + * Add OpenLayers specific layer parameters to an Extension element. + * + * Parameters: + * layer - {} A WMS layer. + * + * Returns: + * {Element} A WMC Extension element (for a layer). + */ + write_wmc_LayerExtension: function(layer) { + var node = this.createElementDefaultNS("Extension"); + + var bounds = layer.maxExtent; + var maxExtent = this.createElementNS( + this.namespaces.ol, "ol:maxExtent" + ); + this.setAttributes(maxExtent, { + minx: bounds.left.toPrecision(10), + miny: bounds.bottom.toPrecision(10), + maxx: bounds.right.toPrecision(10), + maxy: bounds.top.toPrecision(10) + }); + node.appendChild(maxExtent); + + var param = layer.params["TRANSPARENT"]; + if(param) { + var trans = this.createElementNS( + this.namespaces.ol, "ol:transparent" + ); + trans.appendChild(this.createTextNode(param)); + node.appendChild(trans); + } + + var properties = [ + "numZoomLevels", "units", "isBaseLayer", + "opacity", "displayInLayerSwitcher", "singleTile" + ]; + var child; + for(var i=0; i} Layer object. + * + * Returns: + * {Element} A WMC Server element node. + */ + write_wmc_Server: function(layer) { + var node = this.createElementDefaultNS("Server"); + this.setAttributes(node, { + service: "OGC:WMS", + version: layer.params["VERSION"] + }); + + // required OnlineResource element + node.appendChild(this.write_wmc_OnlineResource(layer.url)); + + return node; + }, + + /** + * Method: write_wmc_FormatList + * Create a FormatList node given a layer. + * + * Parameters: + * layer - {} Layer object. + * + * Returns: + * {Element} A WMC FormatList element node. + */ + write_wmc_FormatList: function(layer) { + var node = this.createElementDefaultNS("FormatList"); + node.appendChild(this.createElementDefaultNS( + "Format", layer.params["FORMAT"], {current: "1"} + )); + + return node; + }, + + /** + * Method: write_wmc_StyleList + * Create a StyleList node given a layer. + * + * Parameters: + * layer - {} Layer object. + * + * Returns: + * {Element} A WMC StyleList element node. + */ + write_wmc_StyleList: function(layer) { + var node = this.createElementDefaultNS("StyleList"); + var style = this.createElementDefaultNS( + "Style", null, {current: "1"} + ); + var name = layer.params["STYLES"] ? + layer.params["STYLES"] : this.defaultStyleName; + + style.appendChild(this.createElementDefaultNS("Name", name)); + style.appendChild(this.createElementDefaultNS( + "Title", this.defaultStyleTitle + )); + node.appendChild(style); + return node; + }, + + /** + * Method: write_wmc_OnlineResource + * Create an OnlineResource node given a URL. + * + * Parameters: + * href - {String} URL for the resource. + * + * Returns: + * {Element} A WMC OnlineResource element node. + */ + write_wmc_OnlineResource: function(href) { + var node = this.createElementDefaultNS("OnlineResource"); + this.setAttributeNS(node, this.namespaces.xlink, "xlink:type", "simple"); + this.setAttributeNS(node, this.namespaces.xlink, "xlink:href", href); + return node; + }, + + CLASS_NAME: "OpenLayers.Format.WMC.v1" + +}); diff --git a/lib/OpenLayers/Format/WMC/v1_0_0.js b/lib/OpenLayers/Format/WMC/v1_0_0.js new file mode 100644 index 0000000000..d0095c846d --- /dev/null +++ b/lib/OpenLayers/Format/WMC/v1_0_0.js @@ -0,0 +1,49 @@ +/* Copyright (c) 2006-2008 MetaCarta, Inc., 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/WMC/v1.js + */ + +/** + * Class: OpenLayers.Format.WMC.v1_0_0 + * Read and write WMC version 1.0.0. + * + * Inherits from: + * - + */ +OpenLayers.Format.WMC.v1_0_0 = OpenLayers.Class( + OpenLayers.Format.WMC.v1, { + + /** + * Constant: VERSION + * {String} 1.0.0 + */ + VERSION: "1.0.0", + + /** + * Property: schemaLocation + * {String} http://www.opengis.net/context + * http://schemas.opengis.net/context/1.0.0/context.xsd + */ + schemaLocation: "http://www.opengis.net/context http://schemas.opengis.net/context/1.0.0/context.xsd", + + /** + * Constructor: OpenLayers.Format.WMC.v1_0_0 + * Instances of this class are not created directly. Use the + * constructor instead. + * + * Parameters: + * options - {Object} An optional object whose properties will be set on + * this instance. + */ + initialize: function(options) { + OpenLayers.Format.WMC.v1.prototype.initialize.apply( + this, [options] + ); + }, + + CLASS_NAME: "OpenLayers.Format.WMC.v1_0_0" + +}); \ No newline at end of file diff --git a/lib/OpenLayers/Format/WMC/v1_1_0.js b/lib/OpenLayers/Format/WMC/v1_1_0.js new file mode 100644 index 0000000000..0ba65b3fb5 --- /dev/null +++ b/lib/OpenLayers/Format/WMC/v1_1_0.js @@ -0,0 +1,110 @@ +/* Copyright (c) 2006-2008 MetaCarta, Inc., 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/WMC/v1.js + */ + +/** + * Class: OpenLayers.Format.WMC.v1_1_0 + * Read and write WMC version 1.1.0. + * + * Differences between 1.1.0 and 1.0.0: + * - 1.1.0 Layers have optional sld:MinScaleDenominator and + * sld:MaxScaleDenominator + * + * Inherits from: + * - + */ +OpenLayers.Format.WMC.v1_1_0 = OpenLayers.Class( + OpenLayers.Format.WMC.v1, { + + /** + * Constant: VERSION + * {String} 1.1.0 + */ + VERSION: "1.1.0", + + /** + * Property: schemaLocation + * {String} http://www.opengis.net/context + * http://schemas.opengis.net/context/1.1.0/context.xsd + */ + schemaLocation: "http://www.opengis.net/context http://schemas.opengis.net/context/1.1.0/context.xsd", + + /** + * Constructor: OpenLayers.Format.WMC.v1_1_0 + * Instances of this class are not created directly. Use the + * constructor instead. + * + * Parameters: + * options - {Object} An optional object whose properties will be set on + * this instance. + */ + initialize: function(options) { + OpenLayers.Format.WMC.v1.prototype.initialize.apply( + this, [options] + ); + }, + + /** + * Method: read_sld_MinScaleDenominator + * Read a sld:MinScaleDenominator node. + * + * Parameters: + * layerInfo - {Object} An object representing a layer. + * node - {Element} An element node. + */ + read_sld_MinScaleDenominator: function(layerInfo, node) { + layerInfo.options.maxScale = this.getChildValue(node); + }, + + /** + * Method: read_sld_MaxScaleDenominator + * Read a sld:MaxScaleDenominator node. + * + * Parameters: + * layerInfo - {Object} An object representing a layer. + * node - {Element} An element node. + */ + read_sld_MaxScaleDenominator: function(layerInfo, node) { + layerInfo.options.minScale = this.getChildValue(node); + }, + + /** + * Method: write_wmc_Layer + * Create a Layer node given a layer object. This method adds elements + * specific to version 1.1.0. + * + * Parameters: + * layer - {} Layer object. + * + * Returns: + * {Element} A WMC Layer element node. + */ + write_wmc_Layer: function(layer) { + var node = OpenLayers.Format.WMC.v1.prototype.write_wmc_Layer.apply( + this, [layer] + ); + + // min/max scale denominator elements go before the 4th element in v1 + var minSD = this.createElementNS( + this.namespaces.sld, "sld:MinScaleDenominator" + ); + minSD.appendChild(this.createTextNode(layer.maxScale.toPrecision(10))); + node.insertBefore(minSD, node.childNodes[3]); + + var maxSD = this.createElementNS( + this.namespaces.sld, "sld:MaxScaleDenominator" + ); + maxSD.appendChild(this.createTextNode(layer.minScale.toPrecision(10))); + node.insertBefore(maxSD, node.childNodes[4]); + + return node; + + }, + + CLASS_NAME: "OpenLayers.Format.WMC.v1_1_0" + +}); \ No newline at end of file diff --git a/lib/OpenLayers/Format/XML.js b/lib/OpenLayers/Format/XML.js index 58e9120118..44cc5d26d9 100644 --- a/lib/OpenLayers/Format/XML.js +++ b/lib/OpenLayers/Format/XML.js @@ -348,6 +348,37 @@ OpenLayers.Format.XML = OpenLayers.Class(OpenLayers.Format, { } return found; }, + + /** + * APIMethod: setAttributeNS + * Adds a new attribute or changes the value of an attribute with the given + * namespace and name. + * + * Parameters: + * node - {Element} Element node on which to set the attribute. + * uri - {String} Namespace URI for the attribute. + * name - {String} Qualified name (prefix:localname) for the attribute. + * value - {String} Attribute value. + */ + setAttributeNS: function(node, uri, name, value) { + if(node.setAttributeNS) { + node.setAttributeNS(uri, name, value); + } else { + if(this.xmldom) { + if(uri) { + var attribute = node.ownerDocument.createNode( + 2, name, uri + ); + attribute.nodeValue = value; + node.setAttributeNode(attribute); + } else { + node.setAttribute(name, value); + } + } else { + throw "setAttributeNS not implemented"; + } + } + }, CLASS_NAME: "OpenLayers.Format.XML" diff --git a/tests/Format/test_WMC.html b/tests/Format/test_WMC.html new file mode 100644 index 0000000000..27487f450f --- /dev/null +++ b/tests/Format/test_WMC.html @@ -0,0 +1,142 @@ + + + + + + +
+ + diff --git a/tests/list-tests.html b/tests/list-tests.html index ebb9782bc6..787c3a33d5 100644 --- a/tests/list-tests.html +++ b/tests/list-tests.html @@ -29,6 +29,7 @@
  • Format/test_KML.html
  • Format/test_SLD.html
  • Format/test_WKT.html
  • +
  • Format/test_WMC.html
  • Format/test_XML.html
  • test_Icon.html
  • test_Marker.html