Filter enhancements. Adding support for serializing filters with Filter Encoding 1.1. Adding matchCase attribute to filters with binary operators. Refactoring filter formats to use methods from base XML format. r=ahocevar,adube (closes #1790)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@8812 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2009-02-03 22:28:48 +00:00
parent ebed733327
commit f9939f1cfe
10 changed files with 607 additions and 281 deletions

View File

@@ -21,6 +21,7 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
*/
namespaces: {
ogc: "http://www.opengis.net/ogc",
gml: "http://www.opengis.net/gml",
xlink: "http://www.w3.org/1999/xlink",
xsi: "http://www.w3.org/2001/XMLSchema-instance"
},
@@ -119,20 +120,6 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
this.readChildNodes(node, filter);
obj.filters.push(filter);
},
"PropertyIsEqualTo": function(node, obj) {
var filter = new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO
});
this.readChildNodes(node, filter);
obj.filters.push(filter);
},
"PropertyIsNotEqualTo": function(node, obj) {
var filter = new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.NOT_EQUAL_TO
});
this.readChildNodes(node, filter);
obj.filters.push(filter);
},
"PropertyIsLessThan": function(node, obj) {
var filter = new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.LESS_THAN
@@ -241,10 +228,10 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
var sub = filter.CLASS_NAME.split(".").pop();
if(sub == "FeatureId") {
for(var i=0; i<filter.fids.length; ++i) {
this.writeNode(node, "FeatureId", filter.fids[i]);
this.writeNode("FeatureId", filter.fids[i], node);
}
} else {
this.writeNode(node, this.getFilterType(filter), filter);
this.writeNode(this.getFilterType(filter), filter, node);
}
return node;
},
@@ -259,7 +246,7 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
for(var i=0; i<filter.filters.length; ++i) {
childFilter = filter.filters[i];
this.writeNode(
node, this.getFilterType(childFilter), childFilter
this.getFilterType(childFilter), childFilter, node
);
}
return node;
@@ -270,7 +257,7 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
for(var i=0; i<filter.filters.length; ++i) {
childFilter = filter.filters[i];
this.writeNode(
node, this.getFilterType(childFilter), childFilter
this.getFilterType(childFilter), childFilter, node
);
}
return node;
@@ -279,58 +266,44 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
var node = this.createElementNSPlus("ogc:Not");
var childFilter = filter.filters[0];
this.writeNode(
node, this.getFilterType(childFilter), childFilter
this.getFilterType(childFilter), childFilter, node
);
return node;
},
"PropertyIsEqualTo": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsEqualTo");
// no ogc:expression handling for now
this.writeNode(node, "PropertyName", filter);
this.writeNode(node, "Literal", filter.value);
return node;
},
"PropertyIsNotEqualTo": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsNotEqualTo");
// no ogc:expression handling for now
this.writeNode(node, "PropertyName", filter);
this.writeNode(node, "Literal", filter.value);
return node;
},
"PropertyIsLessThan": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsLessThan");
// no ogc:expression handling for now
this.writeNode(node, "PropertyName", filter);
this.writeNode(node, "Literal", filter.value);
this.writeNode("PropertyName", filter, node);
this.writeNode("Literal", filter.value, node);
return node;
},
"PropertyIsGreaterThan": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsGreaterThan");
// no ogc:expression handling for now
this.writeNode(node, "PropertyName", filter);
this.writeNode(node, "Literal", filter.value);
this.writeNode("PropertyName", filter, node);
this.writeNode("Literal", filter.value, node);
return node;
},
"PropertyIsLessThanOrEqualTo": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsLessThanOrEqualTo");
// no ogc:expression handling for now
this.writeNode(node, "PropertyName", filter);
this.writeNode(node, "Literal", filter.value);
this.writeNode("PropertyName", filter, node);
this.writeNode("Literal", filter.value, node);
return node;
},
"PropertyIsGreaterThanOrEqualTo": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsGreaterThanOrEqualTo");
// no ogc:expression handling for now
this.writeNode(node, "PropertyName", filter);
this.writeNode(node, "Literal", filter.value);
this.writeNode("PropertyName", filter, node);
this.writeNode("Literal", filter.value, node);
return node;
},
"PropertyIsBetween": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsBetween");
// no ogc:expression handling for now
this.writeNode(node, "PropertyName", filter);
this.writeNode(node, "LowerBoundary", filter);
this.writeNode(node, "UpperBoundary", filter);
this.writeNode("PropertyName", filter, node);
this.writeNode("LowerBoundary", filter, node);
this.writeNode("UpperBoundary", filter, node);
return node;
},
"PropertyIsLike": function(filter) {
@@ -340,9 +313,9 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
}
});
// no ogc:expression handling for now
this.writeNode(node, "PropertyName", filter);
this.writeNode("PropertyName", filter, node);
// convert regex string to ogc string
this.writeNode(node, "Literal", filter.regex2value());
this.writeNode("Literal", filter.regex2value(), node);
return node;
},
"PropertyName": function(filter) {
@@ -360,35 +333,28 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
"LowerBoundary": function(filter) {
// no ogc:expression handling for now
var node = this.createElementNSPlus("ogc:LowerBoundary");
this.writeNode(node, "Literal", filter.lowerBoundary);
this.writeNode("Literal", filter.lowerBoundary, node);
return node;
},
"UpperBoundary": function(filter) {
// no ogc:expression handling for now
var node = this.createElementNSPlus("ogc:UpperBoundary");
this.writeNode(node, "Literal", filter.upperBoundary);
return node;
},
"BBOX": function(filter) {
var node = this.createElementNSPlus("ogc:BBOX");
this.writeNode(node, "PropertyName", filter);
var gml = new OpenLayers.Format.GML();
node.appendChild(gml.buildGeometryNode(filter.value));
this.writeNode("Literal", filter.upperBoundary, node);
return node;
},
"DWITHIN": function(filter) {
var node = this.createElementNSPlus("ogc:DWithin");
this.writeNode(node, "PropertyName", filter);
var gml = new OpenLayers.Format.GML();
node.appendChild(gml.buildGeometryNode(filter.value));
this.writeNode(node, "Distance", filter);
this.writeNode("PropertyName", filter, node);
var child = this.writeNode("feature:_geometry", filter.value);
node.appendChild(child.firstChild);
this.writeNode("Distance", filter, node);
return node;
},
"INTERSECTS": function(filter) {
var node = this.createElementNSPlus("ogc:Intersects");
this.writeNode(node, "PropertyName", filter);
var gml = new OpenLayers.Format.GML();
node.appendChild(gml.buildGeometryNode(filter.value));
this.writeNode("PropertyName", filter, node);
var child = this.writeNode("feature:_geometry", filter.value);
node.appendChild(child.firstChild);
return node;
},
"Distance": function(filter) {
@@ -431,154 +397,6 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
"DWITHIN": "DWITHIN",
"INTERSECTS": "INTERSECTS"
},
/**
* Methods below this point are of general use for versioned XML parsers.
* These are candidates for an abstract class.
*/
/**
* Method: getNamespacePrefix
* Get the namespace prefix for a given uri from the <namespaces> object.
*
* Returns:
* {String} A namespace prefix or null if none found.
*/
getNamespacePrefix: function(uri) {
var prefix = null;
if(uri == null) {
prefix = this.namespaces[this.defaultPrefix];
} else {
var gotPrefix = false;
for(prefix in this.namespaces) {
if(this.namespaces[prefix] == uri) {
gotPrefix = true;
break;
}
}
if(!gotPrefix) {
prefix = null;
}
}
return prefix;
},
/**
* Method: readChildNodes
*/
readChildNodes: function(node, obj) {
var children = node.childNodes;
var child, group, reader, prefix, local;
for(var i=0; i<children.length; ++i) {
child = children[i];
if(child.nodeType == 1) {
prefix = this.getNamespacePrefix(child.namespaceURI);
local = child.nodeName.split(":").pop();
group = this.readers[prefix];
if(group) {
reader = group[local];
if(reader) {
reader.apply(this, [child, obj]);
}
}
}
}
},
/**
* Method: writeNode
* Shorthand for applying one of the named writers and appending the
* results to a node. If a qualified name is not provided for the
* second argument (and a local name is used instead), the namespace
* of the parent node will be assumed.
*
* Parameters:
* parent - {DOMElement} Result will be appended to this node.
* name - {String} The name of a node to generate. If a qualified name
* (e.g. "pre:Name") is used, the namespace prefix is assumed to be
* in the <writers> group. If a local name is used (e.g. "Name") then
* the namespace of the parent is assumed.
* obj - {Object} Structure containing data for the writer.
*
* Returns:
* {DOMElement} The child node.
*/
writeNode: function(parent, name, obj) {
var prefix, local;
var split = name.indexOf(":");
if(split > 0) {
prefix = name.substring(0, split);
local = name.substring(split + 1);
} else {
prefix = this.getNamespacePrefix(parent.namespaceURI);
local = name;
}
var child = this.writers[prefix][local].apply(this, [obj]);
parent.appendChild(child);
return child;
},
/**
* Method: createElementNSPlus
* Shorthand for creating namespaced elements with optional attributes and
* child text nodes.
*
* Parameters:
* name - {String} The qualified node name.
* options - {Object} Optional object for node configuration.
*
* Returns:
* {Element} An element node.
*/
createElementNSPlus: function(name, options) {
options = options || {};
var loc = name.indexOf(":");
// order of prefix preference
// 1. in the uri option
// 2. in the prefix option
// 3. in the qualified name
// 4. from the defaultPrefix
var uri = options.uri || this.namespaces[options.prefix];
if(!uri) {
loc = name.indexOf(":");
uri = this.namespaces[name.substring(0, loc)];
}
if(!uri) {
uri = this.namespaces[this.defaultPrefix];
}
var node = this.createElementNS(uri, name);
if(options.attributes) {
this.setAttributes(node, options.attributes);
}
if(options.value) {
node.appendChild(this.createTextNode(options.value));
}
return node;
},
/**
* Method: setAttributes
* Set multiple attributes given key value pairs from an object.
*
* Parameters:
* node - {Element} An element node.
* obj - {Object || Array} An object whose properties represent attribute
* names and values represent attribute values. If an attribute name
* is a qualified name ("prefix:local"), the prefix will be looked up
* in the parsers {namespaces} object. If the prefix is found,
* setAttributeNS will be used instead of setAttribute.
*/
setAttributes: function(node, obj) {
var value, loc, alias, uri;
for(var name in obj) {
value = obj[name].toString();
// check for qualified attribute name ("prefix:local")
uri = this.namespaces[name.substring(0, name.indexOf(":"))] || null;
this.setAttributeNS(node, uri, name, value);
}
},
CLASS_NAME: "OpenLayers.Format.Filter.v1"

View File

@@ -3,6 +3,7 @@
* full text of the license. */
/**
* @requires OpenLayers/Format/GML/v2.js
* @requires OpenLayers/Format/Filter/v1.js
*/
@@ -43,6 +44,72 @@ OpenLayers.Format.Filter.v1_0_0 = OpenLayers.Class(
);
},
/**
* Property: readers
* Contains public functions, grouped by namespace prefix, that will
* be applied when a namespaced node is found matching the function
* name. The function will be applied in the scope of this parser
* with two arguments: the node being read and a context object passed
* from the parent.
*/
readers: {
"ogc": OpenLayers.Util.applyDefaults({
"PropertyIsEqualTo": function(node, obj) {
var filter = new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO
});
this.readChildNodes(node, filter);
obj.filters.push(filter);
},
"PropertyIsNotEqualTo": function(node, obj) {
var filter = new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.NOT_EQUAL_TO
});
this.readChildNodes(node, filter);
obj.filters.push(filter);
}
}, OpenLayers.Format.Filter.v1.prototype.readers["ogc"]),
"gml": OpenLayers.Format.GML.v2.prototype.readers["gml"],
"feature": OpenLayers.Format.GML.v2.prototype.readers["feature"]
},
/**
* Property: writers
* As a compliment to the readers property, this structure contains public
* writing functions grouped by namespace alias and named like the
* node names they produce.
*/
writers: {
"ogc": OpenLayers.Util.applyDefaults({
"PropertyIsEqualTo": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsEqualTo");
// no ogc:expression handling for now
this.writeNode("PropertyName", filter, node);
this.writeNode("Literal", filter.value, node);
return node;
},
"PropertyIsNotEqualTo": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsNotEqualTo");
// no ogc:expression handling for now
this.writeNode("PropertyName", filter, node);
this.writeNode("Literal", filter.value, node);
return node;
},
"BBOX": function(filter) {
var node = this.createElementNSPlus("ogc:BBOX");
this.writeNode("PropertyName", filter, node);
var box = this.writeNode("gml:Box", filter.value, node);
if(filter.projection) {
box.setAttribute("srsName", filter.projection);
}
return node;
}}, OpenLayers.Format.Filter.v1.prototype.writers["ogc"]),
"gml": OpenLayers.Format.GML.v2.prototype.writers["gml"],
"feature": OpenLayers.Format.GML.v2.prototype.writers["feature"]
},
CLASS_NAME: "OpenLayers.Format.Filter.v1_0_0"
});

