- Add support for the new "phonetic" relation modifier,
[cql-java-moved-to-github.git] / src / org / z3950 / zing / cql / ModifierSet.java
1 // $Id: ModifierSet.java,v 1.5 2002-11-17 23:29:02 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.5 2002-11-17 23:29:02 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         StringBuffer buf = new StringBuffer();
83         buf.append (Utils.indent(level) + "<" + topLevelElement + ">\n" +
84                     Utils.indent(level+1) + "<value>" + Utils.xq(base) +
85                     "</value>\n");
86         Vector[] mods = getModifiers();
87         if (mods.length > 0) {
88             buf.append(Utils.indent(level+1) + "<modifiers>\n");
89             for (int i = 0; i < mods.length; i++) {
90                 Vector modifier = mods[i];
91                 buf.append(Utils.indent(level+2)).
92                     append("<modifier>");
93                 if (modifier.get(0) != null)
94                     buf.append("<type>").
95                         append(Utils.xq((String) modifier.get(0))).
96                         append("</type>");
97                 buf.append("<value>").
98                     append(Utils.xq((String) modifier.get(1))).
99                     append("</value>");
100                 buf.append("</modifier>\n");
101             }
102             buf.append(Utils.indent(level+1) + "</modifiers>\n");
103         }
104         buf.append(Utils.indent(level) + "</" + topLevelElement + ">\n");
105         return buf.toString();
106     }
107
108     public String toCQL() {
109         StringBuffer buf = new StringBuffer(base);
110         Vector[] mods = getModifiers();
111         for (int i = 0; i < mods.length; i++) {
112             buf.append("/").append(mods[i].get(1));
113         }
114
115         return buf.toString();
116     }
117
118     public static void main(String[] args) {
119         if (args.length < 1) {
120             System.err.println("Usage: ModifierSet <base> [<type> <name>]...");
121             System.exit(1);
122         }
123
124         ModifierSet res = new ModifierSet(args[0]);
125         for (int i = 1; i < args.length; i += 2) {
126             res.addModifier(args[i], args[i+1]);
127         }
128
129         System.out.println(res.toCQL());
130     }
131 }