You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by re...@apache.org on 2009/12/14 14:35:50 UTC
svn commit: r890314 - in /cocoon/cocoon3/trunk:
cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/
cocoon-sax/src/main/java/org/apache/cocoon/sax/component/
cocoon-sax/src/test/java/org/apache/cocoon/sax/component/
Author: reinhard
Date: Mon Dec 14 13:35:49 2009
New Revision: 890314
URL: http://svn.apache.org/viewvc?rev=890314&view=rev
Log:
merge URLGenerator with SAXGeneratar
add SAXBuffer as input source for SAXGenerator
unit tests for both cases
Removed:
cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/URLGenerator.java
Modified:
cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/CachingPipeline.java
cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/SAXGenerator.java
cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/SAXGeneratorTest.java
Modified: cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/CachingPipeline.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/CachingPipeline.java?rev=890314&r1=890313&r2=890314&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/CachingPipeline.java (original)
+++ cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/CachingPipeline.java Mon Dec 14 13:35:49 2009
@@ -74,12 +74,25 @@
CachingPipelineComponent cachablePipelineComponent = (CachingPipelineComponent) pipelineComponent;
CacheKey cacheKey = cachablePipelineComponent.constructCacheKey();
- result.addCacheKey(cacheKey);
+ if (cacheKey != null) {
+ result.addCacheKey(cacheKey);
+ if (this.logger.isDebugEnabled()) {
+ this.logger.debug(" ~ adding " + cacheKey + " for component " + pipelineComponent);
+ }
+
+ continue;
+ }
+ }
+
+ // support expires caching
+ if (this.expires != null) {
if (this.logger.isDebugEnabled()) {
- this.logger.debug(" ~ adding " + cacheKey + " for component " + pipelineComponent);
+ this.logger.debug(" ~ adding " + ExpiresCacheKey.class.getSimpleName() + " for component: "
+ + pipelineComponent + " (the component doesn't support caching "
+ + "but expires caching is activated)");
}
- continue;
+ return new ExpiresCacheKey(new InvalidCacheKey(this.expiresCacheKey), this.expires);
}
// component does not support caching
@@ -88,18 +101,23 @@
this.logger.debug("Aborting cache key construction");
}
- // support expires caching
- if (this.expires != null) {
- return new ExpiresCacheKey(new InvalidCacheKey(this.expiresCacheKey), this.expires);
- }
return null;
}
// support expires caching
if (this.expires != null) {
- return new ExpiresCacheKey(result, this.expires);
+ CacheKey expiresCacheKey = new ExpiresCacheKey(result, this.expires);
+
+ if (this.logger.isDebugEnabled()) {
+ this.logger.debug("Creating " + expiresCacheKey + " for pipeline " + this);
+ }
+
+ return expiresCacheKey;
}
+ if (this.logger.isDebugEnabled()) {
+ this.logger.debug("Creating " + result + " for pipeline " + this);
+ }
return result;
}
Modified: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/SAXGenerator.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/SAXGenerator.java?rev=890314&r1=890313&r2=890314&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/SAXGenerator.java (original)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/SAXGenerator.java Mon Dec 14 13:35:49 2009
@@ -21,18 +21,27 @@
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
+import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Map;
import org.apache.cocoon.pipeline.PipelineException;
import org.apache.cocoon.pipeline.ProcessingException;
import org.apache.cocoon.pipeline.SetupException;
+import org.apache.cocoon.pipeline.caching.CacheKey;
+import org.apache.cocoon.pipeline.caching.TimestampCacheKey;
+import org.apache.cocoon.pipeline.component.CachingPipelineComponent;
import org.apache.cocoon.pipeline.component.Starter;
import org.apache.cocoon.pipeline.util.StringRepresentation;
+import org.apache.cocoon.pipeline.util.URLConnectionUtils;
import org.apache.cocoon.sax.AbstractSAXGenerator;
import org.apache.cocoon.sax.AbstractSAXProducer;
import org.apache.cocoon.sax.util.XMLUtils;
import org.apache.cocoon.xml.dom.DOMStreamer;
+import org.apache.cocoon.xml.sax.SAXBuffer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Node;
@@ -41,12 +50,16 @@
/**
* General purpose SAX generator that produces SAX events from different sources.
*/
-public class SAXGenerator extends AbstractSAXGenerator {
+public class SAXGenerator extends AbstractSAXGenerator implements CachingPipelineComponent {
private Starter generator;
private final Log logger = LogFactory.getLog(this.getClass());
+ public SAXGenerator() {
+ this((URL) null);
+ }
+
public SAXGenerator(byte[] bytes) {
this.generator = new ByteArrayGenerator(bytes);
}
@@ -67,14 +80,40 @@
this.generator = new NodeGenerator(node);
}
+ public SAXGenerator(SAXBuffer saxBuffer) {
+ this.generator = new SAXBufferGenerator(saxBuffer);
+ }
+
public SAXGenerator(String xmlString) {
this.generator = new StringGenerator(xmlString);
}
+ public SAXGenerator(URL url) {
+ this.generator = new URLGenerator(url);
+ }
+
+ public CacheKey constructCacheKey() {
+ if (this.generator instanceof CachingPipelineComponent) {
+ return ((CachingPipelineComponent) this.generator).constructCacheKey();
+ }
+
+ return null;
+ }
+
public void execute() {
this.generator.execute();
}
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.apache.cocoon.sax.AbstractSAXProducer#setConfiguration(java.util.Map)
+ */
+ @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, "internalGenerator=" + this.generator);
@@ -206,6 +245,10 @@
}
public void execute() {
+ if (SAXGenerator.this.logger.isDebugEnabled()) {
+ SAXGenerator.this.logger.debug("Using a DOM node to produce SAX events.");
+ }
+
DOMStreamer streamer = new DOMStreamer(SAXGenerator.this.getSAXConsumer());
try {
streamer.stream(this.node);
@@ -220,6 +263,38 @@
}
}
+ private class SAXBufferGenerator extends AbstractSAXGenerator {
+
+ private final SAXBuffer saxBuffer;
+
+ public SAXBufferGenerator(SAXBuffer saxBuffer) {
+ super();
+
+ if (saxBuffer == null) {
+ throw new SetupException("A SAXBuffer has to be passed.");
+ }
+
+ this.saxBuffer = saxBuffer;
+ }
+
+ public void execute() {
+ if (SAXGenerator.this.logger.isDebugEnabled()) {
+ SAXGenerator.this.logger.debug("Using a SAXBuffer to produce SAX events.");
+ }
+
+ try {
+ this.saxBuffer.toSAX(SAXGenerator.this.getSAXConsumer());
+ } catch (SAXException e) {
+ throw new ProcessingException("Can't stream " + this + " into the content handler.", e);
+ }
+ }
+
+ @Override
+ public String toString() {
+ return StringRepresentation.buildString(this, "saxBuffer=" + this.saxBuffer);
+ }
+ }
+
private class StringGenerator extends AbstractSAXProducer implements Starter {
private String xmlString;
@@ -257,4 +332,69 @@
return StringRepresentation.buildString(this, "xmlString=" + this.xmlString);
}
}
+
+ private class URLGenerator extends AbstractSAXGenerator implements CachingPipelineComponent {
+
+ private URL source;
+
+ public URLGenerator(URL source) {
+ super();
+ this.source = source;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.apache.cocoon.pipeline.component.CachingPipelineComponent#constructCacheKey()
+ */
+ public CacheKey constructCacheKey() {
+ if (this.source == null) {
+ throw new SetupException(this.getClass().getSimpleName() + " has no source.");
+ }
+
+ URLConnection connection = null;
+ try {
+ connection = this.source.openConnection();
+ TimestampCacheKey timestampCacheKey = new TimestampCacheKey(this.source, connection.getLastModified());
+ return timestampCacheKey;
+ } catch (IOException e) {
+ SAXGenerator.this.logger
+ .error("Can't construct cache key. Error while connecting to " + this.source, e);
+ } finally {
+ URLConnectionUtils.closeQuietly(connection);
+ }
+
+ return null;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.apache.cocoon.pipeline.component.Starter#execute()
+ */
+ public void execute() {
+ if (this.source == null) {
+ throw new ProcessingException(this.getClass().getSimpleName() + " has no source.");
+ }
+
+ if (SAXGenerator.this.logger.isDebugEnabled()) {
+ SAXGenerator.this.logger.debug("Using the URL " + this.source + " to produce SAX events.");
+ }
+
+ try {
+ XMLUtils.toSax(this.source.openConnection(), SAXGenerator.this.getSAXConsumer());
+ } catch (IOException e) {
+ throw new ProcessingException("Can't open connection to " + this.source, e);
+ }
+ }
+
+ public void setSource(URL source) {
+ this.source = source;
+ }
+
+ @Override
+ public String toString() {
+ return StringRepresentation.buildString(this, "source=" + this.source);
+ }
+ }
}
Modified: cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/SAXGeneratorTest.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/SAXGeneratorTest.java?rev=890314&r1=890313&r2=890314&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/SAXGeneratorTest.java (original)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/SAXGeneratorTest.java Mon Dec 14 13:35:49 2009
@@ -22,6 +22,9 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
import junit.framework.Assert;
@@ -29,15 +32,16 @@
import org.apache.cocoon.pipeline.Pipeline;
import org.apache.cocoon.pipeline.ProcessingException;
import org.apache.cocoon.pipeline.SetupException;
+import org.apache.cocoon.sax.AbstractSAXSerializer;
import org.apache.cocoon.sax.SAXPipelineComponent;
import org.apache.cocoon.sax.SAXProducer;
+import org.apache.cocoon.xml.sax.SAXBuffer;
import org.custommonkey.xmlunit.Diff;
import org.junit.Test;
public class SAXGeneratorTest {
private static final String INVALID_XML_STRING = "<?xml version=\"1.0\"?><test>text<test>";
-
private static final String VALID_XML_STRING = "<?xml version=\"1.0\"?><test>text</test>";
@Test
@@ -47,12 +51,33 @@
@Test
public void execFileGenerator() throws Exception {
- runPipeline(new SAXGenerator(this.createXMLFile(VALID_XML_STRING)), VALID_XML_STRING);
+ runPipeline(new SAXGenerator(createXMLFile(VALID_XML_STRING)), VALID_XML_STRING);
}
@Test
public void execInputStreamGenerator() throws Exception {
- runPipeline(new SAXGenerator(new FileInputStream(this.createXMLFile(VALID_XML_STRING))), VALID_XML_STRING);
+ runPipeline(new SAXGenerator(new FileInputStream(createXMLFile(VALID_XML_STRING))), VALID_XML_STRING);
+ }
+
+ @Test
+ public void execSAXBufferGenerator() throws Exception {
+ Pipeline<SAXPipelineComponent> pipeline = new NonCachingPipeline<SAXPipelineComponent>();
+ pipeline.addComponent(new SAXGenerator(VALID_XML_STRING));
+ final SAXBuffer saxBuffer = new SAXBuffer();
+ pipeline.addComponent(new AbstractSAXSerializer() {
+
+ @Override
+ public void setup(Map<String, Object> inputParameters) {
+ super.setup(inputParameters);
+ this.contentHandler = saxBuffer;
+ }
+ });
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ pipeline.setup(baos);
+ pipeline.execute();
+
+ runPipeline(new SAXGenerator(saxBuffer), VALID_XML_STRING);
}
@Test
@@ -60,6 +85,27 @@
runPipeline(new SAXGenerator(VALID_XML_STRING), VALID_XML_STRING);
}
+ @Test
+ public void execURLGenerator() throws Exception {
+ runPipeline(new SAXGenerator(createXMLFile(VALID_XML_STRING).toURL()), VALID_XML_STRING);
+ }
+
+ @Test
+ public void execURLGeneratorBySeparatelyPassingURL() throws Exception {
+ SAXGenerator generator = new SAXGenerator();
+
+ Map<String, Object> configurationParams = new HashMap<String, Object>();
+ configurationParams.put("source", createXMLFile(VALID_XML_STRING).toURL());
+ generator.setConfiguration(configurationParams);
+
+ runPipeline(generator, VALID_XML_STRING);
+ }
+
+ @Test(expected = ProcessingException.class)
+ public void execURLGeneratorWithEmptySource() throws Exception {
+ runPipeline(new SAXGenerator((URL) null), VALID_XML_STRING);
+ }
+
@Test(expected = ProcessingException.class)
public void invalidExecByteArrayGenerator() throws Exception {
runPipeline(new SAXGenerator(INVALID_XML_STRING.getBytes()), INVALID_XML_STRING);
@@ -67,12 +113,12 @@
@Test(expected = ProcessingException.class)
public void invalidExecFileGenerator() throws Exception {
- runPipeline(new SAXGenerator(this.createXMLFile(INVALID_XML_STRING)), INVALID_XML_STRING);
+ runPipeline(new SAXGenerator(createXMLFile(INVALID_XML_STRING)), INVALID_XML_STRING);
}
@Test(expected = ProcessingException.class)
public void invalidExecInputStreamGenerator() throws Exception {
- runPipeline(new SAXGenerator(new FileInputStream(this.createXMLFile(INVALID_XML_STRING))), INVALID_XML_STRING);
+ runPipeline(new SAXGenerator(new FileInputStream(createXMLFile(INVALID_XML_STRING))), INVALID_XML_STRING);
}
@Test(expected = ProcessingException.class)
@@ -100,7 +146,16 @@
new SAXGenerator((InputStream) null);
}
- private File createXMLFile(String xmlString) throws IOException {
+ @Test(expected = SetupException.class)
+ public void testSAXBufferGenerator() {
+ new SAXGenerator((SAXBuffer) null);
+ }
+
+ public void testURLGenerator() {
+ new SAXGenerator((URL) null);
+ }
+
+ private static File createXMLFile(String xmlString) throws IOException {
File temp = File.createTempFile("cocoon", ".tmp");
FileWriter writer = new FileWriter(temp);
writer.write(xmlString);