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