rdftemplate

Library for generating XML documents from RDF data using templates
git clone https://code.djc.id.au/git/rdftemplate/
commit 64b129083fa361b84e61738a43673ab50de2edae
parent 074aeb7423706f2f027b78bc2aa8050aac4448dc
Author: Dan Callaghan <djc@djc.id.au>
Date:   Mon, 23 Nov 2009 20:31:26 +1000

#uri-anchor adaptation

Diffstat:
Msrc/main/java/au/com/miskinhill/rdftemplate/selector/DefaultAdaptationResolver.java | 1+
Asrc/main/java/au/com/miskinhill/rdftemplate/selector/UriAnchorAdaptation.java | 29+++++++++++++++++++++++++++++
Msrc/test/java/au/com/miskinhill/rdftemplate/selector/SelectorEvaluationUnitTest.java | 15+++++++++++----
Asrc/test/java/au/com/miskinhill/rdftemplate/selector/UriAnchorAdaptationUnitTest.java | 23+++++++++++++++++++++++
Msrc/test/resources/au/com/miskinhill/rdftemplate/test-data.xml | 5+++++
5 files changed, 69 insertions(+), 4 deletions(-)
diff --git a/src/main/java/au/com/miskinhill/rdftemplate/selector/DefaultAdaptationResolver.java b/src/main/java/au/com/miskinhill/rdftemplate/selector/DefaultAdaptationResolver.java
@@ -9,6 +9,7 @@ public class DefaultAdaptationResolver implements AdaptationResolver {
     static {
         ADAPTATIONS.put("uri", UriAdaptation.class);
         ADAPTATIONS.put("uri-slice", UriSliceAdaptation.class);
+        ADAPTATIONS.put("uri-anchor", UriAnchorAdaptation.class);
         ADAPTATIONS.put("lv", LiteralValueAdaptation.class);
         ADAPTATIONS.put("comparable-lv", ComparableLiteralValueAdaptation.class);
         ADAPTATIONS.put("string-lv", StringLiteralValueAdaptation.class);
diff --git a/src/main/java/au/com/miskinhill/rdftemplate/selector/UriAnchorAdaptation.java b/src/main/java/au/com/miskinhill/rdftemplate/selector/UriAnchorAdaptation.java
@@ -0,0 +1,29 @@
+package au.com.miskinhill.rdftemplate.selector;
+
+import com.hp.hpl.jena.rdf.model.RDFNode;
+import com.hp.hpl.jena.rdf.model.Resource;
+
+/**
+ * Returns the anchor component of the node's URI (excluding initial #), or the
+ * empty string if it has no anchor component.
+ */
+public class UriAnchorAdaptation implements Adaptation<String> {
+    
+    @Override
+    public Class<String> getDestinationType() {
+        return String.class;
+    }
+
+    @Override
+    public String adapt(RDFNode node) {
+        if (!node.isResource()) {
+            throw new SelectorEvaluationException("Attempted to apply #uri-anchor to non-resource node " + node);
+        }
+        String uri = ((Resource) node).getURI();
+        int hashIndex = uri.lastIndexOf('#');
+        if (hashIndex < 0)
+            return "";
+        return uri.substring(hashIndex + 1);
+    }
+
+}
diff --git a/src/test/java/au/com/miskinhill/rdftemplate/selector/SelectorEvaluationUnitTest.java b/src/test/java/au/com/miskinhill/rdftemplate/selector/SelectorEvaluationUnitTest.java
@@ -69,10 +69,10 @@ public class SelectorEvaluationUnitTest {
     
     @Test
     public void shouldEvaluateInverseTraversal() throws Exception {
-        List<RDFNode> results = selectorFactory.get("!mhs:isIssueOf/!dc:isPartOf")
-                .withResultType(RDFNode.class).result(journal);
-        assertThat(results.size(), equalTo(4));
-        assertThat(results, hasItems((RDFNode) article, (RDFNode) review, (RDFNode) anotherReview, (RDFNode) obituary));
+        List<RDFNode> results = selectorFactory.get("!dc:isPartOf")
+                .withResultType(RDFNode.class).result(issue);
+        assertThat(results.size(), equalTo(3));
+        assertThat(results, hasItems((RDFNode) article, (RDFNode) review, (RDFNode) obituary));
     }
     
     @Test
@@ -123,6 +123,13 @@ public class SelectorEvaluationUnitTest {
     }
     
     @Test
+    public void shouldEvaluateUriAnchorAdaptation() throws Exception {
+        String result = selectorFactory.get("mhs:inSection#uri-anchor")
+                .withResultType(String.class).singleResult(anotherReview);
+        assertThat(result, equalTo("stuff"));
+    }
+    
+    @Test
     public void shouldEvaluateSubscript() throws Exception {
         String result = selectorFactory.get(
                 "!mhs:isIssueOf(~mhs:publicationDate#comparable-lv)[0]/mhs:coverThumbnail#uri")
diff --git a/src/test/java/au/com/miskinhill/rdftemplate/selector/UriAnchorAdaptationUnitTest.java b/src/test/java/au/com/miskinhill/rdftemplate/selector/UriAnchorAdaptationUnitTest.java
@@ -0,0 +1,23 @@
+package au.com.miskinhill.rdftemplate.selector;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import com.hp.hpl.jena.rdf.model.ResourceFactory;
+import org.junit.Test;
+
+public class UriAnchorAdaptationUnitTest {
+
+    @Test
+    public void shouldReturnAnchor() {
+        String result = new UriAnchorAdaptation().adapt(ResourceFactory.createResource("http://example.com/#asdf"));
+        assertThat(result, equalTo("asdf"));
+    }
+    
+    @Test
+    public void shouldReturnEmptyStringForUriWithoutAnchor() {
+        String result = new UriAnchorAdaptation().adapt(ResourceFactory.createResource("http://example.com/"));
+        assertThat(result, equalTo(""));
+    }
+
+}
diff --git a/src/test/resources/au/com/miskinhill/rdftemplate/test-data.xml b/src/test/resources/au/com/miskinhill/rdftemplate/test-data.xml
@@ -79,6 +79,10 @@
     <mhs:onlinePublicationDate rdf:datatype="http://www.w3.org/TR/xmlschema-2/#date">2009-03-01</mhs:onlinePublicationDate>
     <mhs:coverThumbnail rdf:resource="http://miskinhill.com.au/journals/test/2:1/cover.thumb.jpg"/>
   </mhs:Issue>
+    <mhs:ContentsSection rdf:about="http://miskinhill.com.au/journals/test/2:1/#stuff">
+        <dc:isPartOf rdf:resource="http://miskinhill.com.au/journals/test/2:1/" />
+        <mhs:sectionHeading>Stuff</mhs:sectionHeading>
+    </mhs:ContentsSection>
   <mhs:Article rdf:about="http://miskinhill.com.au/journals/test/1:1/article">
     <dc:isPartOf rdf:resource="http://miskinhill.com.au/journals/test/1:1/"/>
     <dc:creator rdf:resource="http://miskinhill.com.au/authors/test-author"/>
@@ -133,6 +137,7 @@
     <dc:isPartOf rdf:resource="http://miskinhill.com.au/journals/test/2:1/"/>
     <mhs:reviews rdf:resource="http://miskinhill.com.au/cited/books/test"/>
     <dc:creator rdf:resource="http://miskinhill.com.au/authors/another-author"/>
+    <mhs:inSection rdf:resource="http://miskinhill.com.au/journals/test/2:1/#stuff" />
   </mhs:Review>
   <mhs:Obituary rdf:about="http://miskinhill.com.au/journals/test/1:1/in-memoriam-john-doe">
     <dc:title xml:lang="en">In memoriam John Doe</dc:title>