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