New
[cql-java-moved-to-github.git] / src / org / z3950 / zing / cql / Modifier.java
1 // $Id: Modifier.java,v 1.1 2007-06-27 17:01:23 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.1 2007-06-27 17:01:23 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     }
31
32     /**
33      * Creates a new Modifier with the specified value but no
34      * comparison or value.
35      */
36     public Modifier(String value) {
37         this.value = value;
38     }
39
40     /**
41      * Returns the type with which the Modifier was created.
42      */
43     public String getType() {
44         return type;
45     }
46
47     /**
48      * Returns the comparison with which the Modifier was created.
49      */
50     public String getComparison() {
51         return comparison;
52     }
53
54     /**
55      * Returns the value with which the Modifier was created.
56      */
57     public String getValue() {
58         return value;
59     }
60
61     public String toXCQL(int level, String relationElement) {
62         StringBuffer buf = new StringBuffer();
63
64         buf.append(Utils.indent(level) + "<modifier>\n");
65         buf.append(Utils.indent(level+1) +
66                    "<type>" + Utils.xq(type) + "</type>\n");
67         if (value != null) {
68             buf.append(Utils.indent(level+1) + "<" + relationElement + ">" +
69                        Utils.xq(comparison) + "</" + relationElement + ">\n");
70             buf.append(Utils.indent(level+1) +
71                        "<value>" + Utils.xq(value) + "</value>\n");
72         }
73         
74         buf.append(Utils.indent(level) + "</modifier>\n");
75         return buf.toString();
76     }
77
78     public String toCQL() {
79         StringBuffer buf = new StringBuffer(type);
80
81         if (value != null)
82             buf.append(" " + comparison + " " + value);
83
84         return buf.toString();
85     }
86 }