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.xs;
017:
018: import java.io.BufferedWriter;
019: import java.io.File;
020: import java.io.FileWriter;
021: import java.io.IOException;
022: import java.io.PrintWriter;
023: import java.lang.reflect.Field;
024: import java.net.URL;
025: import java.util.Map;
026:
027: import javax.xml.namespace.QName;
028:
029: import junit.framework.TestCase;
030:
031: import org.eclipse.xsd.XSDElementDeclaration;
032: import org.eclipse.xsd.XSDFactory;
033: import org.eclipse.xsd.XSDSchema;
034: import org.eclipse.xsd.XSDSimpleTypeDefinition;
035: import org.eclipse.xsd.util.XSDParser;
036: import org.geotools.xml.Binding;
037: import org.geotools.xml.ElementInstance;
038: import org.geotools.xml.Schemas;
039: import org.geotools.xml.SimpleBinding;
040: import org.geotools.xml.impl.BindingLoader;
041: import org.geotools.xml.impl.ElementImpl;
042: import org.geotools.xs.bindings.XS;
043: import org.geotools.xs.bindings.XSBindingConfiguration;
044: import org.picocontainer.defaults.DefaultPicoContainer;
045:
046: public abstract class TestSchema extends TestCase {
047: public static URL url;
048: public static XSDSchema schema;
049: public static XSDSchema xsd;
050: public static BindingLoader factory;
051:
052: static {
053: url = TestSchema.class.getResource("sample.xsd");
054:
055: try {
056: schema = Schemas.parse(url.toString());
057: } catch (IOException e) {
058: throw new RuntimeException(e);
059: }
060:
061: xsd = schema.getSchemaForSchema();
062: factory = new BindingLoader();
063: new XSBindingConfiguration().configure(factory.getContainer());
064: }
065:
066: protected XSDSimpleTypeDefinition typeDef;
067: protected SimpleBinding strategy;
068: protected QName qname;
069:
070: public TestSchema() {
071: }
072:
073: public TestSchema(String name) {
074: super (name);
075: }
076:
077: /**
078: * Limited to a search of simple types, no QName required.
079: *
080: * @param name
081: * @return XSDSimpleTypeDefinition
082: */
083: public XSDSimpleTypeDefinition xsdSimple(String name) {
084: Map simpleTypes = xsd.getSimpleTypeIdMap();
085:
086: //System.out.println( simpleTypes );
087: return (XSDSimpleTypeDefinition) simpleTypes.get(name);
088: }
089:
090: public QName xs(String name) throws Exception {
091: Class xs = XS.class;
092: Field[] fields = xs.getFields();
093:
094: for (int i = 0; i < fields.length; i++) {
095: Field field = fields[i];
096:
097: if (field.getName().equalsIgnoreCase(name)) {
098: return (QName) field.get(null);
099: }
100: }
101:
102: throw new IllegalArgumentException(name);
103: }
104:
105: public Binding stratagy(QName qname) throws Exception {
106: return factory.loadBinding(qname, new DefaultPicoContainer());
107: }
108:
109: public Binding stratagy(String name) throws Exception {
110: return factory
111: .loadBinding(xs(name), new DefaultPicoContainer());
112: }
113:
114: public ElementInstance element(String text, QName qname) {
115: // create a fake element declaration and element instance
116: XSDElementDeclaration declaration = XSDFactory.eINSTANCE
117: .createXSDElementDeclaration();
118: declaration.setTypeDefinition(xsdSimple(qname.getLocalPart()));
119:
120: ElementInstance element = new ElementImpl(declaration);
121: element.setText(text);
122:
123: return element;
124: }
125:
126: public ElementInstance element(String text, QName original,
127: String name) {
128: try {
129: File temp = File.createTempFile("name", "xsd");
130: FileWriter file = new FileWriter(temp);
131: BufferedWriter buff = new BufferedWriter(file);
132: PrintWriter print = new PrintWriter(buff);
133: print
134: .println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
135: + " <xsd:schema xmlns:my=\"http://mails/refractions/net\""
136: + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
137: + " targetNamespace=\"http://localhost//test\">"
138: + " <xsd:element name=\""
139: + name
140: + "\" type=\"xsd:"
141: + original.getLocalPart()
142: + "\"/>"
143: + "</xsd:schema>");
144:
145: URL url = temp.toURL();
146: XSDParser parser = new XSDParser();
147: parser.parse(url.toString());
148:
149: XSDSchema schema = parser.getSchema();
150: Map map = schema.getSimpleTypeIdMap();
151:
152: return (ElementInstance) map.get(name);
153: } catch (Throwable t) {
154: t.printStackTrace();
155:
156: return null;
157: }
158: }
159:
160: /**
161: * Will call the parse method on the strategy object, passing it
162: * <code>given</code> to use as a value. It will then perform
163: * <code>assertEquals(expected, result);</code>
164: *
165: * @param given the value to pass to the parse method
166: * @param expected used to compare against the result of the parse method
167: * @throws Exception
168: */
169: public void validateValues(String given, Object expected)
170: throws Exception {
171: Object result = strategy.parse(element(given, qname), given);
172: assertEquals(expected, result);
173: }
174:
175: /**
176: * Each subclass must indicate which kind of QName they wish to operate against.
177: */
178: protected abstract QName getQName();
179:
180: protected void setUp() throws Exception {
181: // TODO Auto-generated method stub
182: super .setUp();
183:
184: qname = getQName();
185:
186: if (qname != null) {
187: typeDef = xsdSimple(qname.getLocalPart());
188: strategy = (SimpleBinding) stratagy(qname);
189: }
190: }
191:
192: public void testSetUp() {
193: if (getQName() != null) {
194: assertNotNull("XSD typedef", typeDef);
195: assertNotNull(strategy);
196: }
197: }
198: }
|