1 // $Id: CQLNode.java,v 1.17 2002-12-06 12:35:15 mike Exp $
3 package org.z3950.zing.cql;
4 import java.util.Properties;
5 import java.util.Vector;
9 * Represents a node in a CQL parse-tree.
11 * @version $Id: CQLNode.java,v 1.17 2002-12-06 12:35:15 mike Exp $
13 public abstract class CQLNode {
14 CQLNode() {} // prevent javadoc from documenting this
17 * Translates a parse-tree into an XCQL document.
20 * The number of levels to indent the top element of the XCQL
21 * document. This will typically be 0 when invoked by an
22 * application; it takes higher values when this method is
23 * invoked recursively for nodes further down the tree.
25 * A String containing an XCQL document equivalent to the
26 * parse-tree whose root is this node.
28 public String toXCQL(int level) {
29 return toXCQL(level, new Vector());
32 abstract public String toXCQL(int level, Vector prefixes);
34 protected static String renderPrefixes(int level, Vector prefixes) {
35 if (prefixes.size() == 0)
37 String res = indent(level) + "<prefixes>\n";
38 for (int i = 0; i < prefixes.size(); i++) {
39 CQLPrefix p = (CQLPrefix) prefixes.get(i);
40 res += indent(level+1) + "<prefix>\n";
42 res += indent(level+2) + "<name>" + p.name + "</name>\n";
43 res += indent(level+2) +
44 "<identifier>" + p.identifier + "</identifier>\n";
45 res += indent(level+1) + "</prefix>\n";
47 return res + indent(level) + "</prefixes>\n";
51 * Decompiles a parse-tree into a CQL query.
54 * A String containing a CQL query equivalent to the parse-tree
55 * whose root is this node, so that compiling that query will
56 * yield an identical tree.
58 abstract public String toCQL();
61 * Renders a parse-tree into a Yaz-style PQF string.
62 * PQF, or Prefix Query Format, is a cryptic but powerful notation
63 * that can be trivially mapped, one-to-one, int Z39.50 Type-1 and
64 * Type-101 queries. A specification for the format can be found
66 * <A href="http://indexdata.dk/yaz/doc/tools.php#PQF"
67 * >Chapter 7 (Supporting Tools)</A> of the
68 * <A href="http://indexdata.dk/yaz/">YAZ</A> manual.
71 * A <TT>Properties</TT> object containing configuration
72 * information that specifies the mapping from CQL qualifiers,
73 * relations, etc. to Type-1 attributes. The mapping
74 * specification is described in the cql-java distribution's
75 * sample PQF-mapping configuration file,
76 * <TT>etc/pqf.properties</TT>, which see.
78 * A String containing a PQF query equivalent to the parse-tree
79 * whose root is this node. This may be fed into the tool of
80 * your choice to obtain a BER-encoded packet.
82 abstract public String toPQF(Properties config)
83 throws PQFTranslationException;
86 * Returns a String of spaces for indenting to the specified level.
88 protected static String indent(int level) { return Utils.indent(level); }
91 * Returns the argument String quoted for XML.
92 * For example, each occurrence of <TT><</TT> is translated to
95 protected static String xq(String str) { return Utils.xq(str); }
99 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/layman.asc
101 abstract public byte[] toType1(Properties config)
102 throws PQFTranslationException;
105 protected static final int UNIVERSAL = 0;
106 protected static final int APPLICATION = 1;
107 protected static final int CONTEXT = 2;
108 protected static final int PRIVATE = 3;
111 protected static final int PRIMITIVE = 0;
112 protected static final int CONSTRUCTED = 1;
114 // ASN.1 UNIVERSAL data types
115 public static final byte BOOLEAN = 1;
116 public static final byte INTEGER = 2;
117 public static final byte BITSTRING = 3;
118 public static final byte OCTETSTRING = 4;
119 public static final byte NULL = 5;
120 public static final byte OBJECTIDENTIFIER = 6;
121 public static final byte OBJECTDESCRIPTOR = 7;
122 public static final byte EXTERNAL = 8;
123 public static final byte ENUMERATED = 10;
124 public static final byte SEQUENCE = 16;
125 public static final byte SET = 17;
126 public static final byte VISIBLESTRING = 26;
127 public static final byte GENERALSTRING = 27;
129 protected static final int putTag(int asn1class, int fldid, int form,
130 byte[] record, int offset) {
132 record[offset++] = (byte)(fldid + asn1class*64 + form*32);
134 record[offset++] = (byte)(31 + asn1class*64 + form*32);
136 record[offset++] = (byte)(fldid);
138 record[offset++] = (byte)(128 + fldid/128);
139 record[offset++] = (byte)(fldid % 128);
146 * Put a length directly into a BER record.
148 * @param length length to put into record
149 * @return the new, incremented value of the offset parameter.
151 public // ### shouldn't this be protected?
152 static final int putLen(int len, byte[] record, int offset) {
155 record[offset++] = (byte)len;
158 record[offset] = (byte)(lenLen(len) - 1);
159 for (t = record[offset]; t > 0; t--) {
160 record[offset+t] = (byte)(len & 0xff);
164 offset += (record[offset]&0xff) + 1;
165 record[t] += 128; // turn on bit 8 in length byte.
171 * Get the length needed to represent the given length.
173 * @param length determine length needed to encode this
174 * @return length needed to encode given length
176 protected // ### shouldn't this be private?
177 static final int lenLen(int length) {
179 return ((length < 128) ? 1 :
181 (length < 65536L) ? 3 : 4);
185 * Get the length needed to represent the given number.
187 * @param number determine length needed to encode this
188 * @return length needed to encode given number
190 protected static final int numLen(long num) {
191 num = num < 0 ? -num : num;
192 // ### Wouldn't this be better done algorithmically?
193 // Or at least with the constants expressed in hex?
194 return ((num < 128) ? 1 :
196 (num < 8388608) ? 3 :
197 (num < 2147483648L) ? 4 :
198 (num < 549755813888L) ? 5 :
199 (num < 140737488355328L) ? 6 :
200 (num < 36028797018963968L) ? 7 : 8);
204 * Put a number into a given buffer
206 * @param num number to put into buffer
207 * @param record buffer to use
208 * @param offset offset into buffer
209 * @return the new, incremented value of the offset parameter.
211 protected static final int putNum(long num, byte record[], int offset) {
214 for (int count = cnt - 1; count >= 0; count--) {
215 record[offset+count] = (byte)(num & 0xff);
221 // Used only by the makeOID() method
222 private static final java.util.Hashtable madeOIDs =
223 new java.util.Hashtable(10);
225 protected static final byte[] makeOID(String oid) {
227 int dot, offset = 0, oidOffset = 0, value;
229 if ((o = (byte[])madeOIDs.get(oid)) == null) {
232 // Isn't this kind of thing excruciating in Java?
233 while (oidOffset < oid.length() &&
234 Character.isDigit(oid.charAt(oidOffset)) == true) {
235 if (offset > 90) // too large
238 dot = oid.indexOf('.', oidOffset);
242 value = Integer.parseInt(oid.substring(oidOffset, dot));
244 if (offset == 0) { // 1st two are special
245 if (dot == -1) // ### can't happen: -1 is reassigned above
246 return null; // can't be this short
247 oidOffset = dot+1; // skip past '.'
249 dot = oid.indexOf('.', oidOffset);
255 Integer.parseInt(oid.substring(oidOffset,dot));
259 o[offset++] = (byte)value;
262 byte bits[] = new byte[12]; // save a 84 (12*7) bit number
265 bits[count++] = (byte)(value & 0x7f);
269 // Now place in the correct order
271 o[offset++] = (byte)(bits[count] | 0x80);
273 o[offset++] = bits[count];
276 dot = oid.indexOf('.', oidOffset);
283 byte[] ptr = new byte[offset];
284 System.arraycopy(o, 0, ptr, 0, offset);
285 madeOIDs.put(oid, ptr);
291 public static final byte[] makeQuery(CQLNode root, Properties properties)
292 throws PQFTranslationException {
293 byte[] rpnStructure = root.toType1(properties);
294 byte[] qry = new byte[rpnStructure.length+100];
296 offset = putTag(CONTEXT, 1, CONSTRUCTED, qry, offset);
297 qry[offset++] = (byte)(0x80&0xff); // indefinite length
298 offset = putTag(UNIVERSAL, OBJECTIDENTIFIER, PRIMITIVE, qry, offset);
299 byte[] oid = makeOID("1.2.840.10003.3.1"); // bib-1
300 offset = putLen(oid.length, qry, offset);
301 System.arraycopy(oid, 0, qry, offset, oid.length);
302 offset += oid.length;
303 System.arraycopy(rpnStructure, 0, qry, offset, rpnStructure.length);
304 offset += rpnStructure.length;
305 qry[offset++] = 0x00; // end of query
306 qry[offset++] = 0x00;
307 byte[] q = new byte[offset];
308 System.arraycopy(qry, 0, q, 0, offset);