Add override annotations
[cql-java-moved-to-github.git] / src / main / java / org / z3950 / zing / cql / CQLProxNode.java
1 // $Id: CQLProxNode.java,v 1.14 2007-06-29 12:53:03 mike Exp $
2
3 package org.z3950.zing.cql;
4
5
6 /**
7  * Represents a proximity node in a CQL parse-tree.
8  * The left- and right-hand-sides must be satisfied by parts of the
9  * candidate records which are sufficiently close to each other, as
10  * specified by a set of proximity parameters.
11  *
12  * @version     $Id: CQLProxNode.java,v 1.14 2007-06-29 12:53:03 mike Exp $
13  */
14 public class CQLProxNode extends CQLBooleanNode {
15     /**
16      * Creates a new PROX node with the specified left- and right-hand
17      * sides and modifiers.
18      */
19     public CQLProxNode(CQLNode left, CQLNode right, ModifierSet ms) {
20         super(left, right, ms);
21     }
22
23     /*
24      * proximity ::= exclusion distance ordered relation which-code unit-code.
25      * exclusion ::= '1' | '0' | 'void'.
26      * distance ::= integer.
27      * ordered ::= '1' | '0'.
28      * relation ::= integer.
29      * which-code ::= 'known' | 'private' | integer.
30      * unit-code ::= integer.
31      */
32     @Override
33     String opPQF() {
34         int relCode = getRelCode();
35         int unitCode = getProxUnitCode();
36
37         String res = "prox " +
38             "0 " +
39             ms.modifier("distance") + " " +
40             (ms.modifier("ordering").equals("ordered") ? 1 : 0) + " " +
41             relCode + " " +
42             "1 " +
43             unitCode;
44
45         return res;
46     }
47
48     private int getRelCode() {
49         String rel = ms.modifier("relation");
50         if (rel.equals("<")) {
51             return 1;
52         } else if (rel.equals("<=")) {
53             return 2;
54         } else if (rel.equals("=")) {
55             return 3;
56         } else if (rel.equals(">=")) {
57             return 4;
58         } else if (rel.equals(">")) {
59             return 5;
60         } else if (rel.equals("<>")) {
61             return 6;
62         }
63         return 0;
64     }
65
66     private int getProxUnitCode() {
67         String unit = ms.modifier("unit");
68         if (unit.equals("word")) {
69             return 2;
70         } else if (unit.equals("sentence")) {
71             return 3;
72         } else if (unit.equals("paragraph")) {
73             return 4;
74         } else if (unit.equals("element")) {
75             return 8;
76         }
77         return 0;
78     }
79
80
81     @Override
82     byte[] opType1() {
83         byte[] op = new byte[100];
84         int offset, value;
85         offset = putTag(CONTEXT, 46, CONSTRUCTED, op, 0); // Operator
86         op[offset++] = (byte)(0x80&0xff); // indefinite length
87
88         offset = putTag(CONTEXT, 3, CONSTRUCTED, op, offset); // prox
89         op[offset++] = (byte)(0x80&0xff); // indefinite length
90
91         offset = putTag(CONTEXT, 1, PRIMITIVE, op, offset); // exclusion
92         value = 0; // false
93         offset = putLen(numLen(value), op, offset);
94         offset = putNum(value, op, offset);
95
96         offset = putTag(CONTEXT, 2, PRIMITIVE, op, offset); // distance
97         value = Integer.parseInt(ms.modifier("distance"));
98         offset = putLen(numLen(value), op, offset);
99         offset = putNum(value, op, offset);
100
101         offset = putTag(CONTEXT, 3, PRIMITIVE, op, offset); // ordered
102         value = ms.modifier("ordering").equals("ordered") ? 1 : 0;
103         offset = putLen(numLen(value), op, offset);
104         offset = putNum(value, op, offset);
105
106         offset = putTag(CONTEXT, 4, PRIMITIVE, op, offset); // relationType
107         value = getRelCode();
108         offset = putLen(numLen(value), op, offset);
109         offset = putNum(value, op, offset);
110
111         offset = putTag(CONTEXT, 5, CONSTRUCTED, op, offset); // proximityUnitCode
112         op[offset++] = (byte)(0x80&0xff); // indefinite length
113         offset = putTag(CONTEXT, 1, PRIMITIVE, op, offset); // known
114         value = getProxUnitCode();
115         offset = putLen(numLen(value), op, offset);
116         offset = putNum(value, op, offset);
117         op[offset++] = 0x00; // end of proximityUnitCode
118         op[offset++] = 0x00;
119
120         op[offset++] = 0x00; // end of prox
121         op[offset++] = 0x00;
122         op[offset++] = 0x00; // end of Operator
123         op[offset++] = 0x00;
124
125         byte[] o = new byte[offset];
126         System.arraycopy(op, 0, o, 0, offset);
127         return o;
128     }
129 }