1 // $Id: CQLTermNode.java,v 1.16 2002-12-11 00:30:02 mike Exp $
3 package org.z3950.zing.cql;
4 import java.util.Properties;
5 import java.util.Vector;
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.
15 * @version $Id: CQLTermNode.java,v 1.16 2002-12-11 00:30:02 mike Exp $
17 public class CQLTermNode extends CQLNode {
18 private String qualifier;
19 private CQLRelation relation;
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.
27 public CQLTermNode(String qualifier, CQLRelation relation, String term) {
28 this.qualifier = qualifier;
29 this.relation = relation;
33 public String getQualifier() { return qualifier; }
34 public CQLRelation getRelation() { return relation; }
35 public String getTerm() { return term; }
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");
46 public String toCQL() {
47 String quotedQualifier = maybeQuote(qualifier);
48 String quotedTerm = maybeQuote(term);
49 String res = quotedTerm;
51 if (!qualifier.equalsIgnoreCase("srw.serverChoice")) {
52 // ### We don't always need spaces around `relation'.
53 res = quotedQualifier + " " + relation.toCQL() + " " + quotedTerm;
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();
65 // Do this first so that if any other truncation or
66 // completeness attributes are generated, they "overwrite"
67 // those specified here.
69 // ### This approach relies on an unpleasant detail of Index
70 // Data's (admittedly definitive) implementation of PQF,
71 // and should not relied upon.
73 String attr = config.getProperty("always");
77 attr = config.getProperty("qualifier." + qualifier);
79 throw new UnknownQualifierException(qualifier);
82 String rel = relation.getBase();
83 if (rel.equals("=")) {
85 } else if (rel.equals("<=")) {
87 } else if (rel.equals(">=")) {
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);
95 throw new UnknownRelationException(rel);
98 String[] mods = relation.getModifiers();
99 for (int i = 0; i < mods.length; i++) {
100 attr = config.getProperty("relationModifier." + mods[i]);
102 throw new UnknownRelationModifierException(mods[i]);
108 if (text.length() > 0 && text.substring(0, 1).equals("^")) {
109 text = text.substring(1); // ### change not seen by caller
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?
123 attr = config.getProperty("position." + pos);
125 throw new UnknownPositionException(pos);
128 attr = config.getProperty("structure." + rel);
130 attr = config.getProperty("structure.*");
136 public String toPQF(Properties config) throws PQFTranslationException {
137 if (qualifier.equals("srw.resultSet")) {
138 // Special case: ignore relation, modifiers, wildcards, etc.
139 // There's parallel code in toType1()
140 return "@set " + maybeQuote(term);
143 Vector attrs = getAttrs(config);
146 for (int i = 0; i < attrs.size(); i++) {
147 attr = (String) attrs.get(i);
148 s += "@attr " + Utils.replaceString(attr, " ", " @attr ") + " ";
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);
158 return s + maybeQuote(text);
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, "\"", "\\\"") + '"';
179 public byte[] toType1(Properties config) throws PQFTranslationException {
180 if (qualifier.equals("srw.resultSet")) {
181 // Special case: ignore relation, modifiers, wildcards, etc.
182 // There's parallel code in toPQF()
183 byte[] operand = new byte[term.length()+100];
185 offset = putTag(CONTEXT, 0, CONSTRUCTED, operand, 0); // op
186 operand[offset++] = (byte)(0x80&0xff); // indefinite length
187 offset = putTag(CONTEXT, 31, PRIMITIVE, operand, offset); // ResultSetId
188 byte[] t = term.getBytes();
189 offset = putLen(t.length, operand, offset);
190 System.arraycopy(t, 0, operand, offset, t.length);
192 operand[offset++] = 0x00; // end of Operand
193 operand[offset++] = 0x00;
194 byte[] o = new byte[offset];
195 System.arraycopy(operand, 0, o, 0, offset);
200 if (text.length() > 0 && text.substring(0, 1).equals("^"))
201 text = text.substring(1);
202 int len = text.length();
203 if (len > 0 && text.substring(len-1, len).equals("^"))
204 text = text.substring(0, len-1);
206 String attr, attrList, term = maybeQuote(text);
207 System.out.println("in CQLTermNode.toType101(): PQF=" + toPQF(config));
208 byte[] operand = new byte[text.length()+100];
209 int i, j, offset, type, value;
210 offset = putTag(CONTEXT, 0, CONSTRUCTED, operand, 0); // op
211 operand[offset++]=(byte)(0x80&0xff); // indefinite length
212 offset = putTag(CONTEXT, 102, CONSTRUCTED, operand, offset); // AttributesPlusTerm
213 operand[offset++] = (byte)(0x80&0xff); // indefinite length
214 offset = putTag(CONTEXT, 44, CONSTRUCTED, operand, offset); // AttributeList
215 operand[offset++] = (byte)(0x80&0xff); // indefinite length
216 offset = putTag(UNIVERSAL, SEQUENCE, CONSTRUCTED, operand, offset);
217 operand[offset++] = (byte)(0x80&0xff);
219 Vector attrs = getAttrs(config);
220 for(i = 0; i < attrs.size(); i++) {
221 attrList = (String) attrs.get(i);
222 java.util.StringTokenizer st =
223 new java.util.StringTokenizer(attrList);
224 while (st.hasMoreTokens()) {
225 attr = st.nextToken();
226 j = attr.indexOf('=');
227 offset = putTag(CONTEXT, 120, PRIMITIVE, operand, offset);
228 type = Integer.parseInt(attr.substring(0, j));
229 offset = putLen(numLen(type), operand, offset);
230 offset = putNum(type, operand, offset);
232 offset = putTag(CONTEXT, 121, PRIMITIVE, operand, offset);
233 value = Integer.parseInt(attr.substring(j+1));
234 offset = putLen(numLen(value), operand, offset);
235 offset = putNum(value, operand, offset);
238 operand[offset++] = 0x00; // end of SEQUENCE
239 operand[offset++] = 0x00;
240 operand[offset++] = 0x00; // end of AttributeList
241 operand[offset++] = 0x00;
243 offset = putTag(CONTEXT, 45, PRIMITIVE, operand, offset); // general Term
244 byte[] t = term.getBytes();
245 offset = putLen(t.length, operand, offset);
246 System.arraycopy(t, 0, operand, offset, t.length);
249 operand[offset++] = 0x00; // end of AttributesPlusTerm
250 operand[offset++] = 0x00;
251 operand[offset++] = 0x00; // end of Operand
252 operand[offset++] = 0x00;
253 byte[] o = new byte[offset];
254 System.arraycopy(operand, 0, o, 0, offset);