1 // $Id: ModifierSet.java,v 1.13 2007-07-03 13:30:18 mike Exp $
3 package org.z3950.zing.cql;
4 import java.util.ArrayList;
7 import static org.z3950.zing.cql.Utils.*;
10 * Represents a base String and a set of Modifiers.
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.
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.
20 * @version $Id: ModifierSet.java,v 1.13 2007-07-03 13:30:18 mike Exp $
22 public class ModifierSet {
24 private List<Modifier> modifiers;
27 * Creates a new ModifierSet with the specified base.
29 public ModifierSet(String base) {
31 modifiers = new ArrayList<Modifier>();
35 * Returns the base string with which the ModifierSet was created.
37 public String getBase() {
42 * Adds a modifier of the specified <TT>type</TT>,
43 * <TT>comparison</TT> and <TT>value</TT> to a ModifierSet.
45 public void addModifier(String type, String comparison, String value) {
46 Modifier modifier = new Modifier(type, comparison, value);
47 modifiers.add(modifier);
51 * Adds a modifier of the specified <TT>type</TT>, but with no
52 * <TT>comparison</TT> and <TT>value</TT>, to a ModifierSet.
54 public void addModifier(String type) {
55 Modifier modifier = new Modifier(type);
56 modifiers.add(modifier);
60 * Returns the value of the modifier in the specified ModifierSet
61 * that corresponds to the specified type.
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))
74 * Returns an array of the modifiers in a ModifierSet.
76 * An array of Modifiers.
78 public List<Modifier> getModifiers() {
82 public String toXCQL(int level, String topLevelElement) {
83 return underlyingToXCQL(level, topLevelElement, "value");
86 public String sortKeyToXCQL(int level) {
87 return underlyingToXCQL(level, "key", "index");
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"));
102 buf.append(indent(level + 1)).append("</modifiers>\n");
104 buf.append(indent(level)).append("</").append(topLevelElement).
106 return buf.toString();
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());
115 return buf.toString();
118 public static void main(String[] args) {
119 if (args.length < 1) {
120 System.err.println("Usage: ModifierSet <base> [<type> <comparison> <name>]...");
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]);
129 System.out.println(res.toCQL());