Add Ralph's new getResultSetName() method.
[cql-java-moved-to-github.git] / src / org / z3950 / zing / cql / CQLTermNode.java
1 // $Id: CQLTermNode.java,v 1.18 2002-12-12 10:24:25 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.18 2002-12-12 10:24:25 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 getResultSetName() {
38         if (qualifier.equals("srw.resultSet"))
39             return term;
40         else
41             return null;
42     }
43
44     public String toXCQL(int level, Vector prefixes) {
45         return (indent(level) + "<searchClause>\n" +
46                 renderPrefixes(level+1, prefixes) +
47                 indent(level+1) + "<index>" + xq(qualifier) + "</index>\n" +
48                 relation.toXCQL(level+1, new Vector()) +
49                 indent(level+1) + "<term>" + xq(term) + "</term>\n" +
50                 indent(level) + "</searchClause>\n");
51     }
52
53     public String toCQL() {
54         String quotedQualifier = maybeQuote(qualifier);
55         String quotedTerm = maybeQuote(term);
56         String res = quotedTerm;
57
58         if (!qualifier.equalsIgnoreCase("srw.serverChoice")) {
59             // ### We don't always need spaces around `relation'.
60             res = quotedQualifier + " " + relation.toCQL() + " " + quotedTerm;
61         }
62
63         return res;
64     }
65
66     // ### Interaction between this and its callers is not good as
67     //  regards truncation of the term and generation of truncation
68     //  attributes.  Change the interface to fix this.
69     private Vector getAttrs(Properties config) throws PQFTranslationException {
70         Vector attrs = new Vector();
71
72         // Do this first so that if any other truncation or
73         // completeness attributes are generated, they "overwrite"
74         // those specified here.
75         //
76         //  ### This approach relies on an unpleasant detail of Index
77         //      Data's (admittedly definitive) implementation of PQF,
78         //      and should not relied upon.
79         //
80         String attr = config.getProperty("always");
81         if (attr != null)
82             attrs.add(attr);
83
84         attr = config.getProperty("qualifier." + qualifier);
85         if (attr == null)
86             throw new UnknownQualifierException(qualifier);
87         attrs.add(attr);
88
89         String rel = relation.getBase();
90         if (rel.equals("=")) {
91             rel = "eq";
92         } else if (rel.equals("<=")) {
93             rel = "le";
94         } else if (rel.equals(">=")) {
95             rel = "ge";
96         }
97         // ### Handling "any" and "all" properly would involve breaking
98         // the string down into a bunch of individual words and ORring
99         // or ANDing them together.  Another day.
100         attr = config.getProperty("relation." + rel);
101         if (attr == null)
102             throw new UnknownRelationException(rel);
103         attrs.add(attr);
104
105         String[] mods = relation.getModifiers();
106         for (int i = 0; i < mods.length; i++) {
107             attr = config.getProperty("relationModifier." + mods[i]);
108             if (attr == null)
109                 throw new UnknownRelationModifierException(mods[i]);
110             attrs.add(attr);
111         }
112
113         String pos = "any";
114         String text = term;
115         if (text.length() > 0 && text.substring(0, 1).equals("^")) {
116             text = text.substring(1); // ### change not seen by caller
117             pos = "first";
118         }
119         int len = text.length();
120         if (len > 0 && text.substring(len-1, len).equals("^")) {
121             text = text.substring(0, len-1); // ### change not seen by caller
122             pos = pos.equals("first") ? "firstAndLast" : "last";
123             // ### in the firstAndLast case, the standard
124             //  pqf.properties file specifies that we generate a
125             //  completeness=whole-field attributem, which means that
126             //  we don't generate a position attribute at all.  Do we
127             //  care?  Does it matter?
128         }
129
130         attr = config.getProperty("position." + pos);
131         if (attr == null)
132             throw new UnknownPositionException(pos);
133         attrs.add(attr);
134
135         attr = config.getProperty("structure." + rel);
136         if (attr == null)
137             attr = config.getProperty("structure.*");
138         attrs.add(attr);
139
140         return attrs;
141     }
142
143     public String toPQF(Properties config) throws PQFTranslationException {
144         if (qualifier.equals("srw.resultSet")) {
145             // Special case: ignore relation, modifiers, wildcards, etc.
146             // There's parallel code in toType1BER()
147             return "@set " + maybeQuote(term);
148         }
149
150         Vector attrs = getAttrs(config);
151
152         String attr, s = "";
153         for (int i = 0; i < attrs.size(); i++) {
154             attr = (String) attrs.get(i);
155             s += "@attr " + Utils.replaceString(attr, " ", " @attr ") + " ";
156         }
157
158         String text = term;
159         if (text.length() > 0 && text.substring(0, 1).equals("^"))
160             text = text.substring(1);
161         int len = text.length();
162         if (len > 0 && text.substring(len-1, len).equals("^"))
163             text = text.substring(0, len-1);
164
165         return s + maybeQuote(text);
166     }
167
168     static String maybeQuote(String str) {
169         // There _must_ be a better way to make this test ...
170         if (str.length() == 0 ||
171             str.indexOf('"') != -1 ||
172             str.indexOf(' ') != -1 ||
173             str.indexOf('\t') != -1 ||
174             str.indexOf('=') != -1 ||
175             str.indexOf('<') != -1 ||
176             str.indexOf('>') != -1 ||
177             str.indexOf('/') != -1 ||
178             str.indexOf('(') != -1 ||
179             str.indexOf(')') != -1) {
180             str = '"' + Utils.replaceString(str, "\"", "\\\"") + '"';
181         }
182
183         return str;
184     }
185
186     public byte[] toType1BER(Properties config) throws PQFTranslationException {
187         if (qualifier.equals("srw.resultSet")) {
188             // Special case: ignore relation, modifiers, wildcards, etc.
189             // There's parallel code in toPQF()
190             byte[] operand = new byte[term.length()+100];
191             int offset;
192             offset = putTag(CONTEXT, 0, CONSTRUCTED, operand, 0); // op
193             operand[offset++] = (byte)(0x80&0xff); // indefinite length
194             offset = putTag(CONTEXT, 31, PRIMITIVE, operand, offset); // ResultSetId
195             byte[] t = term.getBytes();
196             offset = putLen(t.length, operand, offset);
197             System.arraycopy(t, 0, operand, offset, t.length);
198             offset += t.length;
199             operand[offset++] = 0x00; // end of Operand
200             operand[offset++] = 0x00;
201             byte[] o = new byte[offset];
202             System.arraycopy(operand, 0, o, 0, offset);
203             return o;
204         }
205
206         String text = term;
207         if (text.length() > 0 && text.substring(0, 1).equals("^"))
208             text = text.substring(1);
209         int len = text.length();
210         if (len > 0 && text.substring(len-1, len).equals("^"))
211             text = text.substring(0, len-1);
212
213         String attr, attrList, term = maybeQuote(text);
214         System.out.println("in CQLTermNode.toType1BER(): PQF=" + toPQF(config));
215         byte[] operand = new byte[text.length()+100];
216         int i, j, offset, type, value;
217         offset = putTag(CONTEXT, 0, CONSTRUCTED, operand, 0); // op
218         operand[offset++]=(byte)(0x80&0xff); // indefinite length
219         offset = putTag(CONTEXT, 102, CONSTRUCTED, operand, offset); // AttributesPlusTerm
220         operand[offset++] = (byte)(0x80&0xff); // indefinite length
221         offset = putTag(CONTEXT, 44, CONSTRUCTED, operand, offset); // AttributeList
222         operand[offset++] = (byte)(0x80&0xff); // indefinite length
223         offset = putTag(UNIVERSAL, SEQUENCE, CONSTRUCTED, operand, offset);
224         operand[offset++] = (byte)(0x80&0xff);
225
226         Vector attrs = getAttrs(config);
227         for(i = 0; i < attrs.size(); i++) {
228             attrList = (String) attrs.get(i);
229             java.util.StringTokenizer st =
230                 new java.util.StringTokenizer(attrList);
231             while (st.hasMoreTokens()) {
232                 attr = st.nextToken();
233                 j = attr.indexOf('=');
234                 offset = putTag(CONTEXT, 120, PRIMITIVE, operand, offset);
235                 type = Integer.parseInt(attr.substring(0, j));
236                 offset = putLen(numLen(type), operand, offset);
237                 offset = putNum(type, operand, offset);
238
239                 offset = putTag(CONTEXT, 121, PRIMITIVE, operand, offset);
240                 value = Integer.parseInt(attr.substring(j+1));
241                 offset = putLen(numLen(value), operand, offset);
242                 offset = putNum(value, operand, offset);
243             }
244         }
245         operand[offset++] = 0x00; // end of SEQUENCE
246         operand[offset++] = 0x00;
247         operand[offset++] = 0x00; // end of AttributeList
248         operand[offset++] = 0x00;
249
250         offset = putTag(CONTEXT, 45, PRIMITIVE, operand, offset); // general Term
251         byte[] t = term.getBytes();
252         offset = putLen(t.length, operand, offset);
253         System.arraycopy(t, 0, operand, offset, t.length);
254         offset += t.length;
255
256         operand[offset++] = 0x00; // end of AttributesPlusTerm
257         operand[offset++] = 0x00;
258         operand[offset++] = 0x00; // end of Operand
259         operand[offset++] = 0x00;
260         byte[] o = new byte[offset];
261         System.arraycopy(operand, 0, o, 0, offset);
262         return o;
263     }
264 }