Skip to content

Commit

Permalink
Merge pull request geotools#760 from mbarto/geot5037_xsanytype
Browse files Browse the repository at this point in the history
[GEOT-5037] Fixing Complex xs:anyType handling for simple types
  • Loading branch information
bencaradocdavies committed Mar 4, 2015
2 parents 8cefe23 + efa92f1 commit 5686bad
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 0 deletions.
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
}

}
Expand Up @@ -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);
Expand Down
@@ -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<Property> properties = new ArrayList<Property>();
List<PropertyDescriptor> propertyDescriptors = new ArrayList<PropertyDescriptor>();

// assume attributes from same namespace as typename

Name attName = new NameImpl(typeName.getNamespaceURI(), "class");
// Name name, Class<?> binding, boolean isAbstract, List<Filter> 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);
}
}
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://www.geotools.org/anytypetest"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:test="http://www.geotools.org/anytypetest"
xmlns:gml="http://www.opengis.net/gml"
elementFormDefault="qualified">


<xsd:import namespace="http://www.opengis.net/gml/3.2" schemaLocation="http://schemas.opengis.net/gml/3.2.1/gml.xsd"/>


<xsd:element name="Observation" type="test:ObservationType" substitutionGroup="gml:AbstractObject">
</xsd:element>
<xsd:complexType name="ObservationType">
<xsd:sequence>
<xsd:element name="class">
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

0 comments on commit 5686bad

Please sign in to comment.