diff --git a/externs/olx.js b/externs/olx.js index 527850e10b..9c4bccc627 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -2285,7 +2285,8 @@ olx.format.WFSWriteGetFeatureOptions.prototype.resultType; * srsName: (string|undefined), * handle: (string|undefined), * nativeElements: Array., - * gmlOptions: (olx.format.GMLOptions|undefined)}} + * gmlOptions: (olx.format.GMLOptions|undefined), + * version: (string|undefined)}} */ olx.format.WFSWriteTransactionOptions; @@ -2347,6 +2348,15 @@ olx.format.WFSWriteTransactionOptions.prototype.nativeElements; olx.format.WFSWriteTransactionOptions.prototype.gmlOptions; +/** + * WFS version to use for the transaction. Can be either `1.0.0` or `1.1.0`. + * Default is `1.1.0`. + * @type {string|undefined} + * @api + */ +olx.format.WFSWriteTransactionOptions.prototype.version; + + /** * @typedef {{splitCollection: (boolean|undefined)}} */ diff --git a/src/ol/format/wfs.js b/src/ol/format/wfs.js index 814d3ee5c9..dcba956592 100644 --- a/src/ol/format/wfs.js +++ b/src/ol/format/wfs.js @@ -2,6 +2,7 @@ goog.provide('ol.format.WFS'); goog.require('ol'); goog.require('ol.asserts'); +goog.require('ol.format.GML2'); goog.require('ol.format.GML3'); goog.require('ol.format.GMLBase'); goog.require('ol.format.filter'); @@ -53,7 +54,8 @@ ol.format.WFS = function(opt_options) { * @type {string} */ this.schemaLocation_ = options.schemaLocation ? - options.schemaLocation : ol.format.WFS.SCHEMA_LOCATION; + options.schemaLocation : + ol.format.WFS.SCHEMA_LOCATIONS[ol.format.WFS.DEFAULT_VERSION]; ol.format.XMLFeature.call(this); }; @@ -88,12 +90,23 @@ ol.format.WFS.OGCNS = 'http://www.opengis.net/ogc'; ol.format.WFS.WFSNS = 'http://www.opengis.net/wfs'; +/** + * @const + * @type {Object.} + */ +ol.format.WFS.SCHEMA_LOCATIONS = { + '1.1.0': 'http://www.opengis.net/wfs ' + + 'http://schemas.opengis.net/wfs/1.1.0/wfs.xsd', + '1.0.0': 'http://www.opengis.net/wfs ' + + 'http://schemas.opengis.net/wfs/1.0.0/wfs.xsd' +}; + + /** * @const * @type {string} */ -ol.format.WFS.SCHEMA_LOCATION = 'http://www.opengis.net/wfs ' + - 'http://schemas.opengis.net/wfs/1.1.0/wfs.xsd'; +ol.format.WFS.DEFAULT_VERSION = '1.1.0'; /** @@ -360,9 +373,14 @@ ol.format.WFS.writeFeature_ = function(node, feature, objectStack) { var context = objectStack[objectStack.length - 1]; var featureType = context['featureType']; var featureNS = context['featureNS']; + var gmlVersion = context['gmlVersion']; var child = ol.xml.createElementNS(featureNS, featureType); node.appendChild(child); - ol.format.GML3.prototype.writeFeatureElement(child, feature, objectStack); + if (gmlVersion === 2) { + ol.format.GML2.prototype.writeFeatureElement(child, feature, objectStack); + } else { + ol.format.GML3.prototype.writeFeatureElement(child, feature, objectStack); + } }; @@ -433,7 +451,8 @@ ol.format.WFS.writeUpdate_ = function(node, feature, objectStack) { } } ol.xml.pushSerializeAndPop(/** @type {ol.XmlNodeStackItem} */ ( - {node: node, 'srsName': context['srsName']}), + {'gmlVersion': context['gmlVersion'], node: node, + 'srsName': context['srsName']}), ol.format.WFS.TRANSACTION_SERIALIZERS_, ol.xml.makeSimpleNodeFactory('Property'), values, objectStack); @@ -450,14 +469,21 @@ ol.format.WFS.writeUpdate_ = function(node, feature, objectStack) { */ ol.format.WFS.writeProperty_ = function(node, pair, objectStack) { var name = ol.xml.createElementNS(ol.format.WFS.WFSNS, 'Name'); + var context = objectStack[objectStack.length - 1]; + var gmlVersion = context['gmlVersion']; node.appendChild(name); ol.format.XSD.writeStringTextNode(name, pair.name); if (pair.value !== undefined && pair.value !== null) { var value = ol.xml.createElementNS(ol.format.WFS.WFSNS, 'Value'); node.appendChild(value); if (pair.value instanceof ol.geom.Geometry) { - ol.format.GML3.prototype.writeGeometryElement(value, - pair.value, objectStack); + if (gmlVersion === 2) { + ol.format.GML2.prototype.writeGeometryElement(value, + pair.value, objectStack); + } else { + ol.format.GML3.prototype.writeGeometryElement(value, + pair.value, objectStack); + } } else { ol.format.XSD.writeStringTextNode(value, pair.value); } @@ -851,8 +877,11 @@ ol.format.WFS.prototype.writeTransaction = function(inserts, updates, deletes, options) { var objectStack = []; var node = ol.xml.createElementNS(ol.format.WFS.WFSNS, 'Transaction'); + var version = options.version ? + options.version : ol.format.WFS.DEFAULT_VERSION; + var gmlVersion = version === '1.0.0' ? 2 : 3; node.setAttribute('service', 'WFS'); - node.setAttribute('version', '1.1.0'); + node.setAttribute('version', version); var baseObj; /** @type {ol.XmlNodeStackItem} */ var obj; @@ -862,12 +891,13 @@ ol.format.WFS.prototype.writeTransaction = function(inserts, updates, deletes, node.setAttribute('handle', options.handle); } } + var schemaLocation = ol.format.WFS.SCHEMA_LOCATIONS[version]; ol.xml.setAttributeNS(node, 'http://www.w3.org/2001/XMLSchema-instance', - 'xsi:schemaLocation', this.schemaLocation_); + 'xsi:schemaLocation', schemaLocation); if (inserts) { obj = {node: node, 'featureNS': options.featureNS, 'featureType': options.featureType, 'featurePrefix': options.featurePrefix, - 'srsName': options.srsName}; + 'gmlVersion': gmlVersion, 'srsName': options.srsName}; ol.obj.assign(obj, baseObj); ol.xml.pushSerializeAndPop(obj, ol.format.WFS.TRANSACTION_SERIALIZERS_, @@ -877,7 +907,7 @@ ol.format.WFS.prototype.writeTransaction = function(inserts, updates, deletes, if (updates) { obj = {node: node, 'featureNS': options.featureNS, 'featureType': options.featureType, 'featurePrefix': options.featurePrefix, - 'srsName': options.srsName}; + 'gmlVersion': gmlVersion, 'srsName': options.srsName}; ol.obj.assign(obj, baseObj); ol.xml.pushSerializeAndPop(obj, ol.format.WFS.TRANSACTION_SERIALIZERS_, @@ -887,7 +917,7 @@ ol.format.WFS.prototype.writeTransaction = function(inserts, updates, deletes, if (deletes) { ol.xml.pushSerializeAndPop({node: node, 'featureNS': options.featureNS, 'featureType': options.featureType, 'featurePrefix': options.featurePrefix, - 'srsName': options.srsName}, + 'gmlVersion': gmlVersion, 'srsName': options.srsName}, ol.format.WFS.TRANSACTION_SERIALIZERS_, ol.xml.makeSimpleNodeFactory('Delete'), deletes, objectStack); @@ -895,7 +925,7 @@ ol.format.WFS.prototype.writeTransaction = function(inserts, updates, deletes, if (options.nativeElements) { ol.xml.pushSerializeAndPop({node: node, 'featureNS': options.featureNS, 'featureType': options.featureType, 'featurePrefix': options.featurePrefix, - 'srsName': options.srsName}, + 'gmlVersion': gmlVersion, 'srsName': options.srsName}, ol.format.WFS.TRANSACTION_SERIALIZERS_, ol.xml.makeSimpleNodeFactory('Native'), options.nativeElements, objectStack); diff --git a/test/spec/ol/format/wfs.test.js b/test/spec/ol/format/wfs.test.js index c0cd6ed5be..bb65e998fd 100644 --- a/test/spec/ol/format/wfs.test.js +++ b/test/spec/ol/format/wfs.test.js @@ -795,6 +795,50 @@ describe('ol.format.WFS', function() { }); }); + describe('when writing out a Transaction request', function() { + var text; + var filename = 'spec/ol/format/wfs/TransactionMultiVersion100.xml'; + before(function(done) { + afterLoadText(filename, function(xml) { + text = xml; + done(); + }); + }); + + it('handles the WFS version', function() { + var format = new ol.format.WFS(); + var insertFeature = new ol.Feature({ + the_geom: new ol.geom.LineString([[1.1, 2], [3, 4.2]]), + foo: 'bar', + nul: null + }); + insertFeature.setGeometryName('the_geom'); + var inserts = [insertFeature]; + var updateFeature = new ol.Feature({ + the_geom: new ol.geom.LineString([[1.1, 2], [3, 4.2]]), + foo: 'bar', + // null value gets Property element with no Value + nul: null, + // undefined value means don't create a Property element + unwritten: undefined + }); + updateFeature.setId('fid.42'); + var updates = [updateFeature]; + + var deleteFeature = new ol.Feature(); + deleteFeature.setId('fid.37'); + var deletes = [deleteFeature]; + var serialized = format.writeTransaction(inserts, updates, deletes, { + featureNS: 'http://www.openplans.org/topp', + featureType: 'states', + featurePrefix: 'topp', + version: '1.0.0' + }); + + expect(serialized).to.xmleql(ol.xml.parse(text)); + }); + }); + describe('when writing out a GetFeature request', function() { var text; diff --git a/test/spec/ol/format/wfs/TransactionMultiVersion100.xml b/test/spec/ol/format/wfs/TransactionMultiVersion100.xml new file mode 100644 index 0000000000..2f67d61aea --- /dev/null +++ b/test/spec/ol/format/wfs/TransactionMultiVersion100.xml @@ -0,0 +1,37 @@ + + + + + + 1.1,2 3,4.2 + + + bar + + + + + the_geom + + + 1.1,2 3,4.2 + + + + + foo + bar + + + nul + + + + + + + + + + +