Import statics directly, bump version
[cql-java-moved-to-github.git] / src / main / java / org / z3950 / zing / cql / CQLBooleanNode.java
1 // $Id: CQLBooleanNode.java,v 1.18 2007-07-03 16:03:00 mike Exp $
2
3 package org.z3950.zing.cql;
4 import java.util.List;
5 import java.util.Properties;
6
7 import static org.z3950.zing.cql.Utils.*;
8
9
10 /**
11  * Represents a boolean node in a CQL parse-tree.
12  *
13  * @version     $Id: CQLBooleanNode.java,v 1.18 2007-07-03 16:03:00 mike Exp $
14  */
15 public abstract class CQLBooleanNode extends CQLNode {
16
17     private CQLNode left;
18
19     /**
20      * The root of a parse-tree representing the left-hand side.
21      */
22     public CQLNode getLeft() {
23         return left;
24     }
25
26     private CQLNode right;
27
28     /**
29      * The root of a parse-tree representing the right-hand side.
30      */
31     public CQLNode getRight() {
32         return right;
33     }
34
35     ModifierSet ms;
36     /**
37      * The set of modifiers that are applied to this boolean.
38      */
39     public List<Modifier> getModifiers() {
40         return ms.getModifiers();
41     }
42
43     protected CQLBooleanNode(CQLNode left, CQLNode right, ModifierSet ms) {
44         this.left = left;
45         this.right = right;
46         this.ms = ms;
47     }
48
49     @Override
50     public String toXCQL(int level, List<CQLPrefix> prefixes,
51                          List<ModifierSet> sortkeys) {
52         return (indent(level) + "<triple>\n" +
53                 renderPrefixes(level+1, prefixes) +
54                 ms.toXCQL(level+1, "boolean") +
55                 indent(level+1) + "<leftOperand>\n" +
56                 left.toXCQL(level+2) +
57                 indent(level+1) + "</leftOperand>\n" +
58                 indent(level+1) + "<rightOperand>\n" +
59                 right.toXCQL(level+2) +
60                 indent(level+1) + "</rightOperand>\n" +
61                 renderSortKeys(level+1, sortkeys) +
62                 indent(level) + "</triple>\n");
63     }
64
65     @Override
66     public String toCQL() {
67         // ### We don't always need parens around the operands
68         return ("(" + left.toCQL() + ")" +
69                 " " + ms.toCQL() + " " +
70                 "(" + right.toCQL() + ")");
71     }
72
73     @Override
74     public String toPQF(Properties config) throws PQFTranslationException {
75         return ("@" + opPQF() +
76                 " " + left.toPQF(config) +
77                 " " + right.toPQF(config));
78     }
79
80     // represents the operation for PQF: overridden for CQLProxNode
81     String opPQF() { return ms.getBase(); }
82
83     @Override
84     public byte[] toType1BER(Properties config) throws PQFTranslationException {
85         System.out.println("in CQLBooleanNode.toType1BER(): PQF=" +
86                            toPQF(config));
87         byte[] rpn1 = left.toType1BER(config);
88         byte[] rpn2 = right.toType1BER(config);
89         byte[] op = opType1();
90         byte[] rpnStructure = new byte[rpn1.length+rpn2.length+op.length+4];
91         
92         // rpnRpnOp
93         int offset = putTag(CONTEXT, 1, CONSTRUCTED, rpnStructure, 0);
94
95         rpnStructure[offset++] = (byte)(0x80&0xff); // indefinite length
96         System.arraycopy(rpn1, 0, rpnStructure, offset, rpn1.length);
97         offset += rpn1.length;
98         System.arraycopy(rpn2, 0, rpnStructure, offset, rpn2.length);
99         offset += rpn2.length;
100         System.arraycopy(op, 0, rpnStructure, offset, op.length);
101         offset += op.length;
102         rpnStructure[offset++] = 0x00; // end rpnRpnOp
103         rpnStructure[offset++] = 0x00;
104         return rpnStructure;
105     }
106
107     abstract byte[] opType1();
108 }