All sorts of changes. Generally moving towards first release.
[cql-java-moved-to-github.git] / src / org / z3950 / zing / cql / CQLTermNode.java
index da6c75d..fda7147 100644 (file)
@@ -1,20 +1,20 @@
-// $Id: CQLTermNode.java,v 1.4 2002-10-27 00:46:25 mike Exp $
+// $Id: CQLTermNode.java,v 1.6 2002-10-31 22:22:01 mike Exp $
 
 package org.z3950.zing.cql;
 
 
 /**
- * Represents a terminal node in a CQL parse-tree ...
- * ###
+ * Represents a terminal node in a CQL parse-tree.
+ * ##
  *
- * @version    $Id: CQLTermNode.java,v 1.4 2002-10-27 00:46:25 mike Exp $
+ * @version    $Id: CQLTermNode.java,v 1.6 2002-10-31 22:22:01 mike Exp $
  */
 public class CQLTermNode extends CQLNode {
     private String qualifier;
-    private String relation;
+    private CQLRelation relation;
     private String term;
 
-    public CQLTermNode(String qualifier, String relation, String term) {
+    public CQLTermNode(String qualifier, CQLRelation relation, String term) {
        this.qualifier = qualifier;
        this.relation = relation;
        this.term = term;
@@ -22,34 +22,40 @@ public class CQLTermNode extends CQLNode {
 
     String toXCQL(int level) {
        return (indent(level) + "<searchClause>\n" +
-               indent(level+1) + "<index>" + xq(qualifier) + "<index>\n" +
-               indent(level+1) + "<relation>" + xq(relation) + "<relation>\n"+
-               indent(level+1) + "<term>" + xq(term) + "<term>\n" +
+               indent(level+1) + "<index>" + xq(qualifier) + "</index>\n" +
+               relation.toXCQL(level+1) +
+               indent(level+1) + "<term>" + xq(term) + "</term>\n" +
                indent(level) + "</searchClause>\n");
     }
 
     String toCQL() {
-       String quotedTerm = term;
+       String quotedQualifier = maybeQuote(qualifier);
+       String quotedTerm = maybeQuote(term);
+       String res = quotedTerm;
 
-       if (quotedTerm.indexOf('"') != -1) {
-           // ### precede each '"' with a '/'
+       if (!qualifier.equalsIgnoreCase("srw.serverChoice")) {
+           // ### We don't always need spaces around `relation'.
+           res = quotedQualifier + " " + relation.toCQL() + " " + quotedTerm;
        }
 
-       // ### There must be a better way ...
-       if (quotedTerm.indexOf('"') != -1 ||
-           quotedTerm.indexOf(' ') != -1 ||
-           quotedTerm.indexOf('\t') != -1 ||
-           quotedTerm.indexOf('=') != -1 ||
-           quotedTerm.indexOf('<') != -1 ||
-           quotedTerm.indexOf('>') != -1 ||
-           quotedTerm.indexOf('/') != -1 ||
-           quotedTerm.indexOf('(') != -1 ||
-           quotedTerm.indexOf(')') != -1) {
-           quotedTerm = '"' + quotedTerm + '"';
+       return res;
+    }
+
+    static String maybeQuote(String str) {
+       // There _must_ be a better way to make this test ...
+       if (str.length() == 0 ||
+           str.indexOf('"') != -1 ||
+           str.indexOf(' ') != -1 ||
+           str.indexOf('\t') != -1 ||
+           str.indexOf('=') != -1 ||
+           str.indexOf('<') != -1 ||
+           str.indexOf('>') != -1 ||
+           str.indexOf('/') != -1 ||
+           str.indexOf('(') != -1 ||
+           str.indexOf(')') != -1) {
+           str = '"' + Utils.replaceString(str, "\"", "\\\"") + '"';
        }
 
-       // ### The qualifier may need quoting.
-       // ### We don't always need spaces around `relation'.
-       return qualifier + " " + relation + " " + quotedTerm;
+       return str;
     }
 }