Sometimes we want to build a complex filter quickly, not in XML and not by constructing it object by object using factories. Fortunatly GeoTools includes a parser that will turn simple text into complete filters.
As you can see you need two classes to parse strings, the parser itself and a specific excpeption. There are a few things to note:
- First - The result of the parse method returns either a Filter or an Expression so you need to cast it appropriatly.
- Second - The building needs to be wraped in a try/catch block in case you put in a un-parsable expression. Note that the message obtained from the ParseException will let you know what was wrong with your filter or expression.
OK, here are some more examples...
Note that no schema is specified in the above example, this means that if the attribute 'testInteger' does not exist in the evaluated feture getValue will throw an exception. If you would rather know that your expression is valid in advance then use the parse(FeatureType type, String string) instead.
Now for some actual filters:
The parser understands both square  and round brackets (), by convention the square brackets are used to structure logic filters. Also note that standard Java logic notation can be used, i.e. || && instead of the written out form.