From fd6e7ebe03a006d032374c7bcc89bff4d736647e Mon Sep 17 00:00:00 2001 From: Bart van den Eijnden Date: Mon, 8 Oct 2012 16:28:19 +0200 Subject: [PATCH] allow versioned profiled parsers to fallback to the non-profiled parser, this will help deal with situations in which a WMSC parser is created, the server is requested for WMS 1.1.1, but returns 1.1.0 --- lib/OpenLayers/Format/XML/VersionedOGC.js | 24 +++- tests/Format/WMSCapabilities/v1_1_1_WMSC.html | 136 ++++++++++++++++++ 2 files changed, 158 insertions(+), 2 deletions(-) diff --git a/lib/OpenLayers/Format/XML/VersionedOGC.js b/lib/OpenLayers/Format/XML/VersionedOGC.js index 7b9ef90dfb..ee630cbdff 100644 --- a/lib/OpenLayers/Format/XML/VersionedOGC.js +++ b/lib/OpenLayers/Format/XML/VersionedOGC.js @@ -36,6 +36,17 @@ OpenLayers.Format.XML.VersionedOGC = OpenLayers.Class(OpenLayers.Format.XML, { */ profile: null, + /** + * APIProperty: allowFallback + * {Boolean} If a profiled parser cannot be found for the returned version, + * use a non-profiled parser as the fallback. Application code using this + * should take into account that the return object structure might be + * missing the specifics of the profile. Defaults to false Application code using this + * should take into account that the return object structure might be + * missing the specifics of the profile. Defaults to false. + */ + allowFallback: false, + /** * APIProperty: errorProperty * {String} Which property of the returned object to check for in order to @@ -128,8 +139,17 @@ OpenLayers.Format.XML.VersionedOGC = OpenLayers.Class(OpenLayers.Format.XML, { "v" + version.replace(/\./g, "_") + profile ]; if(!format) { - throw "Can't find a " + this.name + " parser for version " + - version + profile; + if (profile !== "" && this.allowFallback) { + // fallback to the non-profiled version of the parser + profile = ""; + format = OpenLayers.Format[this.name][ + "v" + version.replace(/\./g, "_") + ]; + } + if (!format) { + throw "Can't find a " + this.name + " parser for version " + + version + profile; + } } this.parser = new format(this.options); } diff --git a/tests/Format/WMSCapabilities/v1_1_1_WMSC.html b/tests/Format/WMSCapabilities/v1_1_1_WMSC.html index 10bcfb1923..044773dc45 100644 --- a/tests/Format/WMSCapabilities/v1_1_1_WMSC.html +++ b/tests/Format/WMSCapabilities/v1_1_1_WMSC.html @@ -24,10 +24,146 @@ t.eq(tileset.styles, "", "Styles correctly parsed"); } + function test_read_fallback(t) { + t.plan(1); + var xml = document.getElementById("fallback").firstChild.nodeValue; + var doc = new OpenLayers.Format.XML().read(xml); + var format = new OpenLayers.Format.WMSCapabilities({profile: "WMSC", allowFallback: true}); + var obj = format.read(doc); + t.eq(obj.capability.layers.length, 2, "2 layers parsed with allowFallback true"); + } + +
+