Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add support for typed data for custom elements

  • Loading branch information...
commit ac7d3b9701696491b17c4ec5fb64c9b51aa3bbc6 1 parent 5b2f4b8
Robert Marianski rmarianski authored
35 modules/extension/xsd/xsd-kml/src/main/java/org/geotools/kml/bindings/FeatureTypeBinding.java
View
@@ -17,6 +17,7 @@
package org.geotools.kml.bindings;
import java.net.URI;
+import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@@ -146,6 +147,21 @@ public Class getType() {
return SimpleFeature.class;
}
+ private SimpleFeatureType appendAttributes(SimpleFeatureType acc, SimpleFeatureType typeToAppend) {
+ if (typeToAppend == null) {
+ return acc;
+ }
+ SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
+ tb.init(acc);
+ for (AttributeDescriptor ad : typeToAppend.getAttributeDescriptors()) {
+ // only add attributes that we don't already have
+ if (acc.getDescriptor(ad.getLocalName()) == null) {
+ tb.add(ad);
+ }
+ }
+ return tb.buildFeatureType();
+ }
+
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
@@ -155,7 +171,8 @@ public Class getType() {
public Object parse(ElementInstance instance, Node node, Object value)
throws Exception {
- SimpleFeatureType featureType = null;
+ // start off with the default feature type, and retype as necessary
+ SimpleFeatureType featureType = FeatureType;
// retype based on schema if we have extended data pointing to a url
@SuppressWarnings("unchecked")
@@ -165,17 +182,13 @@ public Object parse(ElementInstance instance, Node node, Object value)
if (schemaURI != null) {
String normalizedSchemaName = normalizeSchemaName(schemaURI);
SimpleFeatureType schemaType = schemaRegistry.get(normalizedSchemaName);
- if (schemaType != null) {
- SimpleFeatureTypeBuilder ftb = new SimpleFeatureTypeBuilder();
- ftb.init(FeatureType);
- ftb.addAll(schemaType.getAttributeDescriptors());
- featureType = ftb.buildFeatureType();
- }
+ featureType = appendAttributes(featureType, schemaType);
}
}
- if (featureType == null) {
- featureType = FeatureType;
- }
+
+ // if we are a custom schema element, add the attributes to the type
+ SimpleFeatureType customFeatureType = schemaRegistry.get(instance.getName());
+ featureType = appendAttributes(featureType, customFeatureType);
SimpleFeatureBuilder b = new SimpleFeatureBuilder(featureType);
@@ -223,6 +236,7 @@ public Object parse(ElementInstance instance, Node node, Object value)
// stick extended data in feature user data
if (extData != null) {
b.featureUserData("UntypedExtendedData", extData.get("untyped"));
+ @SuppressWarnings("unchecked")
Map<String, Object> typedUserData = (Map<String, Object>) extData.get("typed");
if (typedUserData != null) {
for (Entry<String, Object> entry : typedUserData.entrySet()) {
@@ -236,7 +250,6 @@ public Object parse(ElementInstance instance, Node node, Object value)
// if we are a custom schema type
// add in any attributes from that type onto the feature
- SimpleFeatureType customFeatureType = schemaRegistry.get(instance.getName());
if (customFeatureType != null) {
for (AttributeDescriptor ad : customFeatureType.getAttributeDescriptors()) {
String attrName = ad.getLocalName();
20 modules/extension/xsd/xsd-kml/src/test/java/org/geotools/kml/bindings/PlacemarkTypeBindingTest.java
View
@@ -134,4 +134,24 @@ public void testParseTypedAndUntypedData() throws Exception {
Map<String, String> untypedData = (Map<String, String>) userData.get("UntypedExtendedData");
assertEquals("bar", untypedData.get("foo"));
}
+
+ public void testParseCustomElement() throws Exception {
+ String xml = "<Document>" + "<Schema name=\"fooelement\">"
+ + "<SimpleField type=\"int\" name=\"quux\"></SimpleField>" + "</Schema>"
+ + "<fooelement>" + "<name>name</name>" + "<description>description</description>"
+ + "<Point>" + "<coordinates>1,2</coordinates>" + "</Point>" + "<ExtendedData>"
+ + "<SchemaData schemaUrl=\"#foo\">" + "<SimpleData name=\"quux\">morx</SimpleData>"
+ + "</SchemaData>" + "<Data name=\"foo\"><value>bar</value></Data>"
+ + "</ExtendedData>" + "</fooelement></Document>";
+ buildDocument(xml);
+
+ SimpleFeature placemark = parsePlacemarkFromDocument();
+ SimpleFeatureType featureType = placemark.getFeatureType();
+ assertEquals(Integer.class, featureType.getDescriptor("quux").getType().getBinding());
+ assertEquals("morx", placemark.getAttribute("quux"));
+ Map<Object, Object> userData = placemark.getUserData();
+ @SuppressWarnings("unchecked")
+ Map<String, String> untypedData = (Map<String, String>) userData.get("UntypedExtendedData");
+ assertEquals("bar", untypedData.get("foo"));
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.