commit 06c1202e48e104311a72e31664a10e1f8e496878
parent 24898296acd1a8a7a4142c755adaf33dda6bd374
Author: Dan Callaghan <djc@djc.id.au>
Date: Sun, 17 Apr 2011 14:50:16 +1000
lang adaptation
Diffstat:
4 files changed, 83 insertions(+), 1 deletion(-)
diff --git a/src/main/java/au/id/djc/rdftemplate/selector/DefaultAdaptationFactory.java b/src/main/java/au/id/djc/rdftemplate/selector/DefaultAdaptationFactory.java
@@ -16,6 +16,7 @@ public class DefaultAdaptationFactory implements AdaptationFactory {
ADAPTATIONS.put("comparable-lv", ComparableLiteralValueAdaptation.class);
ADAPTATIONS.put("string-lv", StringLiteralValueAdaptation.class);
ADAPTATIONS.put("formatted-dt", FormattedDateTimeAdaptation.class);
+ ADAPTATIONS.put("lang", LanguageAdaptation.class);
}
@Override
diff --git a/src/main/java/au/id/djc/rdftemplate/selector/LanguageAdaptation.java b/src/main/java/au/id/djc/rdftemplate/selector/LanguageAdaptation.java
@@ -0,0 +1,67 @@
+package au.id.djc.rdftemplate.selector;
+
+import java.io.StringReader;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.Attribute;
+import javax.xml.stream.events.StartElement;
+import javax.xml.stream.events.XMLEvent;
+
+import com.hp.hpl.jena.rdf.model.Literal;
+
+// XXX use a better result type than just String
+public class LanguageAdaptation extends AbstractAdaptation<String, Literal> {
+
+ private static final String XHTML_NS_URI = "http://www.w3.org/1999/xhtml";
+ private static final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
+
+ public LanguageAdaptation() {
+ super(String.class, new Class<?>[] { }, Literal.class);
+ }
+
+ @Override
+ protected String doAdapt(Literal literal) {
+ if (literal.isWellFormedXML()) {
+ try {
+ return getXMLLang(literal.getLexicalForm());
+ } catch (XMLStreamException e) {
+ throw new RuntimeException(e);
+ }
+ } else {
+ return literal.getLanguage();
+ }
+ }
+
+ private String getXMLLang(String literal) throws XMLStreamException {
+ XMLEventReader reader = inputFactory.createXMLEventReader(new StringReader(literal));
+ while (reader.hasNext()) {
+ XMLEvent event = reader.nextEvent();
+ if (event.isStartElement()) {
+ return getElementLang(event.asStartElement());
+ }
+ }
+ return null;
+ }
+
+ // ughhhhhh
+ public static String getElementLang(StartElement se) {
+ // xml:lang takes precedence
+ QName xmlLangQName = new QName(
+ se.getNamespaceURI("") == XMLConstants.XML_NS_URI ? "" : XMLConstants.XML_NS_URI, "lang");
+ Attribute xmlLang = se.getAttributeByName(xmlLangQName);
+ if (xmlLang != null)
+ return xmlLang.getValue();
+
+ QName xhtmlLangQName = new QName(se.getNamespaceURI("") == XHTML_NS_URI ? "" : XHTML_NS_URI, "lang");
+ Attribute xhtmlLang = se.getAttributeByName(xhtmlLangQName);
+ if (xhtmlLang != null)
+ return xhtmlLang.getValue();
+
+ return null;
+ }
+
+}
diff --git a/src/test/java/au/id/djc/rdftemplate/selector/SelectorEvaluationUnitTest.java b/src/test/java/au/id/djc/rdftemplate/selector/SelectorEvaluationUnitTest.java
@@ -221,4 +221,18 @@ public class SelectorEvaluationUnitTest {
result.trim().replaceAll("\\s+", " "));
}
+ @Test
+ public void should_evaluate_lang_adaptation_for_plain_literals() throws Exception {
+ String result = selectorFactory.get("dc:title#lang")
+ .withResultType(String.class).singleResult(journal);
+ assertThat(result, equalTo("en"));
+ }
+
+ @Test
+ public void should_evaluate_lang_adaptation_for_xml_literals() throws Exception {
+ String result = selectorFactory.get("!sioc:has_container/sioc:content/awol:body#lang")
+ .withResultType(String.class).singleResult(forum);
+ assertThat(result, equalTo("en"));
+ }
+
}
diff --git a/src/test/resources/au/id/djc/rdftemplate/test-data.xml b/src/test/resources/au/id/djc/rdftemplate/test-data.xml
@@ -18,7 +18,7 @@
<sioc:content>
<awol:Content>
<awol:body rdf:parseType="Literal">
- <div xmlns="http://www.w3.org/1999/xhtml">
+ <div xmlns="http://www.w3.org/1999/xhtml" lang="en">
<p>To coincide with the publication of our second issue, the <a
href="/journals/asees/22:1-2/">2008 volume</a> of <a
href="/journals/asees/"><em>Australian Slavonic and East European