From ed66271be3f12cfb82929beae474304d2d08a2f4 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Wed, 24 Oct 2012 11:16:29 -0600 Subject: [PATCH 1/4] Use ISO 8601 formatting for dates in filters --- lib/OpenLayers/Format/Filter/v1.js | 5 +++- tests/Format/Filter/v1.html | 37 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/OpenLayers/Format/Filter/v1.js b/lib/OpenLayers/Format/Filter/v1.js index bd7cc32f09..bfc563c11a 100644 --- a/lib/OpenLayers/Format/Filter/v1.js +++ b/lib/OpenLayers/Format/Filter/v1.js @@ -6,6 +6,7 @@ * @requires OpenLayers/Format/Filter.js * @requires OpenLayers/Format/XML.js * @requires OpenLayers/Filter/Function.js + * @requires OpenLayers/BaseTypes/Date.js */ /** @@ -405,7 +406,9 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, { }); }, "Literal": function(value) { - // no ogc:expression handling for now + if (value instanceof Date) { + value = OpenLayers.Date.toISOString(value); + } return this.createElementNSPlus("ogc:Literal", { value: value }); diff --git a/tests/Format/Filter/v1.html b/tests/Format/Filter/v1.html index dc4eec2fc0..110895d81c 100644 --- a/tests/Format/Filter/v1.html +++ b/tests/Format/Filter/v1.html @@ -247,6 +247,30 @@ } + function test_date_writing(t) { + t.plan(1); + + // ISO 8601: 2010-11-27T18:19:15.123Z + var start = new Date(Date.UTC(2010, 10, 27, 18, 19, 15, 123)); + + // ISO 8601: 2011-12-27T18:19:15.123Z + var end = new Date(Date.UTC(2011, 11, 27, 18, 19, 15, 123)); + + var filter = new OpenLayers.Filter.Comparison({ + type: OpenLayers.Filter.Comparison.BETWEEN, + property: "when", + lowerBoundary: start, + upperBoundary: end + }); + + var format = new OpenLayers.Format.Filter.v1_0_0(); + + var got = format.write(filter); + var exp = readXML("BetweenDates"); + t.xml_eq(got, exp, "comparison filter with dates"); + } + + function readXML(id) { var xml = document.getElementById(id).firstChild.nodeValue; return new OpenLayers.Format.XML().read(xml).documentElement; @@ -288,6 +312,19 @@ --> +
From 993add338d0cafb394a1faf1c9444408ce67232b Mon Sep 17 00:00:00 2001 From: tschaub Date: Thu, 25 Oct 2012 10:52:54 -0600 Subject: [PATCH 2/4] Allow user to customize literal encoding As suggested by @mpriour, it would be nice if the user could determine how date values are encoded in literal elements. Extending this beyond date values, we can provide an `encodeLiteral` method that can be overridden to provide application specific behavior. --- lib/OpenLayers/Format/Filter/v1.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/OpenLayers/Format/Filter/v1.js b/lib/OpenLayers/Format/Filter/v1.js index bfc563c11a..74c5e7a381 100644 --- a/lib/OpenLayers/Format/Filter/v1.js +++ b/lib/OpenLayers/Format/Filter/v1.js @@ -247,6 +247,25 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, { obj.filters.push(filter); }, + /** + * APIMethod: encodeLiteral + * Generates the string representation of a value for use in + * elements. The default encoder writes Date values as ISO 8601 + * strings. + * + * Parameters: + * value - {Object} Literal value to encode + * + * Returns: + * {String} String representation of the provided value. + */ + encodeLiteral: function(value) { + if (value instanceof Date) { + value = OpenLayers.Date.toISOString(value); + } + return value; + }, + /** * Method: writeOgcExpression * Limited support for writing OGC expressions. Currently it supports @@ -406,11 +425,8 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, { }); }, "Literal": function(value) { - if (value instanceof Date) { - value = OpenLayers.Date.toISOString(value); - } return this.createElementNSPlus("ogc:Literal", { - value: value + value: this.encodeLiteral(value) }); }, "LowerBoundary": function(filter) { From 81d3584e0916100ac34355e8de23a2a3c9afdcce Mon Sep 17 00:00:00 2001 From: tschaub Date: Thu, 25 Oct 2012 11:24:55 -0600 Subject: [PATCH 3/4] Test custom encodeLiteral function --- tests/Format/Filter/v1.html | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/Format/Filter/v1.html b/tests/Format/Filter/v1.html index 110895d81c..60e7002e6e 100644 --- a/tests/Format/Filter/v1.html +++ b/tests/Format/Filter/v1.html @@ -271,6 +271,35 @@ } + function test_custom_date_writing(t) { + t.plan(1); + + // ISO 8601: 2010-11-27T18:19:15.123Z + var start = new Date(Date.UTC(2010, 10, 27, 18, 19, 15, 123)); + + // ISO 8601: 2011-12-27T18:19:15.123Z + var end = new Date(Date.UTC(2011, 11, 27, 18, 19, 15, 123)); + + var filter = new OpenLayers.Filter.Comparison({ + type: OpenLayers.Filter.Comparison.BETWEEN, + property: "when", + lowerBoundary: start, + upperBoundary: end + }); + + var format = new OpenLayers.Format.Filter({ + encodeLiteral: function(value) { + // return just the date and not the time portion + return OpenLayers.Date.toISOString(value).split("T").shift(); + } + }); + + var got = format.write(filter); + var exp = readXML("CustomBetweenDates"); + t.xml_eq(got, exp, "comparison filter with dates"); + } + + function readXML(id) { var xml = document.getElementById(id).firstChild.nodeValue; return new OpenLayers.Format.XML().read(xml).documentElement; @@ -325,6 +354,19 @@ --> +
From 4cf7f99de0dbb3003e0afc20b0472e4fc8b9ba28 Mon Sep 17 00:00:00 2001 From: tschaub Date: Thu, 25 Oct 2012 12:31:20 -0600 Subject: [PATCH 4/4] Work with formats that copy filter writers --- lib/OpenLayers/Format/Filter/v1.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/OpenLayers/Format/Filter/v1.js b/lib/OpenLayers/Format/Filter/v1.js index 74c5e7a381..3bb4214094 100644 --- a/lib/OpenLayers/Format/Filter/v1.js +++ b/lib/OpenLayers/Format/Filter/v1.js @@ -425,8 +425,10 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, { }); }, "Literal": function(value) { + var encode = this.encodeLiteral || + OpenLayers.Format.Filter.v1.prototype.encodeLiteral; return this.createElementNSPlus("ogc:Literal", { - value: this.encodeLiteral(value) + value: encode(value) }); }, "LowerBoundary": function(filter) {