-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathModifierSet.java
More file actions
120 lines (106 loc) · 3.31 KB
/
ModifierSet.java
File metadata and controls
120 lines (106 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package org.z3950.zing.cql;
import java.util.ArrayList;
import java.util.List;
/**
* Represents a base String and a set of Modifiers.
* <P>
* This class is used as a workhorse delegate by both CQLRelation and
* CQLProxNode - two functionally very separate classes that happen to
* require similar data structures and functionality.
* </P>
* <P>
* A ModifierSet consists of a ``base'' string together with a set of
* zero or more <I>type</I> <I>comparison</I> <I>value</I> pairs,
* where type, comparison and value are all strings.
* </P>
*
*/
public class ModifierSet {
private String base;
private List<Modifier> modifiers;
/**
* Creates a new ModifierSet with the specified base.
*/
public ModifierSet(String base) {
this.base = base;
modifiers = new ArrayList<Modifier>();
}
/**
* Returns the base string with which the ModifierSet was created.
*/
public String getBase() {
return base;
}
/**
* Adds a modifier of the specified <code>type</code>,
* <code>comparison</code> and <code>value</code> to a ModifierSet.
*/
public Modifier addModifier(String type, String comparison, String value) {
Modifier modifier = new Modifier(type, comparison, value);
modifiers.add(modifier);
return modifier;
}
/**
* Adds a modifier of the specified <code>type</code>, but with no
* <code>comparison</code> and <code>value</code>, to a ModifierSet.
*/
public Modifier addModifier(String type) {
Modifier modifier = new Modifier(type);
modifiers.add(modifier);
return modifier;
}
/**
* Returns the value of the modifier in the specified ModifierSet
* that corresponds to the specified type.
*/
public String modifier(String type) {
int n = modifiers.size();
for (int i = 0; i < n; i++) {
Modifier mod = modifiers.get(i);
if (mod.type.equals(type))
return mod.value;
}
return null;
}
/**
* Returns an array of the modifiers in a ModifierSet.
* @return
* An array of Modifiers.
*/
public List<Modifier> getModifiers() {
return modifiers;
}
void toXCQLInternal(XCQLBuilder b, int level,
String topLevelElement, String valueElement) {
b.indent(level).append("<").append(topLevelElement).
append(">\n").indent(level + 1).append("<").
append(valueElement).append(">").xq(base).append("</").
append(valueElement).append(">\n");
if (modifiers.size() > 0) {
b.indent(level + 1).append("<modifiers>\n");
for (int i = 0; i < modifiers.size(); i++) {
modifiers.get(i).toXCQLInternal(b, level+2, "comparison");
}
b.indent(level + 1).append("</modifiers>\n");
}
b.indent(level).append("</").append(topLevelElement).append(">\n");
}
public String toCQL() {
StringBuilder buf = new StringBuilder(base);
for (int i = 0; i < modifiers.size(); i++) {
buf.append("/").append(modifiers.get(i).toCQL());
}
return buf.toString();
}
public static void main(String[] args) {
if (args.length < 1) {
System.err.println("Usage: ModifierSet <base> [<type> <comparison> <name>]...");
System.exit(1);
}
ModifierSet res = new ModifierSet(args[0]);
for (int i = 1; i < args.length; i += 3) {
res.addModifier(args[i], args[i+1], args[i+2]);
}
System.out.println(res.toCQL());
}
}