commit 6330b79c168738eb70602afcd0ddd0d400f4211e
parent 92acd9e1c4cb5b1a7a3807c01284c8507bc7f9bd
Author: Dan Callaghan <djc@djc.id.au>
Date: Tue, 6 Oct 2009 21:07:30 +1000
rdf:join
Diffstat:
5 files changed, 38 insertions(+), 116 deletions(-)
diff --git a/src/main/java/au/com/miskinhill/rdftemplate/TemplateInterpolator.java b/src/main/java/au/com/miskinhill/rdftemplate/TemplateInterpolator.java
@@ -45,6 +45,8 @@ public class TemplateInterpolator {
private static final QName FOR_ACTION_QNAME = new QName(NS, FOR_ACTION);
public static final String IF_ACTION = "if";
private static final QName IF_ACTION_QNAME = new QName(NS, IF_ACTION);
+ public static final String JOIN_ACTION = "join";
+ private static final QName JOIN_ACTION_QNAME = new QName(NS, JOIN_ACTION);
private static final QName XML_LANG_QNAME = new QName(XMLConstants.XML_NS_URI, "lang", XMLConstants.XML_NS_PREFIX);
private static final String XHTML_NS_URI = "http://www.w3.org/1999/xhtml";
@@ -90,6 +92,27 @@ public class TemplateInterpolator {
events.remove(0);
interpolate(events.iterator(), node, writer);
}
+ } else if (start.getName().equals(JOIN_ACTION_QNAME)) {
+ Attribute eachAttribute = start.getAttributeByName(new QName("each"));
+ if (eachAttribute == null)
+ throw new TemplateSyntaxException("rdf:join must have an each attribute");
+ String separator = "";
+ Attribute separatorAttribute = start.getAttributeByName(new QName("separator"));
+ if (separatorAttribute != null)
+ separator = separatorAttribute.getValue();
+ Selector<RDFNode> selector = selectorFactory.get(eachAttribute.getValue()).withResultType(RDFNode.class);
+ List<XMLEvent> events = consumeTree(start, reader);
+ // discard enclosing rdf:join
+ events.remove(events.size() - 1);
+ events.remove(0);
+ boolean first = true;
+ for (RDFNode eachNode: selector.result(node)) {
+ if (!first) {
+ writer.add(eventFactory.createCharacters(separator));
+ }
+ interpolate(events.iterator(), eachNode, writer);
+ first = false;
+ }
} 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
@@ -1,18 +1,11 @@
package au.com.miskinhill.rdftemplate;
import static org.hamcrest.CoreMatchers.not;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.containsString;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
-import java.net.URI;
-import java.nio.ByteBuffer;
-import java.nio.channels.FileChannel;
-import java.nio.charset.Charset;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
@@ -78,25 +71,12 @@ public class TemplateInterpolatorUnitTest {
}
@Test
- public void shouldWork() throws Exception {
- Resource journal = model.getResource("http://miskinhill.com.au/journals/test/");
+ public void shouldHandleJoins() throws Exception {
+ Resource citedArticle = model.getResource("http://miskinhill.com.au/cited/journals/asdf/1:1/article");
String result = templateInterpolator.interpolate(
- new InputStreamReader(this.getClass().getResourceAsStream("test-template.xml")), journal);
- String expected = exhaust(this.getClass().getResource("test-template.out.xml").toURI());
- assertEquals(expected.trim(), result.trim());
- }
-
- private String exhaust(URI file) throws IOException { // sigh
- FileChannel channel = new FileInputStream(new File(file)).getChannel();
- Charset charset = Charset.defaultCharset();
- StringBuffer sb = new StringBuffer();
- ByteBuffer b = ByteBuffer.allocate(8192);
- while (channel.read(b) > 0) {
- b.rewind();
- sb.append(charset.decode(b));
- b.flip();
- }
- return sb.toString();
+ new InputStreamReader(this.getClass().getResourceAsStream("join.xml")), citedArticle);
+ assertThat(result, containsString("<p><a href=\"http://miskinhill.com.au/authors/another-author\">Another Author</a>, " +
+ "<a href=\"http://miskinhill.com.au/authors/test-author\">Test Author</a></p>"));
}
}
diff --git a/src/test/resources/au/com/miskinhill/rdftemplate/join.xml b/src/test/resources/au/com/miskinhill/rdftemplate/join.xml
@@ -0,0 +1,8 @@
+<html xmlns="http://www.w3.org/1999/xhtml"
+ xmlns:rdf="http://code.miskinhill.com.au/rdftemplate/">
+<body>
+
+<p><rdf:join each="dc:creator(#uri)" separator=", "><a href="${#uri}" rdf:content="foaf:name" /></rdf:join></p>
+
+</body>
+</html>
+\ No newline at end of file
diff --git a/src/test/resources/au/com/miskinhill/rdftemplate/test-template.out.xml b/src/test/resources/au/com/miskinhill/rdftemplate/test-template.out.xml
@@ -1,46 +0,0 @@
-<?xml version="1.0"?><html xmlns="http://www.w3.org/1999/xhtml" xmlns:rdf="http://code.miskinhill.com.au/rdftemplate/" lang="en">
-
-<head>
- <title xml:lang="en" lang="en">Test Journal of Good Stuff</title>
-</head>
-
-<body>
-
-<h2><em xml:lang="en" lang="en">Test Journal of Good Stuff</em><abbr title="http://miskinhill.com.au/journals/test/" class="unapi-id"></abbr></h2>
-
-<div class="metabox-container">
- <div class="issue-meta metabox">
- <h4>Journal details</h4>
- <p class="cover">
- <img alt="" src="http://miskinhill.com.au/journals/test/2:1/cover.thumb.jpg"></img>
- </p>
- <p class="publisher">Published by <a xml:lang="en" lang="en" href="http://awesome.com">Awesome Publishing House</a></p>
- <p class="issn">ISSN 12345678</p>
- <!-- <p>Metadata:
- ${Markup(u', ').join(r(req, node).anchor() for r in representations.for_node(node) if r.format != 'html')}
- </p> -->
- </div>
-</div>
-
-<h3>Issues available online</h3>
-<ul>
- <li>
- <a href="http://miskinhill.com.au/journals/test/2:1/">
- Vol. 2,
- Issue 1
- (2008)
- </a>
- </li><li>
- <a href="http://miskinhill.com.au/journals/test/1:1/">
- Vol. 1,
- Issue 1
- (2007)
- </a>
- </li>
-</ul>
-
-<h3>About this journal</h3>
-<div><div xmlns="http://www.w3.org/1999/xhtml" lang="en"><p><em>Test Journal</em> is a journal.</p></div></div>
-
-</body>
-</html>
-\ No newline at end of file
diff --git a/src/test/resources/au/com/miskinhill/rdftemplate/test-template.xml b/src/test/resources/au/com/miskinhill/rdftemplate/test-template.xml
@@ -1,42 +0,0 @@
-<html xmlns="http://www.w3.org/1999/xhtml"
- xmlns:rdf="http://code.miskinhill.com.au/rdftemplate/"
- lang="en">
-
-<head>
- <title rdf:content="dc:title" />
-</head>
-
-<body>
-
-<h2><em rdf:content="dc:title" /><abbr class="unapi-id" title="${#uri}" /></h2>
-
-<div class="metabox-container">
- <div class="issue-meta metabox">
- <h4>Journal details</h4>
- <p class="cover">
- <img src="${!mhs:isIssueOf(~mhs:publicationDate#comparable-lv)[0]/mhs:coverThumbnail#uri}" alt="" />
- </p>
- <p class="publisher">Published by <a href="${dc:publisher/foaf:homepage#uri}" rdf:content="dc:publisher/foaf:name" /></p>
- <p class="issn">ISSN ${dc:identifier[uri-prefix='urn:issn:']#uri-slice(9)}</p>
- <!-- <p>Metadata:
- ${Markup(u', ').join(r(req, node).anchor() for r in representations.for_node(node) if r.format != 'html')}
- </p> -->
- </div>
-</div>
-
-<h3>Issues available online</h3>
-<ul>
- <li rdf:for="!mhs:isIssueOf[uri-prefix='http://miskinhill.com.au/journals/'](~mhs:publicationDate#comparable-lv)">
- <a href="${#uri}">
- Vol. ${mhs:volume},
- Issue ${mhs:issueNumber}
- (${dc:coverage})
- </a>
- </li>
-</ul>
-
-<h3>About this journal</h3>
-<div rdf:content="dc:description" />
-
-</body>
-</html>
-\ No newline at end of file