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