1 // $Id: CQLTermNode.java,v 1.9 2002-11-20 01:15:15 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.9 2002-11-20 01:15:15 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+2) + "<term>" + xq(term) + "</term>\n" +
43 // ######### ^-- this should be "1"
44 indent(level) + "</searchClause>\n");
47 public String toCQL() {
48 String quotedQualifier = maybeQuote(qualifier);
49 String quotedTerm = maybeQuote(term);
50 String res = quotedTerm;
52 if (!qualifier.equalsIgnoreCase("srw.serverChoice")) {
53 // ### We don't always need spaces around `relation'.
54 res = quotedQualifier + " " + relation.toCQL() + " " + quotedTerm;
60 public String toPQF(Properties config) throws PQFTranslationException {
61 Vector attrs = new Vector();
64 attr = config.getProperty("qualifier." + qualifier);
66 throw new UnknownQualifierException(qualifier);
69 String rel = relation.getBase();
70 if (rel.equals("=")) {
72 } else if (rel.equals("<=")) {
74 } else if (rel.equals(">=")) {
77 // ### Handling "any" and "all" properly would involve breaking
78 // the string down into a bunch of individual words and ORring
79 // or ANDing them together. Another day.
80 attr = config.getProperty("relation." + rel);
82 throw new UnknownRelationException(rel);
85 String[] mods = relation.getModifiers();
86 for (int i = 0; i < mods.length; i++) {
87 attr = config.getProperty("relationModifier." + mods[i]);
89 throw new UnknownRelationModifierException(mods[i]);
93 String pos = "unanchored";
95 if (text.length() > 0 && text.substring(0, 1).equals("^")) {
96 text = text.substring(1);
99 attr = config.getProperty("position." + pos);
101 throw new UnknownPositionException(pos);
104 attr = config.getProperty("structure." + rel);
106 attr = config.getProperty("structure.*");
109 attr = config.getProperty("always");
114 for (int i = 0; i < attrs.size(); i++) {
115 attr = (String) attrs.get(i);
116 s += "@attr " + Utils.replaceString(attr, " ", " @attr ") + " ";
119 return s + maybeQuote(text);
122 static String maybeQuote(String str) {
123 // There _must_ be a better way to make this test ...
124 if (str.length() == 0 ||
125 str.indexOf('"') != -1 ||
126 str.indexOf(' ') != -1 ||
127 str.indexOf('\t') != -1 ||
128 str.indexOf('=') != -1 ||
129 str.indexOf('<') != -1 ||
130 str.indexOf('>') != -1 ||
131 str.indexOf('/') != -1 ||
132 str.indexOf('(') != -1 ||
133 str.indexOf(')') != -1) {
134 str = '"' + Utils.replaceString(str, "\"", "\\\"") + '"';