Use new Modifier class instead of two-element Vector hack.
[cql-java-moved-to-github.git] / src / org / z3950 / zing / cql / ModifierSet.java
1 // $Id: ModifierSet.java,v 1.9 2007-06-27 17:01:38 mike Exp $
2
3 package org.z3950.zing.cql;
4 import java.util.Vector;
5 import java.lang.StringBuffer;
6
7 /**
8  * Represents a base String and a set of Modifiers.
9  * <P>
10  * This class is used as a workhorse delegate by both CQLRelation and
11  * CQLProxNode - two functionally very separate classes that happen to
12  * require similar data structures and functionality.
13  * <P>
14  * A ModifierSet consists of a ``base'' string together with a set of
15  * zero or more <I>type</I> <I>comparison</I> <I>value</I> pairs,
16  * where type, comparison and value are all strings.
17  *
18  * @version $Id: ModifierSet.java,v 1.9 2007-06-27 17:01:38 mike Exp $
19  */
20 public class ModifierSet {
21     String base;
22     Vector<Modifier> modifiers;
23
24     /**
25      * Creates a new ModifierSet with the specified base.
26      */
27     public ModifierSet(String base) {
28         this.base = base;
29         modifiers = new Vector<Modifier>();
30     }
31
32     /**
33      * Returns the base string with which the ModifierSet was created.
34      */
35     public String getBase() {
36         return base;
37     }
38
39     /**
40      * Adds a modifier of the specified <TT>type</TT> and
41      * <TT>value</TT> to a ModifierSet.
42      */
43     public void addModifier(String type, String value) {
44         // ### Need to have comparison passed in
45         Modifier modifier = new Modifier(type, null, value);
46         modifiers.add(modifier);
47     }
48
49     /**
50      * Returns the value of the modifier in the specified ModifierSet
51      * that corresponds to the specified type.
52      */
53     public String modifier(String type) {
54         int n = modifiers.size();
55         for (int i = 0; i < n; i++) {
56             Modifier mod = modifiers.get(i);
57             if (mod.type.equals(type))
58                 return mod.value;
59         }
60         return null;
61     }
62
63     /**
64      * Returns an array of the modifiers in a ModifierSet.
65      * @return
66      *  An array of Modifiers.
67      */
68     public Vector<Modifier> getModifiers() {
69         return modifiers;
70     }
71
72     public String toXCQL(int level, String topLevelElement) {
73         StringBuffer buf = new StringBuffer();
74         buf.append(Utils.indent(level) + "<" + topLevelElement + ">\n");
75         buf.append(Utils.indent(level+1) +
76                    "<value>" + Utils.xq(base) + "</value>\n");
77         if (modifiers.size() > 0) {
78             buf.append(Utils.indent(level+1) + "<modifiers>\n");
79             for (int i = 0; i < modifiers.size(); i++) {
80                 buf.append(modifiers.get(i).toXCQL(level+2, "relation"));
81             }
82             buf.append(Utils.indent(level+1) + "</modifiers>\n");
83         }
84         buf.append(Utils.indent(level) + "</" + topLevelElement + ">\n");
85         return buf.toString();
86     }
87
88     public String toCQL() {
89         StringBuffer buf = new StringBuffer(base);
90         for (int i = 0; i < modifiers.size(); i++) {
91             buf.append("/" + modifiers.get(i).toCQL());
92         }
93
94         return buf.toString();
95     }
96
97     public static void main(String[] args) {
98         if (args.length < 1) {
99             System.err.println("Usage: ModifierSet <base> [<type> <name>]...");
100             System.exit(1);
101         }
102
103         ModifierSet res = new ModifierSet(args[0]);
104         for (int i = 1; i < args.length; i += 2) {
105             res.addModifier(args[i], args[i+1]);
106         }
107
108         System.out.println(res.toCQL());
109     }
110 }