View File

@@ -0,0 +1,130 @@
/* Copyright (c) 2006-2008 MetaCarta, Inc., 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/Filter/v1.js
* @requires OpenLayers/Format/GML/v3.js
*/
/**
* Class: OpenLayers.Format.Filter.v1_1_0
* Write ogc:Filter version 1.1.0.
*
* Differences from the v1.0.0 parser:
* - uses GML v3 instead of GML v2
* - reads matchCase attribute on ogc:PropertyIsEqual and
* ogc:PropertyIsNotEqualelements.
* - writes matchCase attribute from comparison filters of type EQUAL_TO and
* type NOT_EQUAL_TO.
*
* Inherits from:
* - <OpenLayers.Format.Filter.v1>
*/
OpenLayers.Format.Filter.v1_1_0 = OpenLayers.Class(
OpenLayers.Format.Filter.v1, {
/**
* Constant: VERSION
* {String} 1.1.0
*/
VERSION: "1.1.0",
/**
* Property: schemaLocation
* {String} http://www.opengis.net/ogc/filter/1.1.0/filter.xsd
*/
schemaLocation: "http://www.opengis.net/ogc/filter/1.1.0/filter.xsd",
/**
* Constructor: OpenLayers.Format.Filter.v1_1_0
* Instances of this class are not created directly. Use the
* <OpenLayers.Format.Filter> constructor instead.
*
* Parameters:
* options - {Object} An optional object whose properties will be set on
* this instance.
*/
initialize: function(options) {
OpenLayers.Format.Filter.v1.prototype.initialize.apply(
this, [options]
);
},
/**
* Property: readers
* Contains public functions, grouped by namespace prefix, that will
* be applied when a namespaced node is found matching the function
* name. The function will be applied in the scope of this parser
* with two arguments: the node being read and a context object passed
* from the parent.
*/
readers: {
"ogc": OpenLayers.Util.applyDefaults({
"PropertyIsEqualTo": function(node, obj) {
var matchCase = node.getAttribute("matchCase");
var filter = new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,
matchCase: !(matchCase === "false" || matchCase === "0")
});
this.readChildNodes(node, filter);
obj.filters.push(filter);
},
"PropertyIsNotEqualTo": function(node, obj) {
var matchCase = node.getAttribute("matchCase");
var filter = new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
matchCase: !(matchCase === "false" || matchCase === "0")
});
this.readChildNodes(node, filter);
obj.filters.push(filter);
}
}, OpenLayers.Format.Filter.v1.prototype.readers["ogc"]),
"gml": OpenLayers.Format.GML.v3.prototype.readers["gml"],
"feature": OpenLayers.Format.GML.v3.prototype.readers["feature"]
},
/**
* Property: writers
* As a compliment to the readers property, this structure contains public
* writing functions grouped by namespace alias and named like the
* node names they produce.
*/
writers: {
"ogc": OpenLayers.Util.applyDefaults({
"PropertyIsEqualTo": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsEqualTo", {
attributes: {matchCase: filter.matchCase}
});
// no ogc:expression handling for now
this.writeNode("PropertyName", filter, node);
this.writeNode("Literal", filter.value, node);
return node;
},
"PropertyIsNotEqualTo": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsNotEqualTo", {
attributes: {matchCase: filter.matchCase}
});
// no ogc:expression handling for now
this.writeNode("PropertyName", filter, node);
this.writeNode("Literal", filter.value, node);
return node;
},
"BBOX": function(filter) {
var node = this.createElementNSPlus("ogc:BBOX");
this.writeNode("PropertyName", filter, node);
var box = this.writeNode("gml:Envelope", filter.value);
if(filter.projection) {
box.setAttribute("srsName", filter.projection);
}
node.appendChild(box);
return node;
}}, OpenLayers.Format.Filter.v1.prototype.writers["ogc"]),
"gml": OpenLayers.Format.GML.v3.prototype.writers["gml"],
"feature": OpenLayers.Format.GML.v3.prototype.writers["feature"]
},
CLASS_NAME: "OpenLayers.Format.Filter.v1_1_0"
});