cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thors...@apache.org
Subject svn commit: r1162715 - /cocoon/cocoon3/trunk/cocoon-stax/src/main/java/org/apache/cocoon/stax/component/XMLGenerator.java
Date Mon, 29 Aug 2011 10:15:30 GMT
Author: thorsten
Date: Mon Aug 29 10:15:30 2011
New Revision: 1162715

URL: http://svn.apache.org/viewvc?rev=1162715&view=rev
Log:
Enhancing the StAX based generator to be usable from the sitemap.

Modified:
    cocoon/cocoon3/trunk/cocoon-stax/src/main/java/org/apache/cocoon/stax/component/XMLGenerator.java

Modified: cocoon/cocoon3/trunk/cocoon-stax/src/main/java/org/apache/cocoon/stax/component/XMLGenerator.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-stax/src/main/java/org/apache/cocoon/stax/component/XMLGenerator.java?rev=1162715&r1=1162714&r2=1162715&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-stax/src/main/java/org/apache/cocoon/stax/component/XMLGenerator.java
(original)
+++ cocoon/cocoon3/trunk/cocoon-stax/src/main/java/org/apache/cocoon/stax/component/XMLGenerator.java
Mon Aug 29 10:15:30 2011
@@ -16,8 +16,13 @@
  */
 package org.apache.cocoon.stax.component;
 
+import java.io.ByteArrayInputStream;
+import java.io.FileInputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
+import java.net.URLConnection;
+import java.util.Map;
 
 import javax.xml.stream.FactoryConfigurationError;
 import javax.xml.stream.XMLEventReader;
@@ -27,13 +32,18 @@ import javax.xml.stream.events.XMLEvent;
 
 import org.apache.cocoon.pipeline.SetupException;
 import org.apache.cocoon.pipeline.component.Starter;
+import org.apache.cocoon.pipeline.util.URLConnectionUtils;
 import org.apache.cocoon.stax.AbstractStAXProducer;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.output.ByteArrayOutputStream;
 
 /**
  * General element generator for a StAX pipeline directly taking all elements from an internal
  * {@link XMLEventReader} created from an {@link InputStream} or directly from an {@link
URL}.
  */
 public class XMLGenerator extends AbstractStAXProducer implements Starter {
+    
+    public XMLGenerator(){}
 
     private XMLEventReader reader;
 
@@ -54,7 +64,37 @@ public class XMLGenerator extends Abstra
             throw new SetupException("Error during setup the XMLInputFactory for creating
an XMLEventReader", e);
         }
     }
-
+    @Override
+    public void setConfiguration(
+            final Map<String, ? extends Object> configuration) {
+        URLConnection connection = null;
+        InputStream input = null;
+        try {
+            URL url = (URL) configuration.get("source");
+            // if we are using files we do not buffer
+            if(url.getProtocol().equals("file")){
+                input = new FileInputStream(url.getPath());
+            }else{
+                connection = url.openConnection();
+                input = copyInputstream(connection);  
+            }
+            this.reader = XMLInputFactory.newInstance().createXMLEventReader(input);
+        } catch (Exception e) {
+            throw new SetupException("Error during setup an XMLEventReader on the inputStream",
e);
+        }
+        finally {
+            if (connection != null) {
+                URLConnectionUtils.closeQuietly(connection);
+            }
+        }
+    }
+    protected InputStream copyInputstream(URLConnection connection)
+            throws IOException {
+        ByteArrayOutputStream output = new ByteArrayOutputStream();
+        IOUtils.copy(connection.getInputStream(), output);
+        InputStream input = new ByteArrayInputStream(output.toByteArray());
+        return input;
+    }
     /**
      * {@inheritDoc}
      * 



Mime
View raw message