01: package org.geotools.demo.xml;
02:
03: import java.io.IOException;
04: import java.io.InputStream;
05: import java.io.StringBufferInputStream;
06: import java.io.StringReader;
07:
08: import org.geotools.filter.FilterFilter;
09: import org.geotools.filter.FilterHandler;
10: import org.geotools.gml.GMLFilterDocument;
11: import org.geotools.gml.GMLFilterGeometry;
12: import org.geotools.gml.GMLHandlerGeometry;
13: import org.geotools.gml.GMLHandlerJTS;
14: import org.opengis.filter.Filter;
15: import org.xml.sax.Attributes;
16: import org.xml.sax.ContentHandler;
17: import org.xml.sax.InputSource;
18: import org.xml.sax.SAXException;
19: import org.xml.sax.XMLReader;
20: import org.xml.sax.helpers.DefaultHandler;
21: import org.xml.sax.helpers.XMLReaderFactory;
22:
23: import com.vividsolutions.jts.geom.Geometry;
24:
25: public class SAXExample2 {
26: static String xml = "<Example xmlns:gml=\"http://www.opengis.net/gml\">"
27: + "<gml:Polygon srsName=\"http://www.opengis.net/gml/srs/EPSG#4326\">"
28: + "<gml:outerBoundaryIs>"
29: + "<gml:LinearRing>"
30: + "<gml:coordinates>0,0 0,10 10,10 10,0 0,0</gml:coordinates>"
31: + "</gml:LinearRing>"
32: + "</gml:outerBoundaryIs>"
33: + "</gml:Polygon>" + "</Example>";
34:
35: public static void main(String args[]) throws Exception {
36: StringReader reader = new StringReader(xml);
37: InputSource input = new InputSource(reader);
38:
39: Callback result = parse(input);
40: System.out.println("got:" + result.getGeometry());
41: }
42:
43: public static Callback parse(InputSource input) throws IOException,
44: SAXException {
45:
46: // parse xml
47: XMLReader reader = XMLReaderFactory.createXMLReader();
48:
49: Callback callback = new Callback();
50: GMLFilterGeometry geometryCallback = new GMLFilterGeometry(
51: callback);
52: GMLFilterDocument gmlCallback = new GMLFilterDocument(
53: geometryCallback);
54: reader.setContentHandler(gmlCallback);
55: reader.parse(input);
56:
57: return callback;
58: }
59:
60: /**
61: * This class is called when the SAX parser has finished
62: * parsing a Filter.
63: */
64: static class Callback extends DefaultHandler implements
65: GMLHandlerJTS {
66: Geometry geometry = null;
67:
68: public void geometry(Geometry geometry) {
69: this .geometry = geometry;
70: }
71:
72: public Geometry getGeometry() {
73: return geometry;
74: }
75: }
76: }
|