X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=src%2Forg%2Fz3950%2Fzing%2Fcql%2FCQLNode.java;h=5d731b74f924d95fedc650179c2320ecbe6b29b5;hb=caeab95a0bbe3a694746bb3244e6605c36927c1e;hp=b619bbb827ccd613b503428cf5c65361f93041e7;hpb=dbe5e770526df9b0b8d09cfce0ebe23c3fd00bad;p=cql-java-moved-to-github.git diff --git a/src/org/z3950/zing/cql/CQLNode.java b/src/org/z3950/zing/cql/CQLNode.java index b619bbb..5d731b7 100644 --- a/src/org/z3950/zing/cql/CQLNode.java +++ b/src/org/z3950/zing/cql/CQLNode.java @@ -1,13 +1,13 @@ -// $Id: CQLNode.java,v 1.3 2002-10-25 16:04:44 mike Exp $ +// $Id: CQLNode.java,v 1.8 2002-10-30 11:13:18 mike Exp $ package org.z3950.zing.cql; /** - * Represents a node in a CQL parse-tree ... + * Represents a node in a CQL parse-tree. * ### * - * @version $Id: CQLNode.java,v 1.3 2002-10-25 16:04:44 mike Exp $ + * @version $Id: CQLNode.java,v 1.8 2002-10-30 11:13:18 mike Exp $ */ public abstract class CQLNode { abstract String toXCQL(int level); @@ -21,11 +21,46 @@ public abstract class CQLNode { return x; } + // XML Quote -- + // s/&/&/g; + // s//>/g; + // This is hideously inefficient, but I just don't see a better + // way using the standard JAVA library. + // + protected String xq(String str) { + str = replace(str, "&", "&"); + str = replace(str, "<", "<"); + str = replace(str, ">", ">"); + return str; + } + + // I can't _believe_ I have to write this by hand in 2002 ... + protected static String replace(String str, String from, String to) { + StringBuffer sb = new StringBuffer(); + int ix; // index of next `from' + int offset = 0; // index of previous `from' + length(from) + + while ((ix = str.indexOf(from, offset)) != -1) { + sb.append(str.substring(offset, ix)); + sb.append(to); + offset = ix + from.length(); + } + + // End of string: append last bit and we're done + sb.append(str.substring(offset)); + return sb.toString(); + } + // Test harness public static void main (String[] args) { - CQLNode n1 = new CQLTermNode("dc.author", "=", "kernighan"); - CQLNode n2 = new CQLTermNode("dc.title", "all", "elements style"); + CQLNode n1 = new CQLTermNode("dc.author", + new CQLRelation("="), + "kernighan"); + CQLNode n2 = new CQLTermNode("dc.title", + new CQLRelation("all"), + "elements style"); CQLNode root = new CQLAndNode(n1, n2); - System.out.println(root.toXCQL(3)); + System.out.println(root.toXCQL(0)); } }