1 // $Id: CQLProxNode.java,v 1.9 2007-06-27 22:15:04 mike Exp $
3 package org.z3950.zing.cql;
4 import java.util.Vector;
8 * Represents a proximity node in a CQL parse-tree.
9 * The left- and right-hand-sides must be satisfied by parts of the
10 * candidate records which are sufficiently close to each other, as
11 * specified by a set of proximity parameters.
13 * @version $Id: CQLProxNode.java,v 1.9 2007-06-27 22:15:04 mike Exp $
15 public class CQLProxNode extends CQLBooleanNode {
19 * Creates a new, <I>incomplete</I>, proximity node with the
20 * specified left-hand side. No right-hand side is specified at
21 * this stage: that must be specified later, using the
22 * <TT>addSecondSubterm()</TT> method. (That may seem odd, but
23 * it's just easier to write the parser that way.)
25 * Proximity paramaters may be added at any time, before or after
26 * the right-hand-side sub-tree is added.
28 public CQLProxNode(CQLNode left) {
29 ms = new ModifierSet("prox");
31 // this.right left unresolved for now ...
35 * Sets the right-hand side of the proximity node whose
36 * left-hand-side was specified at creation time.
38 public void addSecondSubterm(CQLNode right) {
43 * Adds a modifier of the specified <TT>type</TT>,
44 * <TT>comparison</TT> and <TT>value</TT> to a proximity node.
46 public void addModifier(String type, String comparison, String value) {
47 ms.addModifier(type, comparison, value);
51 * Returns an array of the modifiers associated with a proximity
54 * An array of Modifier objects.
56 public Vector<Modifier> getModifiers() {
57 return ms.getModifiers();
64 String opXCQL(int level) {
65 return ms.toXCQL(level, "boolean");
69 * proximity ::= exclusion distance ordered relation which-code unit-code.
70 * exclusion ::= '1' | '0' | 'void'.
71 * distance ::= integer.
72 * ordered ::= '1' | '0'.
73 * relation ::= integer.
74 * which-code ::= 'known' | 'private' | integer.
75 * unit-code ::= integer.
78 int relCode = getRelCode();
79 int unitCode = getProxUnitCode();
81 String res = "prox " +
83 ms.modifier("distance") + " " +
84 (ms.modifier("ordering").equals("ordered") ? 1 : 0) + " " +
92 private int getRelCode() {
93 String rel = ms.modifier("relation");
94 if (rel.equals("<")) {
96 } else if (rel.equals("<=")) {
98 } else if (rel.equals("=")) {
100 } else if (rel.equals(">=")) {
102 } else if (rel.equals(">")) {
104 } else if (rel.equals("<>")) {
110 private int getProxUnitCode() {
111 String unit = ms.modifier("unit");
112 if (unit.equals("word")) {
114 } else if (unit.equals("sentence")) {
116 } else if (unit.equals("paragraph")) {
118 } else if (unit.equals("element")) {
126 byte[] op = new byte[100];
128 offset = putTag(CONTEXT, 46, CONSTRUCTED, op, 0); // Operator
129 op[offset++] = (byte)(0x80&0xff); // indefinite length
131 offset = putTag(CONTEXT, 3, CONSTRUCTED, op, offset); // prox
132 op[offset++] = (byte)(0x80&0xff); // indefinite length
134 offset = putTag(CONTEXT, 1, PRIMITIVE, op, offset); // exclusion
136 offset = putLen(numLen(value), op, offset);
137 offset = putNum(value, op, offset);
139 offset = putTag(CONTEXT, 2, PRIMITIVE, op, offset); // distance
140 value = Integer.parseInt(ms.modifier("distance"));
141 offset = putLen(numLen(value), op, offset);
142 offset = putNum(value, op, offset);
144 offset = putTag(CONTEXT, 3, PRIMITIVE, op, offset); // ordered
145 value = ms.modifier("ordering").equals("ordered") ? 1 : 0;
146 offset = putLen(numLen(value), op, offset);
147 offset = putNum(value, op, offset);
149 offset = putTag(CONTEXT, 4, PRIMITIVE, op, offset); // relationType
150 value = getRelCode();
151 offset = putLen(numLen(value), op, offset);
152 offset = putNum(value, op, offset);
154 offset = putTag(CONTEXT, 5, CONSTRUCTED, op, offset); // proximityUnitCode
155 op[offset++] = (byte)(0x80&0xff); // indefinite length
156 offset = putTag(CONTEXT, 1, PRIMITIVE, op, offset); // known
157 value = getProxUnitCode();
158 offset = putLen(numLen(value), op, offset);
159 offset = putNum(value, op, offset);
160 op[offset++] = 0x00; // end of proximityUnitCode
163 op[offset++] = 0x00; // end of prox
165 op[offset++] = 0x00; // end of Operator
168 byte[] o = new byte[offset];
169 System.arraycopy(op, 0, o, 0, offset);