- Change the XCQL output to include the nasty and redundant
[cql-java-moved-to-github.git] / src / org / z3950 / zing / cql / CQLNode.java
1 // $Id: CQLNode.java,v 1.15 2002-11-20 01:15:15 mike Exp $
2
3 package org.z3950.zing.cql;
4 import java.util.Properties;
5 import java.util.Vector;
6
7
8 /**
9  * Represents a node in a CQL parse-tree.
10  *
11  * @version     $Id: CQLNode.java,v 1.15 2002-11-20 01:15:15 mike Exp $
12  */
13 public abstract class CQLNode {
14     CQLNode() {}                // prevent javadoc from documenting this
15
16     /**
17      * Translates a parse-tree into an XCQL document.
18      * <P>
19      * @param level
20      *  The number of levels to indent the top element of the XCQL
21      *  document.  This will typically be 0 when invoked by an
22      *  application; it takes higher values when this method is
23      *  invoked recursively for nodes further down the tree.
24      * @return
25      *  A String containing an XCQL document equivalent to the
26      *  parse-tree whose root is this node.
27      */
28     public String toXCQL(int level) {
29         return toXCQL(level, new Vector());
30     }
31
32     abstract public String toXCQL(int level, Vector prefixes);
33
34     protected static String renderPrefixes(int level, Vector prefixes) {
35         if (prefixes.size() == 0)
36             return "";
37         String res = indent(level) + "<prefixes>\n";
38         for (int i = 0; i < prefixes.size(); i++) {
39             CQLPrefix p = (CQLPrefix) prefixes.get(i);
40             res += indent(level+1) + "<prefix>\n";
41             if (p.name != null)
42                 res += indent(level+2) + "<name>" + p.name + "</name>\n";
43             res += indent(level+2) +
44                 "<identifier>" + p.identifier + "</identifier>\n";
45             res += indent(level+1) + "</prefix>\n";
46         }
47         return res + indent(level) + "</prefixes>\n";
48     }
49
50     /**
51      * Decompiles a parse-tree into a CQL query.
52      * <P>
53      * @return
54      *  A String containing a CQL query equivalent to the parse-tree
55      *  whose root is this node, so that compiling that query will
56      *  yield an identical tree.
57      */
58     abstract public String toCQL();
59
60     /**
61      * Renders a parse-tree into a Yaz-style PQF string.
62      * PQF, or Prefix Query Format, is a cryptic but powerful notation
63      * that can be trivially mapped, one-to-one, int Z39.50 Type-1 and
64      * Type-101 queries.  A specification for the format can be found
65      * in
66      * <A href="http://indexdata.dk/yaz/doc/tools.php#PQF"
67      *  >Chapter 7 (Supporting Tools)</A> of the
68      * <A href="http://indexdata.dk/yaz/">YAZ</A> manual.
69      * <P>
70      * @param config
71      *  A <TT>Properties</TT> object containing configuration
72      *  information that specifies the mapping from CQL qualifiers,
73      *  relations, etc. to Type-1 attributes.  The mapping
74      *  specification is described in the cql-java distribution's
75      *  sample PQF-mapping configuration file,
76      *  <TT>etc/pqf.properties</TT>, which see.
77      * @return
78      *  A String containing a PQF query equivalent to the parse-tree
79      *  whose root is this node.  This may be fed into the tool of
80      *  your choice to obtain a BER-encoded packet.
81      */
82     abstract public String toPQF(Properties config)
83         throws PQFTranslationException;
84
85     /**
86      * Returns a String of spaces for indenting to the specified level.
87      */
88     protected static String indent(int level) { return Utils.indent(level); }
89
90     /**
91      * Returns the argument String quoted for XML.
92      * For example, each occurrence of <TT>&lt;</TT> is translated to
93      * <TT>&amp;lt;</TT>.
94      */
95     protected static String xq(String str) { return Utils.xq(str); }
96 }