First semi-working version.
[cql-java-moved-to-github.git] / src / org / z3950 / zing / cql / CQLTermNode.java
1 // $Id: CQLTermNode.java,v 1.3 2002-10-25 16:56:43 mike Exp $
2
3 package org.z3950.zing.cql;
4
5
6 /**
7  * Represents a terminal node in a CQL parse-tree ...
8  * ###
9  *
10  * @version     $Id: CQLTermNode.java,v 1.3 2002-10-25 16:56:43 mike Exp $
11  */
12 public class CQLTermNode extends CQLNode {
13     private String qualifier;
14     private String relation;
15     private String term;
16
17     public CQLTermNode(String qualifier, String relation, String term) {
18         this.qualifier = qualifier;
19         this.relation = relation;
20         this.term = term;
21     }
22
23     String toXCQL(int level) {
24         return (indent(level) + "<searchClause>\n" +
25                 indent(level+1) + "<index>" + qualifier + "<index>\n" +
26                 indent(level+1) + "<relation>" + relation + "<relation>\n" +
27                 indent(level+1) + "<term>" + term + "<term>\n" +
28                 indent(level) + "</searchClause>\n");
29     }
30
31     String toCQL() {
32         String quotedTerm = term;
33
34         if (quotedTerm.indexOf('"') != -1) {
35             // ### precede each '"' with a '/'
36         }
37
38         // ### There must be a better way ...
39         if (quotedTerm.indexOf('"') != -1 ||
40             quotedTerm.indexOf(' ') != -1 ||
41             quotedTerm.indexOf('\t') != -1 ||
42             quotedTerm.indexOf('=') != -1 ||
43             quotedTerm.indexOf('<') != -1 ||
44             quotedTerm.indexOf('>') != -1 ||
45             quotedTerm.indexOf('/') != -1 ||
46             quotedTerm.indexOf('(') != -1 ||
47             quotedTerm.indexOf(')') != -1) {
48             quotedTerm = '"' + quotedTerm + '"';
49         }
50
51         // ### The qualifier may need quoting.
52         // ### We don't always need spaces around `relation'.
53         return qualifier + " " + relation + " " + quotedTerm;
54     }
55 }