Remove spurious extra indent (was introduced so I could check for
[cql-java-moved-to-github.git] / src / org / z3950 / zing / cql / CQLTermNode.java
1 // $Id: CQLTermNode.java,v 1.10 2002-11-20 15:38:27 mike Exp $
2
3 package org.z3950.zing.cql;
4 import java.util.Properties;
5 import java.util.Vector;
6
7
8 /**
9  * Represents a terminal node in a CQL parse-tree.
10  * A term node consists of the term String itself, together with,
11  * optionally, a qualifier string and a relation.  Neither or both of
12  * these must be provided - you can't have a qualifier without a
13  * relation or vice versa.
14  *
15  * @version     $Id: CQLTermNode.java,v 1.10 2002-11-20 15:38:27 mike Exp $
16  */
17 public class CQLTermNode extends CQLNode {
18     private String qualifier;
19     private CQLRelation relation;
20     private String term;
21
22     /**
23      * Creates a new term node with the specified <TT>qualifier</TT>,
24      * <TT>relation</TT> and <TT>term</TT>.  The first two may be
25      * <TT>null</TT>, but the <TT>term</TT> may not.
26      */
27     public CQLTermNode(String qualifier, CQLRelation relation, String term) {
28         this.qualifier = qualifier;
29         this.relation = relation;
30         this.term = term;
31     }
32
33     public String getQualifier() { return qualifier; }
34     public CQLRelation getRelation() { return relation; }
35     public String getTerm() { return term; }
36
37     public String toXCQL(int level, Vector prefixes) {
38         return (indent(level) + "<searchClause>\n" +
39                 renderPrefixes(level+1, prefixes) +
40                 indent(level+1) + "<index>" + xq(qualifier) + "</index>\n" +
41                 relation.toXCQL(level+1, new Vector()) +
42                 indent(level+1) + "<term>" + xq(term) + "</term>\n" +
43                 indent(level) + "</searchClause>\n");
44     }
45
46     public String toCQL() {
47         String quotedQualifier = maybeQuote(qualifier);
48         String quotedTerm = maybeQuote(term);
49         String res = quotedTerm;
50
51         if (!qualifier.equalsIgnoreCase("srw.serverChoice")) {
52             // ### We don't always need spaces around `relation'.
53             res = quotedQualifier + " " + relation.toCQL() + " " + quotedTerm;
54         }
55
56         return res;
57     }
58
59     public String toPQF(Properties config) throws PQFTranslationException {
60         Vector attrs = new Vector();
61
62         String attr;
63         attr = config.getProperty("qualifier." + qualifier);
64         if (attr == null)
65             throw new UnknownQualifierException(qualifier);
66         attrs.add(attr);
67
68         String rel = relation.getBase();
69         if (rel.equals("=")) {
70             rel = "eq";
71         } else if (rel.equals("<=")) {
72             rel = "le";
73         } else if (rel.equals(">=")) {
74             rel = "ge";
75         }
76         // ### Handling "any" and "all" properly would involve breaking
77         // the string down into a bunch of individual words and ORring
78         // or ANDing them together.  Another day.
79         attr = config.getProperty("relation." + rel);
80         if (attr == null)
81             throw new UnknownRelationException(rel);
82         attrs.add(attr);
83
84         String[] mods = relation.getModifiers();
85         for (int i = 0; i < mods.length; i++) {
86             attr = config.getProperty("relationModifier." + mods[i]);
87             if (attr == null)
88                 throw new UnknownRelationModifierException(mods[i]);
89             attrs.add(attr);
90         }
91
92         String pos = "unanchored";
93         String text = term;
94         if (text.length() > 0 && text.substring(0, 1).equals("^")) {
95             text = text.substring(1);
96             pos = "anchored";
97         }
98         attr = config.getProperty("position." + pos);
99         if (attr == null)
100             throw new UnknownPositionException(pos);
101         attrs.add(attr);
102
103         attr = config.getProperty("structure." + rel);
104         if (attr == null)
105             attr = config.getProperty("structure.*");
106         attrs.add(attr);
107
108         attr = config.getProperty("always");
109         if (attr != null)
110             attrs.add(attr);
111
112         String s = "";
113         for (int i = 0; i < attrs.size(); i++) {
114             attr = (String) attrs.get(i);
115             s += "@attr " + Utils.replaceString(attr, " ", " @attr ") + " ";
116         }
117
118         return s + maybeQuote(text);
119     }
120
121     static String maybeQuote(String str) {
122         // There _must_ be a better way to make this test ...
123         if (str.length() == 0 ||
124             str.indexOf('"') != -1 ||
125             str.indexOf(' ') != -1 ||
126             str.indexOf('\t') != -1 ||
127             str.indexOf('=') != -1 ||
128             str.indexOf('<') != -1 ||
129             str.indexOf('>') != -1 ||
130             str.indexOf('/') != -1 ||
131             str.indexOf('(') != -1 ||
132             str.indexOf(')') != -1) {
133             str = '"' + Utils.replaceString(str, "\"", "\\\"") + '"';
134         }
135
136         return str;
137     }
138 }