commit 3942ee75f0308d5df2da41aff3bdb4cec48d6d94
parent 0839adc9aed6a5734bd63cca44b1e27295a41c2b
Author: Dan Callaghan <djc@djc.id.au>
Date: Sat, 28 Nov 2009 14:19:17 +1000
strip rdf namespace declarations from output
Diffstat:
3 files changed, 38 insertions(+), 9 deletions(-)
diff --git a/src/main/java/au/com/miskinhill/rdftemplate/TemplateInterpolator.java b/src/main/java/au/com/miskinhill/rdftemplate/TemplateInterpolator.java
@@ -21,12 +21,12 @@ import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
-import javax.xml.stream.XMLResolver;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
+import javax.xml.stream.events.Namespace;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
@@ -173,7 +173,7 @@ public class TemplateInterpolator {
consumeTree(start, reader);
break;
}
- start = cloneStartWithAttributes(start, cloneAttributesWithout(start, IF_ACTION_QNAME));
+ start = cloneStart(start, cloneAttributesWithout(start, IF_ACTION_QNAME), cloneNamespacesWithoutRdf(start));
}
if (contentAttribute != null && forAttribute != null) {
throw new TemplateSyntaxException("rdf:for and rdf:content cannot both be present on an element");
@@ -202,7 +202,7 @@ public class TemplateInterpolator {
writeTreeForContent(writer, replacement);
writer.add(eventFactory.createEndElement(start.getName(), start.getNamespaces()));
} else if (forAttribute != null) {
- start = cloneStartWithAttributes(start, cloneAttributesWithout(start, FOR_ACTION_QNAME));
+ start = cloneStart(start, cloneAttributesWithout(start, FOR_ACTION_QNAME), cloneNamespacesWithoutRdf(start));
List<XMLEvent> tree = consumeTree(start, reader);
Selector<RDFNode> selector = selectorFactory.get(forAttribute.getValue()).withResultType(RDFNode.class);
for (RDFNode subNode : selector.result(node)) {
@@ -267,19 +267,30 @@ public class TemplateInterpolator {
replacementAttributes.add(eventFactory.createAttribute(attribute.getName(),
replacementValue));
}
- return cloneStartWithAttributes(start, replacementAttributes);
+ return cloneStart(start, replacementAttributes, cloneNamespacesWithoutRdf(start));
}
- private StartElement cloneStartWithAttributes(StartElement start, Iterable<Attribute> attributes) {
+ private StartElement cloneStart(StartElement start, Iterable<Attribute> attributes, Iterable<Namespace> namespaces) {
return eventFactory.createStartElement(
start.getName().getPrefix(),
start.getName().getNamespaceURI(),
start.getName().getLocalPart(),
attributes.iterator(),
- start.getNamespaces(),
+ namespaces.iterator(),
start.getNamespaceContext());
}
+ @SuppressWarnings("unchecked")
+ private Set<Namespace> cloneNamespacesWithoutRdf(StartElement start) {
+ Set<Namespace> clonedNamespaces = new LinkedHashSet<Namespace>();
+ for (Iterator<Namespace> it = start.getNamespaces(); it.hasNext(); ) {
+ Namespace namespace = it.next();
+ if (!namespace.getNamespaceURI().equals(NS))
+ clonedNamespaces.add(namespace);
+ }
+ return clonedNamespaces;
+ }
+
private static final Pattern SUBSTITUTION_PATTERN = Pattern.compile("\\$\\{([^}]*)\\}");
public String interpolateString(String template, RDFNode node) {
if (!SUBSTITUTION_PATTERN.matcher(template).find()) {
diff --git a/src/test/java/au/com/miskinhill/rdftemplate/TemplateInterpolatorUnitTest.java b/src/test/java/au/com/miskinhill/rdftemplate/TemplateInterpolatorUnitTest.java
@@ -1,8 +1,8 @@
package au.com.miskinhill.rdftemplate;
-import static org.hamcrest.CoreMatchers.not;
-import static org.junit.Assert.assertThat;
-import static org.junit.matchers.JUnitMatchers.containsString;
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+import static org.junit.matchers.JUnitMatchers.*;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -94,4 +94,13 @@ public class TemplateInterpolatorUnitTest {
assertThat(result, containsString("<p>http://miskinhill.com.au/journals/test/2:1/</p>"));
}
+ @Test
+ public void shouldStripRdfNamespaceDeclarations() throws Exception {
+ Resource author = model.getResource("http://miskinhill.com.au/authors/test-author");
+ String result = templateInterpolator.interpolate(
+ new InputStreamReader(this.getClass().getResourceAsStream("namespaces.xml")), author);
+ assertThat(result, not(containsString("xmlns:rdf=\"http://code.miskinhill.com.au/rdftemplate/\"")));
+ assertThat(result, not(containsString("rdf:")));
+ }
+
}
diff --git a/src/test/resources/au/com/miskinhill/rdftemplate/namespaces.xml b/src/test/resources/au/com/miskinhill/rdftemplate/namespaces.xml
@@ -0,0 +1,8 @@
+<html xmlns="http://www.w3.org/1999/xhtml"
+ xmlns:rdf="http://code.miskinhill.com.au/rdftemplate/">
+<body>
+
+<span rdf:if="mhs:biographicalNotes">asdf</span>
+
+</body>
+</html>
+\ No newline at end of file