1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
/* 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
* @requires OpenLayers/Format/OGCExceptionReport.js
*/
/**
* Class: OpenLayers.Format.WFSDescribeFeatureType
* Read WFS DescribeFeatureType response
*
* Inherits from:
* - <OpenLayers.Format.XML>
*/
OpenLayers.Format.WFSDescribeFeatureType = OpenLayers.Class(
OpenLayers.Format.XML, {
/**
* Property: regExes
* Compiled regular expressions for manipulating strings.
*/
regExes: {
trimSpace: (/^\s*|\s*$/g)
},
/**
* Property: namespaces
* {Object} Mapping of namespace aliases to namespace URIs.
*/
namespaces: {
xsd: "http://www.w3.org/2001/XMLSchema"
},
/**
* Constructor: OpenLayers.Format.WFSDescribeFeatureType
* Create a new parser for WFS DescribeFeatureType responses.
*
* Parameters:
* options - {Object} An optional object whose properties will be set on
* this instance.
*/
/**
* 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: {
"xsd": {
"schema": function(node, obj) {
var complexTypes = [];
var customTypes = {};
var schema = {
complexTypes: complexTypes,
customTypes: customTypes
};
var i, len;
this.readChildNodes(node, schema);
var attributes = node.attributes;
var attr, name;
for(i=0, len=attributes.length; i<len; ++i) {
attr = attributes[i];
name = attr.name;
if(name.indexOf("xmlns") === 0) {
this.setNamespace(name.split(":")[1] || "", attr.value);
} else {
obj[name] = attr.value;
}
}
obj.featureTypes = complexTypes;
obj.targetPrefix = this.namespaceAlias[obj.targetNamespace];
// map complexTypes to names of customTypes
var complexType, customType;
for(i=0, len=complexTypes.length; i<len; ++i) {
complexType = complexTypes[i];
customType = customTypes[complexType.typeName];
if(customTypes[complexType.typeName]) {
complexType.typeName = customType.name;
}
}
},
"complexType": function(node, obj) {
var complexType = {
// this is a temporary typeName, it will be overwritten by
// the schema reader with the metadata found in the
// customTypes hash
"typeName": node.getAttribute("name")
};
this.readChildNodes(node, complexType);
obj.complexTypes.push(complexType);
},
"complexContent": function(node, obj) {
this.readChildNodes(node, obj);
},
"extension": function(node, obj) {
this.readChildNodes(node, obj);
},
"sequence": function(node, obj) {
var sequence = {
elements: []
};
this.readChildNodes(node, sequence);
obj.properties = sequence.elements;
},
"element": function(node, obj) {
var type;
if(obj.elements) {
var element = {};
var attributes = node.attributes;
var attr;
for(var i=0, len=attributes.length; i<len; ++i) {
attr = attributes[i];
element[attr.name] = attr.value;
}
type = element.type;
if(!type) {
type = {};
this.readChildNodes(node, type);
element.restriction = type;
element.type = type.base;
}
var fullType = type.base || type;
element.localType = fullType.split(":").pop();
obj.elements.push(element);
this.readChildNodes(node, element);
}
if(obj.complexTypes) {
type = node.getAttribute("type");
var localType = type.split(":").pop();
obj.customTypes[localType] = {
"name": node.getAttribute("name"),
"type": type
};
}
},
"annotation": function(node, obj) {
obj.annotation = {};
this.readChildNodes(node, obj.annotation);
},
"appinfo": function(node, obj) {
if (!obj.appinfo) {
obj.appinfo = [];
}
obj.appinfo.push(this.getChildValue(node));
},
"documentation": function(node, obj) {
if (!obj.documentation) {
obj.documentation = [];
}
var value = this.getChildValue(node);
obj.documentation.push({
lang: node.getAttribute("xml:lang"),
textContent: value.replace(this.regExes.trimSpace, "")
});
},
"simpleType": function(node, obj) {
this.readChildNodes(node, obj);
},
"restriction": function(node, obj) {
obj.base = node.getAttribute("base");
this.readRestriction(node, obj);
}
}
},
/**
* Method: readRestriction
* Reads restriction defined in the child nodes of a restriction element
*
* Parameters:
* node - {DOMElement} the node to parse
* obj - {Object} the object that receives the read result
*/
readRestriction: function(node, obj) {
var children = node.childNodes;
var child, nodeName, value;
for(var i=0, len=children.length; i<len; ++i) {
child = children[i];
if(child.nodeType == 1) {
nodeName = child.nodeName.split(":").pop();
value = child.getAttribute("value");
if(!obj[nodeName]) {
obj[nodeName] = value;
} else {
if(typeof obj[nodeName] == "string") {
obj[nodeName] = [obj[nodeName]];
}
obj[nodeName].push(value);
}
}
}
},
/**
* Method: read
*
* Parameters:
* data - {DOMElement|String} A WFS DescribeFeatureType document.
*
* Returns:
* {Object} An object representing the WFS DescribeFeatureType response.
*/
read: function(data) {
if(typeof data == "string") {
data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
}
if(data && data.nodeType == 9) {
data = data.documentElement;
}
var schema = {};
if (data.nodeName.split(":").pop() === 'ExceptionReport') {
// an exception must have occurred, so parse it
var parser = new OpenLayers.Format.OGCExceptionReport();
schema.error = parser.read(data);
} else {
this.readNode(data, schema);
}
return schema;
},
CLASS_NAME: "OpenLayers.Format.WFSDescribeFeatureType"
});
|