Implement new, wider toXCQL() API.
[cql-java-moved-to-github.git] / src / org / z3950 / zing / cql / CQLBooleanNode.java
1 // $Id: CQLBooleanNode.java,v 1.17 2007-07-03 13:40:13 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.17 2007-07-03 13:40:13 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 prefixes, Vector sortkeys) {
36         if (sortkeys != null)
37             throw new Error("CQLBooleanNode.toXCQL() called with sortkeys");
38
39         return (indent(level) + "<triple>\n" +
40                 renderPrefixes(level+1, prefixes) +
41                 ms.toXCQL(level+1, "boolean") +
42                 indent(level+1) + "<leftOperand>\n" +
43                 left.toXCQL(level+2) +
44                 indent(level+1) + "</leftOperand>\n" +
45                 indent(level+1) + "<rightOperand>\n" +
46                 right.toXCQL(level+2) +
47                 indent(level+1) + "</rightOperand>\n" +
48                 indent(level) + "</triple>\n");
49     }
50
51     public String toCQL() {
52         // ### We don't always need parens around the operands
53         return ("(" + left.toCQL() + ")" +
54                 " " + ms.toCQL() + " " +
55                 "(" + right.toCQL() + ")");
56     }
57
58     public String toPQF(Properties config) throws PQFTranslationException {
59         return ("@" + opPQF() +
60                 " " + left.toPQF(config) +
61                 " " + right.toPQF(config));
62     }
63
64     // represents the operation for PQF: overridden for CQLProxNode
65     String opPQF() { return ms.getBase(); }
66
67     public byte[] toType1BER(Properties config) throws PQFTranslationException {
68         System.out.println("in CQLBooleanNode.toType1BER(): PQF=" +
69                            toPQF(config));
70         byte[] rpn1 = left.toType1BER(config);
71         byte[] rpn2 = right.toType1BER(config);
72         byte[] op = opType1();
73         byte[] rpnStructure = new byte[rpn1.length+rpn2.length+op.length+4];
74         
75         // rpnRpnOp
76         int offset = putTag(CONTEXT, 1, CONSTRUCTED, rpnStructure, 0);
77
78         rpnStructure[offset++] = (byte)(0x80&0xff); // indefinite length
79         System.arraycopy(rpn1, 0, rpnStructure, offset, rpn1.length);
80         offset += rpn1.length;
81         System.arraycopy(rpn2, 0, rpnStructure, offset, rpn2.length);
82         offset += rpn2.length;
83         System.arraycopy(op, 0, rpnStructure, offset, op.length);
84         offset += op.length;
85         rpnStructure[offset++] = 0x00; // end rpnRpnOp
86         rpnStructure[offset++] = 0x00;
87         return rpnStructure;
88     }
89
90     abstract byte[] opType1();
91 }