parse annotation from WFS DescribeFeatureType schemas
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
/**
|
/**
|
||||||
* @requires OpenLayers/Format/XML.js
|
* @requires OpenLayers/Format/XML.js
|
||||||
* @requires OpenLayers/Format/OGCExceptionReport.js
|
* @requires OpenLayers/Format/OGCExceptionReport.js
|
||||||
|
* @requires OpenLayers/Format/JSON.js
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,6 +19,22 @@
|
|||||||
OpenLayers.Format.WFSDescribeFeatureType = OpenLayers.Class(
|
OpenLayers.Format.WFSDescribeFeatureType = OpenLayers.Class(
|
||||||
OpenLayers.Format.XML, {
|
OpenLayers.Format.XML, {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Property: jsonFormat
|
||||||
|
* {OpenLayers.Format.JSON}
|
||||||
|
* Parser instance used to parse JSON for cross browser support. The native
|
||||||
|
* JSON.parse method will be used where available (all except IE<8).
|
||||||
|
*/
|
||||||
|
jsonFormat: null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Property: regExes
|
||||||
|
* Compiled regular expressions for manipulating strings.
|
||||||
|
*/
|
||||||
|
regExes: {
|
||||||
|
trimSpace: (/^\s*|\s*$/g)
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Property: namespaces
|
* Property: namespaces
|
||||||
* {Object} Mapping of namespace aliases to namespace URIs.
|
* {Object} Mapping of namespace aliases to namespace URIs.
|
||||||
@@ -52,15 +69,16 @@ OpenLayers.Format.WFSDescribeFeatureType = OpenLayers.Class(
|
|||||||
complexTypes: complexTypes,
|
complexTypes: complexTypes,
|
||||||
customTypes: customTypes
|
customTypes: customTypes
|
||||||
};
|
};
|
||||||
|
var i, len;
|
||||||
|
|
||||||
this.readChildNodes(node, schema);
|
this.readChildNodes(node, schema);
|
||||||
|
|
||||||
var attributes = node.attributes;
|
var attributes = node.attributes;
|
||||||
var attr, name;
|
var attr, name;
|
||||||
for(var i=0, len=attributes.length; i<len; ++i) {
|
for(i=0, len=attributes.length; i<len; ++i) {
|
||||||
attr = attributes[i];
|
attr = attributes[i];
|
||||||
name = attr.name;
|
name = attr.name;
|
||||||
if(name.indexOf("xmlns") == 0) {
|
if(name.indexOf("xmlns") === 0) {
|
||||||
this.setNamespace(name.split(":")[1] || "", attr.value);
|
this.setNamespace(name.split(":")[1] || "", attr.value);
|
||||||
} else {
|
} else {
|
||||||
obj[name] = attr.value;
|
obj[name] = attr.value;
|
||||||
@@ -71,7 +89,7 @@ OpenLayers.Format.WFSDescribeFeatureType = OpenLayers.Class(
|
|||||||
|
|
||||||
// map complexTypes to names of customTypes
|
// map complexTypes to names of customTypes
|
||||||
var complexType, customType;
|
var complexType, customType;
|
||||||
for(var i=0, len=complexTypes.length; i<len; ++i) {
|
for(i=0, len=complexTypes.length; i<len; ++i) {
|
||||||
complexType = complexTypes[i];
|
complexType = complexTypes[i];
|
||||||
customType = customTypes[complexType.typeName];
|
customType = customTypes[complexType.typeName];
|
||||||
if(customTypes[complexType.typeName]) {
|
if(customTypes[complexType.typeName]) {
|
||||||
@@ -103,6 +121,7 @@ OpenLayers.Format.WFSDescribeFeatureType = OpenLayers.Class(
|
|||||||
obj.properties = sequence.elements;
|
obj.properties = sequence.elements;
|
||||||
},
|
},
|
||||||
"element": function(node, obj) {
|
"element": function(node, obj) {
|
||||||
|
var type;
|
||||||
if(obj.elements) {
|
if(obj.elements) {
|
||||||
var element = {};
|
var element = {};
|
||||||
var attributes = node.attributes;
|
var attributes = node.attributes;
|
||||||
@@ -112,7 +131,7 @@ OpenLayers.Format.WFSDescribeFeatureType = OpenLayers.Class(
|
|||||||
element[attr.name] = attr.value;
|
element[attr.name] = attr.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
var type = element.type;
|
type = element.type;
|
||||||
if(!type) {
|
if(!type) {
|
||||||
type = {};
|
type = {};
|
||||||
this.readChildNodes(node, type);
|
this.readChildNodes(node, type);
|
||||||
@@ -122,10 +141,11 @@ OpenLayers.Format.WFSDescribeFeatureType = OpenLayers.Class(
|
|||||||
var fullType = type.base || type;
|
var fullType = type.base || type;
|
||||||
element.localType = fullType.split(":").pop();
|
element.localType = fullType.split(":").pop();
|
||||||
obj.elements.push(element);
|
obj.elements.push(element);
|
||||||
|
this.readChildNodes(node, element);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(obj.complexTypes) {
|
if(obj.complexTypes) {
|
||||||
var type = node.getAttribute("type");
|
type = node.getAttribute("type");
|
||||||
var localType = type.split(":").pop();
|
var localType = type.split(":").pop();
|
||||||
obj.customTypes[localType] = {
|
obj.customTypes[localType] = {
|
||||||
"name": node.getAttribute("name"),
|
"name": node.getAttribute("name"),
|
||||||
@@ -133,6 +153,24 @@ OpenLayers.Format.WFSDescribeFeatureType = OpenLayers.Class(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"annotation": function(node, obj) {
|
||||||
|
obj.annotation = {};
|
||||||
|
this.readChildNodes(node, obj.annotation);
|
||||||
|
},
|
||||||
|
"appinfo": function(node, obj) {
|
||||||
|
if (!this.jsonFormat) {
|
||||||
|
this.jsonFormat = new OpenLayers.Format.JSON();
|
||||||
|
}
|
||||||
|
obj.appinfo = this.jsonFormat.read(this.getChildValue(node));
|
||||||
|
},
|
||||||
|
"documentation": function(node, obj) {
|
||||||
|
if (!obj.documentation) {
|
||||||
|
obj.documentation = {};
|
||||||
|
}
|
||||||
|
var lang = node.getAttribute("xml:lang");
|
||||||
|
var value = this.getChildValue(node);
|
||||||
|
obj.documentation[lang] = value.replace(this.regExes.trimSpace, "");
|
||||||
|
},
|
||||||
"simpleType": function(node, obj) {
|
"simpleType": function(node, obj) {
|
||||||
this.readChildNodes(node, obj);
|
this.readChildNodes(node, obj);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -390,6 +390,45 @@
|
|||||||
t.ok(!!obj.error, "Error reported correctly");
|
t.ok(!!obj.error, "Error reported correctly");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function test_read_annotation(t) {
|
||||||
|
t.plan(2);
|
||||||
|
var text =
|
||||||
|
'<?xml version="1.0" encoding="UTF-8"?>' +
|
||||||
|
'<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"' +
|
||||||
|
' xmlns:analytics="http://opengeo.org/analytics" xmlns:cite="http://www.opengeospatial.net/cite"' +
|
||||||
|
' xmlns:gml="http://www.opengis.net/gml" xmlns:it.geosolutions="http://www.geo-solutions.it"' +
|
||||||
|
' xmlns:nurc="http://www.nurc.nato.int" xmlns:og="http://opengeo.org"' +
|
||||||
|
' xmlns:sde="http://geoserver.sf.net" xmlns:sf="http://www.openplans.org/spearfish"' +
|
||||||
|
' xmlns:tiger="http://www.census.gov" xmlns:tike="http://opengeo.org/#tike"' +
|
||||||
|
' xmlns:topp="http://www.openplans.org/topp" xmlns:usgs="http://www.usgs.gov/"' +
|
||||||
|
' xmlns:za="http://opengeo.org/za" elementFormDefault="qualified"' +
|
||||||
|
' targetNamespace="http://www.openplans.org/topp">' +
|
||||||
|
' <xsd:import namespace="http://www.opengis.net/gml"' +
|
||||||
|
' schemaLocation="http://demo.opengeo.org/geoserver/schemas/gml/3.1.1/base/gml.xsd"/>' +
|
||||||
|
' <xsd:complexType name="statesType">' +
|
||||||
|
' <xsd:complexContent>' +
|
||||||
|
' <xsd:extension base="gml:AbstractFeatureType">' +
|
||||||
|
' <xsd:sequence>' +
|
||||||
|
' <xsd:element maxOccurs="1" minOccurs="0" name="PERSONS" nillable="true" type="xsd:double">' +
|
||||||
|
' <xsd:annotation>' +
|
||||||
|
' <xsd:appinfo>{"title":{"en":"Population"}}</xsd:appinfo>' +
|
||||||
|
' <xsd:documentation xml:lang="en"> Number of persons living in the state' +
|
||||||
|
' </xsd:documentation>' +
|
||||||
|
' </xsd:annotation>' +
|
||||||
|
' </xsd:element>' +
|
||||||
|
' </xsd:sequence>' +
|
||||||
|
' </xsd:extension>' +
|
||||||
|
' </xsd:complexContent>' +
|
||||||
|
' </xsd:complexType>' +
|
||||||
|
' <xsd:element name="states" substitutionGroup="gml:_Feature" type="topp:statesType"/>' +
|
||||||
|
'</xsd:schema>';
|
||||||
|
var format = new OpenLayers.Format.WFSDescribeFeatureType();
|
||||||
|
var res = format.read(text);
|
||||||
|
var property = res.featureTypes[0].properties[0];
|
||||||
|
t.eq(property.annotation.appinfo.title["en"], "Population", "appinfo read correctly");
|
||||||
|
t.eq(property.annotation.documentation["en"], "Number of persons living in the state", "documentation read correctly");
|
||||||
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
Reference in New Issue
Block a user