001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.gml2.bindings;
017:
018: import junit.framework.TestCase;
019: import org.eclipse.xsd.XSDAttributeDeclaration;
020: import org.eclipse.xsd.XSDElementDeclaration;
021: import org.eclipse.xsd.XSDFactory;
022: import org.eclipse.xsd.XSDSchema;
023: import org.eclipse.xsd.XSDSimpleTypeDefinition;
024: import org.eclipse.xsd.XSDTypeDefinition;
025: import org.eclipse.xsd.util.XSDSchemaLocationResolver;
026: import org.picocontainer.MutablePicoContainer;
027: import org.picocontainer.defaults.DefaultPicoContainer;
028: import java.util.Iterator;
029: import java.util.List;
030: import javax.xml.namespace.QName;
031: import com.vividsolutions.jts.geom.Coordinate;
032: import com.vividsolutions.jts.geom.CoordinateSequence;
033: import com.vividsolutions.jts.geom.CoordinateSequenceFactory;
034: import com.vividsolutions.jts.geom.impl.CoordinateArraySequenceFactory;
035: import org.geotools.feature.AttributeTypeFactory;
036: import org.geotools.feature.DefaultAttributeTypeFactory;
037: import org.geotools.feature.DefaultFeatureTypeFactory;
038: import org.geotools.feature.Feature;
039: import org.geotools.feature.FeatureType;
040: import org.geotools.feature.FeatureTypeBuilder;
041: import org.geotools.gml2.GMLConfiguration;
042: import org.geotools.xml.AttributeInstance;
043: import org.geotools.xml.Binding;
044: import org.geotools.xml.ElementInstance;
045: import org.geotools.xml.InstanceComponent;
046: import org.geotools.xml.Node;
047: import org.geotools.xml.Schemas;
048: import org.geotools.xml.impl.AttributeImpl;
049: import org.geotools.xml.impl.ElementImpl;
050: import org.geotools.xml.impl.NodeImpl;
051:
052: public class AbstractGMLBindingTest extends TestCase {
053: XSDSchema schema;
054: MutablePicoContainer container;
055: FeatureTypeBuilder ftBuilder;
056: AttributeTypeFactory attFactory;
057:
058: protected void setUp() throws Exception {
059: String loc = GMLConfiguration.class.getResource("feature.xsd")
060: .toString();
061:
062: GMLConfiguration configuration = new GMLConfiguration();
063: schema = configuration.schema();
064: container = new DefaultPicoContainer();
065:
066: configuration.getBindingConfiguration().configure(container);
067:
068: ftBuilder = new DefaultFeatureTypeFactory();
069: attFactory = new DefaultAttributeTypeFactory();
070: }
071:
072: protected void tearDown() throws Exception {
073: container.dispose();
074: }
075:
076: Binding getBinding(QName type) {
077: return (Binding) container.getComponentInstance(type);
078: }
079:
080: Node createNode(InstanceComponent instance,
081: ElementInstance[] elements, Object[] elementValues,
082: AttributeInstance[] attributes, Object[] attValues) {
083: NodeImpl node = new NodeImpl(instance);
084:
085: if ((elements != null) && (elements.length > 0)) {
086: for (int i = 0; i < elements.length; i++) {
087: node.addChild(new NodeImpl(elements[i],
088: elementValues[i]));
089: }
090: }
091:
092: if ((attributes != null) && (attributes.length > 0)) {
093: for (int i = 0; i < attributes.length; i++) {
094: node.addAttribute(new NodeImpl(attributes[i],
095: attValues[i]));
096: }
097: }
098:
099: return node;
100: }
101:
102: AttributeInstance createAtribute(String namespace, String name,
103: QName type, String text) {
104: XSDAttributeDeclaration declaration = XSDFactory.eINSTANCE
105: .createXSDAttributeDeclaration();
106: declaration.setName(name);
107: declaration.setTargetNamespace(namespace);
108: declaration
109: .setTypeDefinition((XSDSimpleTypeDefinition) findTypeDefinition(
110: schema, type));
111:
112: AttributeInstance attribute = new AttributeImpl(declaration);
113: attribute.setName(name);
114: attribute.setNamespace(namespace);
115: attribute.setText(text);
116:
117: return attribute;
118: }
119:
120: ElementInstance createElement(String namespace, String name,
121: QName type, String text) {
122: XSDElementDeclaration declaration = XSDFactory.eINSTANCE
123: .createXSDElementDeclaration();
124: declaration.setName(name);
125: declaration.setTargetNamespace(namespace);
126: declaration.setTypeDefinition(findTypeDefinition(schema, type));
127:
128: ElementInstance element = new ElementImpl(declaration);
129:
130: element.setName(name);
131: element.setNamespace(namespace);
132: element.setText(text);
133:
134: return element;
135: }
136:
137: public CoordinateSequence createCoordinateSequence(Coordinate c) {
138: return createCoordinateSequence(new Coordinate[] { c });
139: }
140:
141: public CoordinateSequence createCoordinateSequence(Coordinate[] c) {
142: CoordinateSequenceFactory csFactory = CoordinateArraySequenceFactory
143: .instance();
144:
145: return csFactory.create(c);
146: }
147:
148: public Feature createFeature(String[] names, Class[] types,
149: Object[] values) {
150: ftBuilder.setName("test");
151:
152: for (int i = 0; i < names.length; i++) {
153: ftBuilder.addType(attFactory.newAttributeType(names[i],
154: values[i].getClass()));
155: }
156:
157: try {
158: FeatureType fType = ftBuilder.getFeatureType();
159:
160: return fType.create(values);
161: } catch (Exception e) {
162: throw new RuntimeException(e);
163: }
164: }
165:
166: XSDTypeDefinition findTypeDefinition(XSDSchema schema, QName type) {
167: List types = schema.getTypeDefinitions();
168:
169: for (Iterator itr = types.iterator(); itr.hasNext();) {
170: XSDTypeDefinition typeDef = (XSDTypeDefinition) itr.next();
171:
172: if (type.getNamespaceURI().equals(
173: typeDef.getTargetNamespace())
174: && type.getLocalPart().equals(typeDef.getName())) {
175: return typeDef;
176: }
177: }
178:
179: return null;
180: }
181: }
|