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