From c59e225d8f9599255bd1b72ea0f74956aaec068d Mon Sep 17 00:00:00 2001 From: bartvde Date: Tue, 5 Jul 2011 09:42:34 +0000 Subject: [PATCH] implement versioned format base class, r=ahocevar (closes #2954) git-svn-id: http://svn.openlayers.org/trunk/openlayers@12155 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers.js | 1 + lib/OpenLayers/Format/Context.js | 39 +---- lib/OpenLayers/Format/Filter.js | 65 +------- lib/OpenLayers/Format/OWSCommon.js | 61 +++---- lib/OpenLayers/Format/OWSContext.js | 31 ++-- lib/OpenLayers/Format/SLD.js | 70 +------- lib/OpenLayers/Format/SOSCapabilities.js | 37 +---- lib/OpenLayers/Format/WFSCapabilities.js | 46 ++---- lib/OpenLayers/Format/WMC.js | 25 --- lib/OpenLayers/Format/WMSCapabilities.js | 39 +---- lib/OpenLayers/Format/WMSDescribeLayer.js | 56 +++---- lib/OpenLayers/Format/WMTSCapabilities.js | 35 +--- lib/OpenLayers/Format/WPSCapabilities.js | 37 +---- lib/OpenLayers/Format/XLS.js | 66 +------- lib/OpenLayers/Format/XML/VersionedOGC.js | 190 ++++++++++++++++++++++ tests/Format/XML/VersionedOGC.html | 51 ++++++ tests/list-tests.html | 1 + 17 files changed, 346 insertions(+), 504 deletions(-) create mode 100644 lib/OpenLayers/Format/XML/VersionedOGC.js create mode 100644 tests/Format/XML/VersionedOGC.html diff --git a/lib/OpenLayers.js b/lib/OpenLayers.js index 1d95ad98de..8431b64e33 100644 --- a/lib/OpenLayers.js +++ b/lib/OpenLayers.js @@ -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", diff --git a/lib/OpenLayers/Format/Context.js b/lib/OpenLayers/Format/Context.js index d572a39b59..b73a4204e4 100644 --- a/lib/OpenLayers/Format/Context.js +++ b/lib/OpenLayers/Format/Context.js @@ -4,20 +4,14 @@ * 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({ - - /** - * APIProperty: version - * {String} Specify a version string if one is known. - */ - version: null, +OpenLayers.Format.Context = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, { /** * Property: layerOptions @@ -35,13 +29,6 @@ OpenLayers.Format.Context = OpenLayers.Class({ */ 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 @@ OpenLayers.Format.Context = OpenLayers.Class({ * 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.Format.Context = OpenLayers.Class({ * {} 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 @@ OpenLayers.Format.Context = OpenLayers.Class({ */ 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" diff --git a/lib/OpenLayers/Format/Filter.js b/lib/OpenLayers/Format/Filter.js index 075b77bddd..4fa803e123 100644 --- a/lib/OpenLayers/Format/Filter.js +++ b/lib/OpenLayers/Format/Filter.js @@ -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.Filter = OpenLayers.Class(OpenLayers.Format.XML, { +OpenLayers.Format.Filter = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, { /** * APIProperty: defaultVersion @@ -26,28 +26,6 @@ OpenLayers.Format.Filter = OpenLayers.Class(OpenLayers.Format.XML, { */ 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 @@ OpenLayers.Format.Filter = OpenLayers.Class(OpenLayers.Format.XML, { * 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 @@ OpenLayers.Format.Filter = OpenLayers.Class(OpenLayers.Format.XML, { * Returns: * {} 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" }); diff --git a/lib/OpenLayers/Format/OWSCommon.js b/lib/OpenLayers/Format/OWSCommon.js index e940e987d7..b5e4e85eff 100644 --- a/lib/OpenLayers/Format/OWSCommon.js +++ b/lib/OpenLayers/Format/OWSCommon.js @@ -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.OWSCommon = OpenLayers.Class(OpenLayers.Format.XML, { +OpenLayers.Format.OWSCommon = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, { /** * APIProperty: defaultVersion @@ -23,19 +23,6 @@ OpenLayers.Format.OWSCommon = OpenLayers.Class(OpenLayers.Format.XML, { */ 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 @@ OpenLayers.Format.OWSCommon = OpenLayers.Class(OpenLayers.Format.XML, { */ /** - * 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 @@ OpenLayers.Format.OWSCommon = OpenLayers.Class(OpenLayers.Format.XML, { 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" }); diff --git a/lib/OpenLayers/Format/OWSContext.js b/lib/OpenLayers/Format/OWSContext.js index 28bd933819..9f90332dbc 100644 --- a/lib/OpenLayers/Format/OWSContext.js +++ b/lib/OpenLayers/Format/OWSContext.js @@ -34,32 +34,25 @@ OpenLayers.Format.OWSContext = OpenLayers.Class(OpenLayers.Format.Context,{ */ /** - * 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: - * {} 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; }, /** diff --git a/lib/OpenLayers/Format/SLD.js b/lib/OpenLayers/Format/SLD.js index b5cf2760a9..a6e4713fb5 100644 --- a/lib/OpenLayers/Format/SLD.js +++ b/lib/OpenLayers/Format/SLD.js @@ -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.SLD = OpenLayers.Class(OpenLayers.Format.XML, { +OpenLayers.Format.SLD = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, { /** * APIProperty: defaultVersion @@ -30,10 +30,11 @@ OpenLayers.Format.SLD = OpenLayers.Class(OpenLayers.Format.XML, { 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 @@ -43,22 +44,6 @@ OpenLayers.Format.SLD = OpenLayers.Class(OpenLayers.Format.XML, { */ 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 @@ OpenLayers.Format.SLD = OpenLayers.Class(OpenLayers.Format.XML, { * 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 @@ OpenLayers.Format.SLD = OpenLayers.Class(OpenLayers.Format.XML, { * 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" }); diff --git a/lib/OpenLayers/Format/SOSCapabilities.js b/lib/OpenLayers/Format/SOSCapabilities.js index bd3b4d79e7..ae7643b60b 100644 --- a/lib/OpenLayers/Format/SOSCapabilities.js +++ b/lib/OpenLayers/Format/SOSCapabilities.js @@ -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.SOSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, { +OpenLayers.Format.SOSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, { /** * APIProperty: defaultVersion @@ -22,18 +22,6 @@ OpenLayers.Format.SOSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, { */ defaultVersion: "1.0.0", - /** - * APIProperty: version - * {String} Specify a version string if one is known. - */ - version: null, - - /** - * Property: parser - * {} A cached versioned format used for reading. - */ - parser: null, - /** * Constructor: OpenLayers.Format.SOSCapabilities * Create a new parser for SOS Capabilities. @@ -54,25 +42,6 @@ OpenLayers.Format.SOSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, { * 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" diff --git a/lib/OpenLayers/Format/WFSCapabilities.js b/lib/OpenLayers/Format/WFSCapabilities.js index 45f62a5af2..ba6588459d 100644 --- a/lib/OpenLayers/Format/WFSCapabilities.js +++ b/lib/OpenLayers/Format/WFSCapabilities.js @@ -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.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 @@ OpenLayers.Format.WFSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, { * 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" diff --git a/lib/OpenLayers/Format/WMC.js b/lib/OpenLayers/Format/WMC.js index 77838ebc0c..8b3ad62a58 100644 --- a/lib/OpenLayers/Format/WMC.js +++ b/lib/OpenLayers/Format/WMC.js @@ -32,31 +32,6 @@ OpenLayers.Format.WMC = OpenLayers.Class(OpenLayers.Format.Context, { * this instance. */ - /** - * 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: - * {} 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. diff --git a/lib/OpenLayers/Format/WMSCapabilities.js b/lib/OpenLayers/Format/WMSCapabilities.js index 6d1f0a3d3e..5315c214ae 100644 --- a/lib/OpenLayers/Format/WMSCapabilities.js +++ b/lib/OpenLayers/Format/WMSCapabilities.js @@ -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.WMSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, { +OpenLayers.Format.WMSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, { /** * APIProperty: defaultVersion @@ -22,12 +22,6 @@ OpenLayers.Format.WMSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, { */ 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. @@ -37,12 +31,6 @@ OpenLayers.Format.WMSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, { */ profile: null, - /** - * Property: parser - * {} A cached versioned format used for reading. - */ - parser: null, - /** * Constructor: OpenLayers.Format.WMSCapabilities * Create a new parser for WMS capabilities. @@ -62,27 +50,6 @@ OpenLayers.Format.WMSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, { * 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" diff --git a/lib/OpenLayers/Format/WMSDescribeLayer.js b/lib/OpenLayers/Format/WMSDescribeLayer.js index b7d576c581..02461c995c 100644 --- a/lib/OpenLayers/Format/WMSDescribeLayer.js +++ b/lib/OpenLayers/Format/WMSDescribeLayer.js @@ -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.WMSDescribeLayer = OpenLayers.Class(OpenLayers.Format.XML, { +OpenLayers.Format.WMSDescribeLayer = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, { /** * APIProperty: defaultVersion @@ -24,10 +24,26 @@ OpenLayers.Format.WMSDescribeLayer = OpenLayers.Class(OpenLayers.Format.XML, { 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 @@ OpenLayers.Format.WMSDescribeLayer = OpenLayers.Class(OpenLayers.Format.XML, { * - {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" diff --git a/lib/OpenLayers/Format/WMTSCapabilities.js b/lib/OpenLayers/Format/WMTSCapabilities.js index 273ecfb56b..bc5d35d211 100644 --- a/lib/OpenLayers/Format/WMTSCapabilities.js +++ b/lib/OpenLayers/Format/WMTSCapabilities.js @@ -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.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, { +OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, { /** * APIProperty: defaultVersion @@ -22,18 +22,6 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, { */ defaultVersion: "1.0.0", - /** - * APIProperty: version - * {String} Specify a version string if one is known. - */ - version: null, - - /** - * Property: parser - * {} 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 @@ -67,23 +55,6 @@ OpenLayers.Format.WMTSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, { * 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 diff --git a/lib/OpenLayers/Format/WPSCapabilities.js b/lib/OpenLayers/Format/WPSCapabilities.js index 5a6d4c33dc..aa5513096a 100644 --- a/lib/OpenLayers/Format/WPSCapabilities.js +++ b/lib/OpenLayers/Format/WPSCapabilities.js @@ -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.WPSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, { +OpenLayers.Format.WPSCapabilities = OpenLayers.Class(OpenLayers.Format.XML.VersionedOGC, { /** * APIProperty: defaultVersion @@ -22,18 +22,6 @@ OpenLayers.Format.WPSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, { */ defaultVersion: "1.0.0", - /** - * APIProperty: version - * {String} Specify a version string if one is known. - */ - version: null, - - /** - * Property: parser - * {} A cached versioned format used for reading. - */ - parser: null, - /** * Constructor: OpenLayers.Format.WPSCapabilities * Create a new parser for WPS Capabilities. @@ -54,25 +42,6 @@ OpenLayers.Format.WPSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, { * 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" diff --git a/lib/OpenLayers/Format/XLS.js b/lib/OpenLayers/Format/XLS.js index 967e4653b3..3329491204 100644 --- a/lib/OpenLayers/Format/XLS.js +++ b/lib/OpenLayers/Format/XLS.js @@ -4,7 +4,7 @@ * full text of the license. */ /** - * @requires OpenLayers/Format/XML.js + * @requires OpenLayers/Format/XML/VersionedOGC.js */ /** @@ -14,29 +14,23 @@ * specifically only for Geocoding. No support for Reverse Geocoding as yet. * * Inherits from: - * - + * - */ -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 @@ OpenLayers.Format.XLS = OpenLayers.Class(OpenLayers.Format.XML, { * 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 @@ OpenLayers.Format.XLS = OpenLayers.Class(OpenLayers.Format.XML, { * 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 @@ OpenLayers.Format.XLS = OpenLayers.Class(OpenLayers.Format.XML, { * 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" }); diff --git a/lib/OpenLayers/Format/XML/VersionedOGC.js b/lib/OpenLayers/Format/XML/VersionedOGC.js new file mode 100644 index 0000000000..c0fc4d1b61 --- /dev/null +++ b/lib/OpenLayers/Format/XML/VersionedOGC.js @@ -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.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: + * {} + */ + 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" +}); diff --git a/tests/Format/XML/VersionedOGC.html b/tests/Format/XML/VersionedOGC.html new file mode 100644 index 0000000000..ca96d6322e --- /dev/null +++ b/tests/Format/XML/VersionedOGC.html @@ -0,0 +1,51 @@ + + + + + + + + diff --git a/tests/list-tests.html b/tests/list-tests.html index 7205c06004..a388bae361 100644 --- a/tests/list-tests.html +++ b/tests/list-tests.html @@ -57,6 +57,7 @@
  • Format.html
  • Format/Atom.html
  • Format/ArcXML.html
  • +
  • Format/XML/VersionedOGC.html
  • Format/ArcXML/Features.html
  • Format/CQL.html
  • Format/GeoJSON.html