[OpenLayers-Commits] r12155 - in trunk/openlayers: lib lib/OpenLayers/Format lib/OpenLayers/Format/XML tests tests/Format tests/Format/XML

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Tue Jul 5 05:42:35 EDT 2011


Author: bartvde
Date: 2011-07-05 02:42:34 -0700 (Tue, 05 Jul 2011)
New Revision: 12155

Added:
   trunk/openlayers/lib/OpenLayers/Format/XML/
   trunk/openlayers/lib/OpenLayers/Format/XML/VersionedOGC.js
   trunk/openlayers/tests/Format/XML/
   trunk/openlayers/tests/Format/XML/VersionedOGC.html
Modified:
   trunk/openlayers/lib/OpenLayers.js
   trunk/openlayers/lib/OpenLayers/Format/Context.js
   trunk/openlayers/lib/OpenLayers/Format/Filter.js
   trunk/openlayers/lib/OpenLayers/Format/OWSCommon.js
   trunk/openlayers/lib/OpenLayers/Format/OWSContext.js
   trunk/openlayers/lib/OpenLayers/Format/SLD.js
   trunk/openlayers/lib/OpenLayers/Format/SOSCapabilities.js
   trunk/openlayers/lib/OpenLayers/Format/WFSCapabilities.js
   trunk/openlayers/lib/OpenLayers/Format/WMC.js
   trunk/openlayers/lib/OpenLayers/Format/WMSCapabilities.js
   trunk/openlayers/lib/OpenLayers/Format/WMSDescribeLayer.js
   trunk/openlayers/lib/OpenLayers/Format/WMTSCapabilities.js
   trunk/openlayers/lib/OpenLayers/Format/WPSCapabilities.js
   trunk/openlayers/lib/OpenLayers/Format/XLS.js
   trunk/openlayers/tests/list-tests.html
