rdftemplate

Library for generating XML documents from RDF data using templates
git clone https://code.djc.id.au/git/rdftemplate/
commit 2ee1c410c05b1a9ca12b00bd84b0459bff9ba858
parent 72de1e8518c4de4d8e9d89f23dea0e325fe72b55
Author: Dan Callaghan <djc@djc.id.au>
Date:   Wed, 25 Nov 2009 22:18:29 +1000

rdf:if negation

Diffstat:
Msrc/main/java/au/com/miskinhill/rdftemplate/TemplateInterpolator.java | 18++++++++++++++----
Msrc/test/java/au/com/miskinhill/rdftemplate/TemplateInterpolatorUnitTest.java | 2++
Msrc/test/resources/au/com/miskinhill/rdftemplate/conditional.xml | 2++
3 files changed, 18 insertions(+), 4 deletions(-)
diff --git a/src/main/java/au/com/miskinhill/rdftemplate/TemplateInterpolator.java b/src/main/java/au/com/miskinhill/rdftemplate/TemplateInterpolator.java
@@ -122,10 +122,20 @@ public class TemplateInterpolator {
                     StartElement start = (StartElement) event;
                     if (start.getName().equals(IF_ACTION_QNAME)) {
                         Attribute testAttribute = start.getAttributeByName(new QName("test"));
-                        if (testAttribute == null)
-                            throw new TemplateSyntaxException("rdf:if must have a test attribute");
-                        Selector<?> selector = selectorFactory.get(testAttribute.getValue());
-                        if (selector.result(node).isEmpty()) {
+                        Attribute notAttribute = start.getAttributeByName(new QName("not"));
+                        String condition;
+                        boolean negate = false;
+                        if (testAttribute != null && notAttribute != null)
+                            throw new TemplateSyntaxException("test and not attribute on rdf:if are mutually exclusive");
+                        else if (testAttribute != null)
+                            condition = testAttribute.getValue();
+                        else if (notAttribute != null) {
+                            condition = notAttribute.getValue();
+                            negate = true;
+                        } else
+                            throw new TemplateSyntaxException("rdf:if must have a test attribute or a not attribute");
+                        List<?> selectorResult = selectorFactory.get(condition).result(node);
+                        if (negate ? !selectorResult.isEmpty() : selectorResult.isEmpty()) {
                             consumeTree(start, reader);
                             break;
                         } else {
diff --git a/src/test/java/au/com/miskinhill/rdftemplate/TemplateInterpolatorUnitTest.java b/src/test/java/au/com/miskinhill/rdftemplate/TemplateInterpolatorUnitTest.java
@@ -64,12 +64,14 @@ public class TemplateInterpolatorUnitTest {
         assertThat(result, containsString("attribute test"));
         assertThat(result, containsString("element test"));
         assertThat(result, not(containsString("rdf:if")));
+        assertThat(result, not(containsString("negated test")));
         
         Resource authorWithoutNotes = model.getResource("http://miskinhill.com.au/authors/another-author");
         result = templateInterpolator.interpolate(
                 new InputStreamReader(this.getClass().getResourceAsStream("conditional.xml")), authorWithoutNotes);
         assertThat(result, not(containsString("attribute test")));
         assertThat(result, not(containsString("element test")));
+        assertThat(result, containsString("negated test"));
     }
     
     @Test
diff --git a/src/test/resources/au/com/miskinhill/rdftemplate/conditional.xml b/src/test/resources/au/com/miskinhill/rdftemplate/conditional.xml
@@ -6,5 +6,7 @@
 
 <rdf:if test="mhs:biographicalNotes">element test</rdf:if>
 
+<rdf:if not="mhs:biographicalNotes">negated test</rdf:if>
+
 </body>
 </html>
 \ No newline at end of file