0

我有一个系统,可以在其中查询 REST / Atom 服务器的文档。查询受 GData 启发,如下所示:

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

我必须解析“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"]

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

我在 Regexp、StringTokenizer、StreamTokenizer 等之间有点迷失了……我被 Java 1.4 困住了,所以没有 Parser……

谁能指出我正确的方向?

谢谢 !

4

1 回答 1

3

正确的方法是使用AntlrJFlexJavaCC等解析器生成器。

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

String[] disjunctedPredicateGroups = query.split("\|");
List<String[]> normalizedPredicates = ArrayList<String[]>;
for (String conjunction : disjunctedPredicateGroups ) {
   normalizedPredicates.add(conjunction.split("\[|\]"));
}
// process each predicate
于 2008-10-14T13:20:08.833 回答