cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thors...@apache.org
Subject svn commit: r1203306 - in /cocoon/cocoon3/trunk/cocoon-optional/src: main/java/org/apache/cocoon/optional/pipeline/components/sax/neko/NekoGenerator.java test/java/org/apache/cocoon/optional/pipeline/components/sax/neko/NekoGeneratorTestCase.java
Date Thu, 17 Nov 2011 17:52:41 GMT
Author: thorsten
Date: Thu Nov 17 17:52:40 2011
New Revision: 1203306

URL: http://svn.apache.org/viewvc?rev=1203306&view=rev
Log:
Extending the NekoHtmlGenerator to work as well with String input. Using the xmlgenerator
(sax) as state-of-the-art implementation for this kind of extensions

Modified:
    cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/neko/NekoGenerator.java
    cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/neko/NekoGeneratorTestCase.java

Modified: cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/neko/NekoGenerator.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/neko/NekoGenerator.java?rev=1203306&r1=1203305&r2=1203306&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/neko/NekoGenerator.java
(original)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/neko/NekoGenerator.java
Thu Nov 17 17:52:40 2011
@@ -16,15 +16,24 @@
  */
 package org.apache.cocoon.optional.pipeline.components.sax.neko;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.io.StringReader;
 import java.net.URL;
 import java.util.Map;
 
 import org.apache.cocoon.pipeline.ProcessingException;
 import org.apache.cocoon.pipeline.SetupException;
+import org.apache.cocoon.pipeline.component.Starter;
 import org.apache.cocoon.pipeline.util.StringRepresentation;
 import org.apache.cocoon.sax.AbstractSAXGenerator;
+import org.apache.cocoon.sax.AbstractSAXProducer;
+import org.apache.cocoon.sax.SAXConsumer;
+import org.apache.cocoon.sax.component.XMLGenerator;
 import org.cyberneko.html.parsers.SAXParser;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXNotRecognizedException;
 import org.xml.sax.SAXNotSupportedException;
@@ -43,47 +52,17 @@ public class NekoGenerator extends Abstr
 
     private static final String XHTML_URL = "http://www.w3.org/1999/xhtml";
 
-    private URL htmlSource;
+    private transient Starter generator;
 
-    private SAXParser saxParser;
+    private transient SAXParser saxParser;
+    /**
+     * Logger.
+     */
+    private static final Logger LOG = LoggerFactory
+            .getLogger(XMLGenerator.class);
 
     public NekoGenerator() {
         super();
-    }
-
-    public NekoGenerator(URL htmlSource) {
-        super();
-
-        this.setHtmlSource(htmlSource);
-    }
-
-    public void execute() {
-        this.saxParser.setContentHandler(this.getSAXConsumer());
-
-        try {
-            this.saxParser.parse(this.htmlSource.toExternalForm());
-        } catch (SAXException e) {
-            throw new ProcessingException("Fatal XML error", e);
-        } catch (IOException e) {
-            throw new ProcessingException("Fatal protocol violation", e);
-        }
-    }
-
-    @Override
-    public void setConfiguration(Map<String, ? extends Object> configuration) {
-        super.setConfiguration(configuration);
-
-        this.setHtmlSource((URL) configuration.get("source"));
-    }
-
-    public void setHtmlSource(URL htmlSource) {
-        this.htmlSource = htmlSource;
-    }
-
-    @Override
-    public void setup(Map<String, Object> parameters) {
-        super.setup(parameters);
-
         this.saxParser = new SAXParser();
         try {
             this.saxParser.setFeature(BALANCE_TAGS_URI, true);
@@ -98,11 +77,121 @@ public class NekoGenerator extends Abstr
             throw new SetupException(
                     "Property not supported by the HTML Parser", e);
         }
+
+    }
+
+    public NekoGenerator(URL url) {
+        this();
+        this.generator = new URLGenerator(url, saxParser);
+    }
+
+    public NekoGenerator(final String xmlString) {
+        this();
+        this.generator = new StringGenerator(xmlString, saxParser);
+    }
+
+    public void execute() {
+        this.generator.execute();
+    }
+
+    @Override
+    public void setConfiguration(Map<String, ? extends Object> configuration) {
+        ((URLGenerator) this.generator).setSource((URL) configuration
+                .get("source"));
     }
 
     @Override
     public String toString() {
-        return StringRepresentation.buildString(this, "htmlSource="
-                + this.htmlSource);
+        return StringRepresentation.buildString(this, "internalGenerator="
+                + this.generator);
+    }
+
+    private class StringGenerator extends AbstractSAXProducer implements
+            Starter {
+
+        private transient SAXParser saxParser;
+
+        private final transient String xmlString;
+
+        public StringGenerator(final String xmlString, SAXParser saxParser) {
+            this.saxParser = saxParser;
+            if (xmlString == null) {
+                throw new SetupException("An XML string has to be passed.");
+            }
+
+            this.xmlString = xmlString;
+        }
+
+        /**
+         * {@inheritDoc}
+         * 
+         * @see org.apache.cocoon.pipeline.component.Starter#execute()
+         */
+        public void execute() {
+
+            this.saxParser.setContentHandler(NekoGenerator.this.getSAXConsumer());
+
+            try {
+                InputSource source = new InputSource(
+                        new StringReader(xmlString));
+                this.saxParser.parse(source);
+            } catch (SAXException e) {
+                LOG.error("Fatal XML error", e);
+                throw new ProcessingException("Fatal XML error", e);
+            } catch (IOException e) {
+                LOG.error("Fatal protocol violation", e);
+                throw new ProcessingException("Fatal protocol violation", e);
+            }
+        }
+
+        @Override
+        public String toString() {
+            return StringRepresentation.buildString(this, "xmlString="
+                    + this.xmlString);
+        }
     }
