allow WFS protocol to use a different outputFormat, for instance json, patch by rdewit, r=me (closes #2107)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@9725 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
bartvde
2009-10-07 18:21:59 +00:00
parent 7919d2a314
commit 5a9e1fe0b4
4 changed files with 63 additions and 35 deletions

View File

@@ -181,6 +181,7 @@ OpenLayers.Format.WFST.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
attributes: {
service: "WFS",
version: this.version,
outputFormat: options && options.outputFormat,
maxFeatures: options && options.maxFeatures,
"xsi:schemaLocation": this.schemaLocationAttr(options)
}

View File

@@ -64,6 +64,15 @@ OpenLayers.Protocol.WFS.v1 = OpenLayers.Class(OpenLayers.Protocol, {
* this property can be used to extend the default format options.
*/
formatOptions: null,
/**
* Property: readFormat
* {<OpenLayers.Format>} For WFS requests it is possible to get a
* different output format than GML. In that case, we cannot parse
* the response with the default format (WFST) and we need a different
* format for reading.
*/
readFormat: null,
/**
* Constructor: OpenLayers.Protocol.WFS
@@ -105,7 +114,7 @@ OpenLayers.Protocol.WFS.v1 = OpenLayers.Class(OpenLayers.Protocol, {
this.setNamespace("feature", this.featureNS);
}
return readNode.apply(this, arguments);
}
};
}
},
@@ -188,7 +197,7 @@ OpenLayers.Protocol.WFS.v1 = OpenLayers.Class(OpenLayers.Protocol, {
response.code = OpenLayers.Protocol.Response.FAILURE;
}
options.callback.call(options.scope, response);
};
}
},
/**
@@ -210,7 +219,8 @@ OpenLayers.Protocol.WFS.v1 = OpenLayers.Class(OpenLayers.Protocol, {
if(!doc || doc.length <= 0) {
return null;
}
return this.format.read(doc);
return (this.readFormat !== null) ? this.readFormat.read(doc) :
this.format.read(doc);
},
/**

View File

@@ -12,24 +12,24 @@
featureType: "states"
});
var features = format.read(data);
t.eq(features.length, 1, "number of features read from FeatureCollection is correct");
}
function test_write(t) {
var format = new OpenLayers.Format.WFST({
featureNS: "http://www.openplans.org/topp",
featureType: "states",
featurePrefix: "topp",
geometryName: "the_geom"
});
var feature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(1,2),
{foo: "bar"}
);
var insertFeature = feature.clone();
// null value does not show up in insert
insertFeature.attributes.nul = null;
@@ -44,28 +44,28 @@
var deleteFeature = feature.clone();
deleteFeature.state = OpenLayers.State.DELETE;
deleteFeature.fid = "fid.37";
t.plan(5);
var snippets = {
"GetFeature": {maxFeatures: 1},
"GetFeature": {maxFeatures: 1, outputFormat: 'json'},
"Transaction": null,
"Insert": insertFeature,
"Update": updateFeature,
"Delete": deleteFeature
}
var arg;
for(var snippet in snippets) {
arg = snippets[snippet]
var expected = readXML(snippet);
var got = format.writers["wfs"][snippet].apply(format, [arg]);
t.xml_eq(got, expected, snippet + " request created correctly");
}
}
}
function readXML(id) {
var xml = document.getElementById(id).firstChild.nodeValue;
return new OpenLayers.Format.XML().read(xml).documentElement;
return new OpenLayers.Format.XML().read(xml).documentElement;
}
</script>
@@ -118,7 +118,7 @@
--></div>
<div id="GetFeature"><!--
<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0" maxFeatures="1" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0" outputFormat="json" maxFeatures="1" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<wfs:Query typeName="topp:states" xmlns:topp="http://www.openplans.org/topp"/>
</wfs:GetFeature>
--></div>

View File

@@ -23,7 +23,7 @@
t.ok(protocol instanceof OpenLayers.Protocol.WFS.v1_1_0,
"initialize returns instance of custom versioned protocol")
}
function test_read(t) {
t.plan(6);
@@ -38,7 +38,7 @@
});
var _POST = OpenLayers.Request.POST;
var expected, status;
OpenLayers.Request.POST = function(obj) {
t.xml_eq(new OpenLayers.Format.XML().read(obj.data).documentElement, expected, "GetFeature request is correct");
@@ -48,14 +48,14 @@
t.delay_call(0.1, function() {obj.callback.call(this)});
return obj;
};
expected = readXML("GetFeature_1");
status = 200;
var response = protocol.read({callback: function(response) {
t.eq(response.features, "foo", "user callback properly called with features");
t.eq(response.code, OpenLayers.Protocol.Response.SUCCESS, "success reported properly");
}});
options = {
maxFeatures: 10,
featureType: 'type2',
@@ -71,7 +71,7 @@
OpenLayers.Request.POST = _POST;
}
function test_commit(t){
t.plan(4);
@@ -90,8 +90,8 @@
};
var _POST = OpenLayers.Request.POST;
var expected;
var expected;
OpenLayers.Request.POST = function(obj) {
t.xml_eq(new OpenLayers.Format.XML().read(obj.data).documentElement, expected, "Transaction XML with Insert, Update and Delete created correctly");
obj.responseText = "foo";
@@ -99,7 +99,7 @@
t.delay_call(0.1, function() {obj.callback.call(this)});
return obj;
};
var featureDelete = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(42, 7), {has : "cheeseburger"});
featureDelete.fid = "fid.37";
featureDelete.state = OpenLayers.State.DELETE;
@@ -124,7 +124,7 @@
t.eq(response.code, OpenLayers.Protocol.Response.SUCCESS, "success properly reported to user callback");
}
}
expected = readXML("commit");
var response = protocol.commit([featureInsert, featureModify, featureDelete], options);
@@ -141,7 +141,7 @@
featureNS: "http://namespace.org",
featureType: "type"
});
var filter = new OpenLayers.Filter.Spatial({
type: OpenLayers.Filter.Spatial.BBOX,
value: new OpenLayers.Bounds(-5, -5, 5, 5)
@@ -155,16 +155,16 @@
t.delay_call(0.1, function() {obj.callback.call(this)});
return obj;
};
var response = protocol.filterDelete(filter, {
callback: function() {
t.ok("user callback function called");
}
});
OpenLayers.Request.POST = _POST;
}
function test_abort(t) {
t.plan(1);
var protocol = new OpenLayers.Protocol.WFS({
@@ -172,7 +172,7 @@
featureNS: "http://example.com#namespace",
featureType: "type"
});
var response = {
priv: {
abort: function() {
@@ -180,14 +180,14 @@
}
}
};
// call abort with mocked response
var aborted = false;
protocol.abort(response);
t.eq(aborted, true, "abort called on response.priv");
}
function test_fromWMSLayer(t) {
t.plan(8);
var map = new OpenLayers.Map("map", {
@@ -203,18 +203,35 @@
t.eq(protocol.featureType, "states", "typeName correctly extracted");
t.eq(protocol.srsName, "EPSG:1234", "srsName set correctly");
t.eq(protocol.version, "1.1.0", "version set correctly");
layer.params["LAYERS"] = ["topp:street_centerline", "topp:states"];
layer.projection = new OpenLayers.Projection("EPSG:900913");
protocol = OpenLayers.Protocol.WFS.fromWMSLayer(layer);
t.eq(protocol.featurePrefix, "topp", "featurePrefix from layer param array");
t.eq(protocol.featureType, "street_centerline", "first layer from layer param array as featureType");
t.eq(protocol.srsName, "EPSG:900913", "projection from layer preferred");
t.eq(protocol.featureType, "street_centerline", "first layer from layer param array as featureType");
t.eq(protocol.srsName, "EPSG:900913", "projection from layer preferred");
}
function test_readFormat(t) {
t.plan(1);
var protocol = new OpenLayers.Protocol.WFS({
url: "http://some.url.org",
featureNS: "http://namespace.org",
featureType: "type",
formatOptions: {outputFormat: 'json'},
readFormat: new OpenLayers.Format.GeoJSON()
});
var request = {};
request.responseText = '{"type":"FeatureCollection","features":[{"type":"Feature","id":"V_HECTOPUNTEN.108411","geometry":{"type":"MultiPoint","coordinates":[[190659.467,349576.19]]},"geometry_name":"ORA_GEOMETRY","properties":{"WEGNUMMER":"002","HECTOMTRNG_ORG":2200,"HECTOMTRNG":"220.00","bbox":[190659.467,349576.19,190659.467,349576.19]}}]}';
var features = protocol.parseFeatures(request);
t.eq(features.length, 1, "the right format is used to read the request (GeoJSON)");
}
function readXML(id) {
var xml = document.getElementById(id).firstChild.nodeValue;
return new OpenLayers.Format.XML().read(xml).documentElement;
return new OpenLayers.Format.XML().read(xml).documentElement;
}
</script>