From 312dc6f26491465c1daba111075d629e2caef7c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Wed, 12 Apr 2023 11:56:05 +0200 Subject: [PATCH] add rec context to CQL->RPN converter --- gradle.properties | 2 +- .../z3950/common/cql/CQLRPNGenerator.java | 37 +++++++++++-------- .../org/xbib/z3950/common/cql/rec.properties | 1 + .../xbib/z3950/common/cql/CQL2RPNTest.java | 11 ++++++ 4 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 z3950-common/src/main/resources/org/xbib/z3950/common/cql/rec.properties diff --git a/gradle.properties b/gradle.properties index 2cff51d..760684b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ group = org.xbib name = z3950 -version = 5.1.2 +version = 5.1.3 org.gradle.warning.mode = ALL diff --git a/z3950-common/src/main/java/org/xbib/z3950/common/cql/CQLRPNGenerator.java b/z3950-common/src/main/java/org/xbib/z3950/common/cql/CQLRPNGenerator.java index 613ec3c..2ddea29 100644 --- a/z3950-common/src/main/java/org/xbib/z3950/common/cql/CQLRPNGenerator.java +++ b/z3950-common/src/main/java/org/xbib/z3950/common/cql/CQLRPNGenerator.java @@ -35,9 +35,9 @@ import org.xbib.z3950.common.v3.RPNStructureRpnRpnOp; import java.util.Collection; import java.util.HashMap; import java.util.Map; -import java.util.MissingResourceException; import java.util.ResourceBundle; import java.util.Stack; +import java.util.stream.Collectors; /** * This is a RPN (Type-1 query) generator for CQL queries. @@ -49,16 +49,7 @@ public final class CQLRPNGenerator implements Visitor { /** * Context map. */ - @SuppressWarnings("serial") - private final Map contexts = new HashMap<>() { - { - put("default", ResourceBundle.getBundle("org.xbib.z3950.common.cql.default")); - put("cql", ResourceBundle.getBundle("org.xbib.z3950.common.cql.cql")); - put("bib", ResourceBundle.getBundle("org.xbib.z3950.common.cql.bib-1")); - put("dc", ResourceBundle.getBundle("org.xbib.z3950.common.cql.dc")); - put("gbv", ResourceBundle.getBundle("org.xbib.z3950.common.cql.gbv")); - } - }; + private final Map> contexts; private final Stack attributeElements; @@ -76,6 +67,23 @@ public final class CQLRPNGenerator implements Visitor { this.attributeElements.addAll(attributeElements); } this.result = new Stack<>(); + this.contexts = new HashMap<>(); + addContext("default", ResourceBundle.getBundle("org.xbib.z3950.common.cql.default")); + addContext("cql", ResourceBundle.getBundle("org.xbib.z3950.common.cql.cql")); + addContext("rec", ResourceBundle.getBundle("org.xbib.z3950.common.cql.rec")); + addContext("bib", ResourceBundle.getBundle("org.xbib.z3950.common.cql.bib-1")); + addContext("dc", ResourceBundle.getBundle("org.xbib.z3950.common.cql.dc")); + addContext("gbv", ResourceBundle.getBundle("org.xbib.z3950.common.cql.gbv")); + } + + public void addContext(String context, ResourceBundle bundle) { + addContext(context, bundle.keySet().stream() + .map(k -> Map.entry(k, bundle.getString(k))) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); + } + + public void addContext(String context, Map map) { + contexts.put(context, map); } public RPNQuery getQueryResult() { @@ -292,11 +300,10 @@ public final class CQLRPNGenerator implements Visitor { } private int getUseAttr(String context, String attrName) { - try { - return Integer.parseInt(contexts.get(context).getString(attrName)); - } catch (MissingResourceException e) { - throw new SyntaxException("unknown use attribute '" + attrName + "' for context " + context, e); + if (!contexts.containsKey(context)) { + throw new SyntaxException("unknown use attribute '" + attrName + "' for context " + context); } + return Integer.parseInt(contexts.get(context).get(attrName)); } private ASN1OctetString transformTerm(Term term) { diff --git a/z3950-common/src/main/resources/org/xbib/z3950/common/cql/rec.properties b/z3950-common/src/main/resources/org/xbib/z3950/common/cql/rec.properties new file mode 100644 index 0000000..af89d70 --- /dev/null +++ b/z3950-common/src/main/resources/org/xbib/z3950/common/cql/rec.properties @@ -0,0 +1 @@ +id=12 diff --git a/z3950-common/src/test/java/org/xbib/z3950/common/cql/CQL2RPNTest.java b/z3950-common/src/test/java/org/xbib/z3950/common/cql/CQL2RPNTest.java index bdb74ce..71427d8 100644 --- a/z3950-common/src/test/java/org/xbib/z3950/common/cql/CQL2RPNTest.java +++ b/z3950-common/src/test/java/org/xbib/z3950/common/cql/CQL2RPNTest.java @@ -47,4 +47,15 @@ class CQL2RPNTest { // not really working, it adds, not override assertEquals("{attributeSetId 1.2.840.10003.3.1, rpn {op {attrTerm {attributes {{attributeType 2, attributeValue {numeric 4}}{attributeType 2, attributeValue {numeric 3}}{attributeType 4, attributeValue {numeric 1}}{attributeType 5, attributeValue {numeric 100}}{attributeType 1, attributeValue {numeric 4}}}, term {general \"a phrase\"}}}}}", q); } + + @Test + void testRecContext() { + String cql = "rec.id = 123"; + CQLParser parser = new CQLParser(cql); + parser.parse(); + CQLRPNGenerator generator = new CQLRPNGenerator(); + parser.getCQLQuery().accept(generator); + String q = generator.getQueryResult().toString(); + assertEquals("{attributeSetId 1.2.840.10003.3.1, rpn {op {attrTerm {attributes {{attributeType 2, attributeValue {numeric 3}}{attributeType 3, attributeValue {numeric 3}}{attributeType 4, attributeValue {numeric 2}}{attributeType 5, attributeValue {numeric 100}}{attributeType 6, attributeValue {numeric 1}}{attributeType 1, attributeValue {numeric 12}}}, term {general \"123\"}}}}}", q); + } }