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