Log:
implement versioned format base class, r=ahocevar (closes #2954)

Modified: trunk/openlayers/lib/OpenLayers/Format/Context.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Format/Context.js	2011-07-05 07:48:28 UTC (rev 12154)
+++ trunk/openlayers/lib/OpenLayers/Format/Context.js	2011-07-05 09:42:34 UTC (rev 12155)
@@ -4,22 +4,16 @@
  * full text of the license. */
 
 /**
- * @requires OpenLayers/Format/XML.js
+ * @requires OpenLayers/Format/XML/VersionedOGC.js
  */
 
 /**
  * Class: OpenLayers.Format.Context
  * Base class for both Format.WMC and Format.OWSContext
  */
-OpenLayers.Format.Context = OpenLayers.Class({
+OpenLayers.Format.Context = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, {
 
     /**
-     * APIProperty: version
-     * {String} Specify a version string if one is known.
-     */
-    version: null,
-
-    /**
      * Property: layerOptions
      * {Object} Default options for layers created by the parser. These
      *     options are overridden by the options which are read from the
@@ -36,13 +30,6 @@
     layerParams: 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.Context
      * Create a new parser for Context documents.
      *
@@ -50,10 +37,6 @@
      * 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
@@ -72,16 +55,8 @@
      * {<OpenLayers.Map>} 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");
-        }
-        var parser = this.getParser(version);
-        var context = parser.read(data, options);
+        var context = OpenLayers.Format.XML.VersionedOGC.prototype.read.apply(this, 
+            arguments);
         var map;
         if(options && options.map) {
             this.context = context;
@@ -334,10 +309,8 @@
      */
     write: function(obj, options) {
         obj = this.toContext(obj);
-        var version = options && options.version;
-        var parser = this.getParser(version);
-        var context = parser.write(obj, options);
-        return context;
+        return OpenLayers.Format.XML.VersionedOGC.prototype.write.apply(this,
+            arguments);
     },
 
     CLASS_NAME: "OpenLayers.Format.Context"

Modified: trunk/openlayers/lib/OpenLayers/Format/Filter.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Format/Filter.js	2011-07-05 07:48:28 UTC (rev 12154)
+++ trunk/openlayers/lib/OpenLayers/Format/Filter.js	2011-07-05 09:42:34 UTC (rev 12155)
@@ -4,7 +4,7 @@
  * full text of the license. */
 
 /**
- * @requires OpenLayers/Format/XML.js
+ * @requires OpenLayers/Format/XML/VersionedOGC.js
  * @requires OpenLayers/Filter/FeatureId.js
  * @requires OpenLayers/Filter/Logical.js
  * @requires OpenLayers/Filter/Comparison.js
@@ -16,9 +16,9 @@
  *     constructor.
  * 
  * Inherits from:
- *  - <OpenLayers.Format.XML>
+ *  - <OpenLayers.Format.XML.VersionedOGC>
  */
-OpenLayers.Format.Filter = OpenLayers.Class(OpenLayers.Format.XML, {
+OpenLayers.Format.Filter = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, {
     
     /**
      * APIProperty: defaultVersion
@@ -27,28 +27,6 @@
     defaultVersion: "1.0.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.Filter
-     * Create a new parser for Filter.
-     *
-     * Parameters:
-     * options - {Object} An optional object whose properties will be set on
-     *     this instance.
-     */
-
-    /**
      * APIMethod: write
      * Write an ogc:Filter given a filter object.
      *
@@ -59,22 +37,6 @@
      * Returns:
      * {Elment} An ogc:Filter element node.
      */
-    write: function(filter, options) {
-        var version = (options && options.version) ||
-                      this.version || this.defaultVersion;
-        if(!this.parser || this.parser.VERSION != version) {
-            var format = OpenLayers.Format.Filter[
-                "v" + version.replace(/\./g, "_")
-            ];
-            if(!format) {
-                throw "Can't find a Filter parser for version " +
-                      version;
-            }
-            this.parser = new format(this.options);
-        }
-        return this.parser.write(filter);
-        //return OpenLayers.Format.XML.prototype.write.apply(this, [root]);
-    },
     
     /**
      * APIMethod: read
@@ -86,27 +48,6 @@
      * Returns:
      * {<OpenLayers.Filter>} A filter object.
      */
-    read: function(data) {
-        if(typeof data == "string") {
-            data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
-        }
-        var version = this.version;
-        if(!version) {
-            version = this.defaultVersion;
-        }
-        if(!this.parser || this.parser.VERSION != version) {
-            var format = OpenLayers.Format.Filter[
-                "v" + version.replace(/\./g, "_")
-            ];
-            if(!format) {
-                throw "Can't find a Filter parser for version " +
-                      version;
-            }
-            this.parser = new format(this.options);
-        }
-        var filter = this.parser.read(data);
-        return filter;
-    },
 
     CLASS_NAME: "OpenLayers.Format.Filter" 
 });

Modified: trunk/openlayers/lib/OpenLayers/Format/OWSCommon.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Format/OWSCommon.js	2011-07-05 07:48:28 UTC (rev 12154)
+++ trunk/openlayers/lib/OpenLayers/Format/OWSCommon.js	2011-07-05 09:42:34 UTC (rev 12155)
@@ -4,7 +4,7 @@
  * full text of the license. */
 
 /**
- * @requires OpenLayers/Format/XML.js
+ * @requires OpenLayers/Format/XML/VersionedOGC.js
  */
 
 /**
@@ -13,9 +13,9 @@
  *     constructor.
  * 
  * Inherits from:
- *  - <OpenLayers.Format.XML>
+ *  - <OpenLayers.Format.XML.VersionedOGC>
  */
-OpenLayers.Format.OWSCommon = OpenLayers.Class(OpenLayers.Format.XML, {
+OpenLayers.Format.OWSCommon = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, {
     
     /**
      * APIProperty: defaultVersion
@@ -24,19 +24,6 @@
     defaultVersion: "1.0.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.OWSCommon
      * Create a new parser for OWSCommon.
      *
@@ -46,21 +33,18 @@
      */
 
     /**
-     * APIMethod: read
-     * Read an OWSCommon document and return an object.
+     * Method: getVersion
+     * Returns the version to use. Subclasses can override this function
+     * if a different version detection is needed.
      *
      * Parameters:
-     * data - {String | DOMElement} Data to read.
-     * options - {Object} Options for the reader.
+     * root - {DOMElement}
+     * options - {Object} Optional configuration object.
      *
      * Returns:
-     * {Object} An object representing the structure of the document.
+     * {String} The version to use.
      */
-    read: function(data, options) {
-        if(typeof data == "string") {
-            data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
-        }
-        var root = data.documentElement;
+    getVersion: function(root, options) {
         var version = this.version;
         if(!version) {
             // remember version does not correspond to the OWS version
@@ -75,19 +59,20 @@
                 version = this.defaultVersion;
             }
         }
-        if(!this.parser || this.parser.VERSION != version) {
-            var format = OpenLayers.Format.OWSCommon[
-                "v" + version.replace(/\./g, "_")
-            ];
-            if(!format) {
-                throw "Can't find a OWSCommon parser for version " +
-                      version;
-            }
-            this.parser = new format(this.options);
-        }
-        var ows = this.parser.read(data, options);
-        return ows;
+        return version;
     },
 
+    /**
+     * APIMethod: read
+     * Read an OWSCommon document and return an object.
+     *
+     * Parameters:
+     * data - {String | DOMElement} Data to read.
+     * options - {Object} Options for the reader.
+     *
+     * Returns:
+     * {Object} An object representing the structure of the document.
+     */
+
     CLASS_NAME: "OpenLayers.Format.OWSCommon" 
 });

Modified: trunk/openlayers/lib/OpenLayers/Format/OWSContext.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Format/OWSContext.js	2011-07-05 07:48:28 UTC (rev 12154)
+++ trunk/openlayers/lib/OpenLayers/Format/OWSContext.js	2011-07-05 09:42:34 UTC (rev 12155)
@@ -34,32 +34,25 @@
      */
     
     /**
-     * Method: getParser
-     * Get the OWSContext parser given a version. Create a new parser if it does not
-     * already exist.
+     * Method: getVersion
+     * Returns the version to use. Subclasses can override this function
+     * if a different version detection is needed.
      *
      * Parameters:
-     * version - {String} The version of the parser.
+     * root - {DOMElement}
+     * options - {Object} Optional configuration object.
      *
      * Returns:
-     * {<OpenLayers.Format.OWSContext>} An OWSContext parser.
+     * {String} The version to use.
      */
-    getParser: function(version) {
-        var v = version || this.version || this.defaultVersion;
+    getVersion: function(root, options) {
+        var version = OpenLayers.Format.XML.VersionedOGC.prototype.getVersion.apply(
+            this, arguments);
         // 0.3.1 is backwards compatible with 0.3.0
-        if (v === "0.3.0") {
-            v = this.defaultVersion;
+        if (version === "0.3.0") {
+            version = this.defaultVersion;
         }
-        if(!this.parser || this.parser.VERSION != v) {
-            var format = OpenLayers.Format.OWSContext[
-                "v" + v.replace(/\./g, "_")
-            ];
-            if(!format) {
-                throw "Can't find a OWSContext parser for version " + v;
-            }
-            this.parser = new format(this.options);
-        }
-        return this.parser;
+        return version;
     },
 
     /**

Modified: trunk/openlayers/lib/OpenLayers/Format/SLD.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Format/SLD.js	2011-07-05 07:48:28 UTC (rev 12154)
+++ trunk/openlayers/lib/OpenLayers/Format/SLD.js	2011-07-05 09:42:34 UTC (rev 12155)
@@ -4,7 +4,7 @@
  * full text of the license. */
 
 /**
- * @requires OpenLayers/Format/XML.js
+ * @requires OpenLayers/Format/XML/VersionedOGC.js
  * @requires OpenLayers/Style.js
  * @requires OpenLayers/Rule.js
  * @requires OpenLayers/Filter/FeatureId.js
@@ -19,9 +19,9 @@
  *     constructor.
  * 
  * Inherits from:
- *  - <OpenLayers.Format.XML>
+ *  - <OpenLayers.Format.XML.VersionedOGC>
  */
-OpenLayers.Format.SLD = OpenLayers.Class(OpenLayers.Format.XML, {
+OpenLayers.Format.SLD = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, {
     
     /**
      * APIProperty: defaultVersion
@@ -30,10 +30,11 @@
     defaultVersion: "1.0.0",
     
     /**
-     * APIProperty: version
-     * {String} Specify a version string if one is known.
+     * APIProperty: stringifyOutput
+     * {Boolean} If true, write will return a string otherwise a DOMElement.
+     * Default is true.
      */
-    version: null,
+    stringifyOutput: true,
     
     /**
      * APIProperty: namedLayersAsArray
@@ -44,22 +45,6 @@
     namedLayersAsArray: false,
     
     /**
-     * Property: parser
-     * {Object} Instance of the versioned parser.  Cached for multiple read and
-     *     write calls of the same version.
-     */
-    parser: null,
-
-    /**
-     * Constructor: OpenLayers.Format.SLD
-     * Create a new parser for SLD.
-     *
-     * Parameters:
-     * options - {Object} An optional object whose properties will be set on
-     *     this instance.
-     */
-
-    /**
      * APIMethod: write
      * Write a SLD document given a list of styles.
      *
@@ -70,22 +55,6 @@
      * Returns:
      * {String} An SLD document string.
      */
-    write: function(sld, options) {
-        var version = (options && options.version) ||
-                      this.version || this.defaultVersion;
-        if(!this.parser || this.parser.VERSION != version) {
-            var format = OpenLayers.Format.SLD[
-                "v" + version.replace(/\./g, "_")
-            ];
-            if(!format) {
-                throw "Can't find a SLD parser for version " +
-                      version;
-            }
-            this.parser = new format(this.options);
-        }
-        var root = this.parser.write(sld);
-        return OpenLayers.Format.XML.prototype.write.apply(this, [root]);
-    },
     
     /**
      * APIMethod: read
@@ -98,31 +67,6 @@
      * Returns:
      * {Object} An object representing the SLD.
      */
-    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.SLD[
-                "v" + version.replace(/\./g, "_")
-            ];
-            if(!format) {
-                throw "Can't find a SLD parser for version " +
-                      version;
-            }
-            this.parser = new format(this.options);
-        }
-        var sld = this.parser.read(data, options);
-        return sld;
-    },
 
     CLASS_NAME: "OpenLayers.Format.SLD" 
 });

Modified: trunk/openlayers/lib/OpenLayers/Format/SOSCapabilities.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Format/SOSCapabilities.js	2011-07-05 07:48:28 UTC (rev 12154)
+++ trunk/openlayers/lib/OpenLayers/Format/SOSCapabilities.js	2011-07-05 09:42:34 UTC (rev 12155)
@@ -4,7 +4,7 @@
  * full text of the license. */
 
 /**
- * @requires OpenLayers/Format/XML.js
+ * @requires OpenLayers/Format/XML/VersionedOGC.js
  */
  
 /**
@@ -12,9 +12,9 @@
  * Read SOS Capabilities.
  * 
  * Inherits from:
- *  - <OpenLayers.Format.XML>
+ *  - <OpenLayers.Format.XML.VersionedOGC>
  */
-OpenLayers.Format.SOSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, {
+OpenLayers.Format.SOSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, {
     
     /**
      * APIProperty: defaultVersion
@@ -23,18 +23,6 @@
     defaultVersion: "1.0.0",
     
     /**
-     * APIProperty: version
-     * {String} Specify a version string if one is known.
-     */
-    version: null,
-
-    /**
-     * Property: parser
-     * {<OpenLayers.Format>} A cached versioned format used for reading.
-     */
-    parser: null,     
-
-    /**
      * Constructor: OpenLayers.Format.SOSCapabilities
      * Create a new parser for SOS Capabilities.
      *
@@ -54,25 +42,6 @@
      * Returns:
      * {Object} Info about the SOS
      */
-    read: function(data) {
-        if(typeof data == "string") {
-            data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
-        }
-        var root = data.documentElement;
-        var version = this.version || root.getAttribute("version") || this.defaultVersion;
-        if(!this.parser || this.parser.version !== version) {
-            var constr = OpenLayers.Format.SOSCapabilities[
-                "v" + version.replace(/\./g, "_")
-            ];
-            if(!constr) {
-                throw "Can't find a SOS capabilities parser for version " + version;
-            }
-            var parser = new constr(this.options);
-        }
-        var capabilities = parser.read(data);
-        capabilities.version = version;
-        return capabilities; 
-    },
     
     CLASS_NAME: "OpenLayers.Format.SOSCapabilities" 
 

Modified: trunk/openlayers/lib/OpenLayers/Format/WFSCapabilities.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Format/WFSCapabilities.js	2011-07-05 07:48:28 UTC (rev 12154)
+++ trunk/openlayers/lib/OpenLayers/Format/WFSCapabilities.js	2011-07-05 09:42:34 UTC (rev 12155)
@@ -4,8 +4,7 @@
  * full text of the license. */
 
 /**
- * @requires OpenLayers/Format/XML.js
- * @requires OpenLayers/Format/OGCExceptionReport.js
+ * @requires OpenLayers/Format/XML/VersionedOGC.js
  */
 
 /**
@@ -13,21 +12,24 @@
  * Read WFS Capabilities.
  * 
  * Inherits from:
- *  - <OpenLayers.Format.XML>
+ *  - <OpenLayers.Format.XML.VersionedOGC>
  */
-OpenLayers.Format.WFSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, {
+OpenLayers.Format.WFSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, {
     
     /**
      * 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.
+     * APIProperty: errorProperty
+     * {String} Which property of the returned object to check for in order to
+     * determine whether or not parsing has failed. In the case that the
+     * errorProperty is undefined on the returned object, the document will be
+     * run through an OGCExceptionReport parser.
      */
-    version: null,
+    errorProperty: "service",
 
     /**
      * Constructor: OpenLayers.Format.WFSCapabilities
@@ -48,34 +50,6 @@
      * 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;
-        var version = this.version;
-        if(!version) {
-            version = root.getAttribute("version");
-            if(!version) {
-                version = this.defaultVersion;
-            }
-        }
-        var constr = OpenLayers.Format.WFSCapabilities[
-            "v" + version.replace(/\./g, "_")
-        ];
-        if(!constr) {
-            throw "Can't find a WFS capabilities parser for version " + version;
-        }
-        var parser = new constr(this.options);
-        var capabilities = parser.read(data);
-        if (capabilities.service === undefined) {
-            // an error must have happened, so parse it and report back
-            var format = new OpenLayers.Format.OGCExceptionReport();
-            capabilities.error = format.read(data);
-        }
-        capabilities.version = version;
-        return capabilities;
-    },
     
     CLASS_NAME: "OpenLayers.Format.WFSCapabilities" 
 

Modified: trunk/openlayers/lib/OpenLayers/Format/WMC.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Format/WMC.js	2011-07-05 07:48:28 UTC (rev 12154)
+++ trunk/openlayers/lib/OpenLayers/Format/WMC.js	2011-07-05 09:42:34 UTC (rev 12155)
@@ -33,31 +33,6 @@
      */
     
     /**
-     * Method: getParser
-     * Get the WMC parser given a version. Create a new parser if it does not
-     * already exist.
-     *
-     * Parameters:
-     * version - {String} The version of the parser.
-     *
-     * Returns:
-     * {<OpenLayers.Format.WMC.v1>} A WMC parser.
-     */
-    getParser: function(version) {
-        var v = version || this.version || this.defaultVersion;
-        if(!this.parser || this.parser.VERSION != v) {
-            var format = OpenLayers.Format.WMC[
-                "v" + v.replace(/\./g, "_")
-            ];
-            if(!format) {
-                throw "Can't find a WMC parser for version " + v;
-            }
-            this.parser = new format(this.options);
-        }
-        return this.parser;
-    },
-
-    /**
      * Method: layerToContext
      * Create a layer context object given a wms layer object.
      *

Modified: trunk/openlayers/lib/OpenLayers/Format/WMSCapabilities.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Format/WMSCapabilities.js	2011-07-05 07:48:28 UTC (rev 12154)
+++ trunk/openlayers/lib/OpenLayers/Format/WMSCapabilities.js	2011-07-05 09:42:34 UTC (rev 12155)
@@ -4,7 +4,7 @@
  * full text of the license. */
 
 /**
- * @requires OpenLayers/Format/XML.js
+ * @requires OpenLayers/Format/XML/VersionedOGC.js
  */
 
 /**
@@ -12,9 +12,9 @@
  * Read WMS Capabilities.
  * 
  * Inherits from:
- *  - <OpenLayers.Format.XML>
+ *  - <OpenLayers.Format.XML.VersionedOGC>
  */
-OpenLayers.Format.WMSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, {
+OpenLayers.Format.WMSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, {
     
     /**
      * APIProperty: defaultVersion
@@ -23,12 +23,6 @@
     defaultVersion: "1.1.1",
     
     /**
-     * APIProperty: version
-     * {String} Specify a version string if one is known.
-     */
-    version: null,
-    
-    /**
      * APIProperty: profile
      * {String} If provided, use a custom profile.
      *
@@ -38,12 +32,6 @@
     profile: null,
     
     /**
-     * Property: parser
-     * {<OpenLayers.Format>} A cached versioned format used for reading.
-     */
-    parser: null,
-
-    /**
      * Constructor: OpenLayers.Format.WMSCapabilities
      * Create a new parser for WMS capabilities.
      *
@@ -62,27 +50,6 @@
      * 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;
-        var version = this.version || root.getAttribute("version") || this.defaultVersion;
-        var profile = this.profile ? "_" + this.profile : "";
-        if(!this.parser || this.parser.version !== version) {
-            var constr = OpenLayers.Format.WMSCapabilities[
-                "v" + version.replace(/\./g, "_") + profile
-            ];
-            if(!constr) {
-                throw "Can't find a WMS capabilities parser for version " +
-                    version + profile;
-            }
-            this.parser = new constr(this.options);
-        }
-        var capabilities = this.parser.read(data);
-        capabilities.version = version;
-        return capabilities;
-    },
     
     CLASS_NAME: "OpenLayers.Format.WMSCapabilities" 
 

Modified: trunk/openlayers/lib/OpenLayers/Format/WMSDescribeLayer.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Format/WMSDescribeLayer.js	2011-07-05 07:48:28 UTC (rev 12154)
+++ trunk/openlayers/lib/OpenLayers/Format/WMSDescribeLayer.js	2011-07-05 09:42:34 UTC (rev 12155)
@@ -4,7 +4,7 @@
  * full text of the license. */
 
 /**
- * @requires OpenLayers/Format/XML.js
+ * @requires OpenLayers/Format/XML/VersionedOGC.js
  */
 
 /**
@@ -13,9 +13,9 @@
  * DescribeLayer is meant to couple WMS to WFS and WCS
  * 
  * Inherits from:
- *  - <OpenLayers.Format.XML>
+ *  - <OpenLayers.Format.XML.VersionedOGC>
  */
-OpenLayers.Format.WMSDescribeLayer = OpenLayers.Class(OpenLayers.Format.XML, {
+OpenLayers.Format.WMSDescribeLayer = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, {
 
     /**
      * APIProperty: defaultVersion
@@ -24,10 +24,26 @@
     defaultVersion: "1.1.1",
    
     /**
-     * APIProperty: version
-     * {String} Specify a version string if one is known.
+     * Method: getVersion
+     * Returns the version to use. Subclasses can override this function
+     * if a different version detection is needed.
+     *
+     * Parameters:
+     * root - {DOMElement}
+     * options - {Object} Optional configuration object.
+     *
+     * Returns:
+     * {String} The version to use.
      */
-    version: null,
+    getVersion: function(root, options) {
+        var version = OpenLayers.Format.XML.VersionedOGC.prototype.getVersion.apply(
+            this, arguments);
+        // these are identical to us, but some WMS use 1.1.1 and some use 1.1.0
+        if (version == "1.1.1" || version == "1.1.0") {
+            version = "1.1";
+        }
+        return version;
+    },
 
     /**
      * Constructor: OpenLayers.Format.WMSDescribeLayer
@@ -53,34 +69,6 @@
      * - {String} owsURL: the online resource
      * - {String} typeName: the name of the typename on the service
      */
-    read: function(data) {
-        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;
-            }
-        }
-        // these are identical to us, but some WMS use 1.1.1 and some use 1.1.0
-        if (version == "1.1.1" || version == "1.1.0") {
-            version = "1.1";
-        }
-        var constructor = OpenLayers.Format.WMSDescribeLayer[
-            "v" + version.replace(/\./g, "_")
-        ];
-        if(!constructor) {
-            throw "Can't find a WMS DescribeLayer parser for version " + 
-                version;
-        }
-        var parser = new constructor(this.options);
-        var describelayer = parser.read(data);
-        describelayer.version = version;
-        return describelayer;
-    },
     
     CLASS_NAME: "OpenLayers.Format.WMSDescribeLayer" 
 

Modified: trunk/openlayers/lib/OpenLayers/Format/WMTSCapabilities.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Format/WMTSCapabilities.js	2011-07-05 07:48:28 UTC (rev 12154)
+++ trunk/openlayers/lib/OpenLayers/Format/WMTSCapabilities.js	2011-07-05 09:42:34 UTC (rev 12155)
@@ -4,7 +4,7 @@
  * full text of the license. */
 
 /**
- * @requires OpenLayers/Format/XML.js
+ * @requires OpenLayers/Format/XML/VersionedOGC.js
  */
  
 /**
@@ -12,9 +12,9 @@
  * Read WMTS Capabilities.
  * 
  * Inherits from:
- *  - <OpenLayers.Format.XML>
+ *  - <OpenLayers.Format.XML.VersionedOGC>
  */
-OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, {
+OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, {
     
     /**
      * APIProperty: defaultVersion
@@ -23,18 +23,6 @@
     defaultVersion: "1.0.0",
     
     /**
-     * APIProperty: version
-     * {String} Specify a version string if one is known.
-     */
-    version: null,
-
-    /**
-     * Property: parser
-     * {<OpenLayers.Format>} A cached versioned format used for reading.
-     */
-    parser: null,
-    
-    /**
      * APIProperty: yx
      * {Object} Members in the yx object are used to determine if a CRS URN
      *     corresponds to a CRS with y,x axis order.  Member names are CRS URNs
@@ -67,23 +55,6 @@
      * Returns:
      * {Object} Info about the WMTS Capabilities
      */
-    read: function(data) {
-        if (typeof data == "string") {
-            data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
-        }
-        var root = data.documentElement;
-        var version = this.version || root.getAttribute("version") || this.defaultVersion;
-        if (!this.parser || this.parser.version !== version) {
-            var constr = OpenLayers.Format.WMTSCapabilities[
-                "v" + version.replace(/\./g, "_")
-            ];
-            if (!constr) {
-                throw new Error("Can't find a WMTS capabilities parser for version " + version);
-            }
-            this.parser = new constr(this.options);
-        }
-        return this.parser.read(data);
-    },
 
     /**
      * APIMethod: createLayer

Modified: trunk/openlayers/lib/OpenLayers/Format/WPSCapabilities.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Format/WPSCapabilities.js	2011-07-05 07:48:28 UTC (rev 12154)
+++ trunk/openlayers/lib/OpenLayers/Format/WPSCapabilities.js	2011-07-05 09:42:34 UTC (rev 12155)
@@ -4,7 +4,7 @@
  * full text of the license. */
 
 /**
- * @requires OpenLayers/Format/XML.js
+ * @requires OpenLayers/Format/XML/VersionedOGC.js
  */
  
 /**
@@ -12,9 +12,9 @@
  * Read WPS Capabilities.
  * 
  * Inherits from:
- *  - <OpenLayers.Format.XML>
+ *  - <OpenLayers.Format.XML.VersionedOGC>
  */
-OpenLayers.Format.WPSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, {
+OpenLayers.Format.WPSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, {
     
     /**
      * APIProperty: defaultVersion
@@ -23,18 +23,6 @@
     defaultVersion: "1.0.0",
     
     /**
-     * APIProperty: version
-     * {String} Specify a version string if one is known.
-     */
-    version: null,
-
-    /**
-     * Property: parser
-     * {<OpenLayers.Format>} A cached versioned format used for reading.
-     */
-    parser: null,     
-
-    /**
      * Constructor: OpenLayers.Format.WPSCapabilities
      * Create a new parser for WPS Capabilities.
      *
@@ -54,25 +42,6 @@
      * Returns:
      * {Object} Info about the WPS
      */
-    read: function(data) {
-        if(typeof data == "string") {
-            data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
-        }
-        var root = data.documentElement;
-        var version = this.version || root.getAttribute("version") || this.defaultVersion;
-        if(!this.parser || this.parser.version !== version) {
-            var constr = OpenLayers.Format.WPSCapabilities[
-                "v" + version.replace(/\./g, "_")
-            ];
-            if(!constr) {
-                throw "Can't find a WPS capabilities parser for version " + version;
-            }
-            var parser = new constr(this.options);
-        }
-        var capabilities = parser.read(data);
-        capabilities.version = version;
-        return capabilities; 
-    },
     
     CLASS_NAME: "OpenLayers.Format.WPSCapabilities" 
 

Modified: trunk/openlayers/lib/OpenLayers/Format/XLS.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Format/XLS.js	2011-07-05 07:48:28 UTC (rev 12154)
+++ trunk/openlayers/lib/OpenLayers/Format/XLS.js	2011-07-05 09:42:34 UTC (rev 12155)
@@ -4,7 +4,7 @@
  * full text of the license. */
 
 /**
- * @requires OpenLayers/Format/XML.js
+ * @requires OpenLayers/Format/XML/VersionedOGC.js
  */
 
 /**
@@ -14,30 +14,24 @@
  *     specifically only for Geocoding. No support for Reverse Geocoding as yet.
  * 
  * Inherits from:
- *  - <OpenLayers.Format.XML>
+ *  - <OpenLayers.Format.XML.VersionedOGC>
  */
-OpenLayers.Format.XLS = OpenLayers.Class(OpenLayers.Format.XML, {
+OpenLayers.Format.XLS = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, {
     
     /**
      * 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.
+     * APIProperty: stringifyOutput
+     * {Boolean} If true, write will return a string otherwise a DOMElement.
+     * Default is true.
      */
-    version: null,
+    stringifyOutput: true,
     
     /**
-     * Property: parser
-     * {Object} Instance of the versioned parser.  Cached for multiple read and
-     *     write calls of the same version.
-     */
-    parser: null,
-
-    /**
      * Constructor: OpenLayers.Format.XLS
      * Create a new parser for XLS.
      *
@@ -45,9 +39,6 @@
      * options - {Object} An optional object whose properties will be set on
      *     this instance.
      */
-    initialize: function(options) {
-        OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
-    },
 
     /**
      * APIMethod: write
@@ -60,22 +51,6 @@
      * Returns:
      * {String} An XLS document string.
      */
-    write: function(request, options) {
-        var version = (options && options.version) ||
-                      this.version || this.defaultVersion;
-        if(!this.parser || this.parser.VERSION != version) {
-            var format = OpenLayers.Format.XLS[
-                "v" + version.replace(/\./g, "_")
-            ];
-            if(!format) {
-                throw "Can't find an XLS parser for version " +
-                      version;
-            }
-            this.parser = new format(this.options);
-        }
-        var root = this.parser.write(request);
-        return OpenLayers.Format.XML.prototype.write.apply(this, [root]);
-    },
     
     /**
      * APIMethod: read
@@ -88,31 +63,6 @@
      * Returns:
      * {Object} An object representing the GeocodeResponse.
      */
-    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.XLS[
-                "v" + version.replace(/\./g, "_")
-            ];
-            if(!format) {
-                throw "Can't find an XLS parser for version " +
-                      version;
-            }
-            this.parser = new format(this.options);
-        }
-        var xls = this.parser.read(data, options);
-        return xls;
-    },
 
     CLASS_NAME: "OpenLayers.Format.XLS" 
 });

Added: trunk/openlayers/lib/OpenLayers/Format/XML/VersionedOGC.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Format/XML/VersionedOGC.js	                        (rev 0)
+++ trunk/openlayers/lib/OpenLayers/Format/XML/VersionedOGC.js	2011-07-05 09:42:34 UTC (rev 12155)
@@ -0,0 +1,190 @@
+/* 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/OGCExceptionReport.js
+ */
+
+/**
+ * Class: OpenLayers.Format.XML.VersionedOGC
+ * Base class for versioned formats, i.e. a format which supports multiple
+ * versions.
+ * 
+ * Inherits from:
+ *  - <OpenLayers.Format.XML>
+ */
+OpenLayers.Format.XML.VersionedOGC = OpenLayers.Class(OpenLayers.Format.XML, {
+    
+    /**
+     * APIProperty: defaultVersion
+     * {String} Version number to assume if none found.
+     */
+    defaultVersion: null,
+    
+    /**
+     * APIProperty: version
+     * {String} Specify a version string if one is known.
+     */
+    version: null,
+
+    /**
+     * APIProperty: profile
+     * {String} If provided, use a custom profile.
+     */
+    profile: null,
+
+    /**
+     * APIProperty: errorProperty
+     * {String} Which property of the returned object to check for in order to
+     * determine whether or not parsing has failed. In the case that the
+     * errorProperty is undefined on the returned object, the document will be
+     * run through an OGCExceptionReport parser.
+     */
+    errorProperty: null,
+
+    /**
+     * Property: name
+     * {String} The name of this parser, this is the part of the CLASS_NAME
+     * except for "OpenLayers.Format."
+     */
+    name: null,
+
+    /**
+     * APIProperty: stringifyOutput
+     * {Boolean} If true, write will return a string otherwise a DOMElement.
+     * Default is false.
+     */
+    stringifyOutput: false,
+
+    /**
+     * Property: parser
+     * {Object} Instance of the versioned parser.  Cached for multiple read and
+     *     write calls of the same version.
+     */
+    parser: null,
+
+    /**
+     * Constructor: OpenLayers.Format.XML.VersionedOGC.
+     * Constructor.
+     *
+     * Parameters:
+     * options - {Object} Optional object whose properties will be set on
+     *     the object.
+     */
+    initialize: function(options) {
+        OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
+        var className = this.CLASS_NAME;
+        this.name = className.substring(className.lastIndexOf(".")+1);
+    },
+
+    /**
+     * Method: getVersion
+     * Returns the version to use. Subclasses can override this function
+     * if a different version detection is needed.
+     *
+     * Parameters:
+     * root - {DOMElement}
+     * options - {Object} Optional configuration object.
+     *
+     * Returns:
+     * {String} The version to use.
+     */
+    getVersion: function(root, options) {
+        var version;
+        // read
+        if (root) {
+            version = this.version;
+            if(!version) {
+                version = root.getAttribute("version");
+                if(!version) {
+                    version = this.defaultVersion;
+                }
+            }
+        } else { // write
+            version = (options && options.version) || 
+                this.version || this.defaultVersion;
+        }
+        return version;
+    },
+
+    /**
+     * Method: getParser
+     * Get an instance of the cached parser if available, otherwise create one.
+     *
+     * Parameters:
+     * version - {String}
+     *
+     * Returns:
+     * {<OpenLayers.Format>}
+     */
+    getParser: function(version) {
+        version = version || this.defaultVersion;
+        var profile = this.profile ? "_" + this.profile : "";
+        if(!this.parser || this.parser.VERSION != version) {
+            var format = OpenLayers.Format[this.name][
+                "v" + version.replace(/\./g, "_") + profile
+            ];
+            if(!format) {
+                throw "Can't find a " + this.name + " parser for version " +
+                      version + profile;
+            }
+            this.parser = new format(this.options);
+        }
+        return this.parser;
+    },
+
+    /**
+     * APIMethod: write
+     * Write a document.
+     *
+     * Parameters:
+     * obj - {Object} An object representing the document.
+     * options - {Object} Optional configuration object.
+     *
+     * Returns:
+     * {String} The document as a string
+     */
+    write: function(obj, options) {
+        var version = this.getVersion(null, options);
+        this.parser = this.getParser(version);
+        var root = this.parser.write(obj, options);
+        if (this.stringifyOutput === false) {
+            return root;
+        } else {
+            return OpenLayers.Format.XML.prototype.write.apply(this, [root]);
+        }
+    },
+
+    /**
+     * APIMethod: read
+     * Read a doc and return an object representing the document.
+     *
+     * Parameters:
+     * data - {String | DOMElement} Data to read.
+     * options - {Object} Options for the reader.
+     *
+     * Returns:
+     * {Object} An object representing the document.
+     */
+    read: function(data, options) {
+        if(typeof data == "string") {
+            data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
+        }
+        var root = data.documentElement;
+        var version = this.getVersion(root);
+        this.parser = this.getParser(version);
+        var obj = this.parser.read(data, options);
+        if (this.errorProperty !== null && obj[this.errorProperty] === undefined) {
+            // an error must have happened, so parse it and report back
+            var format = new OpenLayers.Format.OGCExceptionReport();
+            obj.error = format.read(data);
+        }
+        obj.version = version;
+        return obj;
+    },
+
+    CLASS_NAME: "OpenLayers.Format.XML.VersionedOGC"
+});

Modified: trunk/openlayers/lib/OpenLayers.js
===================================================================
--- trunk/openlayers/lib/OpenLayers.js	2011-07-05 07:48:28 UTC (rev 12154)
+++ trunk/openlayers/lib/OpenLayers.js	2011-07-05 09:42:34 UTC (rev 12155)
@@ -268,6 +268,7 @@
                 "OpenLayers/Format.js",
                 "OpenLayers/Format/QueryStringFilter.js",
                 "OpenLayers/Format/XML.js",
+                "OpenLayers/Format/XML/VersionedOGC.js",
                 "OpenLayers/Format/Context.js",
                 "OpenLayers/Format/ArcXML.js",
                 "OpenLayers/Format/ArcXML/Features.js",

Added: trunk/openlayers/tests/Format/XML/VersionedOGC.html
===================================================================
--- trunk/openlayers/tests/Format/XML/VersionedOGC.html	                        (rev 0)
+++ trunk/openlayers/tests/Format/XML/VersionedOGC.html	2011-07-05 09:42:34 UTC (rev 12155)
@@ -0,0 +1,51 @@
+<html> 
+<head> 
+    <script src="../../OLLoader.js"></script> 
+    <script type="text/javascript">
+
+    var snippet = '<foo version="2.0.0"></foo>';
+    var snippet2 = '<foo></foo>';
+
+    function test_Format_Versioned_constructor(t) { 
+        t.plan(5); 
+         
+        var format = new OpenLayers.Format.XML.VersionedOGC({version: "1.0.0"}); 
+        t.ok(format instanceof OpenLayers.Format.XML.VersionedOGC, 
+             "new OpenLayers.Format.XML.VersionedOGC returns object" ); 
+        t.eq(format.version, "1.0.0", "constructor sets version correctly");
+        t.eq(format.defaultVersion, null, "defaultVersion should be null if not specified");
+        t.eq(typeof format.read, "function", "format has a read function");
+        t.eq(typeof format.write, "function", "format has a read function");
+    }
+
+    function test_getVersion(t) {
+        t.plan(6);
+        var format = new OpenLayers.Format.XML.VersionedOGC();
+        // read
+        var data = new OpenLayers.Format.XML().read(snippet);
+        var root = data.documentElement;
+        var version = format.getVersion(root);
+        t.eq(version, "2.0.0", "Version taken from document");
+        format = new OpenLayers.Format.XML.VersionedOGC({version: "1.0.0"});
+        version = format.getVersion(root);
+        t.eq(version, "1.0.0", "Version taken from parser takes preference");
+        format = new OpenLayers.Format.XML.VersionedOGC({defaultVersion: "3.0.0"});
+        data = new OpenLayers.Format.XML().read(snippet2);
+        root = data.documentElement;
+        version = format.getVersion(root);
+        t.eq(version, "3.0.0", "If nothing else is set, defaultVersion should be returned");
+        // write
+        version = format.getVersion(null, {version: "1.3.0"});
+        t.eq(version, "1.3.0", "Version from options returned");
+        version = format.getVersion(null);
+        t.eq(version, "3.0.0", "defaultVersion returned if no version specified in options and no version on the format");
+        format.version = "2.1.3";
+        version = format.getVersion(null);
+        t.eq(version, "2.1.3", "version returned of the Format if no version specified in options");
+    }
+
+    </script> 
+</head> 
+<body> 
+</body> 
+</html> 

Modified: trunk/openlayers/tests/list-tests.html
===================================================================
--- trunk/openlayers/tests/list-tests.html	2011-07-05 07:48:28 UTC (rev 12154)
+++ trunk/openlayers/tests/list-tests.html	2011-07-05 09:42:34 UTC (rev 12155)
@@ -57,6 +57,7 @@
     <li>Format.html</li>
     <li>Format/Atom.html</li>
     <li>Format/ArcXML.html</li>
+    <li>Format/XML/VersionedOGC.html</li>
     <li>Format/ArcXML/Features.html</li>
     <li>Format/CQL.html</li>
     <li>Format/GeoJSON.html</li>



More information about the Commits mailing list