Skip to content

Commit

Permalink
Merge pull request #2 from zcc39r/cql-java-1
Browse files Browse the repository at this point in the history
Added support for truncation attribute
  • Loading branch information
MikeTaylor committed Mar 10, 2016
2 parents 6e0bb6c + 68b82ac commit 05d07a7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
11 changes: 11 additions & 0 deletions etc/pqf.properties
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ structure.any = 4=2
structure.* = 4=1
# phrase

# Truncation attributes used to implement CQL wildcard patterns. The
# simpler forms, left, right- and both-truncation will be used for the
# simplest patterns, so that we produce PQF queries that conform more
# closely to the Bath Profile. However, when a more complex pattern
# such as "foo*bar" is used, we fall back on Z39.58-style masking.
#
truncation.right = 5=1
truncation.left = 5=2
truncation.both = 5=3
truncation.none = 5=100

# Finally, any additional attributes that should always be included
# with each term can be specified in the "always" property.
#
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/z3950/zing/cql/CQLTermNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,19 @@ private List<String> getAttrs(Properties config) throws PQFTranslationException
}

String pos = "any";
String truncation = "none";
String text = term;
if (text.length() > 0 && text.substring(0, 1).equals("^")) {
text = text.substring(1); // ### change not seen by caller
pos = "first";
}
if (text.startsWith("*") && text.endsWith("*")) {
truncation = "both";
} else if (text.startsWith("*")) {
truncation = "left";
} else if (text.endsWith("*")) {
truncation = "right";
}
int len = text.length();
if (len > 0 && text.substring(len-1, len).equals("^")) {
text = text.substring(0, len-1); // ### change not seen by caller
Expand All @@ -156,6 +164,11 @@ private List<String> getAttrs(Properties config) throws PQFTranslationException
throw new UnknownPositionException(pos);
attrs.add(attr);

attr = config.getProperty("truncation." + truncation);
if (attr == null)
throw new UnknownTruncationException(truncation);
attrs.add(attr);

attr = config.getProperty("structure." + rel);
if (attr == null)
attr = config.getProperty("structure.*");
Expand Down Expand Up @@ -187,6 +200,15 @@ public String toPQF(Properties config) throws PQFTranslationException {
if (len > 0 && text.substring(len-1, len).equals("^"))
text = text.substring(0, len-1);

len = text.length();
if (text.startsWith("*") && text.endsWith("*")) {
text = text.substring(1, len-1);
} else if (text.startsWith("*")) {
text = text.substring(1);
} else if (text.endsWith("*")) {
text = text.substring(0, len-1);
}

return s + maybeQuote(text);
}

Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/z3950/zing/cql/UnknownTruncationException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.z3950.zing.cql;

/**
* Exception indicating that a truncation was not recognised. When rendering a
* tree out as PQF, each term is classified either as <TT>left</TT>,
* <TT>right</TT>, <TT>left and right</TT> truncated, depending on whether
* it begins and/or ends with the character <TT>*</TT>. Its
* classification is looked up as a <TT>truncation</TT> in the PQF
* configuration. If the truncation is not configured, we throw one of these
* babies.
*
*/
public class UnknownTruncationException extends PQFTranslationException {
private static final long serialVersionUID = 6971993723734811253L;

/**
* Creates a new <TT>UnknownTruncationException</TT>.
*
* @param s
* The truncation for which there was no PQF configuration.
*/
public UnknownTruncationException(String s) {
super(s);
}
}

0 comments on commit 05d07a7

Please sign in to comment.