a08fa6487ee0ee45dc3ee63f3dfd3333326c57e6
[cql-java-moved-to-github.git] / src / org / z3950 / zing / cql / CQLPrefixNode.java
1 // $Id: CQLPrefixNode.java,v 1.8 2007-06-27 22:39:55 mike Exp $
2
3 package org.z3950.zing.cql;
4 import java.lang.String;
5 import java.util.Properties;
6 import java.util.Vector;
7
8
9 /**
10  * Represents a prefix node in a CQL parse-tree.
11  *
12  * @version     $Id: CQLPrefixNode.java,v 1.8 2007-06-27 22:39:55 mike Exp $
13  */
14 public class CQLPrefixNode extends CQLNode {
15     /**
16      * The prefix definition that governs the subtree.
17      */
18     public CQLPrefix prefix;
19
20     /**
21      * The root of a parse-tree representing the part of the query
22      * that is governed by this prefix definition.
23      */ 
24     public CQLNode subtree;
25
26     /**
27      * Creates a new CQLPrefixNode inducing a mapping from the
28      * specified index-set name to the specified identifier across
29      * the specified subtree.
30      */
31     public CQLPrefixNode(String name, String identifier, CQLNode subtree) {
32         this.prefix = new CQLPrefix(name, identifier);
33         this.subtree = subtree;
34     }
35
36     public String toXCQL(int level, Vector<CQLPrefix> prefixes) {
37 //      String maybeName = "";
38 //      if (prefix.name != null)
39 //          maybeName = indent(level+1) + "<name>" + prefix.name + "</name>\n";
40 //
41 //      return (indent(level) + "<prefix>\n" + maybeName +
42 //              indent(level+1) +
43 //                  "<identifier>" + prefix.identifier + "</identifier>\n" +
44 //              subtree.toXCQL(level+1, prefixes) +
45 //              indent(level) + "</prefix>\n");
46         Vector<CQLPrefix> tmp = new Vector<CQLPrefix>(prefixes);
47         tmp.add(prefix);
48         return subtree.toXCQL(level, tmp);
49     }
50
51     public String toCQL() {
52         // ### We don't always need parens around the subtree
53         if (prefix.name == null) {
54             return ">\"" + prefix.identifier + "\" " +
55                 "(" + subtree.toCQL() + ")";
56         } else {
57             return ">" + prefix.name + "=\"" + prefix.identifier + "\" " +
58                 "(" + subtree.toCQL() + ")";
59         }
60     }
61
62     public String toPQF(Properties config) throws PQFTranslationException {
63         // Prefixes and their identifiers don't actually play any role
64         // in PQF translation, since the meanings of the indexes,
65         // including their prefixes if any, are instead wired into
66         // `config'.
67         return subtree.toPQF(config);
68     }
69
70     public byte[] toType1BER(Properties config) throws PQFTranslationException {
71         // See comment on toPQF()
72         return subtree.toType1BER(config);
73     }
74 }