rdftemplate

Library for generating XML documents from RDF data using templates
git clone https://code.djc.id.au/git/rdftemplate/
commit e3b9b112320bebc5239fde60d28b453ae7ea21d4
parent 3fc1b5040826e317263e0b2dd260a74e3d1c8109
Author: Dan Callaghan <djc@djc.id.au>
Date:   Sun,  1 Nov 2009 16:17:27 +1000

rdf:for element

Diffstat:
Msrc/main/java/au/com/miskinhill/rdftemplate/TemplateInterpolator.java | 12++++++++++++
Msrc/test/java/au/com/miskinhill/rdftemplate/TemplateInterpolatorUnitTest.java | 11+++++++++++
Asrc/test/resources/au/com/miskinhill/rdftemplate/for.xml | 11+++++++++++
3 files changed, 34 insertions(+), 0 deletions(-)
diff --git a/src/main/java/au/com/miskinhill/rdftemplate/TemplateInterpolator.java b/src/main/java/au/com/miskinhill/rdftemplate/TemplateInterpolator.java
@@ -131,6 +131,18 @@ public class TemplateInterpolator {
                             interpolate(events.iterator(), eachNode, writer);
                             first = false;
                         }
+                    } else if (start.getName().equals(FOR_ACTION_QNAME)) {
+                        Attribute eachAttribute = start.getAttributeByName(new QName("each"));
+                        if (eachAttribute == null)
+                            throw new TemplateSyntaxException("rdf:for must have an each attribute");
+                        List<XMLEvent> events = consumeTree(start, reader);
+                        // discard enclosing rdf:for
+                        events.remove(events.size() - 1);
+                        events.remove(0);
+                        Selector<RDFNode> selector = selectorFactory.get(eachAttribute.getValue()).withResultType(RDFNode.class);
+                        for (RDFNode subNode : selector.result(node)) {
+                            interpolate(events.iterator(), subNode, writer);
+                        }
                     } else {
                         Attribute ifAttribute = start.getAttributeByName(IF_ACTION_QNAME);
                         Attribute contentAttribute = start.getAttributeByName(CONTENT_ACTION_QNAME);
diff --git a/src/test/java/au/com/miskinhill/rdftemplate/TemplateInterpolatorUnitTest.java b/src/test/java/au/com/miskinhill/rdftemplate/TemplateInterpolatorUnitTest.java
@@ -79,4 +79,15 @@ public class TemplateInterpolatorUnitTest {
                 "<a href=\"http://miskinhill.com.au/authors/test-author\">Test Author</a></p>"));
     }
     
+    @Test
+    public void shouldHandleFor() throws Exception {
+        Resource journal = model.getResource("http://miskinhill.com.au/journals/test/");
+        String result = templateInterpolator.interpolate(
+                new InputStreamReader(this.getClass().getResourceAsStream("for.xml")), journal);
+        assertThat(result, containsString("<span>http://miskinhill.com.au/journals/test/1:1/</span>"));
+        assertThat(result, containsString("<span>http://miskinhill.com.au/journals/test/2:1/</span>"));
+        assertThat(result, containsString("<p>http://miskinhill.com.au/journals/test/1:1/</p>"));
+        assertThat(result, containsString("<p>http://miskinhill.com.au/journals/test/2:1/</p>"));
+    }
+    
 }
diff --git a/src/test/resources/au/com/miskinhill/rdftemplate/for.xml b/src/test/resources/au/com/miskinhill/rdftemplate/for.xml
@@ -0,0 +1,10 @@
+<html xmlns="http://www.w3.org/1999/xhtml"
+      xmlns:rdf="http://code.miskinhill.com.au/rdftemplate/">
+<body>
+
+<span rdf:for="!mhs:isIssueOf">${#uri}</span>
+
+<rdf:for each="!mhs:isIssueOf"><p rdf:content="#uri" /></rdf:for>
+
+</body>
+</html>
+\ No newline at end of file