1 // $Id: ModifierSet.java,v 1.12 2007-06-29 10:20:41 mike Exp $
3 package org.z3950.zing.cql;
4 import java.util.Vector;
5 import java.lang.StringBuffer;
8 * Represents a base String and a set of Modifiers.
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.
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.
18 * @version $Id: ModifierSet.java,v 1.12 2007-06-29 10:20:41 mike Exp $
20 public class ModifierSet {
22 Vector<Modifier> modifiers;
25 * Creates a new ModifierSet with the specified base.
27 public ModifierSet(String base) {
29 modifiers = new Vector<Modifier>();
33 * Returns the base string with which the ModifierSet was created.
35 public String getBase() {
40 * Adds a modifier of the specified <TT>type</TT>,
41 * <TT>comparison</TT> and <TT>value</TT> to a ModifierSet.
43 public void addModifier(String type, String comparison, String value) {
44 Modifier modifier = new Modifier(type, comparison, value);
45 modifiers.add(modifier);
49 * Adds a modifier of the specified <TT>type</TT>, but with no
50 * <TT>comparison</TT> and <TT>value</TT>, to a ModifierSet.
52 public void addModifier(String type) {
53 Modifier modifier = new Modifier(type);
54 modifiers.add(modifier);
58 * Returns the value of the modifier in the specified ModifierSet
59 * that corresponds to the specified type.
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))
72 * Returns an array of the modifiers in a ModifierSet.
74 * An array of Modifiers.
76 public Vector<Modifier> getModifiers() {
80 public String toXCQL(int level, String topLevelElement) {
81 StringBuffer buf = new StringBuffer();
82 buf.append(Utils.indent(level) + "<" + topLevelElement + ">\n");
83 buf.append(Utils.indent(level+1) +
84 "<value>" + Utils.xq(base) + "</value>\n");
85 if (modifiers.size() > 0) {
86 buf.append(Utils.indent(level+1) + "<modifiers>\n");
87 for (int i = 0; i < modifiers.size(); i++) {
88 buf.append(modifiers.get(i).toXCQL(level+2, "comparison"));
90 buf.append(Utils.indent(level+1) + "</modifiers>\n");
92 buf.append(Utils.indent(level) + "</" + topLevelElement + ">\n");
93 return buf.toString();
96 public String toCQL() {
97 StringBuffer buf = new StringBuffer(base);
98 for (int i = 0; i < modifiers.size(); i++) {
99 buf.append("/" + modifiers.get(i).toCQL());
102 return buf.toString();
105 public static void main(String[] args) {
106 if (args.length < 1) {
107 System.err.println("Usage: ModifierSet <base> [<type> <comparison> <name>]...");
111 ModifierSet res = new ModifierSet(args[0]);
112 for (int i = 1; i < args.length; i += 3) {
113 res.addModifier(args[i], args[i+1], args[i+2]);
116 System.out.println(res.toCQL());