1 // $Id: CQLTermNode.java,v 1.23 2007-06-06 12:22:01 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.23 2007-06-06 12:22:01 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 private static boolean isResultSetQualifier(String qual) {
38 return (qual.equals("srw.resultSet") ||
39 qual.equals("srw.resultSetId") ||
40 qual.equals("srw.resultSetName"));
43 public String getResultSetName() {
44 if (isResultSetQualifier(qualifier))
50 public String toXCQL(int level, Vector prefixes) {
51 return (indent(level) + "<searchClause>\n" +
52 renderPrefixes(level+1, prefixes) +
53 indent(level+1) + "<index>" + xq(qualifier) + "</index>\n" +
54 relation.toXCQL(level+1, new Vector<String>()) +
55 indent(level+1) + "<term>" + xq(term) + "</term>\n" +
56 indent(level) + "</searchClause>\n");
59 public String toCQL() {
60 String quotedQualifier = maybeQuote(qualifier);
61 String quotedTerm = maybeQuote(term);
62 String res = quotedTerm;
64 if (qualifier != null &&
65 !qualifier.equalsIgnoreCase("srw.serverChoice")) {
66 // ### We don't always need spaces around `relation'.
67 res = quotedQualifier + " " + relation.toCQL() + " " + quotedTerm;
73 // ### Interaction between this and its callers is not good as
74 // regards truncation of the term and generation of truncation
75 // attributes. Change the interface to fix this.
76 private Vector getAttrs(Properties config) throws PQFTranslationException {
77 Vector<String> attrs = new Vector<String>();
79 // Do this first so that if any other truncation or
80 // completeness attributes are generated, they "overwrite"
81 // those specified here.
83 // ### This approach relies on an unpleasant detail of Index
84 // Data's (admittedly definitive) implementation of PQF,
85 // and should not relied upon.
87 String attr = config.getProperty("always");
91 attr = config.getProperty("qualifier." + qualifier);
93 throw new UnknownQualifierException(qualifier);
96 String rel = relation.getBase();
97 if (rel.equals("=")) {
99 } else if (rel.equals("<=")) {
101 } else if (rel.equals(">=")) {
104 // ### Handling "any" and "all" properly would involve breaking
105 // the string down into a bunch of individual words and ORring
106 // or ANDing them together. Another day.
107 attr = config.getProperty("relation." + rel);
109 throw new UnknownRelationException(rel);
112 String[] mods = relation.getModifiers();
113 for (int i = 0; i < mods.length; i++) {
114 attr = config.getProperty("relationModifier." + mods[i]);
116 throw new UnknownRelationModifierException(mods[i]);
122 if (text.length() > 0 && text.substring(0, 1).equals("^")) {
123 text = text.substring(1); // ### change not seen by caller
126 int len = text.length();
127 if (len > 0 && text.substring(len-1, len).equals("^")) {
128 text = text.substring(0, len-1); // ### change not seen by caller
129 pos = pos.equals("first") ? "firstAndLast" : "last";
130 // ### in the firstAndLast case, the standard
131 // pqf.properties file specifies that we generate a
132 // completeness=whole-field attributem, which means that
133 // we don't generate a position attribute at all. Do we
134 // care? Does it matter?
137 attr = config.getProperty("position." + pos);
139 throw new UnknownPositionException(pos);
142 attr = config.getProperty("structure." + rel);
144 attr = config.getProperty("structure.*");
150 public String toPQF(Properties config) throws PQFTranslationException {
151 if (isResultSetQualifier(qualifier)) {
152 // Special case: ignore relation, modifiers, wildcards, etc.
153 // There's parallel code in toType1BER()
154 return "@set " + maybeQuote(term);
157 Vector attrs = getAttrs(config);
160 for (int i = 0; i < attrs.size(); i++) {
161 attr = (String) attrs.get(i);
162 s += "@attr " + Utils.replaceString(attr, " ", " @attr ") + " ";
166 if (text.length() > 0 && text.substring(0, 1).equals("^"))
167 text = text.substring(1);
168 int len = text.length();
169 if (len > 0 && text.substring(len-1, len).equals("^"))
170 text = text.substring(0, len-1);
172 return s + maybeQuote(text);
175 static String maybeQuote(String str) {
179 // There _must_ be a better way to make this test ...
180 if (str.length() == 0 ||
181 str.indexOf('"') != -1 ||
182 str.indexOf(' ') != -1 ||
183 str.indexOf('\t') != -1 ||
184 str.indexOf('=') != -1 ||
185 str.indexOf('<') != -1 ||
186 str.indexOf('>') != -1 ||
187 str.indexOf('/') != -1 ||
188 str.indexOf('(') != -1 ||
189 str.indexOf(')') != -1) {
190 str = '"' + Utils.replaceString(str, "\"", "\\\"") + '"';
196 public byte[] toType1BER(Properties config) throws PQFTranslationException {
197 if (isResultSetQualifier(qualifier)) {
198 // Special case: ignore relation, modifiers, wildcards, etc.
199 // There's parallel code in toPQF()
200 byte[] operand = new byte[term.length()+100];
202 offset = putTag(CONTEXT, 0, CONSTRUCTED, operand, 0); // op
203 operand[offset++] = (byte)(0x80&0xff); // indefinite length
204 offset = putTag(CONTEXT, 31, PRIMITIVE, operand, offset); // ResultSetId
205 byte[] t = term.getBytes();
206 offset = putLen(t.length, operand, offset);
207 System.arraycopy(t, 0, operand, offset, t.length);
209 operand[offset++] = 0x00; // end of Operand
210 operand[offset++] = 0x00;
211 byte[] o = new byte[offset];
212 System.arraycopy(operand, 0, o, 0, offset);
217 if (text.length() > 0 && text.substring(0, 1).equals("^"))
218 text = text.substring(1);
219 int len = text.length();
220 if (len > 0 && text.substring(len-1, len).equals("^"))
221 text = text.substring(0, len-1);
223 String attr, attrList, term = text;
224 byte[] operand = new byte[text.length()+100];
225 int i, j, offset, type, value;
226 offset = putTag(CONTEXT, 0, CONSTRUCTED, operand, 0); // op
227 operand[offset++]=(byte)(0x80&0xff); // indefinite length
228 offset = putTag(CONTEXT, 102, CONSTRUCTED, operand, offset); // AttributesPlusTerm
229 operand[offset++] = (byte)(0x80&0xff); // indefinite length
230 offset = putTag(CONTEXT, 44, CONSTRUCTED, operand, offset); // AttributeList
231 operand[offset++] = (byte)(0x80&0xff); // indefinite length
233 Vector attrs = getAttrs(config);
234 for(i = 0; i < attrs.size(); i++) {
235 attrList = (String) attrs.get(i);
236 java.util.StringTokenizer st =
237 new java.util.StringTokenizer(attrList);
238 while (st.hasMoreTokens()) {
239 attr = st.nextToken();
240 j = attr.indexOf('=');
241 offset = putTag(UNIVERSAL, SEQUENCE, CONSTRUCTED, operand, offset);
242 operand[offset++] = (byte)(0x80&0xff);
243 offset = putTag(CONTEXT, 120, PRIMITIVE, operand, offset);
244 type = Integer.parseInt(attr.substring(0, j));
245 offset = putLen(numLen(type), operand, offset);
246 offset = putNum(type, operand, offset);
248 offset = putTag(CONTEXT, 121, PRIMITIVE, operand, offset);
249 value = Integer.parseInt(attr.substring(j+1));
250 offset = putLen(numLen(value), operand, offset);
251 offset = putNum(value, operand, offset);
252 operand[offset++] = 0x00; // end of SEQUENCE
253 operand[offset++] = 0x00;
256 operand[offset++] = 0x00; // end of AttributeList
257 operand[offset++] = 0x00;
259 offset = putTag(CONTEXT, 45, PRIMITIVE, operand, offset); // general Term
260 byte[] t = term.getBytes();
261 offset = putLen(t.length, operand, offset);
262 System.arraycopy(t, 0, operand, offset, t.length);
265 operand[offset++] = 0x00; // end of AttributesPlusTerm
266 operand[offset++] = 0x00;
267 operand[offset++] = 0x00; // end of Operand
268 operand[offset++] = 0x00;
269 byte[] o = new byte[offset];
270 System.arraycopy(operand, 0, o, 0, offset);