Mavenized.
[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     String opPQF() {
33         int relCode = getRelCode();
34         int unitCode = getProxUnitCode();
35
36         String res = "prox " +
37             "0 " +
38             ms.modifier("distance") + " " +
39             (ms.modifier("ordering").equals("ordered") ? 1 : 0) + " " +
40             relCode + " " +
41             "1 " +
42             unitCode;
43
44         return res;
45     }
46
47     private int getRelCode() {
48         String rel = ms.modifier("relation");
49         if (rel.equals("<")) {
50             return 1;
51         } else if (rel.equals("<=")) {
52             return 2;
53         } else if (rel.equals("=")) {
54             return 3;
55         } else if (rel.equals(">=")) {
56             return 4;
57         } else if (rel.equals(">")) {
58             return 5;
59         } else if (rel.equals("<>")) {
60             return 6;
61         }
62         return 0;
63     }
64
65     private int getProxUnitCode() {
66         String unit = ms.modifier("unit");
67         if (unit.equals("word")) {
68             return 2;
69         } else if (unit.equals("sentence")) {
70             return 3;
71         } else if (unit.equals("paragraph")) {
72             return 4;
73         } else if (unit.equals("element")) {
74             return 8;
75         }
76         return 0;
77     }
78
79
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 }