5889354c478be34e3db824ebef47f32a4e9473c5
[cql-java-moved-to-github.git] / src / org / z3950 / zing / cql / CQLTermNode.java
1 // $Id: CQLTermNode.java,v 1.7 2002-11-06 00:05:58 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.7 2002-11-06 00:05:58 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) {
38         return (indent(level) + "<searchClause>\n" +
39                 indent(level+1) + "<index>" + xq(qualifier) + "</index>\n" +
40                 relation.toXCQL(level+1) +
41                 indent(level+1) + "<term>" + xq(term) + "</term>\n" +
42                 indent(level) + "</searchClause>\n");
43     }
44
45     public String toCQL() {
46         String quotedQualifier = maybeQuote(qualifier);
47         String quotedTerm = maybeQuote(term);
48         String res = quotedTerm;
49
50         if (!qualifier.equalsIgnoreCase("srw.serverChoice")) {
51             // ### We don't always need spaces around `relation'.
52             res = quotedQualifier + " " + relation.toCQL() + " " + quotedTerm;
53         }
54
55         return res;
56     }
57
58     public String toPQF(Properties config)
59         throws UnknownQualifierException, UnknownRelationException {
60         Vector attrs = new Vector();
61
62         if (qualifier != null) {
63             String s = config.getProperty(qualifier);
64             if (s == null)
65                 throw new UnknownQualifierException(qualifier);
66             attrs.add(s);
67         } else {
68             // ### get a default access point from properties?
69         }
70
71         if (relation != null) {
72             String rel = relation.getBase();
73             // ### handle "any" and "all"
74             String s = config.getProperty("cql-java.relation." + rel);
75             if (s == null)
76                 throw new UnknownRelationException(rel);
77             attrs.add(s);
78         } else {
79             // ### get a default relation from properties?
80         }
81
82         // ### handle position attributes
83         // ### handle structure attributes
84         // ### handle "always" attributes
85
86         // ### should split Vector elements on spaces
87         String s = "";
88         for (int i = 0; i < attrs.size(); i++) {
89             s += "@attr " + (String) attrs.get(i) + " ";
90         }
91
92         return s + maybeQuote(term);
93     }
94
95     static String maybeQuote(String str) {
96         // There _must_ be a better way to make this test ...
97         if (str.length() == 0 ||
98             str.indexOf('"') != -1 ||
99             str.indexOf(' ') != -1 ||
100             str.indexOf('\t') != -1 ||
101             str.indexOf('=') != -1 ||
102             str.indexOf('<') != -1 ||
103             str.indexOf('>') != -1 ||
104             str.indexOf('/') != -1 ||
105             str.indexOf('(') != -1 ||
106             str.indexOf(')') != -1) {
107             str = '"' + Utils.replaceString(str, "\"", "\\\"") + '"';
108         }
109
110         return str;
111     }
112 }