1 // $Id: CQLNode.java,v 1.6 2002-10-29 10:15:58 mike Exp $
3 package org.z3950.zing.cql;
7 * Represents a node in a CQL parse-tree ...
10 * @version $Id: CQLNode.java,v 1.6 2002-10-29 10:15:58 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 String replace(String str, String from, String to) {
39 StringBuffer sb = new StringBuffer();
40 int ix; // index of next `from'
41 int offset = 0; // index of previous `from' + length(from)
43 while ((ix = str.indexOf(from, offset)) != -1) {
44 sb.append(str.substring(offset, ix));
46 offset = ix + from.length();
49 // End of string: append last bit and we're done
50 sb.append(str.substring(offset));
55 public static void main (String[] args) {
56 CQLNode n1 = new CQLTermNode("dc.author", "=", "kernighan");
57 CQLNode n2 = new CQLTermNode("dc.title", "all", "elements style");
58 CQLNode root = new CQLAndNode(n1, n2);
59 System.out.println(root.toXCQL(0));