Remove old proximity-specific parsing code.
[cql-java-moved-to-github.git] / src / org / z3950 / zing / cql / CQLProxNode.java
1 // $Id: CQLProxNode.java,v 1.13 2007-06-29 12:26:44 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.13 2007-06-29 12:26:44 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     String op() {
24         return ms.toCQL();
25     }
26
27     /*
28      * proximity ::= exclusion distance ordered relation which-code unit-code.
29      * exclusion ::= '1' | '0' | 'void'.
30      * distance ::= integer.
31      * ordered ::= '1' | '0'.
32      * relation ::= integer.
33      * which-code ::= 'known' | 'private' | integer.
34      * unit-code ::= integer.
35      */
36     String opPQF() {
37         int relCode = getRelCode();
38         int unitCode = getProxUnitCode();
39
40         String res = "prox " +
41             "0 " +
42             ms.modifier("distance") + " " +
43             (ms.modifier("ordering").equals("ordered") ? 1 : 0) + " " +
44             relCode + " " +
45             "1 " +
46             unitCode;
47
48         return res;
49     }
50
51     private int getRelCode() {
52         String rel = ms.modifier("relation");
53         if (rel.equals("<")) {
54             return 1;
55         } else if (rel.equals("<=")) {
56             return 2;
57         } else if (rel.equals("=")) {
58             return 3;
59         } else if (rel.equals(">=")) {
60             return 4;
61         } else if (rel.equals(">")) {
62             return 5;
63         } else if (rel.equals("<>")) {
64             return 6;
65         }
66         return 0;
67     }
68
69     private int getProxUnitCode() {
70         String unit = ms.modifier("unit");
71         if (unit.equals("word")) {
72             return 2;
73         } else if (unit.equals("sentence")) {
74             return 3;
75         } else if (unit.equals("paragraph")) {
76             return 4;
77         } else if (unit.equals("element")) {
78             return 8;
79         }
80         return 0;
81     }
82
83
84     byte[] opType1() {
85         byte[] op = new byte[100];
86         int offset, value;
87         offset = putTag(CONTEXT, 46, CONSTRUCTED, op, 0); // Operator
88         op[offset++] = (byte)(0x80&0xff); // indefinite length
89
90         offset = putTag(CONTEXT, 3, CONSTRUCTED, op, offset); // prox
91         op[offset++] = (byte)(0x80&0xff); // indefinite length
92
93         offset = putTag(CONTEXT, 1, PRIMITIVE, op, offset); // exclusion
94         value = 0; // false
95         offset = putLen(numLen(value), op, offset);
96         offset = putNum(value, op, offset);
97
98         offset = putTag(CONTEXT, 2, PRIMITIVE, op, offset); // distance
99         value = Integer.parseInt(ms.modifier("distance"));
100         offset = putLen(numLen(value), op, offset);
101         offset = putNum(value, op, offset);
102
103         offset = putTag(CONTEXT, 3, PRIMITIVE, op, offset); // ordered
104         value = ms.modifier("ordering").equals("ordered") ? 1 : 0;
105         offset = putLen(numLen(value), op, offset);
106         offset = putNum(value, op, offset);
107
108         offset = putTag(CONTEXT, 4, PRIMITIVE, op, offset); // relationType
109         value = getRelCode();
110         offset = putLen(numLen(value), op, offset);
111         offset = putNum(value, op, offset);
112
113         offset = putTag(CONTEXT, 5, CONSTRUCTED, op, offset); // proximityUnitCode
114         op[offset++] = (byte)(0x80&0xff); // indefinite length
115         offset = putTag(CONTEXT, 1, PRIMITIVE, op, offset); // known
116         value = getProxUnitCode();
117         offset = putLen(numLen(value), op, offset);
118         offset = putNum(value, op, offset);
119         op[offset++] = 0x00; // end of proximityUnitCode
120         op[offset++] = 0x00;
121
122         op[offset++] = 0x00; // end of prox
123         op[offset++] = 0x00;
124         op[offset++] = 0x00; // end of Operator
125         op[offset++] = 0x00;
126
127         byte[] o = new byte[offset];
128         System.arraycopy(op, 0, o, 0, offset);
129         return o;
130     }
131 }