-// $Id: CQLTermNode.java,v 1.1 2002-10-25 07:38:17 mike Exp $
+// $Id: CQLTermNode.java,v 1.5 2002-10-30 09:19:26 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.1 2002-10-25 07:38:17 mike Exp $
+ * @version $Id: CQLTermNode.java,v 1.5 2002-10-30 09:19:26 mike Exp $
*/
-class CQLTermNode extends CQLNode {
+public class CQLTermNode extends CQLNode {
private String qualifier;
- private String relation;
- private String value;
+ private CQLRelation relation;
+ private String term;
- public CQLTermNode(String qualifier, String relation, String value) {
+ public CQLTermNode(String qualifier, CQLRelation relation, String term) {
this.qualifier = qualifier;
this.relation = relation;
- this.value = value;
+ this.term = term;
}
String toXCQL(int level) {
return (indent(level) + "<searchClause>\n" +
- indent(level+1) + "<index>" + qualifier + "<index>\n" +
- indent(level+1) + "<relation>" + relation + "<relation>\n" +
- indent(level+1) + "<term>" + value + "<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 res = value;
+ String quotedQualifier = maybeQuote(qualifier);
+ String quotedTerm = maybeQuote(term);
- if (res.indexOf('"') != -1) {
- // ### precede each '"' with a '/'
- }
+ // ### We don't always need spaces around `relation'.
+ return quotedQualifier + " " + relation.toCQL() + " " + quotedTerm;
+ }
- if (res.indexOf('"') != -1 ||
- res.indexOf(' ') != -1 ||
- res.indexOf('\t') != -1 ||
- res.indexOf('=') != -1 ||
- res.indexOf('<') != -1 ||
- res.indexOf('>') != -1 ||
- res.indexOf('/') != -1 ||
- res.indexOf('(') != -1 ||
- res.indexOf(')') != -1) {
- res = '"' + 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 = '"' + replace(str, "\"", "\\\"") + '"';
}
- // ### The qualifier may need quoting.
- // ### We don't always need spaces around `relation'.
- return qualifier + " " + relation + " " + value;
+ return str;
}
}