001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-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; either
009: * version 2.1 of the License, or (at your option) any later version.
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.xml;
017:
018: import java.io.File;
019: import java.net.URI;
020: import java.util.HashMap;
021: import java.util.logging.Level;
022:
023: import javax.xml.parsers.SAXParser;
024: import javax.xml.parsers.SAXParserFactory;
025:
026: import junit.framework.TestCase;
027:
028: import org.geotools.TestData;
029: import org.geotools.feature.Feature;
030: import org.geotools.feature.FeatureCollection;
031: import org.geotools.feature.FeatureIterator;
032: import org.geotools.xml.gml.GMLFeatureCollection;
033: import org.geotools.xml.gml.GMLSchema;
034: import org.geotools.xml.schema.Schema;
035: import org.xml.sax.SAXException;
036:
037: /**
038: * <p>
039: * DOCUMENT ME!
040: * </p>
041: * @
042: *
043: * @author dzwiers www.refractions.net
044: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/xml/src/test/java/org/geotools/xml/GMLParserTest.java $
045: */
046: public class GMLParserTest extends TestCase {
047: public void testSchema() {
048: Schema s = SchemaFactory.getInstance(GMLSchema.NAMESPACE);
049: assertNotNull(s);
050: }
051:
052: public void testParseEmptyCollectionFeatures() throws Exception {
053: SAXParserFactory spf = SAXParserFactory.newInstance();
054: spf.setNamespaceAware(true);
055: spf.setValidating(false);
056:
057: SAXParser parser = spf.newSAXParser();
058:
059: String path = "xml/fme/empty-collection/lakes.xml";
060: File f = TestData.copy(this , path);
061: TestData.copy(this , "xml/fme/empty-collection/lakes.xsd");
062: URI u = f.toURI();
063:
064: XMLSAXHandler xmlContentHandler = new XMLSAXHandler(u, null);
065: XMLSAXHandler.setLogLevel(Level.WARNING);
066: XSISAXHandler.setLogLevel(Level.WARNING);
067: XMLElementHandler.setLogLevel(Level.WARNING);
068: XSIElementHandler.setLogLevel(Level.WARNING);
069:
070: parser.parse(f, xmlContentHandler);
071:
072: Object doc = xmlContentHandler.getDocument();
073: assertNotNull("Document missing", doc);
074:
075: FeatureCollection collection = (FeatureCollection) doc;
076: assertEquals(0, collection.size());
077:
078: }
079:
080: public void skippedtestOneFeature() throws Exception {
081: SAXParserFactory spf = SAXParserFactory.newInstance();
082: spf.setNamespaceAware(true);
083: spf.setValidating(false);
084:
085: SAXParser parser = spf.newSAXParser();
086:
087: String path = "xml/geoserver/oneFeature.xml";
088: File f = TestData.copy(this , path);
089: TestData.copy(this , "xml/geoserver/roadSchema.xsd");
090: TestData.copy(this , "xml/wfs/WFS-basic.xsd");
091: URI u = f.toURI();
092:
093: XMLSAXHandler xmlContentHandler = new XMLSAXHandler(u, null);
094: XMLSAXHandler.setLogLevel(Level.FINEST);
095: XSISAXHandler.setLogLevel(Level.FINEST);
096: XMLElementHandler.setLogLevel(Level.FINEST);
097: XSIElementHandler.setLogLevel(Level.FINEST);
098:
099: parser.parse(f, xmlContentHandler);
100:
101: Object doc = xmlContentHandler.getDocument();
102: assertNotNull("Document missing", doc);
103: // System.out.println(doc);
104:
105: checkFeatureCollection((FeatureCollection) doc);
106:
107: }
108:
109: public void skippedtestMoreFeatures() {
110: try {
111: SAXParserFactory spf = SAXParserFactory.newInstance();
112: spf.setNamespaceAware(true);
113: spf.setValidating(false);
114:
115: SAXParser parser = spf.newSAXParser();
116:
117: String path = "xml/geoserver/roads.xml";
118: File f = TestData.copy(this , path);
119: TestData.copy(this , "xml/geoserver/roadSchema.xsd");
120: TestData.copy(this , "xml/wfs/WFS-basic.xsd");
121: URI u = f.toURI();
122:
123: XMLSAXHandler xmlContentHandler = new XMLSAXHandler(u, null);
124: XMLSAXHandler.setLogLevel(Level.WARNING);
125: XSISAXHandler.setLogLevel(Level.WARNING);
126: XMLElementHandler.setLogLevel(Level.WARNING);
127: XSIElementHandler.setLogLevel(Level.WARNING);
128:
129: parser.parse(f, xmlContentHandler);
130:
131: Object doc = xmlContentHandler.getDocument();
132: assertNotNull("Document missing", doc);
133: // System.out.println(doc);
134:
135: checkFeatureCollection((FeatureCollection) doc);
136:
137: } catch (Throwable e) {
138: e.printStackTrace();
139: fail(e.toString());
140: }
141: }
142:
143: public void testFMERoadsFeatures() {
144: try {
145: SAXParserFactory spf = SAXParserFactory.newInstance();
146: spf.setNamespaceAware(true);
147: spf.setValidating(false);
148:
149: SAXParser parser = spf.newSAXParser();
150:
151: String path = "xml/fme/roads/roads.xml";
152: File f = TestData.copy(this , path);
153: TestData.copy(this , "xml/fme/roads/roads.xsd");
154: URI u = f.toURI();
155:
156: XMLSAXHandler xmlContentHandler = new XMLSAXHandler(u, null);
157: XMLSAXHandler.setLogLevel(Level.WARNING);
158: XSISAXHandler.setLogLevel(Level.WARNING);
159: XMLElementHandler.setLogLevel(Level.WARNING);
160: XSIElementHandler.setLogLevel(Level.WARNING);
161:
162: parser.parse(f, xmlContentHandler);
163:
164: Object doc = xmlContentHandler.getDocument();
165: assertNotNull("Document missing", doc);
166: // System.out.println(doc);
167:
168: checkFeatureCollection((FeatureCollection) doc);
169:
170: } catch (Throwable e) {
171: e.printStackTrace();
172: fail(e.toString());
173: }
174: }
175:
176: public void testFMELakesFeatures() {
177: try {
178: SAXParserFactory spf = SAXParserFactory.newInstance();
179: spf.setNamespaceAware(true);
180: spf.setValidating(false);
181:
182: SAXParser parser = spf.newSAXParser();
183:
184: String path = "xml/fme/lakes/lakes.xml";
185: File f = TestData.copy(this , path);
186: TestData.copy(this , "xml/fme/lakes/lakes.xsd");
187: URI u = f.toURI();
188:
189: XMLSAXHandler xmlContentHandler = new XMLSAXHandler(u, null);
190: XMLSAXHandler.setLogLevel(Level.WARNING);
191: XSISAXHandler.setLogLevel(Level.WARNING);
192: XMLElementHandler.setLogLevel(Level.WARNING);
193: XSIElementHandler.setLogLevel(Level.WARNING);
194:
195: parser.parse(f, xmlContentHandler);
196:
197: Object doc = xmlContentHandler.getDocument();
198: assertNotNull("Document missing", doc);
199: // System.out.println(doc);
200:
201: checkFeatureCollection((FeatureCollection) doc);
202:
203: } catch (Throwable e) {
204: e.printStackTrace();
205: fail(e.toString());
206: }
207: }
208:
209: private void checkFeatureCollection(FeatureCollection doc) {
210:
211: //remaining slot (s) should be feature(s)
212: assertTrue("Requires atleast one feature", doc.size() > 0); //bbox + feature
213: FeatureIterator i = doc.features();
214: int j = 1;
215: while (i.hasNext()) {
216: Feature ft = i.next();
217: assertNotNull("Feature #" + j + " is null", ft);
218: // assertNotNull("Feature #"+j+" missing crs ",ft.getFeatureType().getDefaultGeometry().getCoordinateSystem());
219: // System.out.println("Feature "+j+" : "+ft);
220: j++;
221: }
222: System.out.println("Found " + j + " Features");
223: }
224:
225: public void skippedtestOneFeatureWrite() {
226:
227: try {
228: String path = "xml/geoserver/oneFeature.xml";
229: File f = TestData.copy(this , path);
230: TestData.copy(this , "xml/geoserver/roadSchema.xsd");
231: TestData.copy(this , "xml/wfs/WFS-basic.xsd");
232:
233: GMLFeatureCollection doc = (GMLFeatureCollection) DocumentFactory
234: .getInstance(f.toURI(), null, Level.WARNING);
235: assertNotNull("Document missing", doc);
236:
237: Schema s = SchemaFactory.getInstance(new URI(
238: "http://www.openplans.org/topp"));
239:
240: path = "oneFeature_out.xml";
241: f = new File(f.getParentFile(), path);
242: if (f.exists())
243: f.delete();
244: f.createNewFile();
245:
246: assertNotNull("Bounds exists", doc.getBounds());
247: DocumentWriter.writeDocument(doc, s, f, null);
248:
249: // doc = (GMLFeatureCollection)DocumentFactory.getInstance(f.toURI(),null,Level.WARNING);
250: // assertNotNull("New Document missing", doc);
251: //
252: // assertTrue("file was not created +f",f.exists());
253: System.out.println(f);
254: } catch (SAXException e) {
255: e.printStackTrace();
256: fail(e.toString());
257: } catch (Throwable e) {
258: e.printStackTrace();
259: fail(e.toString());
260: }
261: }
262:
263: public void skippedtestOneFeatureWriteWithHints() {
264:
265: try {
266: String path = "xml/geoserver/oneFeature.xml";
267:
268: File f = TestData.copy(this , path);
269: TestData.copy(this , "xml/geoserver/roadSchema.xsd");
270: TestData.copy(this , "xml/wfs/WFS-basic.xsd");
271:
272: GMLFeatureCollection doc = (GMLFeatureCollection) DocumentFactory
273: .getInstance(f.toURI(), null, Level.WARNING);
274: assertNotNull("Document missing", doc);
275:
276: Schema s = SchemaFactory.getInstance(new URI(
277: "http://www.openplans.org/topp"));
278:
279: path = "oneFeature_out_hints.xml";
280: f = new File(f.getParentFile(), path);
281: if (f.exists())
282: f.delete();
283: f.createNewFile();
284:
285: HashMap hints = new HashMap();
286: hints.put(DocumentWriter.SCHEMA_ORDER, new String[] {
287: "http://www.opengis.net/wfs",
288: "http://www.openplans.org/topp" });
289: assertNotNull("Bounds exists", doc.getBounds());
290: DocumentWriter.writeDocument(doc, s, f, hints);
291:
292: // doc = (GMLFeatureCollection)DocumentFactory.getInstance(f.toURI(),null,Level.WARNING);
293: // assertNotNull("New Document missing", doc);
294: //
295: // assertTrue("file was not created +f",f.exists());
296: System.out.println(f);
297: } catch (SAXException e) {
298: e.printStackTrace();
299: fail(e.toString());
300: } catch (Throwable e) {
301: e.printStackTrace();
302: fail(e.toString());
303: }
304: }
305:
306: public void testProblemFeatures() {
307: try {
308: SAXParserFactory spf = SAXParserFactory.newInstance();
309: spf.setNamespaceAware(true);
310: spf.setValidating(false);
311:
312: SAXParser parser = spf.newSAXParser();
313:
314: String path = "xml/iba-gml-bad.xml";
315: File f = TestData.copy(this , path);
316: URI u = f.toURI();
317:
318: XMLSAXHandler xmlContentHandler = new XMLSAXHandler(u, null);
319: XMLSAXHandler.setLogLevel(Level.WARNING);
320: XSISAXHandler.setLogLevel(Level.WARNING);
321: XMLElementHandler.setLogLevel(Level.WARNING);
322: XSIElementHandler.setLogLevel(Level.WARNING);
323:
324: parser.parse(f, xmlContentHandler);
325:
326: Object doc = xmlContentHandler.getDocument();
327: assertNotNull("Document missing", doc);
328: // System.out.println(doc);
329:
330: checkFeatureCollection((FeatureCollection) doc);
331: fail("Didn't catch an exception :(");
332: } catch (Throwable e) {
333: // e.printStackTrace();
334: }
335: }
336: }
|