X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=src%2Forg%2Fz3950%2Fzing%2Fcql%2FCQLNode.java;h=5965da621c6eea1afa47700f5ebfad68da2e3d0d;hb=561a15208b3be53c0ad3a1eb5524badfc377c478;hp=3d16a37636653ddfbedcb760f30c67fa36997986;hpb=ac8fb2ba7ecad42eb920c65b7056e3a3e677275c;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 3d16a37..5965da6 100644 --- a/src/org/z3950/zing/cql/CQLNode.java +++ b/src/org/z3950/zing/cql/CQLNode.java @@ -1,61 +1,96 @@ -// $Id: CQLNode.java,v 1.6 2002-10-29 10:15:58 mike Exp $ +// $Id: CQLNode.java,v 1.15 2002-11-20 01:15:15 mike Exp $ package org.z3950.zing.cql; +import java.util.Properties; +import java.util.Vector; /** - * Represents a node in a CQL parse-tree ... - * ### + * Represents a node in a CQL parse-tree. * - * @version $Id: CQLNode.java,v 1.6 2002-10-29 10:15:58 mike Exp $ + * @version $Id: CQLNode.java,v 1.15 2002-11-20 01:15:15 mike Exp $ */ public abstract class CQLNode { - abstract String toXCQL(int level); - abstract String toCQL(); + CQLNode() {} // prevent javadoc from documenting this - protected String indent(int level) { - String x = ""; - while (level-- > 0) { - x += " "; - } - return x; + /** + * Translates a parse-tree into an XCQL document. + *

+ * @param level + * The number of levels to indent the top element of the XCQL + * document. This will typically be 0 when invoked by an + * application; it takes higher values when this method is + * invoked recursively for nodes further down the tree. + * @return + * A String containing an XCQL document equivalent to the + * parse-tree whose root is this node. + */ + public String toXCQL(int level) { + return toXCQL(level, new Vector()); } - // 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; + abstract public String toXCQL(int level, Vector prefixes); + + protected static String renderPrefixes(int level, Vector prefixes) { + if (prefixes.size() == 0) + return ""; + String res = indent(level) + "\n"; + for (int i = 0; i < prefixes.size(); i++) { + CQLPrefix p = (CQLPrefix) prefixes.get(i); + res += indent(level+1) + "\n"; + if (p.name != null) + res += indent(level+2) + "" + p.name + "\n"; + res += indent(level+2) + + "" + p.identifier + "\n"; + res += indent(level+1) + "\n"; + } + return res + indent(level) + "\n"; } - 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) + /** + * Decompiles a parse-tree into a CQL query. + *

+ * @return + * A String containing a CQL query equivalent to the parse-tree + * whose root is this node, so that compiling that query will + * yield an identical tree. + */ + abstract public String toCQL(); - while ((ix = str.indexOf(from, offset)) != -1) { - sb.append(str.substring(offset, ix)); - sb.append(to); - offset = ix + from.length(); - } + /** + * Renders a parse-tree into a Yaz-style PQF string. + * PQF, or Prefix Query Format, is a cryptic but powerful notation + * that can be trivially mapped, one-to-one, int Z39.50 Type-1 and + * Type-101 queries. A specification for the format can be found + * in + * Chapter 7 (Supporting Tools) of the + * YAZ manual. + *

+ * @param config + * A Properties object containing configuration + * information that specifies the mapping from CQL qualifiers, + * relations, etc. to Type-1 attributes. The mapping + * specification is described in the cql-java distribution's + * sample PQF-mapping configuration file, + * etc/pqf.properties, which see. + * @return + * A String containing a PQF query equivalent to the parse-tree + * whose root is this node. This may be fed into the tool of + * your choice to obtain a BER-encoded packet. + */ + abstract public String toPQF(Properties config) + throws PQFTranslationException; - // End of string: append last bit and we're done - sb.append(str.substring(offset)); - return sb.toString(); - } + /** + * Returns a String of spaces for indenting to the specified level. + */ + protected static String indent(int level) { return Utils.indent(level); } - // 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 root = new CQLAndNode(n1, n2); - System.out.println(root.toXCQL(0)); - } + /** + * Returns the argument String quoted for XML. + * For example, each occurrence of < is translated to + * &lt;. + */ + protected static String xq(String str) { return Utils.xq(str); } }