[OpenLayers-Commits] r5617 - in sandbox/tschaub/wmc/lib/OpenLayers/Format: . WMC

commits at openlayers.org commits at openlayers.org
Tue Jan 1 17:23:02 EST 2008


Author: tschaub
Date: 2008-01-01 17:23:02 -0500 (Tue, 01 Jan 2008)
New Revision: 5617

Modified:
   sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC.js
   sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1.js
   sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1_0_0.js
   sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1_1_0.js
   sandbox/tschaub/wmc/lib/OpenLayers/Format/XML.js
Log:
read/write wmc

Modified: sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1.js
===================================================================
--- sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1.js	2008-01-01 18:47:37 UTC (rev 5616)
+++ sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1.js	2008-01-01 22:23:02 UTC (rev 5617)
@@ -1,22 +1,59 @@
 /**
- * @requires OpenLayers/Format/WMC.js
- *
+ * @requires OpenLayers/Format/XML.js
+ */
+
+/**
  * Class: OpenLayers.Format.WMC.v1
  * Superclass for WMC version 1 parsers.
+ *
+ * Inherits from:
+ *  - <OpenLayers.Format.XML>
  */
-OpenLayers.Format.WMC.v1 = OpenLayers.Class({
+OpenLayers.Format.WMC.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
     
     /**
      * Property: namespaces
      * {Object} Mapping of namespace aliases to namespace URIs.
      */
     namespaces: {
-        context: "http://www.opengeospatial.net/context",
+        wmc: "http://www.opengeospatial.net/context",
         sld: "http://www.opengeospatial.net/sld",
         xlink: "http://www.w3.org/1999/xlink"
     },
+    
+    /**
+     * Method: getNamespacePrefix
+     * Get the namespace prefix for a given uri from the <namespaces> 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,
+    
+    /**
      * Constructor: OpenLayers.Format.WMC.v1
      * Instances of this class are not created directly.  Use the
      *     <OpenLayers.Format.WMC> constructor instead.
@@ -30,7 +67,7 @@
     },
 
     /**
-     * APIMethod: read
+     * Method: read
      * Read capabilities data from a string, and return a list of layers. 
      * 
      * Parameters: 
@@ -44,6 +81,7 @@
             data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
         }
         var root = data.documentElement;
+        this.rootPrefix = root.prefix;
         var context = {
             version: root.getAttribute("version")
         };
@@ -56,11 +94,14 @@
      */
     runChildNodes: function(obj, node) {
         var children = node.childNodes;
-        var childNode, processor;
+        var childNode, processor, prefix;
         for(var i=0; i<children.length; ++i) {
             childNode = children[i];
             if(childNode.nodeType == 1) {
-                processor = this["read" + childNode.nodeName];
+                prefix = (childNode.prefix == this.rootPrefix) ?
+                            this.defaultPrefix :
+                            this.getNamespacePrefix(childNode.namespaceURI);
+                processor = this["read_" + prefix + "_" + childNode.nodeName];
                 if(processor) {
                     processor.apply(this, [obj, childNode]);
                 }
@@ -69,16 +110,16 @@
     },
     
     /**
-     * Method: readGeneral
+     * Method: read_wmc_General
      */
-    readGeneral: function(context, node) {
+    read_wmc_General: function(context, node) {
         this.runChildNodes(context, node);
     },
     
     /**
-     * Method: readBoundingBox
+     * Method: read_wmc_BoundingBox
      */
-    readBoundingBox: function(context, node) {
+    read_wmc_BoundingBox: function(context, node) {
         context.srs = node.getAttribute("SRS");
         context.bounds = new OpenLayers.Bounds(
             parseFloat(node.getAttribute("minx")),
@@ -89,17 +130,17 @@
     },
     
     /**
-     * Method: readLayerList
+     * Method: read_wmc_LayerList
      */
-    readLayerList: function(context, node) {
+    read_wmc_LayerList: function(context, node) {
         context.layers = [];
         this.runChildNodes(context, node);
     },
     
     /**
-     * Method: readLayer
+     * Method: read_wmc_Layer
      */
-    readLayer: function(context, node) {
+    read_wmc_Layer: function(context, node) {
         var layerInfo = {
             visibility: (node.getAttribute("hidden") != "1"),
             queryable: (node.getAttribute("queryable") == "1"),
@@ -122,24 +163,24 @@
     },
     
     /**
-     * Method: readServer
+     * Method: read_wmc_Server
      */
-    readServer: function(layerInfo, node) {
+    read_wmc_Server: function(layerInfo, node) {
         layerInfo.version = node.getAttribute("version");
         this.runChildNodes(layerInfo, node);
     },
 
     /**
-     * Method: readFormatList
+     * Method: read_wmc_FormatList
      */
-    readFormatList: function(layerInfo, node) {
+    read_wmc_FormatList: function(layerInfo, node) {
         this.runChildNodes(layerInfo, node);
     },
 
     /**
-     * Method: readFormat
+     * Method: read_wmc_Format
      */
-    readFormat: function(layerInfo, node) {
+    read_wmc_Format: function(layerInfo, node) {
         var format = this.getChildValue(node)
         layerInfo.formats.push(format);
         if(node.getAttribute("current") == "1") {
@@ -148,16 +189,16 @@
     },
     
     /**
-     * Method: readStyleList
+     * Method: read_wmc_StyleList
      */
-    readStyleList: function(layerInfo, node) {
+    read_wmc_StyleList: function(layerInfo, node) {
         this.runChildNodes(layerInfo, node);
     },
 
     /**
-     * Method: readStyle
+     * Method: read_wmc_Style
      */
-    readStyle: function(layerInfo, node) {
+    read_wmc_Style: function(layerInfo, node) {
         var style = {};
         this.runChildNodes(style, node);
         if(node.getAttribute("current") == "1") {
@@ -167,18 +208,18 @@
     },
 
     /**
-     * Method: readOnlineResource
+     * Method: read_wmc_OnlineResource
      */
-    readOnlineResource: function(obj, node) {
+    read_wmc_OnlineResource: function(obj, node) {
         obj.href = this.getAttributeNS(
-            node, "http://www.w3.org/1999/xlink", "href"
+            node, this.namespaces.xlink, "href"
         );
     },
     
     /**
-     * Method: readName
+     * Method: read_wmc_Name
      */
-    readName: function(obj, node) {
+    read_wmc_Name: function(obj, node) {
         var name = this.getChildValue(node);
         if(name) {
             obj.name = name;
@@ -186,9 +227,9 @@
     },
 
     /**
-     * Method: readTitle
+     * Method: read_wmc_Title
      */
-    readTitle: function(obj, node) {
+    read_wmc_Title: function(obj, node) {
         var title = this.getChildValue(node);
         if(title) {
             obj.title = title;
@@ -196,9 +237,9 @@
     },
 
     /**
-     * Method: readAbstract
+     * Method: read_wmc_Abstract
      */
-    readAbstract: function(obj, node) {
+    read_wmc_Abstract: function(obj, node) {
         var abst = this.getChildValue(node);
         if(abst) {
             obj["abstract"] = abst;
@@ -206,9 +247,9 @@
     },
     
     /**
-     * Method: readLatLonBoundingBox
+     * Method: read_wmc_LatLonBoundingBox
      */
-    readLatLonBoundingBox: function(layer, node) {
+    read_wmc_LatLonBoundingBox: function(layer, node) {
         layer.llbbox = [
             parseFloat(node.getAttribute("minx")),
             parseFloat(node.getAttribute("miny")),
@@ -218,21 +259,266 @@
     },
 
     /**
-     * Method: readLegendURL
+     * Method: read_wmc_LegendURL
      */
-    readLegendURL: function(style, node) {
+    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.readOnlineResource(legend, links[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_")
+        });
 
+        // 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 <defaultPrefix>.
+     *     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) {
+        for(var name in obj) {
+            node.setAttribute(name, obj[name]);
+        }
+    },
+
+    /**
+     * 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, miny: bounds.bottom,
+                maxx: bounds.right, maxy: bounds.top,
+                SRS: context.srs
+            }
+        ));
+
+        // required Title element
+        node.appendChild(this.createElementDefaultNS(
+            "Title", context.title
+        ));
+        
+        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<context.layers.length; ++i) {
+            list.appendChild(this.write_wmc_Layer(context.layers[i]));
+        }
+        
+        return list;
+    },
+
+    /**
+     * Method: write_wmc_Layer
+     * Create a Layer node given a layer object.
+     *
+     * Parameters:
+     * layer - {<OpenLayers.Layer.WMS>} Layer object.
+     *
+     * Returns:
+     * {Element} A WMC Layer element node.
+     */
+    write_wmc_Layer: function(layer) {
+        var node = this.createElementDefaultNS(
+            "Layer", null, {
+                queryable: "true",
+                hidden: layer.visibility ? "true" : "false"
+            }
+        );
+        
+        // required Name element
+        node.appendChild(this.createElementDefaultNS(
+            "Name", layer.params["LAYERS"]
+        ));
+        
+        // required Title element
+        node.appendChild(this.createElementDefaultNS(
+            "Title", layer.name
+        ));
+
+        // required Server element
+        node.appendChild(this.write_wmc_Server(layer));
+
+        return node;
+    },
+
+    /**
+     * Method: write_wmc_Server
+     * Create a Server node given a layer object.
+     *
+     * Parameters:
+     * layer - {<OpenLayers.Layer.WMS>} 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 - {<OpenLayers.Layer.WMS>} 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: "true"}
+        ));
+
+        return node;
+    },
+
+    /**
+     * Method: write_wmc_StyleList
+     * Create a StyleList node given a layer.
+     *
+     * Parameters:
+     * layer - {<OpenLayers.Layer.WMS>} Layer object.
+     *
+     * Returns:
+     * {Element} A WMC StyleList element node.
+     */
+    write_wmc_StyleList: function(layer) {
+        var node = this.createElementDefaultNS("StyleList");
+        node.appendChild(this.createElementDefaultNS(
+            "Style", layer.params["STYLES"], {current: "true"}
+        ));
+
+        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" 
 
 });
\ No newline at end of file

Modified: sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1_0_0.js
===================================================================
--- sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1_0_0.js	2008-01-01 18:47:37 UTC (rev 5616)
+++ sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1_0_0.js	2008-01-01 22:23:02 UTC (rev 5617)
@@ -1,6 +1,8 @@
 /**
- * @requires OpenLayers/Format/WMC.js
- *
+ * @requires OpenLayers/Format/WMC/v1.js
+ */
+
+/**
  * Class: OpenLayers.Format.WMC.v1_0_0
  * Read and write WMC version 1.0.0.
  * 

Modified: sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1_1_0.js
===================================================================
--- sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1_1_0.js	2008-01-01 18:47:37 UTC (rev 5616)
+++ sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1_1_0.js	2008-01-01 22:23:02 UTC (rev 5617)
@@ -1,6 +1,8 @@
 /**
- * @requires OpenLayers/Format/WMC.js
- *
+ * @requires OpenLayers/Format/WMC/v1.js
+ */
+
+/**
  * Class: OpenLayers.Format.WMC.v1_1_0
  * Read and write WMC version 1.1.0.
  *

Modified: sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC.js
===================================================================
--- sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC.js	2008-01-01 18:47:37 UTC (rev 5616)
+++ sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC.js	2008-01-01 22:23:02 UTC (rev 5617)
@@ -1,13 +1,15 @@
 /**
  * @requires OpenLayers/Format/XML.js
- *
+ */
+
+/**
  * Class: OpenLayers.Format.WMC
  * Read and write Web Map Context documents.
- * 
+ *
  * Inherits from:
  *  - <OpenLayers.Format.XML>
  */
-OpenLayers.Format.WMC = OpenLayers.Class(OpenLayers.Format.XML, {
+OpenLayers.Format.WMC = OpenLayers.Class({
     
     /**
      * APIProperty: defaultVersion
@@ -37,7 +39,7 @@
      *     this instance.
      */
     initialize: function(options) {
-        OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
+        OpenLayers.Util.extend(this, options);
         this.options = options;
     },
 
@@ -135,6 +137,7 @@
      */
     mapToContext: function(map) {
         var context = {};
+        return context;
     },
 
     CLASS_NAME: "OpenLayers.Format.WMC" 

Modified: sandbox/tschaub/wmc/lib/OpenLayers/Format/XML.js
===================================================================
--- sandbox/tschaub/wmc/lib/OpenLayers/Format/XML.js	2008-01-01 18:47:37 UTC (rev 5616)
+++ sandbox/tschaub/wmc/lib/OpenLayers/Format/XML.js	2008-01-01 22:23:02 UTC (rev 5617)
@@ -348,6 +348,25 @@
         }
         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.
+     * namespace - {String} Namespace URI for the attribute.
+     * name - {String} Qualified name (prefix:localName) for the attribute.
+     * value - {String} Attribute value.
+     */
+    setAttributeNS: function(node, namespace, name, value) {
+        if(node.setAttributeNS) {
+            node.setAttributeNS(namespace, name, value);
+        } else {
+            throw "setAttributeNS not implemented";
+        }
+    },
 
     CLASS_NAME: "OpenLayers.Format.XML" 
 



More information about the Commits mailing list