Use StringBuilder instead of StringBuffer, optimize
[cql-java-moved-to-github.git] / src / main / java / org / z3950 / zing / cql / ModifierSet.java
1 // $Id: ModifierSet.java,v 1.13 2007-07-03 13:30:18 mike Exp $
2
3 package org.z3950.zing.cql;
4 import java.util.ArrayList;
5 import java.util.List;
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.13 2007-07-03 13:30:18 mike Exp $
19  */
20 public class ModifierSet {
21     private String base;
22     private List<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 ArrayList<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>,
41      * <TT>comparison</TT> and <TT>value</TT> to a ModifierSet.
42      */
43     public void addModifier(String type, String comparison, String value) {
44         Modifier modifier = new Modifier(type, comparison, value);
45         modifiers.add(modifier);
46     }
47
48     /**
49      * Adds a modifier of the specified <TT>type</TT>, but with no
50      * <TT>comparison</TT> and <TT>value</TT>, to a ModifierSet.
51      */
52     public void addModifier(String type) {
53         Modifier modifier = new Modifier(type);
54         modifiers.add(modifier);
55     }
56
57     /**
58      * Returns the value of the modifier in the specified ModifierSet
59      * that corresponds to the specified type.
60      */
61     public String modifier(String type) {
62         int n = modifiers.size();
63         for (int i = 0; i < n; i++) {
64             Modifier mod = modifiers.get(i);
65             if (mod.type.equals(type))
66                 return mod.value;
67         }
68         return null;
69     }
70
71     /**
72      * Returns an array of the modifiers in a ModifierSet.
73      * @return
74      *  An array of Modifiers.
75      */
76     public List<Modifier> getModifiers() {
77         return modifiers;
78     }
79
80     public String toXCQL(int level, String topLevelElement) {
81         return underlyingToXCQL(level, topLevelElement, "value");
82     }
83
84     public String sortKeyToXCQL(int level) {
85         return underlyingToXCQL(level, "key", "index");
86     }
87
88     private String underlyingToXCQL(int level, String topLevelElement,
89                                     String valueElement) {
90         StringBuilder buf = new StringBuilder();
91         buf.append(Utils.indent(level)).append("<").append(topLevelElement).
92             append(">\n").append(Utils.indent(level + 1)).append("<").
93             append(valueElement).append(">").append(Utils.xq(base)).append("</").
94             append(valueElement).append(">\n");
95         if (modifiers.size() > 0) {
96             buf.append(Utils.indent(level + 1)).append("<modifiers>\n");
97             for (int i = 0; i < modifiers.size(); i++) {
98                 buf.append(modifiers.get(i).toXCQL(level+2, "comparison"));
99             }
100             buf.append(Utils.indent(level + 1)).append("</modifiers>\n");
101         }
102         buf.append(Utils.indent(level)).append("</").append(topLevelElement).
103             append(">\n");
104         return buf.toString();
105     }
106
107     public String toCQL() {
108         StringBuilder buf = new StringBuilder(base);
109         for (int i = 0; i < modifiers.size(); i++) {
110             buf.append("/").append(modifiers.get(i).toCQL());
111         }
112
113         return buf.toString();
114     }
115
116     public static void main(String[] args) {
117         if (args.length < 1) {
118             System.err.println("Usage: ModifierSet <base> [<type> <comparison> <name>]...");
119             System.exit(1);
120         }
121
122         ModifierSet res = new ModifierSet(args[0]);
123         for (int i = 1; i < args.length; i += 3) {
124             res.addModifier(args[i], args[i+1], args[i+2]);
125         }
126
127         System.out.println(res.toCQL());
128     }
129 }