2 package org.z3950.zing.cql;
3 import java.util.ArrayList;
7 * Represents a base String and a set of Modifiers.
9 * This class is used as a workhorse delegate by both CQLRelation and
10 * CQLProxNode - two functionally very separate classes that happen to
11 * require similar data structures and functionality.
13 * A ModifierSet consists of a ``base'' string together with a set of
14 * zero or more <I>type</I> <I>comparison</I> <I>value</I> pairs,
15 * where type, comparison and value are all strings.
18 public class ModifierSet {
20 private List<Modifier> modifiers;
23 * Creates a new ModifierSet with the specified base.
25 public ModifierSet(String base) {
27 modifiers = new ArrayList<Modifier>();
31 * Returns the base string with which the ModifierSet was created.
33 public String getBase() {
38 * Adds a modifier of the specified <TT>type</TT>,
39 * <TT>comparison</TT> and <TT>value</TT> to a ModifierSet.
41 public void addModifier(String type, String comparison, String value) {
42 Modifier modifier = new Modifier(type, comparison, value);
43 modifiers.add(modifier);
47 * Adds a modifier of the specified <TT>type</TT>, but with no
48 * <TT>comparison</TT> and <TT>value</TT>, to a ModifierSet.
50 public void addModifier(String type) {
51 Modifier modifier = new Modifier(type);
52 modifiers.add(modifier);
56 * Returns the value of the modifier in the specified ModifierSet
57 * that corresponds to the specified type.
59 public String modifier(String type) {
60 int n = modifiers.size();
61 for (int i = 0; i < n; i++) {
62 Modifier mod = modifiers.get(i);
63 if (mod.type.equals(type))
70 * Returns an array of the modifiers in a ModifierSet.
72 * An array of Modifiers.
74 public List<Modifier> getModifiers() {
78 void toXCQLInternal(XCQLBuilder b, int level,
79 String topLevelElement, String valueElement) {
80 b.indent(level).append("<").append(topLevelElement).
81 append(">\n").indent(level + 1).append("<").
82 append(valueElement).append(">").xq(base).append("</").
83 append(valueElement).append(">\n");
84 if (modifiers.size() > 0) {
85 b.indent(level + 1).append("<modifiers>\n");
86 for (int i = 0; i < modifiers.size(); i++) {
87 modifiers.get(i).toXCQLInternal(b, level+2, "comparison");
89 b.indent(level + 1).append("</modifiers>\n");
91 b.indent(level).append("</").append(topLevelElement).append(">\n");
94 public String toCQL() {
95 StringBuilder buf = new StringBuilder(base);
96 for (int i = 0; i < modifiers.size(); i++) {
97 buf.append("/").append(modifiers.get(i).toCQL());
100 return buf.toString();
103 public static void main(String[] args) {
104 if (args.length < 1) {
105 System.err.println("Usage: ModifierSet <base> [<type> <comparison> <name>]...");
109 ModifierSet res = new ModifierSet(args[0]);
110 for (int i = 1; i < args.length; i += 3) {
111 res.addModifier(args[i], args[i+1], args[i+2]);
114 System.out.println(res.toCQL());