rdftemplate

Library for generating XML documents from RDF data using templates
git clone https://code.djc.id.au/git/rdftemplate/
commit f0b520a65d9eab265282e1b142eb3cd2f1732264
parent 55416f9d0386b4620012bd3cc5dfae75f8a83df3
Author: Dan Callaghan <djc@djc.id.au>
Date:   Sat, 21 Nov 2009 16:41:01 +1000

be more forgiving of whitespace

Diffstat:
Msrc/main/antlr3/au/com/miskinhill/rdftemplate/selector/Selector.g | 39++++++++++++++++++++++++++++-----------
Msrc/test/java/au/com/miskinhill/rdftemplate/selector/SelectorParserUnitTest.java | 8++++++++
2 files changed, 36 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
@@ -54,8 +54,11 @@ unionSelector returns [Selector<?> result]
 @init {
     List<Selector<?>> selectors = new ArrayList<Selector<?>>();
 }
-    : s=selector { selectors.add(s); }
-      ( '|'
+    : WHITESPACE*
+      s=selector { selectors.add(s); }
+      ( WHITESPACE*
+        '|'
+        WHITESPACE*
         s=selector { selectors.add(s); }
       )*
       {
@@ -71,8 +74,7 @@ selector returns [Selector<?> result]
     Class<? extends Adaptation<?>> adaptationClass;
     Adaptation<?> adaptation = null;
 }
-    : ' '*
-      ( ts=traversingSelector { result = ts; }
+    : ( ts=traversingSelector { result = ts; }
       | { result = new NoopSelector(); }
       )
       ( '#'
@@ -112,7 +114,7 @@ selector returns [Selector<?> result]
         { $result = new SelectorWithAdaptation(result, adaptation); }
       |
       )
-      ' '*
+      WHITESPACE*
     ;
 
 traversingSelector returns [TraversingSelector result]
@@ -120,7 +122,9 @@ traversingSelector returns [TraversingSelector result]
     result = new TraversingSelector();
 }
     : t=traversal { $result.addTraversal(t); }
-      ( '/'
+      ( WHITESPACE*
+        '/'
+        WHITESPACE*
         t=traversal { $result.addTraversal(t); }
       ) *
     ;
@@ -130,26 +134,35 @@ traversal returns [Traversal result]
     result = new Traversal();
 }
     : ( '!' { $result.setInverse(true); }
+        WHITESPACE*
       | // optional
       )
       nsprefix=XMLTOKEN { $result.setPropertyNamespace(ns($nsprefix.text)); }
       ':'
       localname=XMLTOKEN { $result.setPropertyLocalName($localname.text); }
       ( '['
+        WHITESPACE*
         p=booleanPredicate { $result.setPredicate(p); }
+        WHITESPACE*
         ']'
       | // optional
       )
       ( '('
+        WHITESPACE*
         so=sortOrder { $result.addSortOrderComparator(so); }
-        ( ','
+        ( WHITESPACE*
+          ','
+          WHITESPACE*
           so=sortOrder { $result.addSortOrderComparator(so); }
         )*
+        WHITESPACE*
         ')'
       | // optional
       )
       ( '['
+        WHITESPACE*
         subscript=INTEGER { $result.setSubscript(Integer.parseInt($subscript.text)); }
+        WHITESPACE*
         ']'
       | // optional
       )
@@ -160,6 +173,7 @@ sortOrder returns [SelectorComparator<? extends Comparable<?>> result]
     result = new SelectorComparator();
 }
     : ( '~' { $result.setReversed(true); }
+        WHITESPACE*
       | // optional
       )
       s=selector { $result.setSelector((Selector) s.withResultType(Comparable.class)); }
@@ -168,9 +182,9 @@ sortOrder returns [SelectorComparator<? extends Comparable<?>> result]
 booleanPredicate returns [Predicate result]
     : ( p=predicate { result = p; }
       | left=predicate
-        ' '+
+        WHITESPACE+
         'and'
-        ' '+
+        WHITESPACE+
         right=booleanPredicate
         { result = new BooleanAndPredicate(left, right); }
       )
@@ -186,7 +200,9 @@ predicate returns [Predicate result]
                 if (predicateClass == null)
                     throw new InvalidSelectorSyntaxException("No predicate named " + $predicateName.text);
             }
+      WHITESPACE*
       '='
+      WHITESPACE*
       ( sq=SINGLE_QUOTED {
                 try {
                     result = predicateClass.getConstructor(String.class).newInstance($sq.text);
@@ -214,4 +230,5 @@ SINGLE_QUOTED : '\'' ( options {greedy=false;} : . )* '\''
         // strip quotes
         String txt = getText();
         setText(txt.substring(1, txt.length() -1));
-    };
-\ No newline at end of file
+    };
+WHITESPACE : (' '|'\n'|'\r'|'\t') ;
+\ No newline at end of file
diff --git a/src/test/java/au/com/miskinhill/rdftemplate/selector/SelectorParserUnitTest.java b/src/test/java/au/com/miskinhill/rdftemplate/selector/SelectorParserUnitTest.java
@@ -41,6 +41,14 @@ public class SelectorParserUnitTest {
     }
     
     @Test
+    public void shouldRecogniseMultipleTraversalsWithWhitespace() throws Exception {
+        Selector<RDFNode> selector = factory.get("dc:creator / foaf:name").withResultType(RDFNode.class);
+        assertThat(selector, selector(
+                traversal(DCTerms.NS, "creator"),
+                traversal(FOAF_NS, "name")));
+    }
+    
+    @Test
     public void shouldRecogniseInverseTraversal() throws Exception {
         Selector<RDFNode> selector = factory.get("!dc:isPartOf/!dc:isPartOf").withResultType(RDFNode.class);
         assertThat(selector, selector(