GeoTools : Create a Filter from XML

Say we have a bit of XML for a filter that we want to parse and get a Filter. First, we need a FilterHandler to receive the Filter from the parser. If we don't need to do anything else at that point, something like the following is sufficient:
import org.geotools.filter.Filter;
import org.geotools.filter.FilterHandler;
import org.xml.sax.helpers.DefaultHandler;

public class SimpleFilterHandler extends DefaultHandler implements FilterHandler {

    private Filter filter;

    public void filter(Filter filter) {
        this.filter = filter;

    public Filter getFilter() {
        return filter;


Then, given an InputSource for the XML, our parse code would look like:

"Parse Method"

public Filter parse(InputSource input) throws IOException, SAXException {
    // setup ContentHandler
    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();
    return simpleFilterHandler.getFilter();