Fix a silly bug, recently introduced, which prevented prox modifiers
[cql-java-moved-to-github.git] / src / org / z3950 / zing / cql / ModifierSet.java
1 // $Id: ModifierSet.java,v 1.6 2002-11-20 17:26:42 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 modifier Strings.
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>value</I> pairs, where both type and
16  * value are strings.  Types may be null, values may not.
17  *
18  * @version $Id: ModifierSet.java,v 1.6 2002-11-20 17:26:42 mike Exp $
19  */
20 public class ModifierSet {
21     String base;
22     Vector 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();
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         Vector modifier = new Vector();
45         modifier.add(type);
46         modifier.add(value);
47         modifiers.add(modifier);
48     }
49
50     /**
51      * Returns the value of the modifier in the specified ModifierSet
52      * that corresponds to the specified type.
53      */
54     public String modifier(String type) {
55         int n = modifiers.size();
56         for (int i = 0; i < n; i++) {
57             Vector pair = (Vector) modifiers.get(i);
58             if (pair.get(0).equals(type))
59                 return (String) pair.get(1);
60         }
61         return null;
62     }
63
64     /**
65      * Returns an array of the modifiers in a ModifierSet.
66      * @return
67      *  An array of modifiers, each represented by a two-element
68      *  <TT>Vector</TT>, in which element 0 is the modifier type and
69      *  element 1 is the associated value.
70      */
71     public Vector[] getModifiers() {
72         int n = modifiers.size();
73         Vector[] res = new Vector[n];
74         for (int i = 0; i < n; i++) {
75             res[i] = (Vector) modifiers.get(i);
76         }
77
78         return res;
79     }
80
81     public String toXCQL(int level, String topLevelElement) {
82         System.err.println("in ModifierSet.toXCQL()");
83         StringBuffer buf = new StringBuffer();
84         buf.append (Utils.indent(level) + "<" + topLevelElement + ">\n" +
85                     Utils.indent(level+1) + "<value>" + Utils.xq(base) +
86                     "</value>\n");
87         Vector[] mods = getModifiers();
88         if (mods.length > 0) {
89             buf.append(Utils.indent(level+1) + "<modifiers>\n");
90             for (int i = 0; i < mods.length; i++) {
91                 Vector modifier = mods[i];
92                 buf.append(Utils.indent(level+2)).
93                     append("<modifier>");
94                 if (modifier.get(0) != null)
95                     buf.append("<type>").
96                         append(Utils.xq((String) modifier.get(0))).
97                         append("</type>");
98                 buf.append("<value>").
99                     append(Utils.xq((String) modifier.get(1))).
100                     append("</value>");
101                 buf.append("</modifier>\n");
102             }
103             buf.append(Utils.indent(level+1) + "</modifiers>\n");
104         }
105         buf.append(Utils.indent(level) + "</" + topLevelElement + ">\n");
106         return buf.toString();
107     }
108
109     public String toCQL() {
110         StringBuffer buf = new StringBuffer(base);
111         Vector[] mods = getModifiers();
112         for (int i = 0; i < mods.length; i++) {
113             buf.append("/").append(mods[i].get(1));
114         }
115
116         return buf.toString();
117     }
118
119     public static void main(String[] args) {
120         if (args.length < 1) {
121             System.err.println("Usage: ModifierSet <base> [<type> <name>]...");
122             System.exit(1);
123         }
124
125         ModifierSet res = new ModifierSet(args[0]);
126         for (int i = 1; i < args.length; i += 2) {
127             res.addModifier(args[i], args[i+1]);
128         }
129
130         System.out.println(res.toCQL());
131     }
132 }