Support Adam's new position configuration elements
[cql-java-moved-to-github.git] / src / org / z3950 / zing / cql / CQLTermNode.java
1 // $Id: CQLTermNode.java,v 1.11 2002-11-29 16:43:23 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.11 2002-11-29 16:43:23 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         // Do this first so that if any other truncation or
63         // completeness attributes are generated, they "overwrite"
64         // those specified here.
65         //
66         //  ### This approach relies on an unpleasant detail of Index
67         //      Data's (admittedly definitive) implementation of PQF,
68         //      and should not relied upon.
69         //
70         String attr = config.getProperty("always");
71         if (attr != null)
72             attrs.add(attr);
73
74         attr = config.getProperty("qualifier." + qualifier);
75         if (attr == null)
76             throw new UnknownQualifierException(qualifier);
77         attrs.add(attr);
78
79         String rel = relation.getBase();
80         if (rel.equals("=")) {
81             rel = "eq";
82         } else if (rel.equals("<=")) {
83             rel = "le";
84         } else if (rel.equals(">=")) {
85             rel = "ge";
86         }
87         // ### Handling "any" and "all" properly would involve breaking
88         // the string down into a bunch of individual words and ORring
89         // or ANDing them together.  Another day.
90         attr = config.getProperty("relation." + rel);
91         if (attr == null)
92             throw new UnknownRelationException(rel);
93         attrs.add(attr);
94
95         String[] mods = relation.getModifiers();
96         for (int i = 0; i < mods.length; i++) {
97             attr = config.getProperty("relationModifier." + mods[i]);
98             if (attr == null)
99                 throw new UnknownRelationModifierException(mods[i]);
100             attrs.add(attr);
101         }
102
103         String pos = "any";
104         String text = term;
105         //System.err.println("before check: text='" + text + "'");
106         if (text.length() > 0 && text.substring(0, 1).equals("^")) {
107             //System.err.println("first in field");
108             text = text.substring(1);
109             pos = "first";
110         }
111         //System.err.println("between checks: text='" + text + "'");
112         int len = text.length();
113         if (len > 0 && text.substring(len-1, len).equals("^")) {
114             //System.err.println("last in field");
115             text = text.substring(0, len-1);
116             pos = pos.equals("first") ? "firstAndLast" : "last";
117             // ### in the firstAndLast case, the standard
118             //  pqf.properties file specifies that we generate a
119             //  completeness=whole-field attributem, which means that
120             //  we don't generate a position attribute at all.  Do we
121             //  care?  Does it matter?
122         }
123         //System.err.println("after checks: text='" + text + "'");
124         attr = config.getProperty("position." + pos);
125         if (attr == null)
126             throw new UnknownPositionException(pos);
127         attrs.add(attr);
128
129         attr = config.getProperty("structure." + rel);
130         if (attr == null)
131             attr = config.getProperty("structure.*");
132         attrs.add(attr);
133
134         String s = "";
135         for (int i = 0; i < attrs.size(); i++) {
136             attr = (String) attrs.get(i);
137             s += "@attr " + Utils.replaceString(attr, " ", " @attr ") + " ";
138         }
139
140         return s + maybeQuote(text);
141     }
142
143     static String maybeQuote(String str) {
144         // There _must_ be a better way to make this test ...
145         if (str.length() == 0 ||
146             str.indexOf('"') != -1 ||
147             str.indexOf(' ') != -1 ||
148             str.indexOf('\t') != -1 ||
149             str.indexOf('=') != -1 ||
150             str.indexOf('<') != -1 ||
151             str.indexOf('>') != -1 ||
152             str.indexOf('/') != -1 ||
153             str.indexOf('(') != -1 ||
154             str.indexOf(')') != -1) {
155             str = '"' + Utils.replaceString(str, "\"", "\\\"") + '"';
156         }
157
158         return str;
159     }
160 }