diff --git a/src/ol/format/xmlformat.js b/src/ol/format/xmlformat.js index 2fd8ddbc54..e957102d82 100644 --- a/src/ol/format/xmlformat.js +++ b/src/ol/format/xmlformat.js @@ -1,6 +1,8 @@ goog.provide('ol.format.XML'); +goog.require('goog.array'); goog.require('goog.asserts'); +goog.require('goog.dom.NodeType'); goog.require('goog.dom.xml'); goog.require('ol.format.Format'); goog.require('ol.format.FormatType'); @@ -17,57 +19,6 @@ ol.format.XML = function() { goog.inherits(ol.format.XML, ol.format.Format); -/** - * @param {Document|Node|Object|string} source Source. - * @private - * @return {Document} Document. - */ -ol.format.XML.prototype.getDocument_ = function(source) { - if (source instanceof Document) { - return source; - } else if (goog.isString(source)) { - return goog.dom.xml.loadXml(source); - } else { - goog.asserts.fail(); - return null; - } -}; - - -/** - * @param {Document|Node|Object|string} source Source. - * @private - * @return {Document|Node} Document. - */ -ol.format.XML.prototype.getDocumentOrNode_ = function(source) { - if (source instanceof Document) { - return source; - } else if (source instanceof Node) { - return source; - } else if (goog.isString(source)) { - return goog.dom.xml.loadXml(source); - } else { - goog.asserts.fail(); - return null; - } -}; - - -/** - * @param {Document|Node|Object|string} source Source. - * @private - * @return {Node} Node. - */ -ol.format.XML.prototype.getNode_ = function(source) { - if (source instanceof Node) { - return source; - } else { - goog.asserts.fail(); - return null; - } -}; - - /** * @inheritDoc */ @@ -80,7 +31,31 @@ ol.format.XML.prototype.getType = function() { * @inheritDoc */ ol.format.XML.prototype.readFeature = function(source) { - return this.readFeatureFromNode(this.getNode_(source)); + if (source instanceof Document) { + return this.readFeatureFromDocument(source); + } else if (source instanceof Node) { + return this.readFeatureFromNode(source); + } else if (goog.isString(source)) { + var doc = goog.dom.xml.loadXml(source); + return this.readFeatureFromDocument(doc); + } else { + goog.asserts.fail(); + return null; + } +}; + + +/** + * @param {Document} doc Document. + * @return {ol.Feature} Feature. + */ +ol.format.XML.prototype.readFeatureFromDocument = function(doc) { + var features = this.readFeaturesFromDocument(doc); + if (features.length > 0) { + return features[0]; + } else { + return null; + } }; @@ -95,11 +70,13 @@ ol.format.XML.prototype.readFeatureFromNode = goog.abstractMethod; * @inheritDoc */ ol.format.XML.prototype.readFeatures = function(source) { - var documentOrNode = this.getDocumentOrNode_(source); - if (documentOrNode instanceof Document) { - return this.readFeaturesFromDocument(documentOrNode); - } else if (documentOrNode instanceof Node) { - return this.readFeaturesFromNode(documentOrNode); + if (source instanceof Document) { + return this.readFeaturesFromDocument(source); + } else if (source instanceof Node) { + return this.readFeaturesFromNode(source); + } else if (goog.isString(source)) { + var doc = goog.dom.xml.loadXml(source); + return this.readFeaturesFromDocument(doc); } else { goog.asserts.fail(); return null; @@ -113,8 +90,15 @@ ol.format.XML.prototype.readFeatures = function(source) { * @return {Array.} Features. */ ol.format.XML.prototype.readFeaturesFromDocument = function(doc) { - goog.asserts.assert(doc.childNodes.length == 1); - return this.readFeaturesFromNode(doc.firstChild); + /** @type {Array.} */ + var features = []; + var n; + for (n = doc.firstChild; !goog.isNull(n); n = n.nextSibling) { + if (n.nodeType == goog.dom.NodeType.ELEMENT) { + goog.array.extend(features, this.readFeaturesFromNode(n)); + } + } + return features; }; @@ -130,10 +114,28 @@ ol.format.XML.prototype.readFeaturesFromNode = goog.abstractMethod; * @inheritDoc */ ol.format.XML.prototype.readGeometry = function(source) { - return this.readGeometryFromNode(this.getNode_(source)); + if (source instanceof Document) { + return this.readGeometryFromDocument(source); + } else if (source instanceof Node) { + return this.readGeometryFromNode(source); + } else if (goog.isString(source)) { + var doc = goog.dom.xml.loadXml(source); + return this.readGeometryFromDocument(doc); + } else { + goog.asserts.fail(); + return null; + } }; +/** + * @param {Document} doc Document. + * @protected + * @return {ol.geom.Geometry} Geometry. + */ +ol.format.XML.prototype.readGeometryFromDocument = goog.abstractMethod; + + /** * @param {Node} node Node. * @protected @@ -146,10 +148,28 @@ ol.format.XML.prototype.readGeometryFromNode = goog.abstractMethod; * @inheritDoc */ ol.format.XML.prototype.readProjection = function(source) { - return this.readProjectionFromNode(this.getNode_(source)); + if (source instanceof Document) { + return this.readProjectionFromDocument(source); + } else if (source instanceof Node) { + return this.readProjectionFromNode(source); + } else if (goog.isString(source)) { + var doc = goog.dom.xml.loadXml(source); + return this.readProjectionFromDocument(doc); + } else { + goog.asserts.fail(); + return null; + } }; +/** + * @param {Document} doc Document. + * @protected + * @return {ol.proj.Projection} Projection. + */ +ol.format.XML.prototype.readProjectionFromDocument = goog.abstractMethod; + + /** * @param {Node} node Node. * @protected