Added by jgarnett, last edited by jgarnett on Oct 30, 2006  (view change)

Labels

 
(None)

SAX is an event based system, to use SAX directly GeoTools (actually Rob Hranac) defined a series of ContentHandlers:

  • org.geotools.gml: GMLHandlerJTS, GMLHandlerGeometry, GMLHandlerFeature
  • etc ...

Levels

To make matters exciting the Content Handlers are defined at different "levels" - lets use GML as an example:

  Interface Class Purpose
LEVEL 1 raw sax GMLFilterDocument basic alerts for GML types; to ...
LEVEL 2 GMLHandlerGeometry GMLHandlerGeometry GMLFilterGeometry - translates coordinates and GML events into OGC simple types; for ...
LEVEL 3 GMLHandlerJTS GMLFilterFeature translates JTS elements and attributes into features
LEVEL 4 GMLHandlerFeature your code accepts features

You can see that in order to use literal JTS Geometries the SAX Filter parser will need to implement GMLHandlerJTS:

Example Code with Filter

First of all we need a Handler to catch the result, a Filter Handler

public class SimpleFilterHandler extends DefaultHandler implements FilterHandler {
  private Filter filter;
  public void filter(Filter filter) {
    this.filter = filter;
  }
  public Filter getFilter() {
    return filter;
  }
}

And then we can get down to actual parsing:

public Filter parse(InputSource input) throws IOException, SAXException {
    SimpleFilterHandler simpleFilterHandler = new SimpleFilterHandler();
    FilterFilter filterFilter = new FilterFilter(simpleFilterHandler, null);
    GMLFilterGeometry filterGeometry = new GMLFilterGeometry(filterFilter);
    GMLFilterDocument filterDocument = new GMLFilterDocument(filterGeometry);

    // parse xml
    XMLReader reader = XMLReaderFactory.createXMLReader();
    reader.setContentHandler(filterDocument);
    reader.parse(input);
    
    return simpleFilterHandler.getFilter();
}

Serialization of XMLNS attributes

XML serializers from latest versions of JRE (1.5.0_12 e.g.) ignore attributes with prefix starting from "xmlns:..." if the namespace for this attribute was not specified. XML specification says:

The prefix xmlns is used only to declare namespace bindings and is by definition bound to the namespace name http://www.w3.org/2000/xmlns/. It MUST NOT be declared . Other prefixes MUST NOT be bound to this namespace name, and it MUST NOT be declared as the default namespace. Element names MUST NOT have the prefix xmlns.


So, JRE default XML serializer ignores encoding "xmlns:..." attributes in XML tag when the attribute with prefix "xmlns" itself does not declare the namespace or uses the default.
As a result of this wrong behavior SLDTransformer, for example, generates the following XML output:

...<sld:UserStyle> ...

instead of expected:

...<sld:UserStyle xmlns:sld="http://www.opengis.net/sld"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:gml="http://www.opengis.net/gml"> ...

When we want to print namespace attributes in XML tag (starting from prefix "xmlns") using simple call

org.xml.sax.ContentHandler.startElement (String uri, String localName, String qName, Attributes atts)

(all serializers also implement this interface as XML SAX parsers for uniform access API - to read from XML and to write XML) we should create these "xmlns" attributes with non-empty(non-default) namespace like in this snapshot with SLD namespace:

atts.addAttribute("http://www.w3.org/2000/xmlns/", null, "xmlns:sld", "CDATA", "http://www.opengis.net/sld");

Later on the serializer handles this attribute in a right way and prints it in XML tag as expected.