From: Jakub Skoczen Date: Tue, 5 Mar 2013 12:02:39 +0000 (+0100) Subject: Allow registration of custom relations X-Git-Tag: v1.11~3 X-Git-Url: http://lists.indexdata.dk/?a=commitdiff_plain;h=beabf8e9a3c5bd13dfafb0fb4bac0eb9fa0a90e5;p=cql-java-moved-to-github.git Allow registration of custom relations --- diff --git a/src/main/java/org/z3950/zing/cql/CQLParser.java b/src/main/java/org/z3950/zing/cql/CQLParser.java index 2509563..6bd6d01 100644 --- a/src/main/java/org/z3950/zing/cql/CQLParser.java +++ b/src/main/java/org/z3950/zing/cql/CQLParser.java @@ -8,7 +8,9 @@ import java.io.FileNotFoundException; import java.io.Reader; import java.io.StringReader; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; /** @@ -21,6 +23,8 @@ public class CQLParser { private CQLLexer lexer; private PositionAwareReader par; //active reader with position private int compat; // When false, implement CQL 1.2 + private final Set customRelations = new HashSet(); + public static final int V1POINT1 = 12368; public static final int V1POINT2 = 12369; public static final int V1POINT1SORT = 12370; @@ -58,6 +62,25 @@ public class CQLParser { } /** + * Registers custom relation in this parser. Note that when a custom relation + * is registered the parser is no longer strictly compliant with the chosen spec. + * @param relation + * @return true if custom relation has not been registered already + */ + public boolean registerCustomRelation(String relation) { + return customRelations.add(relation); + } + + /** + * Unregisters previously registered custom relation in this instance of the parser. + * @param relation + * @return true is relation has been previously registered + */ + public boolean unregisterCustomRelation(String relation) { + return customRelations.remove(relation); + } + + /** * Compiles a CQL query. *

* The resulting parse tree may be further processed by hand (see @@ -272,7 +295,8 @@ public class CQLParser { lexer.sval.equals("encloses") || (lexer.sval.equals("exact") && compat != V1POINT2) || (lexer.sval.equals("scr") && compat != V1POINT2) || - (lexer.sval.equals("adj") && compat == V1POINT2))) + (lexer.sval.equals("adj") && compat == V1POINT2) || + customRelations.contains(lexer.sval))) return true; return isSymbolicRelation(); @@ -444,6 +468,7 @@ public class CQLParser { } CQLParser parser = new CQLParser(compat); + parser.registerCustomRelation("@"); CQLNode root = null; try { root = parser.parse(cql);