diff --git a/modules/extension/xsd/xsd-gml3/src/main/java/org/geotools/gml3/bindings/ComplexSupportXSAnyTypeBinding.java b/modules/extension/xsd/xsd-gml3/src/main/java/org/geotools/gml3/bindings/ComplexSupportXSAnyTypeBinding.java index d9708c9034e..81767e304d9 100644 --- a/modules/extension/xsd/xsd-gml3/src/main/java/org/geotools/gml3/bindings/ComplexSupportXSAnyTypeBinding.java +++ b/modules/extension/xsd/xsd-gml3/src/main/java/org/geotools/gml3/bindings/ComplexSupportXSAnyTypeBinding.java @@ -48,6 +48,7 @@ import org.opengis.filter.identity.Identifier; import org.w3c.dom.Document; import org.w3c.dom.Element; +import org.w3c.dom.Text; import org.xml.sax.Attributes; /** @@ -405,8 +406,22 @@ public Element encode(Object object, Document document, Element value) throws Ex } GML3EncodingUtils.encodeClientProperties(complex, value); GML3EncodingUtils.encodeSimpleContent(complex, document, value); + } else if(!isPlaceholderObject(object)) { + GML3EncodingUtils.encodeAsText(document, value, object); } return value; } + /** + * We need to skip placeholder objects (new Object()) + * used in many places to represent objects that + * should not be encoded. + * + * @param object + * @return + */ + private boolean isPlaceholderObject(Object object) { + return object != null && object.getClass().isAssignableFrom(Object.class); + } + } diff --git a/modules/extension/xsd/xsd-gml3/src/main/java/org/geotools/gml3/bindings/GML3EncodingUtils.java b/modules/extension/xsd/xsd-gml3/src/main/java/org/geotools/gml3/bindings/GML3EncodingUtils.java index 8e7216f8f0f..1c9d1abd8db 100644 --- a/modules/extension/xsd/xsd-gml3/src/main/java/org/geotools/gml3/bindings/GML3EncodingUtils.java +++ b/modules/extension/xsd/xsd-gml3/src/main/java/org/geotools/gml3/bindings/GML3EncodingUtils.java @@ -368,6 +368,10 @@ public static void encodeClientProperties(Property complex, Element element) { public static void encodeSimpleContent(ComplexAttribute complex, Document document, Element element) { Object value = getSimpleContent(complex); + encodeAsText(document, element, value); + } + + public static void encodeAsText(Document document, Element element, Object value) { if (value != null) { Text text = document.createTextNode(Converters.convert(value, String.class)); element.appendChild(text); diff --git a/modules/extension/xsd/xsd-gml3/src/test/java/org/geotools/gml3/bindings/XSAnyTypeBindingTest.java b/modules/extension/xsd/xsd-gml3/src/test/java/org/geotools/gml3/bindings/XSAnyTypeBindingTest.java new file mode 100644 index 00000000000..3fbe63dbafe --- /dev/null +++ b/modules/extension/xsd/xsd-gml3/src/test/java/org/geotools/gml3/bindings/XSAnyTypeBindingTest.java @@ -0,0 +1,165 @@ +/* + * GeoTools - The Open Source Java GIS Toolkit + * http://geotools.org + * + * (C) 2015, Open Source Geospatial Foundation (OSGeo) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ +package org.geotools.gml3.bindings; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import javax.xml.namespace.QName; + +import org.geotools.feature.AttributeImpl; +import org.geotools.feature.ComplexAttributeImpl; +import org.geotools.feature.NameImpl; +import org.geotools.feature.type.AttributeDescriptorImpl; +import org.geotools.feature.type.AttributeTypeImpl; +import org.geotools.feature.type.ComplexTypeImpl; +import org.geotools.gml3.GML3TestSupport; +import org.geotools.gml3.GMLConfiguration; +import org.geotools.xml.Configuration; +import org.geotools.xml.SchemaLocator; +import org.geotools.xml.XSD; +import org.opengis.feature.ComplexAttribute; +import org.opengis.feature.Property; +import org.opengis.feature.type.AttributeDescriptor; +import org.opengis.feature.type.AttributeType; +import org.opengis.feature.type.Name; +import org.opengis.feature.type.PropertyDescriptor; +import org.picocontainer.MutablePicoContainer; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +public class XSAnyTypeBindingTest extends GML3TestSupport { + + private static final String SAMPLE_CLASS_VALUE = "1.1.1"; + + + static class ANYTYPETEST extends XSD { + /** + * singleton instance + */ + private static ANYTYPETEST instance = new ANYTYPETEST(); + + public static final String NAMESPACE = "http://www.geotools.org/anytypetest"; + + public static final QName OBSERVATION = new QName("http://www.geotools.org/anytypetest", + "Observation"); + + /** + * private constructor. + */ + private ANYTYPETEST() { + } + + public static ANYTYPETEST getInstance() { + return instance; + } + + + + public String getNamespaceURI() { + return NAMESPACE; + } + + /** + * Returns the location of 'AnyTypeTest.xsd'. + */ + public String getSchemaLocation() { + return getClass().getResource("AnyTypeTest.xsd").toString(); + } + + public SchemaLocator createSchemaLocator() { + //we explicity return null here because of a circular dependnecy with + //gml3 schema... returning null breaks the circle when the schemas are + //being built + return null; + } + + /* Attributes */ + } + + class AnyTypeTestConfiguration extends Configuration { + public AnyTypeTestConfiguration() { + super(ANYTYPETEST.getInstance()); + } + + protected void registerBindings(MutablePicoContainer container) { + } + } + + class MyConfiguration extends Configuration { + + public MyConfiguration() { + super(ANYTYPETEST.getInstance()); + addDependency(new GMLConfiguration()); + } + + } + + @Override + protected void registerNamespaces(Element root) { + super.registerNamespaces(root); + root.setAttribute("xmlns:test", "http://www.geotools.org/anytypetest"); + } + + + + @Override + protected Configuration createConfiguration() { + return new MyConfiguration(); + } + + + + public void testEncode() throws Exception { + QName observation = ANYTYPETEST.OBSERVATION; + ComplexAttribute myCode = testAnyTypeTest(observation, SAMPLE_CLASS_VALUE); + Document dom = encode(myCode, observation); + print(dom); + assertEquals("test:Observation", dom.getDocumentElement().getNodeName()); + assertEquals(1, dom.getDocumentElement().getElementsByTagName("test:class").getLength()); + assertNotNull(dom.getDocumentElement().getElementsByTagName("test:class").item(0).getFirstChild()); + assertEquals(SAMPLE_CLASS_VALUE,dom.getDocumentElement().getElementsByTagName("test:class").item(0).getFirstChild().getNodeValue()); + } + + + public ComplexAttribute testAnyTypeTest(QName typeName, String classValue) { + Name myType = new NameImpl(typeName.getNamespaceURI(), typeName.getLocalPart()); + + List properties = new ArrayList(); + List propertyDescriptors = new ArrayList(); + + // assume attributes from same namespace as typename + + Name attName = new NameImpl(typeName.getNamespaceURI(), "class"); + // Name name, Class binding, boolean isAbstract, List restrictions, + // PropertyType superType, InternationalString description + AttributeType p = new AttributeTypeImpl(attName, String.class, false, false, null, null, + null); + AttributeDescriptor pd = new AttributeDescriptorImpl(p, attName, 0, 0, false, null); + + propertyDescriptors.add(pd); + properties.add(new AttributeImpl(classValue, pd, null)); + + ComplexTypeImpl at = new ComplexTypeImpl(myType, propertyDescriptors, false, false, + Collections.EMPTY_LIST, null, null); + + AttributeDescriptorImpl ai = new AttributeDescriptorImpl(at, myType, 0, 0, false, null); + + return new ComplexAttributeImpl(properties, ai, null); + } +} diff --git a/modules/extension/xsd/xsd-gml3/src/test/resources/org/geotools/gml3/bindings/AnyTypeTest.xsd b/modules/extension/xsd/xsd-gml3/src/test/resources/org/geotools/gml3/bindings/AnyTypeTest.xsd new file mode 100644 index 00000000000..4d1901d770e --- /dev/null +++ b/modules/extension/xsd/xsd-gml3/src/test/resources/org/geotools/gml3/bindings/AnyTypeTest.xsd @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + +