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