4992b25da436c30a309a767849f6789ddc6e4980
[cql-java-moved-to-github.git] / src / main / java / org / z3950 / zing / cql / Modifier.java
1 // $Id: Modifier.java,v 1.4 2007-07-03 13:29:34 mike Exp $
2
3 package org.z3950.zing.cql;
4 import java.util.Vector;
5 import java.lang.StringBuffer;
6
7 /**
8  * Represents a single modifier, consisting of three elements: a type,
9  * a comparision and a value.  For example, "distance", "<", "3".  The
10  * type is mandatory; either the comparison and value must both occur,
11  * or neither must.
12  * <P>
13  * This class is used only by ModifierSet.
14  *
15  * @version $Id: Modifier.java,v 1.4 2007-07-03 13:29:34 mike Exp $
16  */
17 public class Modifier {
18     String type;
19     String comparison;
20     String value;
21
22     /**
23      * Creates a new Modifier with the specified type, comparison
24      * and value.
25      */
26     public Modifier(String type, String comparison, String value) {
27         this.type = type;
28         this.comparison = comparison;
29         this.value = value;
30         //System.err.println("Made new modifier with " + "type='" + type + "', " + "comparison='" + comparison + "', " + "value='" + value + "',\n");
31     }
32
33     /**
34      * Creates a new Modifier with the specified type but no
35      * comparison or value.
36      */
37     public Modifier(String type) {
38         this.type = type;
39         //System.err.println("Made new modifier of type '" + type + "'\n");
40     }
41
42     /**
43      * Returns the type with which the Modifier was created.
44      */
45     public String getType() {
46         return type;
47     }
48
49     /**
50      * Returns the comparison with which the Modifier was created.
51      */
52     public String getComparison() {
53         return comparison;
54     }
55
56     /**
57      * Returns the value with which the Modifier was created.
58      */
59     public String getValue() {
60         return value;
61     }
62
63     public String toXCQL(int level, String relationElement) {
64         StringBuffer buf = new StringBuffer();
65
66         buf.append(Utils.indent(level) + "<modifier>\n");
67         buf.append(Utils.indent(level+1) +
68                    "<type>" + Utils.xq(type) + "</type>\n");
69         if (value != null) {
70             buf.append(Utils.indent(level+1) + "<" + relationElement + ">" +
71                        Utils.xq(comparison) + "</" + relationElement + ">\n");
72             buf.append(Utils.indent(level+1) +
73                        "<value>" + Utils.xq(value) + "</value>\n");
74         }
75         
76         buf.append(Utils.indent(level) + "</modifier>\n");
77         return buf.toString();
78     }
79
80     public String toCQL() {
81         StringBuffer buf = new StringBuffer(type);
82         if (value != null)
83             buf.append(" " + comparison + " " + value);
84
85         return buf.toString();
86     }
87 }