ccd2b3f100c3e535b12cbd9d64ec193c4c8e45e6
[cql-java-moved-to-github.git] / src / org / z3950 / zing / ralph / CQLBooleanNode.java
1 // $Id: CQLBooleanNode.java,v 1.1 2002-12-04 16:54: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.1 2002-12-04 16:54: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         return (indent(level) + "<triple>\n" +
28                 renderPrefixes(level+1, prefixes) +
29                 opXCQL(level+1) +
30                 indent(level+1) + "<leftOperand>\n" +
31                 left.toXCQL(level+2, new Vector()) +
32                 indent(level+1) + "</leftOperand>\n" +
33                 indent(level+1) + "<rightOperand>\n" +
34                 right.toXCQL(level+2, new Vector()) +
35                 indent(level+1) + "</rightOperand>\n" +
36                 indent(level) + "</triple>\n");
37     }
38
39     // Represents the boolean operation itself: overridden for CQLProxNode
40     String opXCQL(int level) {
41         return(indent(level) + "<boolean>\n" +
42                indent(level+1) + "<value>" + op() + "</value>\n" +
43                indent(level) + "</boolean>\n");
44     }
45
46     public String toCQL() {
47         // ### We don't always need parens around the operands
48         return "(" + left.toCQL() + ") " + op() + " (" + right.toCQL() + ")";
49     }
50
51     public String toPQF(Properties config) throws PQFTranslationException {
52         return ("@" + opPQF() +
53                 " " + left.toPQF(config) +
54                 " " + right.toPQF(config));
55     }
56
57     public byte[] toType1(Properties config) throws PQFTranslationException {
58         System.out.println("in CQLBooleanNode.toType101(): PQF="+toPQF(config));
59         byte[] rpn1=left.toType1(config);
60         byte[] rpn2=right.toType1(config);
61         byte[] op=opType1();
62         byte[] rpnStructure=new byte[rpn1.length+rpn2.length+op.length+4];
63         
64         int offset=putTag(CONTEXT, 1, CONSTRUCTED, rpnStructure, 0); // rpnRpnOp
65         rpnStructure[offset++]=(byte)(0x80&0xff); // indefinite length
66         System.arraycopy(rpn1, 0, rpnStructure, offset, rpn1.length);
67         offset+=rpn1.length;
68         System.arraycopy(rpn2, 0, rpnStructure, offset, rpn2.length);
69         offset+=rpn2.length;
70         System.arraycopy(op, 0, rpnStructure, offset, op.length);
71         offset+=op.length;
72         rpnStructure[offset++]=0x00; // end rpnRpnOp
73         rpnStructure[offset++]=0x00;
74         return rpnStructure;
75     }
76
77     // represents the operation for PQF: overridden for CQLProxNode
78     String opPQF() { return op(); }
79
80     abstract String op();
81     abstract byte[] opType1();
82 }