+
+    private class URLGenerator extends AbstractSAXGenerator {
+
+        private transient SAXParser saxParser;
+
+        private transient URL source;
+
+        public URLGenerator(final URL source, SAXParser saxParser) {
+            this.saxParser = saxParser;
+            this.source = source;
+        }
+
+        /**
+         * {@inheritDoc}
+         * 
+         * @see org.apache.cocoon.pipeline.component.Starter#execute()
+         */
+        public void execute() {
+            SAXConsumer saxConsumer = NekoGenerator.this.getSAXConsumer();
+            this.saxParser.setContentHandler(saxConsumer);
+
+            try {
+                String externalForm = this.source.toExternalForm();
+                this.saxParser.parse(externalForm);
+            } catch (SAXException e) {
+                LOG.error("Fatal XML error", e);
+                throw new ProcessingException("Fatal XML error", e);
+            } catch (IOException e) {
+                LOG.error("Fatal protocol violation", e);
+                throw new ProcessingException("Fatal protocol violation", e);
+            }
+        }
+
+        public void setSource(final URL source) {
+            this.source = source;
+        }
+
+        public String toString() {
+            return StringRepresentation.buildString(this, "source="
+                    + this.source);
+        }
+
+    }
+
 }

Modified: cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/neko/NekoGeneratorTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/neko/NekoGeneratorTestCase.java?rev=1203306&r1=1203305&r2=1203306&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/neko/NekoGeneratorTestCase.java
(original)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/neko/NekoGeneratorTestCase.java
Thu Nov 17 17:52:40 2011
@@ -19,6 +19,7 @@ package org.apache.cocoon.optional.pipel
 import static org.custommonkey.xmlunit.XMLAssert.assertNodeTestPasses;
 
 import java.io.ByteArrayOutputStream;
+import java.net.URL;
 
 import org.apache.cocoon.pipeline.NonCachingPipeline;
 import org.apache.cocoon.pipeline.Pipeline;
@@ -29,19 +30,39 @@ import org.junit.Test;
 import org.w3c.dom.Node;
 
 public final class NekoGeneratorTestCase {
-
+    
     @Test
     public void testPipelineWithNekoGenerator() throws Exception {
+       
+        URL resource = this.getClass().getResource("sample.html");
+        NekoGenerator generator = new NekoGenerator(resource);
+        createPipeAndExec(generator);
+       
+    }
+    
+    @Test
+    public void testPipelineWithNekoGeneratorString() throws Exception {
+        String html = "<html> <head> <title>Sample HTML</title> </head>
<body> <p> <ul> <li>not balanced, but Neko takes care of it </ul>
</p> </body> </html>";
+        NekoGenerator generator = new NekoGenerator(html);
+        createPipeAndExec(generator);
+    }
+    
+    private void createPipeAndExec(NekoGenerator generator) throws Exception{
         Pipeline<SAXPipelineComponent> pipeline = new NonCachingPipeline<SAXPipelineComponent>();
-        pipeline.addComponent(new NekoGenerator(this.getClass().getResource("sample.html")));
+        pipeline.addComponent(generator);
         pipeline.addComponent(new XMLSerializer());
 
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         pipeline.setup(baos);
         pipeline.execute();
 
+        String string = new String(baos.toByteArray());
+        compareResult(string);
+    }
+    
+    private void compareResult(String string) throws Exception {
         CountingNodeTester countingNodeTester = new CountingNodeTester(9);
-
-        assertNodeTestPasses(new String(baos.toByteArray()), countingNodeTester, Node.TEXT_NODE);
+        assertNodeTestPasses(string, countingNodeTester, Node.TEXT_NODE);
     }
+    
 }



Mime
View raw message