X-Git-Url: http://sru.miketaylor.org.uk/?a=blobdiff_plain;f=src%2Forg%2Fz3950%2Fzing%2Fcql%2FCQLTermNode.java;h=fda71474ade6a65488c1a043f2cf317d5d4952be;hb=f8154c71944186a9b64ddb782082a2026c5a912f;hp=da6c75d9b6539b915ffa7424647853a33ce0cdcb;hpb=9b7bbaf04b9492a1071d10fe5a1ce3f1ee60a0fc;p=cql-java-moved-to-github.git diff --git a/src/org/z3950/zing/cql/CQLTermNode.java b/src/org/z3950/zing/cql/CQLTermNode.java index da6c75d..fda7147 100644 --- a/src/org/z3950/zing/cql/CQLTermNode.java +++ b/src/org/z3950/zing/cql/CQLTermNode.java @@ -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) + "\n" + - indent(level+1) + "" + xq(qualifier) + "\n" + - indent(level+1) + "" + xq(relation) + "\n"+ - indent(level+1) + "" + xq(term) + "\n" + + indent(level+1) + "" + xq(qualifier) + "\n" + + relation.toXCQL(level+1) + + indent(level+1) + "" + xq(term) + "\n" + indent(level) + "\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; } }