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