Arnab Biswas
Arnab Biswas

Reputation: 4605

Parsing "SQL like" query strings

For the web services interface of our product we are defining sql like query strings. They don't follow the exact sql syntax. It works like this. The strings are present in the xml requests. My application is parsing the string and creating Java object based on the content. Those java objects are used to query the DB. Here are few examples of the query strings:

 objectType==device && deviceType==mobile && returnType==full
 objectType==device && deviceType==computer && deviceState==connected && returnType==basic
 objectType==networkEntity && namePattern==*.nw && networkEntityType==SGSN

The keys (objectType for example) and the values (device/networkEntity) are generally enumerated. So the expectation from the parsing mechanism are:

  1. If any unknown element(key/value) is there in the query String, it should fail.
  2. The elements (key/value) should appear in a defined sequence (This is just to simplify the parsing logic)
  3. In future, in addition to "==" and "&&", other operations may also get introduced.
  4. There can be different combinations of the key/values resulting in large number of unique query strings.

Right now I am using a combination of String.split and Scanner to parse the String. But, I am finding that code is becoming complex and harder to debug. Till now, I have not introduced any major validations. With that I am expecting the code to become even more complex and "ugly".

[Question] Is there any library which can help me in parsing such strings. Any other suggestions/thoughts will also be appreciated.

Upvotes: 3

Views: 2187

Answers (1)

Petr
Petr

Reputation: 63349

There are several Java libraries for parsing input data into a tree of objects. Notably:

  • JParsec - parser combinator framework (tutorial).
  • ANTLR (ANother Tool for Language Recognition) - a language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions (tutorial).
  • JavaCC - (tutorial).

It depends on your preferences and background which one to use. JParsec constructs parsers in native Java language only (no external grammar files etc), the other two generate parsers from a grammar description file.

Using such a parser library might seem to be a bit scary at first, but it's not that difficult and it will save you a lot of troubles debugging and maintaining your own parser. And if you later need to improve the language (add new operators, operator precedence, parentheses, etc.), it will be very easy.

See also Yacc equivalent for Java

Upvotes: 5

Related Questions