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.opengis.filter.Filter;
13: import org.xml.sax.InputSource;
14: import org.xml.sax.SAXException;
15: import org.xml.sax.XMLReader;
16: import org.xml.sax.helpers.DefaultHandler;
17: import org.xml.sax.helpers.XMLReaderFactory;
18:
19: public class SAXExample {
20: static String xml = "<Filter xmlns:gml=\"http://www.opengis.net/gml\">"
21: + " <Overlaps>"
22: + " <PropertyName>testGeometry</PropertyName>"
23: + "<gml:Polygon srsName=\"http://www.opengis.net/gml/srs/EPSG#4326\">"
24: + "<gml:outerBoundaryIs>"
25: + "<gml:LinearRing>"
26: + "<gml:coordinates>0,0 0,10 10,10 10,0 0,0</gml:coordinates>"
27: + "</gml:LinearRing>"
28: + "</gml:outerBoundaryIs>"
29: + "</gml:Polygon>" + " </Overlaps>" + "</Filter>";
30:
31: public static void main(String args[]) throws Exception {
32: StringReader reader = new StringReader(xml);
33: InputSource input = new InputSource(reader);
34:
35: Filter filter = parse(input);
36: System.out.println("got:" + filter);
37: }
38:
39: public static Filter parse(InputSource input) throws IOException,
40: SAXException {
41: SimpleFilterHandler simpleFilterHandler = new SimpleFilterHandler();
42: FilterFilter filterFilter = new FilterFilter(
43: simpleFilterHandler, null);
44: GMLFilterGeometry filterGeometry = new GMLFilterGeometry(
45: filterFilter);
46: GMLFilterDocument filterDocument = new GMLFilterDocument(
47: filterGeometry);
48:
49: // parse xml
50: XMLReader reader = XMLReaderFactory.createXMLReader();
51: reader.setContentHandler(filterDocument);
52: reader.parse(input);
53:
54: return simpleFilterHandler.getFilter();
55: }
56:
57: /**
58: * This class is called when the SAX parser has finished
59: * parsing a Filter.
60: */
61: static class SimpleFilterHandler extends DefaultHandler implements
62: FilterHandler {
63: private Filter filter;
64:
65: public void filter(Filter filter) {
66: this .filter = filter;
67: }
68:
69: public Filter getFilter() {
70: return filter;
71: }
72: }
73: }
|