如何用Java解析基本查询(即Google数据)?

时间:2021-07-28 14:52:51

I have a system where I query a REST / Atom server for documents. The queries are inspired by GData and look like :

我有一个系统,我在其中查询REST / Atom服务器的文档。查询的灵感来自GData,如下所示:

http://server/base/feeds/documents?bq=[type in {'news'}]

I have to parse the "bq" parameter to know which type of documents will be returned without actually doing the query. So for example,

我必须解析“bq”参数以了解将返回哪种类型的文档而不实际执行查询。例如,

bq=[type = 'news']                      ->  return ["news"]
bq=[type in {'news'}]                   ->  return ["news"]
bq=[type in {'news', 'article'}]        ->  return ["news", "article"]
bq=[type = 'news']|[type = 'article']   ->  return ["news", "article"]
bq=[type = 'news']|[title = 'My Title'] ->  return ["news"]

Basically, the query language is a list of predicate that can be combined with OR ("|") or AND (no separator). Each predicate is constraint on a field. The constraint can be =, <, >, <=, >=, in, etc... There can be spaces everywhere where it make sense.

基本上,查询语言是可以与OR(“|”)或AND(无分隔符)组合的谓词列表。每个谓词都是对字段的约束。约束可以是=,<,>,<=,> =,in等...在任何地方都可以有空格。

I'm a bit lost between Regexp, StringTokenizer, StreamTokenizer, etc... and I am stuck with Java 1.4, so no Parser ...

我在Regexp,StringTokenizer,StreamTokenizer等之间有点迷失......而且我坚持使用Java 1.4,所以没有Parser ......

Who can point me in the right direction ?

谁能指出我正确的方向?

Thanks !

1 个解决方案

#1


3  

The right way would be to use parser generator like Antlr, JFlex or JavaCC.

正确的方法是使用像Antlr,JFlex或JavaCC这样的解析器生成器。

A quick and dirty way would be:

一种快速而肮脏的方式是:

String[] disjunctedPredicateGroups = query.split("\|");
List<String[]> normalizedPredicates = ArrayList<String[]>;
for (String conjunction : disjunctedPredicateGroups ) {
   normalizedPredicates.add(conjunction.split("\[|\]"));
}
// process each predicate

#1


3  

The right way would be to use parser generator like Antlr, JFlex or JavaCC.

正确的方法是使用像Antlr,JFlex或JavaCC这样的解析器生成器。

A quick and dirty way would be:

一种快速而肮脏的方式是:

String[] disjunctedPredicateGroups = query.split("\|");
List<String[]> normalizedPredicates = ArrayList<String[]>;
for (String conjunction : disjunctedPredicateGroups ) {
   normalizedPredicates.add(conjunction.split("\[|\]"));
}
// process each predicate