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:
- If any unknown element(key/value) is there in the query String, it should fail.
- The elements (key/value) should appear in a defined sequence (This is just to simplify the parsing logic)
- In future, in addition to "==" and "&&", other operations may also get introduced.
- 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.