add rec context to CQL->RPN converter

This commit is contained in:
Jörg Prante 2023-04-12 11:56:05 +02:00
parent a66d7b039e
commit 312dc6f264
4 changed files with 35 additions and 16 deletions

View file

@ -1,5 +1,5 @@
group = org.xbib group = org.xbib
name = z3950 name = z3950
version = 5.1.2 version = 5.1.3
org.gradle.warning.mode = ALL org.gradle.warning.mode = ALL

View file

@ -35,9 +35,9 @@ import org.xbib.z3950.common.v3.RPNStructureRpnRpnOp;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.Stack; import java.util.Stack;
import java.util.stream.Collectors;
/** /**
* This is a RPN (Type-1 query) generator for CQL queries. * This is a RPN (Type-1 query) generator for CQL queries.
@ -49,16 +49,7 @@ public final class CQLRPNGenerator implements Visitor {
/** /**
* Context map. * Context map.
*/ */
@SuppressWarnings("serial") private final Map<String, Map<String, String>> contexts;
private final Map<String, ResourceBundle> 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 Stack<AttributeElement> attributeElements; private final Stack<AttributeElement> attributeElements;
@ -76,6 +67,23 @@ public final class CQLRPNGenerator implements Visitor {
this.attributeElements.addAll(attributeElements); this.attributeElements.addAll(attributeElements);
} }
this.result = new Stack<>(); 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<String, String> map) {
contexts.put(context, map);
} }
public RPNQuery getQueryResult() { public RPNQuery getQueryResult() {
@ -292,11 +300,10 @@ public final class CQLRPNGenerator implements Visitor {
} }
private int getUseAttr(String context, String attrName) { private int getUseAttr(String context, String attrName) {
try { if (!contexts.containsKey(context)) {
return Integer.parseInt(contexts.get(context).getString(attrName)); throw new SyntaxException("unknown use attribute '" + attrName + "' for context " + context);
} catch (MissingResourceException e) {
throw new SyntaxException("unknown use attribute '" + attrName + "' for context " + context, e);
} }
return Integer.parseInt(contexts.get(context).get(attrName));
} }
private ASN1OctetString transformTerm(Term term) { private ASN1OctetString transformTerm(Term term) {

View file

@ -47,4 +47,15 @@ class CQL2RPNTest {
// not really working, it adds, not override // 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); 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);
}
} }