commit a6e061b9419469d7833ac557c7bd77831ecde971 parent 2d2d6aaf99d4ba3def2fc2f1fa478b62ca6f0866 Author: Dan Callaghan <djc@djc.id.au> Date: Wed, 30 Sep 2009 22:40:12 +1000 insert XML literals as actual XML instead of character data Diffstat:
6 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/main/java/au/com/miskinhill/rdftemplate/TemplateInterpolator.java b/src/main/java/au/com/miskinhill/rdftemplate/TemplateInterpolator.java @@ -1,6 +1,7 @@ package au.com.miskinhill.rdftemplate; import java.io.Reader; +import java.io.StringReader; import java.io.StringWriter; import java.util.ArrayList; import java.util.Deque; @@ -13,8 +14,10 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.xml.XMLConstants; +import javax.xml.namespace.NamespaceContext; import javax.xml.namespace.QName; import javax.xml.stream.XMLEventFactory; +import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLEventWriter; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLOutputFactory; @@ -208,7 +211,11 @@ public class TemplateInterpolator { } writer.add(eventFactory.createStartElement(start.getName(), attributes.iterator(), start.getNamespaces())); - writer.add(eventFactory.createCharacters(literal.getValue().toString())); + if (literal.isWellFormedXML()) { + writeXMLLiteral(start.getNamespaceContext(), literal.getLexicalForm(), writer); + } else { + writer.add(eventFactory.createCharacters(literal.getValue().toString())); + } writer.add(eventFactory.createEndElement(start.getName(), start.getNamespaces())); } else { throw new UnsupportedOperationException("Not a literal: " + replacementNode); @@ -230,4 +237,19 @@ public class TemplateInterpolator { return attributes; } + private static void writeXMLLiteral(NamespaceContext nsContext, String literal, XMLEventWriter writer) + throws XMLStreamException { + XMLEventReader reader = inputFactory.createXMLEventReader(new StringReader(literal)); + while (reader.hasNext()) { + XMLEvent event = reader.nextEvent(); + switch (event.getEventType()) { + case XMLStreamConstants.START_DOCUMENT: + case XMLStreamConstants.END_DOCUMENT: + break; // discard + default: + writer.add(event); + } + } + } + } diff --git a/src/test/java/au/com/miskinhill/rdftemplate/TemplateInterpolatorUnitTest.java b/src/test/java/au/com/miskinhill/rdftemplate/TemplateInterpolatorUnitTest.java @@ -45,11 +45,20 @@ public class TemplateInterpolatorUnitTest { Resource journal = model.getResource("http://miskinhill.com.au/journals/test/"); String result = TemplateInterpolator.interpolate( new InputStreamReader(this.getClass().getResourceAsStream("replace-subtree.xml")), journal); - assertThat(result, containsString("<div xml:lang=\"en\" lang=\"en\">A journal, you know, with some stuff in it</div>")); + assertThat(result, containsString("<div xml:lang=\"en\" lang=\"en\">Test Journal of Good Stuff</div>")); assertThat(result, not(containsString("<p>This should all go <em>away</em>!</p>"))); } @Test + public void shouldHandleXMLLiterals() throws Exception { + Resource journal = model.getResource("http://miskinhill.com.au/journals/test/"); + String result = TemplateInterpolator.interpolate( + new InputStreamReader(this.getClass().getResourceAsStream("replace-xml.xml")), journal); + assertThat(result, containsString( + "<div xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\"><p><em>Test Journal</em> is a journal.</p></div>")); + } + + @Test public void shouldWork() throws Exception { Resource journal = model.getResource("http://miskinhill.com.au/journals/test/"); String result = TemplateInterpolator.interpolate( diff --git a/src/test/resources/au/com/miskinhill/rdftemplate/replace-subtree.xml b/src/test/resources/au/com/miskinhill/rdftemplate/replace-subtree.xml @@ -2,7 +2,7 @@ xmlns:rdf="http://code.miskinhill.com.au/rdftemplate/"> <body> -<div rdf:content="dc:description"> +<div rdf:content="dc:title"> <p>This should all go <em>away</em>!</p> </div> diff --git a/src/test/resources/au/com/miskinhill/rdftemplate/replace-xml.xml b/src/test/resources/au/com/miskinhill/rdftemplate/replace-xml.xml @@ -0,0 +1,8 @@ +<html xmlns="http://www.w3.org/1999/xhtml" + xmlns:rdf="http://code.miskinhill.com.au/rdftemplate/"> +<body> + +<div rdf:content="dc:description" /> + +</body> +</html> +\ No newline at end of file diff --git a/src/test/resources/au/com/miskinhill/rdftemplate/test-data.xml b/src/test/resources/au/com/miskinhill/rdftemplate/test-data.xml @@ -10,7 +10,7 @@ <dc:title xml:lang="en">Test Journal of Good Stuff</dc:title> <dc:publisher rdf:nodeID="pub"/> <dc:identifier rdf:resource="urn:issn:12345678"/> - <dc:description xml:lang="en">A journal, you know, with some stuff in it</dc:description> + <dc:description rdf:parseType="Literal"><div xmlns="http://www.w3.org/1999/xhtml" lang="en"><p><em>Test Journal</em> is a journal.</p></div></dc:description> <dc:language rdf:resource="http://www.lingvoj.org/lang/en" /> <dc:language rdf:resource="http://www.lingvoj.org/lang/ru" /> <mhs:beginningDate>1987</mhs:beginningDate> 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 @@ -40,7 +40,7 @@ </ul> <h3>About this journal</h3> -<div xml:lang="en" lang="en">A journal, you know, with some stuff in it</div> +<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