rdftemplate

Library for generating XML documents from RDF data using templates
git clone https://code.djc.id.au/git/rdftemplate/
commit b57f527c4f3ba3e78ad7fda8055d0588d7889f40
parent eacca34000e8963d36a7323a59ef38ad9dc871b7
Author: Dan Callaghan <djc@djc.id.au>
Date:   Sun, 15 May 2011 19:50:36 +1000

clean up and flesh out TemplateInterpolator constructors

Diffstat:
Msrc/main/java/au/id/djc/rdftemplate/TemplateInterpolator.java | 100+++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------
1 file changed, 70 insertions(+), 30 deletions(-)
diff --git a/src/main/java/au/id/djc/rdftemplate/TemplateInterpolator.java b/src/main/java/au/id/djc/rdftemplate/TemplateInterpolator.java
@@ -67,31 +67,55 @@ public class TemplateInterpolator {
     }
     
     public String interpolate(Reader reader, RDFNode node) {
+        StringWriter writer = new StringWriter();
+        interpolate(reader, node, writer);
+        return writer.toString();
+    }
+    
+    public void interpolate(Reader reader, RDFNode node, Writer writer) {
+        interpolate(reader, node, adaptToEventConsumer(writer));
+    }
+    
+    public void interpolate(Reader reader, RDFNode node, Collection<XMLEvent> destination) {
+        interpolate(reader, node, adaptToEventConsumer(destination));
+    }
+    
+    public void interpolate(Reader reader, RDFNode node, XMLEventConsumer writer) {
         try {
-            StringWriter writer = new StringWriter();
-            final XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
-            XMLEventConsumer destination = new XMLEventConsumer() {
-                @Override
-                public void add(XMLEvent event) throws XMLStreamException {
-                    eventWriter.add(event);
-                }
-            };
-            interpolate(reader, node, destination);
-            return writer.toString();
+            interpolate(adaptToEventIterator(reader), node, writer);
         } catch (XMLStreamException e) {
-            throw new TemplateSyntaxException(e);            
+            throw new TemplateSyntaxException(e);
+        } finally {
+            try {
+                reader.close();
+            } catch (IOException e) {
+                throw new RuntimeException(e);
+            }
         }
     }
     
-    @SuppressWarnings("unchecked")
-    public void interpolate(Reader reader, RDFNode node, XMLEventConsumer writer) {
+    public String interpolate(InputStream inputStream, RDFNode node) {
+        StringWriter writer = new StringWriter();
+        interpolate(inputStream, node, writer);
+        return writer.toString();
+    }
+    
+    public void interpolate(InputStream inputStream, RDFNode node, Writer writer) {
+        interpolate(inputStream, node, adaptToEventConsumer(writer));
+    }
+    
+    public void interpolate(InputStream inputStream, RDFNode node, Collection<XMLEvent> destination) {
+        interpolate(inputStream, node, adaptToEventConsumer(destination));
+    }
+    
+    public void interpolate(InputStream inputStream, RDFNode node, XMLEventConsumer writer) {
         try {
-            interpolate(inputFactory.createXMLEventReader(reader), node, writer);
+            interpolate(adaptToEventIterator(inputStream), node, writer);
         } catch (XMLStreamException e) {
-            throw new RuntimeException(e);
+            throw new TemplateSyntaxException(e);
         } finally {
             try {
-                reader.close();
+                inputStream.close();
             } catch (IOException e) {
                 throw new RuntimeException(e);
             }
@@ -99,28 +123,44 @@ public class TemplateInterpolator {
     }
     
     @SuppressWarnings("unchecked")
-    public void interpolate(InputStream inputStream, RDFNode node, Writer writer) {
+    private Iterator<XMLEvent> adaptToEventIterator(Reader reader) {
         try {
-            final XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
-            XMLEventConsumer destination = new XMLEventConsumer() {
-                @Override
-                public void add(XMLEvent event) throws XMLStreamException {
-                    eventWriter.add(event);
-                }
-            };
-            interpolate(inputFactory.createXMLEventReader(inputStream), node, destination);
+            return inputFactory.createXMLEventReader(reader);
+        } catch (XMLStreamException e) {
+            throw new TemplateSyntaxException(e);
+        }
+    }
+    
+    @SuppressWarnings("unchecked")
+    private Iterator<XMLEvent> adaptToEventIterator(InputStream inputStream) {
+        try {
+            return inputFactory.createXMLEventReader(inputStream);
         } catch (XMLStreamException e) {
-            throw new RuntimeException(e);
+            throw new TemplateSyntaxException(e);
         }
     }
     
-    public void interpolate(Reader reader, RDFNode node, final Collection<XMLEvent> destination) {
-        interpolate(reader, node, new XMLEventConsumer() {
+    private XMLEventConsumer adaptToEventConsumer(final Collection<XMLEvent> destination) {
+        return new XMLEventConsumer() {
             @Override
             public void add(XMLEvent event) {
                 destination.add(event);
             }
-        });
+        };
+    }
+    
+    private XMLEventConsumer adaptToEventConsumer(Writer writer) {
+        try {
+            final XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
+            return new XMLEventConsumer() {
+                @Override
+                public void add(XMLEvent event) throws XMLStreamException {
+                    eventWriter.add(event);
+                }
+            };
+        } catch (XMLStreamException e) {
+            throw new TemplateSyntaxException(e);
+        }
     }
     
     public void interpolate(Iterator<XMLEvent> reader, RDFNode node, XMLEventConsumer writer)
@@ -274,7 +314,7 @@ public class TemplateInterpolator {
         }
     }
     
-    private List<XMLEvent> consumeTree(StartElement start, Iterator<XMLEvent> reader) throws XMLStreamException {
+    private List<XMLEvent> consumeTree(StartElement start, Iterator<XMLEvent> reader) {
         List<XMLEvent> events = new ArrayList<XMLEvent>();
         events.add(start);
         Deque<QName> elementStack = new LinkedList<QName>();