commit fdf70b2241c88c214fd1202f26222871f706c48b
parent 1c7e982342fe1a26de636b44f547176e58d0f18d
Author: Dan Callaghan <djc@djc.id.au>
Date: Sun, 1 Nov 2009 16:40:02 +1000
resolver for predicates, so that they need not be hardcoded in the grammar
Diffstat:
5 files changed, 77 insertions(+), 11 deletions(-)
diff --git a/src/main/antlr3/au/com/miskinhill/rdftemplate/selector/Selector.g b/src/main/antlr3/au/com/miskinhill/rdftemplate/selector/Selector.g
@@ -12,11 +12,16 @@ package au.com.miskinhill.rdftemplate.selector;
}
private AdaptationResolver adaptationResolver;
+ private PredicateResolver predicateResolver;
public void setAdaptationResolver(AdaptationResolver adaptationResolver) {
this.adaptationResolver = adaptationResolver;
}
+ public void setPredicateResolver(PredicateResolver predicateResolver) {
+ this.predicateResolver = predicateResolver;
+ }
+
}
@lexer::header {
@@ -159,22 +164,36 @@ booleanPredicate returns [Predicate result]
;
predicate returns [Predicate result]
- : ( URI_PREFIX_PREDICATE
- '='
- uriPrefix=SINGLE_QUOTED { result = new UriPrefixPredicate($uriPrefix.text); }
- | TYPE_PREDICATE
- '='
- nsprefix=XMLTOKEN
+@init {
+ Class<? extends Predicate> predicateClass;
+}
+ : predicateName=XMLTOKEN
+ {
+ predicateClass = predicateResolver.getByName($predicateName.text);
+ if (predicateClass == null)
+ throw new InvalidSelectorSyntaxException("No predicate named " + $predicateName.text);
+ }
+ '='
+ ( sq=SINGLE_QUOTED {
+ try {
+ result = predicateClass.getConstructor(String.class).newInstance($sq.text);
+ } catch (Exception e) {
+ throw new InvalidSelectorSyntaxException(e);
+ }
+ }
+ | nsprefix=XMLTOKEN
':'
localname=XMLTOKEN
- { result = new TypePredicate($nsprefix.text, $localname.text); }
+ {
+ try {
+ result = predicateClass.getConstructor(String.class, String.class).newInstance($nsprefix.text, $localname.text);
+ } catch (Exception e) {
+ throw new InvalidSelectorSyntaxException(e);
+ }
+ }
)
;
-URI_PREFIX_PREDICATE : 'uri-prefix' ;
-TYPE_PREDICATE : 'type' ;
-FIRST_PREDICATE : 'first' ;
-
XMLTOKEN : ('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9'|'_'|'-')* ;
INTEGER : ('0'..'9')+ ;
SINGLE_QUOTED : '\'' ( options {greedy=false;} : . )* '\''
diff --git a/src/main/java/au/com/miskinhill/rdftemplate/selector/AntlrSelectorFactory.java b/src/main/java/au/com/miskinhill/rdftemplate/selector/AntlrSelectorFactory.java
@@ -8,13 +8,26 @@ import org.antlr.runtime.RecognitionException;
public class AntlrSelectorFactory implements SelectorFactory {
private final AdaptationResolver adaptationResolver;
+ private final PredicateResolver predicateResolver;
public AntlrSelectorFactory() {
this.adaptationResolver = new DefaultAdaptationResolver();
+ this.predicateResolver = new DefaultPredicateResolver();
}
public AntlrSelectorFactory(AdaptationResolver adaptationResolver) {
this.adaptationResolver = adaptationResolver;
+ this.predicateResolver = new DefaultPredicateResolver();
+ }
+
+ public AntlrSelectorFactory(PredicateResolver predicateResolver) {
+ this.adaptationResolver = new DefaultAdaptationResolver();
+ this.predicateResolver = predicateResolver;
+ }
+
+ public AntlrSelectorFactory(AdaptationResolver adaptationResolver, PredicateResolver predicateResolver) {
+ this.adaptationResolver = adaptationResolver;
+ this.predicateResolver = predicateResolver;
}
@Override
@@ -24,6 +37,7 @@ public class AntlrSelectorFactory implements SelectorFactory {
CommonTokenStream tokens = new CommonTokenStream(lexer);
SelectorParser parser = new SelectorParser(tokens);
parser.setAdaptationResolver(adaptationResolver);
+ parser.setPredicateResolver(predicateResolver);
try {
return parser.unionSelector();
} catch (RecognitionException e) {
diff --git a/src/main/java/au/com/miskinhill/rdftemplate/selector/DefaultPredicateResolver.java b/src/main/java/au/com/miskinhill/rdftemplate/selector/DefaultPredicateResolver.java
@@ -0,0 +1,19 @@
+package au.com.miskinhill.rdftemplate.selector;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class DefaultPredicateResolver implements PredicateResolver {
+
+ private static final Map<String, Class<? extends Predicate>> PREDICATES = new HashMap<String, Class<? extends Predicate>>();
+ static {
+ PREDICATES.put("type", TypePredicate.class);
+ PREDICATES.put("uri-prefix", UriPrefixPredicate.class);
+ }
+
+ @Override
+ public Class<? extends Predicate> getByName(String name) {
+ return PREDICATES.get(name);
+ }
+
+}
diff --git a/src/main/java/au/com/miskinhill/rdftemplate/selector/PredicateResolver.java b/src/main/java/au/com/miskinhill/rdftemplate/selector/PredicateResolver.java
@@ -0,0 +1,7 @@
+package au.com.miskinhill.rdftemplate.selector;
+
+public interface PredicateResolver {
+
+ Class<? extends Predicate> getByName(String name);
+
+}
diff --git a/src/test/java/au/com/miskinhill/rdftemplate/selector/SelectorParserUnitTest.java b/src/test/java/au/com/miskinhill/rdftemplate/selector/SelectorParserUnitTest.java
@@ -173,6 +173,13 @@ public class SelectorParserUnitTest {
.withAdaptation(formattedDTAdaptation("d MMMM yyyy")));
}
+ @Test
+ public void shouldRecogniseRdfType() throws Exception {
+ // was broken due to ANTLR being confused about the literal string "type" which was hardcoded to be a predicate
+ Selector<RDFNode> selector = factory.get("rdf:type").withResultType(RDFNode.class);
+ assertThat(selector, selector(traversal("rdf", "type")));
+ }
+
@Test(expected = InvalidSelectorSyntaxException.class)
public void shouldThrowForInvalidSyntax() throws Exception {
factory.get("dc:creator]["); // this is a parser error