/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for * full list of contributors). Published under the 2-clause BSD license. * See license.txt in the OpenLayers distribution or repository for the * full text of the license. */ /** * @requires OpenLayers/Format/XML.js */ /** * Class: OpenLayers.Format.WMSGetFeatureInfo * Class to read GetFeatureInfo responses from Web Mapping Services * * Inherits from: * - */ OpenLayers.Format.WMSGetFeatureInfo = OpenLayers.Class(OpenLayers.Format.XML, { /** * APIProperty: layerIdentifier * {String} All xml nodes containing this search criteria will populate an * internal array of layer nodes. */ layerIdentifier: '_layer', /** * APIProperty: featureIdentifier * {String} All xml nodes containing this search criteria will populate an * internal array of feature nodes for each layer node found. */ featureIdentifier: '_feature', /** * Property: regExes * Compiled regular expressions for manipulating strings. */ regExes: { trimSpace: (/^\s*|\s*$/g), removeSpace: (/\s*/g), splitSpace: (/\s+/), trimComma: (/\s*,\s*/g) }, /** * Property: gmlFormat * {} internal GML format for parsing geometries * in msGMLOutput */ gmlFormat: null, /** * Constructor: OpenLayers.Format.WMSGetFeatureInfo * Create a new parser for WMS GetFeatureInfo responses * * Parameters: * options - {Object} An optional object whose properties will be set on * this instance. */ /** * APIMethod: read * Read WMS GetFeatureInfo data from a string, and return an array of features * * Parameters: * data - {String} or {DOMElement} data to read/parse. * * Returns: * {Array()} An array of features. */ read: function(data) { var result; if(typeof data == "string") { data = OpenLayers.Format.XML.prototype.read.apply(this, [data]); } var root = data.documentElement; if(root) { var scope = this; var read = this["read_" + root.nodeName]; if(read) { result = read.call(this, root); } else { // fall-back to GML since this is a common output format for WMS // GetFeatureInfo responses result = new OpenLayers.Format.GML((this.options ? this.options : {})).read(data); } } else { result = data; } return result; }, /** * Method: read_msGMLOutput * Parse msGMLOutput nodes. * * Parameters: * data - {DOMElement} * * Returns: * {Array} */ read_msGMLOutput: function(data) { var response = []; var layerNodes = this.getSiblingNodesByTagCriteria(data, this.layerIdentifier); if (layerNodes) { for (var i=0, len=layerNodes.length; i 0) { for(j=0; j 0 && tagName.indexOf(criteria) > -1) { nodes.push(child); } else { matchNodes = this.getSiblingNodesByTagCriteria( child, criteria); if(matchNodes.length > 0){ (nodes.length == 0) ? nodes = matchNodes : nodes.push(matchNodes); } } } } return nodes; }, /** * Method: parseAttributes * * Parameters: * node - {} * * Returns: * {Object} An attributes object. * * Notes: * Assumes that attributes are direct child xml nodes of the passed node * and contain only a single text node. */ parseAttributes: function(node){ var attributes = {}; if (node.nodeType == 1) { var children = node.childNodes; var n = children.length; for (var i = 0; i < n; ++i) { var child = children[i]; if (child.nodeType == 1) { var grandchildren = child.childNodes; var name = (child.prefix) ? child.nodeName.split(":")[1] : child.nodeName; if (grandchildren.length == 0) { attributes[name] = null; } else if (grandchildren.length == 1) { var grandchild = grandchildren[0]; if (grandchild.nodeType == 3 || grandchild.nodeType == 4) { var value = grandchild.nodeValue.replace( this.regExes.trimSpace, ""); attributes[name] = value; } } } } } return attributes; }, /** * Method: parseGeometry * Parse the geometry and the feature bounds out of the node using * Format.GML * * Parameters: * node - {} * * Returns: * {Object} An object containing the geometry and the feature bounds */ parseGeometry: function(node) { // we need to use the old Format.GML parser since we do not know the // geometry name if (!this.gmlFormat) { this.gmlFormat = new OpenLayers.Format.GML(); } var feature = this.gmlFormat.parseFeature(node); var geometry, bounds = null; if (feature) { geometry = feature.geometry && feature.geometry.clone(); bounds = feature.bounds && feature.bounds.clone(); feature.destroy(); } return {geometry: geometry, bounds: bounds}; }, CLASS_NAME: "OpenLayers.Format.WMSGetFeatureInfo" });