Use generics (what C++ programmers call templates)
[cql-java-moved-to-github.git] / src / org / z3950 / zing / cql / CQLBooleanNode.java
1 // $Id: CQLBooleanNode.java,v 1.14 2007-06-06 12:22:01 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.14 2007-06-06 12:22:01 mike Exp $
12  */
13 public abstract class CQLBooleanNode extends CQLNode {
14     CQLBooleanNode() {}         // prevent javadoc from documenting this
15
16     /**
17      * The root of a parse-tree representing the left-hand side.
18      */ 
19     public CQLNode left;
20
21     /**
22      * The root of a parse-tree representing the right-hand side.
23      */ 
24     public CQLNode right;
25
26     public String toXCQL(int level, Vector prefixes) {
27         // ### Should this use CQLNode.toXCQL(level+2, prefixes)?
28         return (indent(level) + "<triple>\n" +
29                 renderPrefixes(level+1, prefixes) +
30                 opXCQL(level+1) +
31                 indent(level+1) + "<leftOperand>\n" +
32                 left.toXCQL(level+2) +
33                 indent(level+1) + "</leftOperand>\n" +
34                 indent(level+1) + "<rightOperand>\n" +
35                 right.toXCQL(level+2) +
36                 indent(level+1) + "</rightOperand>\n" +
37                 indent(level) + "</triple>\n");
38     }
39
40     // Represents the boolean operation itself: overridden for CQLProxNode
41     String opXCQL(int level) {
42         return(indent(level) + "<boolean>\n" +
43                indent(level+1) + "<value>" + op() + "</value>\n" +
44                indent(level) + "</boolean>\n");
45     }
46
47     public String toCQL() {
48         // ### We don't always need parens around the operands
49         return "(" + left.toCQL() + ") " + op() + " (" + right.toCQL() + ")";
50     }
51
52     public String toPQF(Properties config) throws PQFTranslationException {
53         return ("@" + opPQF() +
54                 " " + left.toPQF(config) +
55                 " " + right.toPQF(config));
56     }
57
58     // represents the operation for PQF: overridden for CQLProxNode
59     String opPQF() { return op(); }
60
61     abstract String op();
62
63     public byte[] toType1BER(Properties config) throws PQFTranslationException {
64         System.out.println("in CQLBooleanNode.toType1BER(): PQF=" +
65                            toPQF(config));
66         byte[] rpn1 = left.toType1BER(config);
67         byte[] rpn2 = right.toType1BER(config);
68         byte[] op = opType1();
69         byte[] rpnStructure = new byte[rpn1.length+rpn2.length+op.length+4];
70         
71         // rpnRpnOp
72         int offset = putTag(CONTEXT, 1, CONSTRUCTED, rpnStructure, 0);
73
74         rpnStructure[offset++] = (byte)(0x80&0xff); // indefinite length
75         System.arraycopy(rpn1, 0, rpnStructure, offset, rpn1.length);
76         offset += rpn1.length;
77         System.arraycopy(rpn2, 0, rpnStructure, offset, rpn2.length);
78         offset += rpn2.length;
79         System.arraycopy(op, 0, rpnStructure, offset, op.length);
80         offset += op.length;
81         rpnStructure[offset++] = 0x00; // end rpnRpnOp
82         rpnStructure[offset++] = 0x00;
83         return rpnStructure;
84     }
85
86     abstract byte[] opType1();
87 }