From f8f333c94f7654313b529a6e879b5d23364c34e3 Mon Sep 17 00:00:00 2001 From: mike Date: Thu, 5 Dec 2002 17:14:52 +0000 Subject: [PATCH] Integrate Ralph's changes. --- src/org/z3950/zing/cql/CQLAndNode.java | 14 +++- src/org/z3950/zing/cql/CQLBooleanNode.java | 29 +++++++- src/org/z3950/zing/cql/CQLNotNode.java | 13 +++- src/org/z3950/zing/cql/CQLOrNode.java | 13 +++- src/org/z3950/zing/cql/CQLPrefixNode.java | 9 ++- src/org/z3950/zing/cql/CQLProxNode.java | 102 +++++++++++++++++++++------- src/org/z3950/zing/cql/CQLRelation.java | 10 ++- src/org/z3950/zing/cql/CQLTermNode.java | 98 ++++++++++++++++++++------ 8 files changed, 232 insertions(+), 56 deletions(-) diff --git a/src/org/z3950/zing/cql/CQLAndNode.java b/src/org/z3950/zing/cql/CQLAndNode.java index 1f5157f..90d54a1 100644 --- a/src/org/z3950/zing/cql/CQLAndNode.java +++ b/src/org/z3950/zing/cql/CQLAndNode.java @@ -1,4 +1,4 @@ -// $Id: CQLAndNode.java,v 1.5 2002-11-06 00:05:58 mike Exp $ +// $Id: CQLAndNode.java,v 1.6 2002-12-05 17:14:52 mike Exp $ package org.z3950.zing.cql; @@ -6,7 +6,7 @@ package org.z3950.zing.cql; /** * Represents an AND node in a CQL parse-tree. * - * @version $Id: CQLAndNode.java,v 1.5 2002-11-06 00:05:58 mike Exp $ + * @version $Id: CQLAndNode.java,v 1.6 2002-12-05 17:14:52 mike Exp $ */ public class CQLAndNode extends CQLBooleanNode { /** @@ -20,4 +20,14 @@ public class CQLAndNode extends CQLBooleanNode { String op() { return "and"; } + + // ### Too much code duplication here with OR and NOT + byte[] opType1() { + byte[] op = new byte[5]; + putTag(CONTEXT, 46, CONSTRUCTED, op, 0); // Operator + putLen(2, op, 2); + putTag(CONTEXT, 0, PRIMITIVE, op, 3); // and + putLen(0, op, 4); + return op; + } } diff --git a/src/org/z3950/zing/cql/CQLBooleanNode.java b/src/org/z3950/zing/cql/CQLBooleanNode.java index d761463..6e772eb 100644 --- a/src/org/z3950/zing/cql/CQLBooleanNode.java +++ b/src/org/z3950/zing/cql/CQLBooleanNode.java @@ -1,4 +1,4 @@ -// $Id: CQLBooleanNode.java,v 1.11 2002-11-20 17:56:22 mike Exp $ +// $Id: CQLBooleanNode.java,v 1.12 2002-12-05 17:14:52 mike Exp $ package org.z3950.zing.cql; import java.util.Properties; @@ -8,7 +8,7 @@ import java.util.Vector; /** * Represents a boolean node in a CQL parse-tree. * - * @version $Id: CQLBooleanNode.java,v 1.11 2002-11-20 17:56:22 mike Exp $ + * @version $Id: CQLBooleanNode.java,v 1.12 2002-12-05 17:14:52 mike Exp $ */ public abstract class CQLBooleanNode extends CQLNode { CQLBooleanNode() {} // prevent javadoc from documenting this @@ -58,4 +58,29 @@ public abstract class CQLBooleanNode extends CQLNode { String opPQF() { return op(); } abstract String op(); + + public byte[] toType1(Properties config) throws PQFTranslationException { + System.out.println("in CQLBooleanNode.toType101(): PQF=" + + toPQF(config)); + byte[] rpn1 = left.toType1(config); + byte[] rpn2 = right.toType1(config); + byte[] op = opType1(); + byte[] rpnStructure = new byte[rpn1.length+rpn2.length+op.length+4]; + + // rpnRpnOp + int offset = putTag(CONTEXT, 1, CONSTRUCTED, rpnStructure, 0); + + rpnStructure[offset++] = (byte)(0x80&0xff); // indefinite length + System.arraycopy(rpn1, 0, rpnStructure, offset, rpn1.length); + offset += rpn1.length; + System.arraycopy(rpn2, 0, rpnStructure, offset, rpn2.length); + offset += rpn2.length; + System.arraycopy(op, 0, rpnStructure, offset, op.length); + offset += op.length; + rpnStructure[offset++] = 0x00; // end rpnRpnOp + rpnStructure[offset++] = 0x00; + return rpnStructure; + } + + abstract byte[] opType1(); } diff --git a/src/org/z3950/zing/cql/CQLNotNode.java b/src/org/z3950/zing/cql/CQLNotNode.java index a2a64c2..070c909 100644 --- a/src/org/z3950/zing/cql/CQLNotNode.java +++ b/src/org/z3950/zing/cql/CQLNotNode.java @@ -1,4 +1,4 @@ -// $Id: CQLNotNode.java,v 1.5 2002-11-06 00:05:58 mike Exp $ +// $Id: CQLNotNode.java,v 1.6 2002-12-05 17:14:52 mike Exp $ package org.z3950.zing.cql; @@ -6,7 +6,7 @@ package org.z3950.zing.cql; /** * Represents a NOT node in a CQL parse-tree. * - * @version $Id: CQLNotNode.java,v 1.5 2002-11-06 00:05:58 mike Exp $ + * @version $Id: CQLNotNode.java,v 1.6 2002-12-05 17:14:52 mike Exp $ */ public class CQLNotNode extends CQLBooleanNode { /** @@ -20,4 +20,13 @@ public class CQLNotNode extends CQLBooleanNode { String op() { return "not"; } + + byte[] opType1() { + byte[] op = new byte[5]; + putTag(CONTEXT, 46, CONSTRUCTED, op, 0); // Operator + putLen(2, op, 2); + putTag(CONTEXT, 2, PRIMITIVE, op, 3); // and-not + putLen(0, op, 4); + return op; + } } diff --git a/src/org/z3950/zing/cql/CQLOrNode.java b/src/org/z3950/zing/cql/CQLOrNode.java index bacca98..dde3368 100644 --- a/src/org/z3950/zing/cql/CQLOrNode.java +++ b/src/org/z3950/zing/cql/CQLOrNode.java @@ -1,4 +1,4 @@ -// $Id: CQLOrNode.java,v 1.5 2002-11-06 00:05:58 mike Exp $ +// $Id: CQLOrNode.java,v 1.6 2002-12-05 17:14:52 mike Exp $ package org.z3950.zing.cql; @@ -6,7 +6,7 @@ package org.z3950.zing.cql; /** * Represents an OR node in a CQL parse-tree. * - * @version $Id: CQLOrNode.java,v 1.5 2002-11-06 00:05:58 mike Exp $ + * @version $Id: CQLOrNode.java,v 1.6 2002-12-05 17:14:52 mike Exp $ */ public class CQLOrNode extends CQLBooleanNode { /** @@ -20,4 +20,13 @@ public class CQLOrNode extends CQLBooleanNode { String op() { return "or"; } + + byte[] opType1() { + byte[] op = new byte[5]; + putTag(CONTEXT, 46, CONSTRUCTED, op, 0); // Operator + putLen(2, op, 2); + putTag(CONTEXT, 1, PRIMITIVE, op, 3); // or + putLen(0, op, 4); + return op; + } } diff --git a/src/org/z3950/zing/cql/CQLPrefixNode.java b/src/org/z3950/zing/cql/CQLPrefixNode.java index 4e603b7..8841c77 100644 --- a/src/org/z3950/zing/cql/CQLPrefixNode.java +++ b/src/org/z3950/zing/cql/CQLPrefixNode.java @@ -1,4 +1,4 @@ -// $Id: CQLPrefixNode.java,v 1.3 2002-11-20 01:15:15 mike Exp $ +// $Id: CQLPrefixNode.java,v 1.4 2002-12-05 17:14:52 mike Exp $ package org.z3950.zing.cql; import java.lang.String; @@ -9,7 +9,7 @@ import java.util.Vector; /** * Represents a prefix node in a CQL parse-tree. * - * @version $Id: CQLPrefixNode.java,v 1.3 2002-11-20 01:15:15 mike Exp $ + * @version $Id: CQLPrefixNode.java,v 1.4 2002-12-05 17:14:52 mike Exp $ */ public class CQLPrefixNode extends CQLNode { /** @@ -61,4 +61,9 @@ public class CQLPrefixNode extends CQLNode { // `config'. return subtree.toPQF(config); } + + public byte[] toType1(Properties config) throws PQFTranslationException { + // See comment on toPQF() + return subtree.toType1(config); + } } diff --git a/src/org/z3950/zing/cql/CQLProxNode.java b/src/org/z3950/zing/cql/CQLProxNode.java index 9546187..9c7dff9 100644 --- a/src/org/z3950/zing/cql/CQLProxNode.java +++ b/src/org/z3950/zing/cql/CQLProxNode.java @@ -1,4 +1,4 @@ -// $Id: CQLProxNode.java,v 1.6 2002-11-20 17:56:22 mike Exp $ +// $Id: CQLProxNode.java,v 1.7 2002-12-05 17:14:52 mike Exp $ package org.z3950.zing.cql; import java.util.Vector; @@ -10,7 +10,7 @@ import java.util.Vector; * candidate records which are sufficiently close to each other, as * specified by a set of proximity parameters. * - * @version $Id: CQLProxNode.java,v 1.6 2002-11-20 17:56:22 mike Exp $ + * @version $Id: CQLProxNode.java,v 1.7 2002-12-05 17:14:52 mike Exp $ */ public class CQLProxNode extends CQLBooleanNode { ModifierSet ms; @@ -85,42 +85,98 @@ public class CQLProxNode extends CQLBooleanNode { * unit-code ::= integer. */ String opPQF() { + int relCode = getRelCode(); + int unitCode = getProxUnitCode(); + + String res = "prox " + + "0 " + + ms.modifier("distance") + " " + + (ms.modifier("ordering").equals("ordered") ? 1 : 0) + " " + + relCode + " " + + "1 " + + unitCode; + + return res; + } + + private int getRelCode() { String rel = ms.modifier("relation"); - int relCode = 0; if (rel.equals("<")) { - relCode = 1; + return 1; } else if (rel.equals("<=")) { - relCode = 2; + return 2; } else if (rel.equals("=")) { - relCode = 3; + return 3; } else if (rel.equals(">=")) { - relCode = 4; + return 4; } else if (rel.equals(">")) { - relCode = 5; + return 5; } else if (rel.equals("<>")) { - relCode = 6; + return 6; } + return 0; + } + private int getProxUnitCode() { String unit = ms.modifier("unit"); - int unitCode = 0; if (unit.equals("word")) { - unitCode = 2; + return 2; } else if (unit.equals("sentence")) { - unitCode = 3; + return 3; } else if (unit.equals("paragraph")) { - unitCode = 4; + return 4; } else if (unit.equals("element")) { - unitCode = 8; + return 8; } + return 0; + } - String res = "prox " + - "0 " + - ms.modifier("distance") + " " + - (ms.modifier("ordering").equals("ordered") ? 1 : 0) + " " + - relCode + " " + - "1 " + - unitCode; - - return res; + + byte[] opType1() { + byte[] op = new byte[100]; + int offset, value; + offset = putTag(CONTEXT, 46, CONSTRUCTED, op, 0); // Operator + op[offset++] = (byte)(0x80&0xff); // indefinite length + + offset = putTag(CONTEXT, 3, CONSTRUCTED, op, offset); // prox + op[offset++] = (byte)(0x80&0xff); // indefinite length + + offset = putTag(CONTEXT, 1, PRIMITIVE, op, offset); // exclusion + value = 0; // false + offset = putLen(numLen(value), op, offset); + offset = putNum(value, op, offset); + + offset = putTag(CONTEXT, 2, PRIMITIVE, op, offset); // distance + value = Integer.parseInt(ms.modifier("distance")); + offset = putLen(numLen(value), op, offset); + offset = putNum(value, op, offset); + + offset = putTag(CONTEXT, 3, PRIMITIVE, op, offset); // ordered + value = ms.modifier("ordering").equals("ordered") ? 1 : 0; + offset = putLen(numLen(value), op, offset); + offset = putNum(value, op, offset); + + offset = putTag(CONTEXT, 4, PRIMITIVE, op, offset); // relationType + value = getRelCode(); + offset = putLen(numLen(value), op, offset); + offset = putNum(value, op, offset); + + offset = putTag(CONTEXT, 5, CONSTRUCTED, op, offset); // proximityUnitCode + op[offset++] = (byte)(0x80&0xff); // indefinite length + offset = putTag(CONTEXT, 1, PRIMITIVE, op, offset); // known + value = getProxUnitCode(); + offset = putLen(numLen(value), op, offset); + offset = putNum(value, op, offset); + op[offset++] = 0x00; // end of proximityUnitCode + op[offset++] = 0x00; + + op[offset++] = 0x00; // end of prox + op[offset++] = 0x00; + op[offset++] = 0x00; // end of Operator + op[offset++] = 0x00; + + byte[] o = new byte[offset]; + System.arraycopy(op, 0, o, 0, offset); + return o; } } diff --git a/src/org/z3950/zing/cql/CQLRelation.java b/src/org/z3950/zing/cql/CQLRelation.java index 6f007bc..dc04417 100644 --- a/src/org/z3950/zing/cql/CQLRelation.java +++ b/src/org/z3950/zing/cql/CQLRelation.java @@ -1,4 +1,4 @@ -// $Id: CQLRelation.java,v 1.7 2002-11-20 01:15:15 mike Exp $ +// $Id: CQLRelation.java,v 1.8 2002-12-05 17:14:52 mike Exp $ package org.z3950.zing.cql; import java.util.Vector; @@ -8,7 +8,7 @@ import java.lang.StringBuffer; /** * Represents a relation between a CQL qualifier and term. * - * @version $Id: CQLRelation.java,v 1.7 2002-11-20 01:15:15 mike Exp $ + * @version $Id: CQLRelation.java,v 1.8 2002-12-05 17:14:52 mike Exp $ */ public class CQLRelation extends CQLNode { ModifierSet ms; @@ -70,4 +70,10 @@ public class CQLRelation extends CQLNode { public String toPQF(Properties config) throws PQFTranslationException { throw new Error("CQLRelation.toPQF() can never be called"); } + + // ### THIS IS NOT THE REAL CODE. I'm waiting for Ralph to send it. + public byte[] toType1(Properties config) { + byte[] op = new byte[0]; + return op; + } } diff --git a/src/org/z3950/zing/cql/CQLTermNode.java b/src/org/z3950/zing/cql/CQLTermNode.java index b5244d2..516fd33 100644 --- a/src/org/z3950/zing/cql/CQLTermNode.java +++ b/src/org/z3950/zing/cql/CQLTermNode.java @@ -1,4 +1,4 @@ -// $Id: CQLTermNode.java,v 1.11 2002-11-29 16:43:23 mike Exp $ +// $Id: CQLTermNode.java,v 1.12 2002-12-05 17:14:52 mike Exp $ package org.z3950.zing.cql; import java.util.Properties; @@ -12,7 +12,7 @@ import java.util.Vector; * these must be provided - you can't have a qualifier without a * relation or vice versa. * - * @version $Id: CQLTermNode.java,v 1.11 2002-11-29 16:43:23 mike Exp $ + * @version $Id: CQLTermNode.java,v 1.12 2002-12-05 17:14:52 mike Exp $ */ public class CQLTermNode extends CQLNode { private String qualifier; @@ -56,7 +56,10 @@ public class CQLTermNode extends CQLNode { return res; } - public String toPQF(Properties config) throws PQFTranslationException { + // ### Interaction between this and its callers is not good as + // regards truncation of the term and generation of truncation + // attributes. Change the interface to fix this. + private Vector getAttrs(Properties config) throws PQFTranslationException { Vector attrs = new Vector(); // Do this first so that if any other truncation or @@ -100,27 +103,12 @@ public class CQLTermNode extends CQLNode { attrs.add(attr); } - String pos = "any"; + String pos = "unanchored"; String text = term; - //System.err.println("before check: text='" + text + "'"); if (text.length() > 0 && text.substring(0, 1).equals("^")) { - //System.err.println("first in field"); text = text.substring(1); - pos = "first"; - } - //System.err.println("between checks: text='" + text + "'"); - int len = text.length(); - if (len > 0 && text.substring(len-1, len).equals("^")) { - //System.err.println("last in field"); - text = text.substring(0, len-1); - pos = pos.equals("first") ? "firstAndLast" : "last"; - // ### in the firstAndLast case, the standard - // pqf.properties file specifies that we generate a - // completeness=whole-field attributem, which means that - // we don't generate a position attribute at all. Do we - // care? Does it matter? + pos = "anchored"; } - //System.err.println("after checks: text='" + text + "'"); attr = config.getProperty("position." + pos); if (attr == null) throw new UnknownPositionException(pos); @@ -131,12 +119,21 @@ public class CQLTermNode extends CQLNode { attr = config.getProperty("structure.*"); attrs.add(attr); - String s = ""; + return attrs; + } + + public String toPQF(Properties config) throws PQFTranslationException { + Vector attrs = getAttrs(config); + + String attr, s = ""; for (int i = 0; i < attrs.size(); i++) { attr = (String) attrs.get(i); s += "@attr " + Utils.replaceString(attr, " ", " @attr ") + " "; } + String text = term; + if (text.length() > 0 && text.substring(0, 1).equals("^")) + text = text.substring(1); return s + maybeQuote(text); } @@ -157,4 +154,63 @@ public class CQLTermNode extends CQLNode { return str; } + + /** + * ### Document this! + */ + public byte[] toType1(Properties config) throws PQFTranslationException { + String text = term; + if (text.length() > 0 && text.substring(0, 1).equals("^")) + text = text.substring(1); + String attr, attrList, term = maybeQuote(text); + System.out.println("in CQLTermNode.toType101(): PQF=" + toPQF(config)); + byte[] operand = new byte[text.length()+100]; + int i, j, offset, type, value; + offset = putTag(CONTEXT, 0, CONSTRUCTED, operand, 0); // op + operand[offset++]=(byte)(0x80&0xff); // indefinite length + offset = putTag(CONTEXT, 102, CONSTRUCTED, operand, offset); // AttributesPlusTerm + operand[offset++] = (byte)(0x80&0xff); // indefinite length + offset = putTag(CONTEXT, 44, CONSTRUCTED, operand, offset); // AttributeList + operand[offset++] = (byte)(0x80&0xff); // indefinite length + offset = putTag(UNIVERSAL, SEQUENCE, CONSTRUCTED, operand, offset); + operand[offset++] = (byte)(0x80&0xff); + + Vector attrs = getAttrs(config); + for(i = 0; i < attrs.size(); i++) { + attrList = (String) attrs.get(i); + java.util.StringTokenizer st = + new java.util.StringTokenizer(attrList); + while (st.hasMoreTokens()) { + attr = st.nextToken(); + j = attr.indexOf('='); + offset = putTag(CONTEXT, 120, PRIMITIVE, operand, offset); + type = Integer.parseInt(attr.substring(0, j)); + offset = putLen(numLen(type), operand, offset); + offset = putNum(type, operand, offset); + + offset = putTag(CONTEXT, 121, PRIMITIVE, operand, offset); + value = Integer.parseInt(attr.substring(j+1)); + offset = putLen(numLen(value), operand, offset); + offset = putNum(value, operand, offset); + } + } + operand[offset++] = 0x00; // end of SEQUENCE + operand[offset++] = 0x00; + operand[offset++] = 0x00; // end of AttributeList + operand[offset++] = 0x00; + + offset = putTag(CONTEXT, 45, PRIMITIVE, operand, offset); // general Term + byte[] t = term.getBytes(); + offset = putLen(t.length, operand, offset); + System.arraycopy(t, 0, operand, offset, t.length); + offset += t.length; + + operand[offset++] = 0x00; // end of AttributesPlusTerm + operand[offset++] = 0x00; + operand[offset++] = 0x00; // end of Operand + operand[offset++] = 0x00; + byte[] o = new byte[offset]; + System.arraycopy(operand, 0, o, 0, offset); + return o; + } } -- 1.7.10.4