Reputation: 4605
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:
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
Reputation: 63349
There are several Java libraries for parsing input data into a tree of objects. Notably:
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