1
|
package eu.dnetlib.functionality.cql;
|
2
|
|
3
|
import java.util.Set;
|
4
|
|
5
|
import org.apache.commons.lang3.StringUtils;
|
6
|
import org.z3950.zing.cql.CQLBooleanNode;
|
7
|
import org.z3950.zing.cql.CQLNode;
|
8
|
import org.z3950.zing.cql.CQLOrNode;
|
9
|
import org.z3950.zing.cql.CQLParser;
|
10
|
import org.z3950.zing.cql.CQLTermNode;
|
11
|
import org.z3950.zing.cql.ModifierSet;
|
12
|
|
13
|
public class CQLExpander {
|
14
|
|
15
|
private static final String OP = "or";
|
16
|
|
17
|
private static final String _OP_ = " " + OP + " ";
|
18
|
|
19
|
public CQLNode expand(CQLNode node, Set<String> fields) {
|
20
|
return doExpand(node, fields);
|
21
|
}
|
22
|
|
23
|
private static CQLNode doExpand(CQLNode node, Set<String> fields) {
|
24
|
|
25
|
if (node instanceof CQLBooleanNode) {
|
26
|
return doExpand((CQLBooleanNode) node, fields);
|
27
|
}
|
28
|
|
29
|
if (node instanceof CQLTermNode) {
|
30
|
return doExpand((CQLTermNode) node, fields);
|
31
|
}
|
32
|
|
33
|
// if (node == null)
|
34
|
// return terms;
|
35
|
|
36
|
throw new RuntimeException("error choice");
|
37
|
}
|
38
|
|
39
|
private static CQLNode doExpand(CQLBooleanNode node, Set<String> fields) {
|
40
|
|
41
|
CQLNode left = doExpand(node.left, fields);
|
42
|
CQLNode right = doExpand(node.right, fields);
|
43
|
|
44
|
return new CQLOrNode(left, right, new ModifierSet("or"));
|
45
|
}
|
46
|
|
47
|
private static CQLNode doExpand(CQLTermNode node, Set<String> fields) {
|
48
|
String expand = "";
|
49
|
for (String field : fields) {
|
50
|
expand += field + "=" + node.getTerm() + _OP_;
|
51
|
}
|
52
|
|
53
|
expand = StringUtils.removeEnd(expand, _OP_);
|
54
|
try {
|
55
|
return new CQLParser().parse(expand);
|
56
|
} catch (Exception e) {
|
57
|
throw new RuntimeException("unable to parse: " + expand);
|
58
|
}
|
59
|
}
|
60
|
|
61
|
}
|