commit 074aeb7423706f2f027b78bc2aa8050aac4448dc
parent f0b520a65d9eab265282e1b142eb3cd2f1732264
Author: Dan Callaghan <djc@djc.id.au>
Date: Sat, 21 Nov 2009 18:24:50 +1000
string-lv adaptation
Diffstat:
4 files changed, 77 insertions(+), 3 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
@@ -11,6 +11,7 @@ public class DefaultAdaptationResolver implements AdaptationResolver {
ADAPTATIONS.put("uri-slice", UriSliceAdaptation.class);
ADAPTATIONS.put("lv", LiteralValueAdaptation.class);
ADAPTATIONS.put("comparable-lv", ComparableLiteralValueAdaptation.class);
+ ADAPTATIONS.put("string-lv", StringLiteralValueAdaptation.class);
ADAPTATIONS.put("formatted-dt", FormattedDateTimeAdaptation.class);
}
diff --git a/src/main/java/au/com/miskinhill/rdftemplate/selector/StringLiteralValueAdaptation.java b/src/main/java/au/com/miskinhill/rdftemplate/selector/StringLiteralValueAdaptation.java
@@ -0,0 +1,51 @@
+package au.com.miskinhill.rdftemplate.selector;
+
+import java.io.StringReader;
+
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.XMLEvent;
+
+import com.hp.hpl.jena.rdf.model.Literal;
+import com.hp.hpl.jena.rdf.model.RDFNode;
+
+public class StringLiteralValueAdaptation implements Adaptation<String> {
+
+ private static final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
+
+ @Override
+ public Class<String> getDestinationType() {
+ return String.class;
+ }
+
+ @Override
+ public String adapt(RDFNode node) {
+ if (!node.isLiteral()) {
+ throw new SelectorEvaluationException("Attempted to apply #lv to non-literal node " + node);
+ }
+ Literal literal = node.as(Literal.class);
+ if (literal.isWellFormedXML()) {
+ try {
+ return stripTags(literal.getLexicalForm());
+ } catch (XMLStreamException e) {
+ throw new RuntimeException(e);
+ }
+ } else {
+ return literal.getValue().toString();
+ }
+ }
+
+ private String stripTags(String literal) throws XMLStreamException {
+ StringBuilder sb = new StringBuilder();
+ XMLEventReader reader = inputFactory.createXMLEventReader(new StringReader(literal));
+ while (reader.hasNext()) {
+ XMLEvent event = reader.nextEvent();
+ if (event.isCharacters()) {
+ sb.append(event.asCharacters().getData());
+ }
+ }
+ return sb.toString();
+ }
+
+}
diff --git a/src/test/java/au/com/miskinhill/rdftemplate/TestNamespacePrefixMap.java b/src/test/java/au/com/miskinhill/rdftemplate/TestNamespacePrefixMap.java
@@ -25,6 +25,7 @@ public final class TestNamespacePrefixMap extends HashMap<String, String> {
put("foaf", FOAF_NS);
put("rdf", RDF.getURI());
put("sioc", "http://rdfs.org/sioc/ns#");
+ put("awol", "http://bblfish.net/work/atom-owl/2006-06-06/#");
put("lingvoj", "http://www.lingvoj.org/ontology#");
}
diff --git a/src/test/java/au/com/miskinhill/rdftemplate/selector/SelectorEvaluationUnitTest.java b/src/test/java/au/com/miskinhill/rdftemplate/selector/SelectorEvaluationUnitTest.java
@@ -1,8 +1,8 @@
package au.com.miskinhill.rdftemplate.selector;
-import static org.hamcrest.CoreMatchers.equalTo;
-import static org.junit.Assert.assertThat;
-import static org.junit.matchers.JUnitMatchers.hasItems;
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+import static org.junit.matchers.JUnitMatchers.*;
import java.io.InputStream;
import java.util.List;
@@ -191,4 +191,25 @@ public class SelectorEvaluationUnitTest {
assertThat(result, equalTo("2009-06-15T18:21:32+10:00"));
}
+ @Test
+ public void shouldEvaluateStringLVAdaptation() throws Exception {
+ List<String> results = selectorFactory.get("dc:language/lingvoj:iso1#string-lv")
+ .withResultType(String.class).result(journal);
+ assertThat(results.size(), equalTo(2));
+ assertThat(results, hasItems("en", "ru"));
+ }
+
+ @Test
+ public void stringLVAdaptationShouldStripTagsFromXMLLiteral() throws Exception {
+ String result = selectorFactory.get("!sioc:has_container/sioc:content/awol:body#string-lv")
+ .withResultType(String.class).singleResult(forum);
+ assertEquals("To coincide with the publication of our second issue, " +
+ "the 2008 volume of Australian Slavonic and East European Studies, " +
+ "we are making available two new data feeds: an Atom feed of all " +
+ "journal issues published on this site, and the complete RDF dataset " +
+ "underlying the site. We hope this helps our users and aggregators " +
+ "to discover new content as it is published.",
+ result.trim().replaceAll("\\s+", " "));
+ }
+
}