1 // $Id: CQLNode.java,v 1.7 2002-10-30 09:19:26 mike Exp $
3 package org.z3950.zing.cql;
7 * Represents a node in a CQL parse-tree.
10 * @version $Id: CQLNode.java,v 1.7 2002-10-30 09:19:26 mike Exp $
12 public abstract class CQLNode {
13 abstract String toXCQL(int level);
14 abstract String toCQL();
16 protected String indent(int level) {
28 // This is hideously inefficient, but I just don't see a better
29 // way using the standard JAVA library.
31 protected String xq(String str) {
32 str = replace(str, "&", "&");
33 str = replace(str, "<", "<");
34 str = replace(str, ">", ">");
38 // I can't _believe_ I have to write this by hand in 2002 ...
39 protected static String replace(String str, String from, String to) {
40 StringBuffer sb = new StringBuffer();
41 int ix; // index of next `from'
42 int offset = 0; // index of previous `from' + length(from)
44 while ((ix = str.indexOf(from, offset)) != -1) {
45 sb.append(str.substring(offset, ix));
47 offset = ix + from.length();
50 // End of string: append last bit and we're done
51 sb.append(str.substring(offset));
56 public static void main (String[] args) {
57 CQLNode n1 = new CQLTermNode("dc.author", "=", "kernighan");
58 CQLNode n2 = new CQLTermNode("dc.title", "all", "elements style");
59 CQLNode root = new CQLAndNode(n1, n2);
60 System.out.println(root.toXCQL(0));