Finish (more or less) to CQL-to-PQF translator.
[cql-java-moved-to-github.git] / src / org / z3950 / zing / cql / CQLNode.java
index 3267432..eab6db4 100644 (file)
@@ -1,62 +1,80 @@
-// $Id: CQLNode.java,v 1.7 2002-10-30 09:19:26 mike Exp $
+// $Id: CQLNode.java,v 1.12 2002-11-06 20:13:45 mike Exp $
 
 package org.z3950.zing.cql;
+import java.util.Properties;
 
 
 /**
  * Represents a node in a CQL parse-tree.
- * ###
  *
- * @version    $Id: CQLNode.java,v 1.7 2002-10-30 09:19:26 mike Exp $
+ * @version    $Id: CQLNode.java,v 1.12 2002-11-06 20:13:45 mike Exp $
  */
 public abstract class CQLNode {
-    abstract String toXCQL(int level);
-    abstract String toCQL();
-
-    protected String indent(int level) {
-       String x = "";
-       while (level-- > 0) {
-           x += "  ";
-       }
-       return x;
-    }
-
-    // XML Quote --
-    // s/&/&/g;
-    // s/</&lt;/g;
-    // s/>/&gt;/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, "&", "&amp;");
-       str = replace(str, "<", "&lt;");
-       str = replace(str, ">", "&gt;");
-       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 root = new CQLAndNode(n1, n2);
-       System.out.println(root.toXCQL(0));
-    }
+    CQLNode() {}               // prevent javadoc from documenting this
+
+    /**
+     * Translates a parse-tree into an XCQL document.
+     * <P>
+     * @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.
+     */
+    abstract public String toXCQL(int level);
+
+    /**
+     * Decompiles a parse-tree into a CQL query.
+     * <P>
+     * @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();
+
+    /**
+     * Renders a parse-tree into a Yaz-style PQF string.
+     * <P>
+     * <PRE>
+       query ::= top-set query-struct.
+       top-set ::= [ '@attrset' string ]
+       query-struct ::= attr-spec | simple | complex | '@term' term-type
+       attr-spec ::= '@attr' [ string ] string query-struct
+       complex ::= operator query-struct query-struct.
+       operator ::= '@and' | '@or' | '@not' | '@prox' proximity.
+       simple ::= result-set | term.
+       result-set ::= '@set' string.
+       term ::= string.
+       proximity ::= exclusion distance ordered relation which-code unit-code.
+       exclusion ::= '1' | '0' | 'void'.
+       distance ::= integer.
+       ordered ::= '1' | '0'.
+       relation ::= integer.
+       which-code ::= 'known' | 'private' | integer.
+       unit-code ::= integer.
+       term-type ::= 'general' | 'numeric' | 'string' | 'oid' | 'datetime' | 'null'.
+     * </PRE>
+     * @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;
+
+    /**
+     * Returns a String of spaces for indenting to the specified level.
+     */
+    protected static String indent(int level) { return Utils.indent(level); }
+
+    /**
+     * Returns the argument String quoted for XML.
+     * For example, each occurrence of <TT>&lt;</TT> is translated to
+     * <TT>&amp;lt;</TT>.
+     */
+    protected static String xq(String str) { return Utils.xq(str); }
 }