Skip to content

Commit ec294af

Browse files
author
Bart van den Eijnden
committed
Merge pull request openlayers#762 from bartvde/schema
parse annotation from WFS DescribeFeatureType schemas (r=@ahocevar)
2 parents c48458f + 52d69ec commit ec294af

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

lib/OpenLayers/Format/WFSDescribeFeatureType.js

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
*/
1818
OpenLayers.Format.WFSDescribeFeatureType = OpenLayers.Class(
1919
OpenLayers.Format.XML, {
20+
21+
/**
22+
* Property: regExes
23+
* Compiled regular expressions for manipulating strings.
24+
*/
25+
regExes: {
26+
trimSpace: (/^\s*|\s*$/g)
27+
},
2028

2129
/**
2230
* Property: namespaces
@@ -52,15 +60,16 @@ OpenLayers.Format.WFSDescribeFeatureType = OpenLayers.Class(
5260
complexTypes: complexTypes,
5361
customTypes: customTypes
5462
};
63+
var i, len;
5564

5665
this.readChildNodes(node, schema);
5766

5867
var attributes = node.attributes;
5968
var attr, name;
60-
for(var i=0, len=attributes.length; i<len; ++i) {
69+
for(i=0, len=attributes.length; i<len; ++i) {
6170
attr = attributes[i];
6271
name = attr.name;
63-
if(name.indexOf("xmlns") == 0) {
72+
if(name.indexOf("xmlns") === 0) {
6473
this.setNamespace(name.split(":")[1] || "", attr.value);
6574
} else {
6675
obj[name] = attr.value;
@@ -71,7 +80,7 @@ OpenLayers.Format.WFSDescribeFeatureType = OpenLayers.Class(
7180

7281
// map complexTypes to names of customTypes
7382
var complexType, customType;
74-
for(var i=0, len=complexTypes.length; i<len; ++i) {
83+
for(i=0, len=complexTypes.length; i<len; ++i) {
7584
complexType = complexTypes[i];
7685
customType = customTypes[complexType.typeName];
7786
if(customTypes[complexType.typeName]) {
@@ -103,6 +112,7 @@ OpenLayers.Format.WFSDescribeFeatureType = OpenLayers.Class(
103112
obj.properties = sequence.elements;
104113
},
105114
"element": function(node, obj) {
115+
var type;
106116
if(obj.elements) {
107117
var element = {};
108118
var attributes = node.attributes;
@@ -112,7 +122,7 @@ OpenLayers.Format.WFSDescribeFeatureType = OpenLayers.Class(
112122
element[attr.name] = attr.value;
113123
}
114124

115-
var type = element.type;
125+
type = element.type;
116126
if(!type) {
117127
type = {};
118128
this.readChildNodes(node, type);
@@ -122,17 +132,38 @@ OpenLayers.Format.WFSDescribeFeatureType = OpenLayers.Class(
122132
var fullType = type.base || type;
123133
element.localType = fullType.split(":").pop();
124134
obj.elements.push(element);
135+
this.readChildNodes(node, element);
125136
}
126137

127138
if(obj.complexTypes) {
128-
var type = node.getAttribute("type");
139+
type = node.getAttribute("type");
129140
var localType = type.split(":").pop();
130141
obj.customTypes[localType] = {
131142
"name": node.getAttribute("name"),
132143
"type": type
133144
};
134145
}
135146
},
147+
"annotation": function(node, obj) {
148+
obj.annotation = {};
149+
this.readChildNodes(node, obj.annotation);
150+
},
151+
"appinfo": function(node, obj) {
152+
if (!obj.appinfo) {
153+
obj.appinfo = [];
154+
}
155+
obj.appinfo.push(this.getChildValue(node));
156+
},
157+
"documentation": function(node, obj) {
158+
if (!obj.documentation) {
159+
obj.documentation = [];
160+
}
161+
var value = this.getChildValue(node);
162+
obj.documentation.push({
163+
lang: node.getAttribute("xml:lang"),
164+
textContent: value.replace(this.regExes.trimSpace, "")
165+
});
166+
},
136167
"simpleType": function(node, obj) {
137168
this.readChildNodes(node, obj);
138169
},

tests/Format/WFSDescribeFeatureType.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,45 @@
390390
t.ok(!!obj.error, "Error reported correctly");
391391
}
392392

393+
function test_read_annotation(t) {
394+
t.plan(2);
395+
var text =
396+
'<?xml version="1.0" encoding="UTF-8"?>' +
397+
'<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"' +
398+
' xmlns:analytics="http://opengeo.org/analytics" xmlns:cite="http://www.opengeospatial.net/cite"' +
399+
' xmlns:gml="http://www.opengis.net/gml" xmlns:it.geosolutions="http://www.geo-solutions.it"' +
400+
' xmlns:nurc="http://www.nurc.nato.int" xmlns:og="http://opengeo.org"' +
401+
' xmlns:sde="http://geoserver.sf.net" xmlns:sf="http://www.openplans.org/spearfish"' +
402+
' xmlns:tiger="http://www.census.gov" xmlns:tike="http://opengeo.org/#tike"' +
403+
' xmlns:topp="http://www.openplans.org/topp" xmlns:usgs="http://www.usgs.gov/"' +
404+
' xmlns:za="http://opengeo.org/za" elementFormDefault="qualified"' +
405+
' targetNamespace="http://www.openplans.org/topp">' +
406+
' <xsd:import namespace="http://www.opengis.net/gml"' +
407+
' schemaLocation="http://demo.opengeo.org/geoserver/schemas/gml/3.1.1/base/gml.xsd"/>' +
408+
' <xsd:complexType name="statesType">' +
409+
' <xsd:complexContent>' +
410+
' <xsd:extension base="gml:AbstractFeatureType">' +
411+
' <xsd:sequence>' +
412+
' <xsd:element maxOccurs="1" minOccurs="0" name="PERSONS" nillable="true" type="xsd:double">' +
413+
' <xsd:annotation>' +
414+
' <xsd:appinfo>{"title":{"en":"Population"}}</xsd:appinfo>' +
415+
' <xsd:documentation xml:lang="en"> Number of persons living in the state' +
416+
' </xsd:documentation>' +
417+
' </xsd:annotation>' +
418+
' </xsd:element>' +
419+
' </xsd:sequence>' +
420+
' </xsd:extension>' +
421+
' </xsd:complexContent>' +
422+
' </xsd:complexType>' +
423+
' <xsd:element name="states" substitutionGroup="gml:_Feature" type="topp:statesType"/>' +
424+
'</xsd:schema>';
425+
var format = new OpenLayers.Format.WFSDescribeFeatureType();
426+
var res = format.read(text);
427+
var property = res.featureTypes[0].properties[0];
428+
t.eq(property.annotation.appinfo[0], '{"title":{"en":"Population"}}', "appinfo read correctly");
429+
t.eq(property.annotation.documentation[0], {lang: "en", textContent: 'Number of persons living in the state'}, "documentation read correctly");
430+
}
431+
393432
</script>
394433
</head>
395434
<body>

0 commit comments

Comments
 (0)