commit 72de1e8518c4de4d8e9d89f23dea0e325fe72b55
parent 64b129083fa361b84e61738a43673ab50de2edae
Author: Dan Callaghan <djc@djc.id.au>
Date: Wed, 25 Nov 2009 19:31:14 +1000
new public interface to allow interpolating to a Collection rather than an XMLEventWriter, for inclusion into other XML event streams
Diffstat:
2 files changed, 42 insertions(+), 6 deletions(-)
diff --git a/src/main/java/au/com/miskinhill/rdftemplate/TemplateInterpolator.java b/src/main/java/au/com/miskinhill/rdftemplate/TemplateInterpolator.java
@@ -4,6 +4,7 @@ import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedHashSet;
@@ -68,11 +69,17 @@ public class TemplateInterpolator {
this.selectorFactory = selectorFactory;
}
- @SuppressWarnings("unchecked")
public String interpolate(Reader reader, RDFNode node) {
try {
StringWriter writer = new StringWriter();
- interpolate(inputFactory.createXMLEventReader(reader), node, outputFactory.createXMLEventWriter(writer));
+ final XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
+ XMLEventDestination destination = new XMLEventDestination() {
+ @Override
+ public void add(XMLEvent event) throws XMLStreamException {
+ eventWriter.add(event);
+ }
+ };
+ interpolate(reader, node, destination);
// all this to get self-closing tags -- weird?? XXX
StringWriter transformedWriter = new StringWriter();
@@ -88,7 +95,25 @@ public class TemplateInterpolator {
}
}
- public void interpolate(Iterator<XMLEvent> reader, RDFNode node, XMLEventWriter writer)
+ @SuppressWarnings("unchecked")
+ public void interpolate(Reader reader, RDFNode node, XMLEventDestination writer) {
+ try {
+ interpolate(inputFactory.createXMLEventReader(reader), node, writer);
+ } catch (XMLStreamException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void interpolate(Reader reader, RDFNode node, final Collection<XMLEvent> destination) {
+ interpolate(reader, node, new XMLEventDestination() {
+ @Override
+ public void add(XMLEvent event) {
+ destination.add(event);
+ }
+ });
+ }
+
+ public void interpolate(Iterator<XMLEvent> reader, RDFNode node, XMLEventDestination writer)
throws XMLStreamException {
while (reader.hasNext()) {
XMLEvent event = reader.next();
@@ -290,7 +315,7 @@ public class TemplateInterpolator {
return substituted.toString();
}
- private void interpolateCharacters(XMLEventWriter writer, Characters characters, RDFNode node) throws XMLStreamException {
+ private void interpolateCharacters(XMLEventDestination writer, Characters characters, RDFNode node) throws XMLStreamException {
String template = characters.getData();
if (!SUBSTITUTION_PATTERN.matcher(template).find()) {
writer.add(characters); // fast path
@@ -308,7 +333,7 @@ public class TemplateInterpolator {
writer.add(eventFactory.createCharacters(template.substring(lastAppendedPos)));
}
- private void writeTreeForContent(XMLEventWriter writer, Object replacement)
+ private void writeTreeForContent(XMLEventDestination writer, Object replacement)
throws XMLStreamException {
if (replacement instanceof RDFNode) {
RDFNode replacementNode = (RDFNode) replacement;
@@ -343,7 +368,7 @@ public class TemplateInterpolator {
return attributes;
}
- private void writeXMLLiteral(String literal, XMLEventWriter writer)
+ private void writeXMLLiteral(String literal, XMLEventDestination writer)
throws XMLStreamException {
XMLEventReader reader = inputFactory.createXMLEventReader(new StringReader(literal));
while (reader.hasNext()) {
diff --git a/src/main/java/au/com/miskinhill/rdftemplate/XMLEventDestination.java b/src/main/java/au/com/miskinhill/rdftemplate/XMLEventDestination.java
@@ -0,0 +1,11 @@
+package au.com.miskinhill.rdftemplate;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.XMLEvent;
+
+/** Common superinterface which can be implemented by XMLEventWriter or List<XMLEvent>. */
+public interface XMLEventDestination {
+
+ void add(XMLEvent event) throws XMLStreamException;
+
+}