2 package org.z3950.zing.cql;
3 import java.util.HashMap;
6 import java.util.Properties;
11 * Represents a node in a CQL parse-tree.
14 public abstract class CQLNode {
17 * Returns the name of the result-set to which this query is a
18 * reference, if and only if the entire query consists only of a
19 * result-set reference. If it's anything else, including a
20 * boolean combination of a result-set reference with something
21 * else, then null is returned instead.
22 * @return the name of the referenced result-set
24 public String getResultSetName() {
29 * Translates a parse-tree into an XCQL document.
32 * A String containing an XCQL document equivalent to the
33 * parse-tree whose root is this node.
35 public String toXCQL() {
36 StringBuilder sb = new StringBuilder();
37 toXCQLInternal(new XCQLBuilder(sb), 0);
41 void toXCQLInternal(XCQLBuilder b, int level) {
42 toXCQLInternal(b, level, null, null);
45 abstract void toXCQLInternal(XCQLBuilder b, int level,
46 List<CQLPrefix> prefixes, List<ModifierSet> sortkeys);
48 static void renderPrefixes(XCQLBuilder b,
49 int level, List<CQLPrefix> prefixes) {
50 if (prefixes == null || prefixes.size() == 0)
52 b.indent(level).append("<prefixes>\n");
53 for (int i = 0; i < prefixes.size(); i++) {
54 CQLPrefix p = prefixes.get(i);
55 b.indent(level+1).append("<prefix>\n");
57 b.indent(level + 2).append("<name>").
58 append(p.name).append("</name>\n");
59 b.indent(level + 2).append("<identifier>").
60 append(p.identifier).append("</identifier>\n");
61 b.indent(level+1).append("</prefix>\n");
63 b.indent(level).append("</prefixes>\n");
66 static void renderSortKeys(XCQLBuilder b, int level,
67 List<ModifierSet> sortkeys) {
68 if (sortkeys == null || sortkeys.size() == 0)
70 b.indent(level).append("<sortKeys>\n");
71 for (int i = 0; i < sortkeys.size(); i++) {
72 ModifierSet key = sortkeys.get(i);
73 key.toXCQLInternal(b, level+1, "key", "index");
75 b.indent(level).append("</sortKeys>\n");
79 * Decompiles a parse-tree into a CQL query.
82 * A String containing a CQL query equivalent to the parse-tree
83 * whose root is this node, so that compiling that query will
84 * yield an identical tree.
86 abstract public String toCQL();
89 * Renders a parse-tree into a Yaz-style PQF string.
90 * PQF, or Prefix Query Format, is a cryptic but powerful notation
91 * that can be trivially mapped, one-to-one, int Z39.50 Type-1 and
92 * Type-101 queries. A specification for the format can be found
94 * <A href="http://indexdata.dk/yaz/doc/tools.php#PQF"
95 * >Chapter 7 (Supporting Tools)</A> of the
96 * <A href="http://indexdata.dk/yaz/">YAZ</A> manual.
99 * A <TT>Properties</TT> object containing configuration
100 * information that specifies the mapping from CQL indexes,
101 * relations, etc. to Type-1 attributes. The mapping
102 * specification is described in the CQL-Java distribution's
103 * sample PQF-mapping configuration file,
104 * <TT>etc/pqf.properties</TT>, which see.
106 * A String containing a PQF query equivalent to the parse-tree
107 * whose root is this node.
109 abstract public String toPQF(Properties config)
110 throws PQFTranslationException;
113 * Renders a parser-tree into a BER-endoded packet representing an
114 * equivalent Z39.50 Type-1 query. If you don't know what that
115 * means, then you don't need this method :-) This is useful
116 * primarily for SRW-to-Z39.50 gateways.
119 * A <TT>Properties</TT> object containing configuration
120 * information that specifies the mapping from CQL indexes,
121 * relations, etc. to Type-1 attributes. The mapping
122 * specification is described in the CQL-Java distribution's
123 * sample PQF-mapping configuration file,
124 * <TT>etc/pqf.properties</TT>, which see.
126 * A byte array containing the BER packet.
128 * <A href="ftp://ftp.rsasecurity.com/pub/pkcs/ascii/layman.asc"
129 * >ftp://ftp.rsasecurity.com/pub/pkcs/ascii/layman.asc</A>
131 abstract public byte[] toType1BER(Properties config)
132 throws PQFTranslationException;
135 protected static final int UNIVERSAL = 0;
136 protected static final int APPLICATION = 1;
137 protected static final int CONTEXT = 2;
138 protected static final int PRIVATE = 3;
141 protected static final int PRIMITIVE = 0;
142 protected static final int CONSTRUCTED = 1;
144 // ASN.1 UNIVERSAL data types
145 public static final byte BOOLEAN = 1;
146 public static final byte INTEGER = 2;
147 public static final byte BITSTRING = 3;
148 public static final byte OCTETSTRING = 4;
149 public static final byte NULL = 5;
150 public static final byte OBJECTIDENTIFIER = 6;
151 public static final byte OBJECTDESCRIPTOR = 7;
152 public static final byte EXTERNAL = 8;
153 public static final byte ENUMERATED = 10;
154 public static final byte SEQUENCE = 16;
155 public static final byte SET = 17;
156 public static final byte VISIBLESTRING = 26;
157 public static final byte GENERALSTRING = 27;
159 protected static final int putTag(int asn1class, int fldid, int form,
160 byte[] record, int offset) {
162 record[offset++] = (byte)(fldid + asn1class*64 + form*32);
164 record[offset++] = (byte)(31 + asn1class*64 + form*32);
166 record[offset++] = (byte)(fldid);
168 record[offset++] = (byte)(128 + fldid/128);
169 record[offset++] = (byte)(fldid % 128);
176 * Put a length directly into a BER record.
178 * @param len length to put into record
179 * @return the new, incremented value of the offset parameter.
181 static final int putLen(int len, byte[] record, int offset) {
184 record[offset++] = (byte)len;
187 record[offset] = (byte)(lenLen(len) - 1);
188 for (t = record[offset]; t > 0; t--) {
189 record[offset+t] = (byte)(len & 0xff);
193 offset += (record[offset]&0xff) + 1;
194 record[t] += 128; // turn on bit 8 in length byte.
200 * Get the length needed to represent the given length.
202 * @param length determine length needed to encode this
203 * @return length needed to encode given length
205 protected // ### shouldn't this be private?
206 static final int lenLen(int length) {
208 return ((length < 128) ? 1 :
210 (length < 65536L) ? 3 : 4);
214 * Get the length needed to represent the given number.
216 * @param num determine length needed to encode this
217 * @return length needed to encode given number
219 protected static final int numLen(long num) {
220 num = num < 0 ? -num : num;
221 // ### Wouldn't this be better done algorithmically?
222 // Or at least with the constants expressed in hex?
223 return ((num < 128) ? 1 :
225 (num < 8388608) ? 3 :
226 (num < 2147483648L) ? 4 :
227 (num < 549755813888L) ? 5 :
228 (num < 140737488355328L) ? 6 :
229 (num < 36028797018963968L) ? 7 : 8);
233 * Put a number into a given buffer
235 * @param num number to put into buffer
236 * @param record buffer to use
237 * @param offset offset into buffer
238 * @return the new, incremented value of the offset parameter.
240 protected static final int putNum(long num, byte record[], int offset) {
243 for (int count = cnt - 1; count >= 0; count--) {
244 record[offset+count] = (byte)(num & 0xff);
250 // Used only by the makeOID() method
251 private static final Map<String, byte[]> madeOIDs =
252 new HashMap<String, byte[]>(10);
254 protected static final byte[] makeOID(String oid) {
256 int dot, offset = 0, oidOffset = 0, value;
258 if ((o = (byte[])madeOIDs.get(oid)) == null) {
261 // Isn't this kind of thing excruciating in Java?
262 while (oidOffset < oid.length() &&
263 Character.isDigit(oid.charAt(oidOffset)) == true) {
264 if (offset > 90) // too large
267 dot = oid.indexOf('.', oidOffset);
271 value = Integer.parseInt(oid.substring(oidOffset, dot));
273 if (offset == 0) { // 1st two are special
274 if (dot == -1) // ### can't happen: -1 is reassigned above
275 return null; // can't be this short
276 oidOffset = dot+1; // skip past '.'
278 dot = oid.indexOf('.', oidOffset);
284 Integer.parseInt(oid.substring(oidOffset,dot));
288 o[offset++] = (byte)value;
291 byte bits[] = new byte[12]; // save a 84 (12*7) bit number
294 bits[count++] = (byte)(value & 0x7f);
298 // Now place in the correct order
300 o[offset++] = (byte)(bits[count] | 0x80);
302 o[offset++] = bits[count];
305 dot = oid.indexOf('.', oidOffset);
312 byte[] ptr = new byte[offset];
313 System.arraycopy(o, 0, ptr, 0, offset);
314 madeOIDs.put(oid, ptr);
320 public static final byte[] makeQuery(CQLNode root, Properties properties)
321 throws PQFTranslationException {
322 byte[] rpnStructure = root.toType1BER(properties);
323 byte[] qry = new byte[rpnStructure.length+100];
325 offset = putTag(CONTEXT, 1, CONSTRUCTED, qry, offset);
326 qry[offset++] = (byte)(0x80&0xff); // indefinite length
327 offset = putTag(UNIVERSAL, OBJECTIDENTIFIER, PRIMITIVE, qry, offset);
328 byte[] oid = makeOID("1.2.840.10003.3.1"); // bib-1
329 offset = putLen(oid.length, qry, offset);
330 System.arraycopy(oid, 0, qry, offset, oid.length);
331 offset += oid.length;
332 System.arraycopy(rpnStructure, 0, qry, offset, rpnStructure.length);
333 offset += rpnStructure.length;
334 qry[offset++] = 0x00; // end of query
335 qry[offset++] = 0x00;
336 byte[] q = new byte[offset];
337 System.arraycopy(qry, 0, q, 0, offset);