forked from geotools/geotools
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request geotools#760 from mbarto/geot5037_xsanytype
[GEOT-5037] Fixing Complex xs:anyType handling for simple types
- Loading branch information
Showing
4 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
...extension/xsd/xsd-gml3/src/test/java/org/geotools/gml3/bindings/XSAnyTypeBindingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
modules/extension/xsd/xsd-gml3/src/test/resources/org/geotools/gml3/bindings/AnyTypeTest.xsd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |