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