cziegeler 02/05/07 01:44:35 Modified: src/java/org/apache/cocoon/components/source AbstractStreamWriteableSource.java FileSource.java Added: src/java/org/apache/cocoon/components/source WriteableSAXSource.java WriteableSource.java Log: Added WritableSource/WriteableSAXSource interfaces Revision Changes Path 1.3 +25 -25 xml-cocoon2/src/java/org/apache/cocoon/components/source/AbstractStreamWriteableSource.java Index: AbstractStreamWriteableSource.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/source/AbstractStreamWriteableSource.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- AbstractStreamWriteableSource.java 28 Feb 2002 18:00:50 -0000 1.2 +++ AbstractStreamWriteableSource.java 7 May 2002 08:44:35 -0000 1.3 @@ -58,7 +58,6 @@ import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.component.ComponentSelector; -import org.apache.cocoon.environment.WriteableSource; import org.apache.cocoon.serialization.Serializer; import org.apache.cocoon.xml.AbstractXMLPipe; import org.apache.cocoon.ProcessingException; @@ -77,23 +76,25 @@ /** * This abstract class provides convenience methods to implement - * a stream based <code>WriteableSource</code>. Implement getOutputStream() - * to obtain a valid implementation. + * a stream based <code>org.apache.cocoon.environment.WriteableSource</code>. + * Implement getOutputStream() to obtain a valid implementation. * <p> * This base implementation creates a <code>ContentHandler</code> by using * the sitemap 'xml' serializer to write SAX events to the stream returned by * <code>getOutputStream()</code>. * * @author <a href="mailto:[EMAIL PROTECTED]">Sylvain Wallez</a> - * @version $Id: AbstractStreamWriteableSource.java,v 1.2 2002/02/28 18:00:50 sylvain Exp $ + * @version $Id: AbstractStreamWriteableSource.java,v 1.3 2002/05/07 08:44:35 cziegeler Exp $ */ -public abstract class AbstractStreamWriteableSource extends AbstractStreamSource implements WriteableSource { - +public abstract class AbstractStreamWriteableSource + extends AbstractStreamSource + implements org.apache.cocoon.environment.WriteableSource { + protected AbstractStreamWriteableSource(ComponentManager manager) { super(manager); } - + /** * Checks if the <code>OutputStream</code> under <code>handler</code> can be cancelled. * @@ -153,12 +154,12 @@ * exist in this source's component manager. */ public ContentHandler getContentHandler() throws SAXException, ProcessingException { - + Serializer serializer; ComponentSelector selector; - + String serializerName = this.isHTMLContent() ? "html" : "xml"; - + // Get the serializer try { selector = @@ -167,7 +168,7 @@ } catch(ComponentException ce) { throw new ProcessingException("Cannot get '" + serializerName + "' serializer"); } - + try { return new WritingPipe(getOutputStream(), selector, serializer); } catch(IOException ioe) { @@ -175,34 +176,34 @@ throw new ProcessingException("Cannot open stream for " + this.getSystemId(), ioe); } } - + /** * A pipe that closes the outputstream at the end of the document and handles cancel(). */ private class WritingPipe extends AbstractXMLPipe { - + // The output stream private OutputStream output; - + // Serialier and its selector for proper release private Serializer serializer; private ComponentSelector selector; - + public WritingPipe(OutputStream output, ComponentSelector selector, Serializer serializer) throws IOException { this.output = output; this.selector = selector; this.serializer = serializer; - + // Connect this pipe, the serializer and the output stream this.setConsumer(this.serializer); this.serializer.setOutputStream(this.output); } - - public WriteableSource getSource() { + + public org.apache.cocoon.environment.WriteableSource getSource() { return AbstractStreamWriteableSource.this; } - + /** * Close the underlying stream */ @@ -215,11 +216,11 @@ throw new SAXException("Error while closing output stream", e); } } - + public boolean canCancel() { return this.output != null; } - + /** * Cancel the wrapped output stream */ @@ -227,7 +228,7 @@ AbstractStreamWriteableSource.this.cancel(output); close(); } - + private void close() throws IOException { if (this.serializer != null) { // Disconnect serializer; @@ -236,13 +237,13 @@ this.selector.release(this.serializer); this.serializer = null; } - + if (this.output != null) { this.output.close(); this.output = null; } } - + // Ensure all is closed properly protected void finalize() throws Throwable { close(); @@ -250,4 +251,3 @@ } } } - \ No newline at end of file 1.3 +32 -33 xml-cocoon2/src/java/org/apache/cocoon/components/source/FileSource.java Index: FileSource.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/source/FileSource.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- FileSource.java 28 Feb 2002 18:00:50 -0000 1.2 +++ FileSource.java 7 May 2002 08:44:35 -0000 1.3 @@ -56,7 +56,6 @@ import org.apache.avalon.framework.component.ComponentManager; -import org.apache.cocoon.environment.WriteableSource; import org.apache.cocoon.ProcessingException; import java.io.File; @@ -72,52 +71,53 @@ import java.util.ConcurrentModificationException; /** - * A <code>WriteableSource</code> for 'file:/' system IDs. + * A <code>org.apache.cocoon.environment.WriteableSource</code> for 'file:/' system IDs. * * @author <a href="mailto:[EMAIL PROTECTED]">Sylvain Wallez</a> - * @version $Id: FileSource.java,v 1.2 2002/02/28 18:00:50 sylvain Exp $ + * @version $Id: FileSource.java,v 1.3 2002/05/07 08:44:35 cziegeler Exp $ */ -public class FileSource extends AbstractStreamWriteableSource implements WriteableSource { - +public class FileSource extends AbstractStreamWriteableSource + implements org.apache.cocoon.environment.WriteableSource { + /** The underlying file. */ private File file; - + /** The system ID for this source (lazily created by getSystemId()) */ private String systemId = null; - + /** Is this an html file ? */ private boolean isHTMLContent; - + /** * Create a file source from a 'file:' url and a component manager. */ public FileSource(String url, ComponentManager manager) { - + super(manager); - + if (!url.startsWith("file:")) { throw new IllegalArgumentException("Malformed url for a file source : " + url); } - + if (url.endsWith(".htm") || url.endsWith(".html")) { this.isHTMLContent = true; } - + this.file = new File(url.substring(5)); // 5 == "file:".length() } - + public boolean exists() { return this.file.exists(); } - + /** * Returns <code>true</code> if the file name ends with ".htm" or ".html". */ protected boolean isHTMLContent() { return this.isHTMLContent; } - + /** * Return the unique identifer for this source */ @@ -139,15 +139,15 @@ public InputStream getInputStream() throws IOException, ProcessingException { return new FileInputStream(this.file); } - + public long getLastModified() { return this.file.lastModified(); } - + public long getContentLength() { return this.file.length(); } - + /** * Get an output stream to write to this source. The output stream returned * actually writes to a temp file that replaces the real one on close. This @@ -158,25 +158,25 @@ * writing to this file. */ public OutputStream getOutputStream() throws IOException, ProcessingException { - + // Create a temp file. It will replace the right one when writing terminates, // and serve as a lock to prevent concurrent writes. File tmpFile = new File(this.file.getPath() + ".tmp"); - + // Ensure the directory exists tmpFile.getParentFile().mkdirs(); - + // Can we write the file ? if (this.file.exists() && !this.file.canWrite()) { throw new IOException("Cannot write to file " + this.file.getPath()); } - + // Check if it temp file already exists, meaning someone else currently writing if (!tmpFile.createNewFile()) { throw new ConcurrentModificationException("File " + this.file.getPath() + " is already being written by another thread"); } - + // Return a stream that will rename the temp file on close. return new FileSourceOutputStream(tmpFile); } @@ -212,21 +212,21 @@ // Not a valid stream for this source throw new IllegalArgumentException("The stream is not associated to this source"); } - + /** * A file outputStream that will rename the temp file to the destination file upon close() * and discard the temp file upon cancel(). */ private class FileSourceOutputStream extends FileOutputStream { - + private File tmpFile; private boolean isClosed = false; - + public FileSourceOutputStream(File tmpFile) throws IOException { super(tmpFile); this.tmpFile = tmpFile; } - + public FileSource getSource() { return FileSource.this; } @@ -241,7 +241,7 @@ } // Rename temp file to destination file tmpFile.renameTo(FileSource.this.file); - + } finally { // Ensure temp file is deleted, ie lock is released. // If there was a failure above, written data is lost. @@ -251,21 +251,21 @@ this.isClosed = true; } } - + public boolean canCancel() { return !this.isClosed; } - + public void cancel() throws Exception { if (this.isClosed) { throw new IllegalStateException("Cannot cancel : outputstrem is already closed"); } - + this.isClosed = true; super.close(); this.tmpFile.delete(); } - + public void finalize() { if (!this.isClosed && tmpFile.exists()) { // Something wrong happened while writing : delete temp file @@ -274,4 +274,3 @@ } } } - \ No newline at end of file 1.1 xml-cocoon2/src/java/org/apache/cocoon/components/source/WriteableSAXSource.java Index: WriteableSAXSource.java =================================================================== /* * The Apache Software License, Version 1.1 * * * Copyright (c) 2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache Cocoon" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ package org.apache.cocoon.components.source; import org.apache.excalibur.source.SourceException; import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; /** * A {@link Source} that can be written to. It provides two methods that * allow for SAX-based and byte-based output. * <p> * Callers will use the most appropriate method for their use and * it's up to the implementation to handle both sources. For example, * an XML-based implementation can use a parser to convert bytes written * to the <code>OutputStream</code> to SAX events, and a byte-based * implementation (such as file), can use a serializer to convert * SAX events to a byte stream. * * @since @next-version@ * @author <a href="[EMAIL PROTECTED]">Sylvain Wallez</a> * @version CVS $Id: WriteableSAXSource.java,v 1.1 2002/05/07 08:44:35 cziegeler Exp $ */ public interface WriteableSAXSource extends WriteableSource { /** * Get a <code>ContentHandler</code> where an XML document can * be written using SAX events. * <p> * Care should be taken that the returned handler can actually * be a {@link org.apache.cocoon.xml.XMLConsumer} supporting also * lexical events such as comments. * * @return a handler for SAX events */ ContentHandler getContentHandler() throws SAXException, SourceException; /** * Can the data sent to a <code>ContentHandler</code> returned by * {@link #getContentHandler()} be cancelled ? * * @return true if the handler can be cancelled */ boolean canCancel(ContentHandler handler); /** * Cancel the data sent to a <code>ContentHandler</code> returned by * {@link #getContentHandler()}. * <p> * After cancel, the handler should no more be used. */ void cancel(ContentHandler handler) throws SAXException, SourceException; } 1.1 xml-cocoon2/src/java/org/apache/cocoon/components/source/WriteableSource.java Index: WriteableSource.java =================================================================== /* * The Apache Software License, Version 1.1 * * * Copyright (c) 2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache Cocoon" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ package org.apache.cocoon.components.source; import org.apache.excalibur.source.Source; import org.apache.excalibur.source.SourceException; import java.io.IOException; import java.io.OutputStream; /** * A {@link Source} that can be written to. It provides methods that * allow for byte-based output. * <p>If a writable source can handle sax streams, it should implement * the <code>WriteableSAXSource</code> interface instead. * <p> * Callers will use the most appropriate method for their use and * it's up to the implementation to handle both sources. For example, * an XML-based implementation can use a parser to convert bytes written * to the <code>OutputStream</code> to SAX events, and a byte-based * implementation (such as file), can use a serializer to convert * SAX events to a byte stream. * * @since @next-version@ * @author <a href="[EMAIL PROTECTED]">Sylvain Wallez</a> * @version CVS $Id: WriteableSource.java,v 1.1 2002/05/07 08:44:35 cziegeler Exp $ */ public interface WriteableSource extends Source { /** * Does this source actually exist ? * * @return true if the resource exists. */ boolean exists(); /** * Get an <code>InputStream</code> where raw bytes can be written to. * The signification of these bytes is implementation-dependent and * is not restricted to a serialized XML document. * * @return a stream to write to */ OutputStream getOutputStream() throws IOException, SourceException; /** * Can the data sent to an <code>OutputStream</code> returned by * {@link #getOutputStream()} be cancelled ? * * @return true if the stream can be cancelled */ boolean canCancel(OutputStream stream); /** * Cancel the data sent to an <code>OutputStream</code> returned by * {@link #getOutputStream()}. * <p> * After cancel, the stream should no more be used. */ void cancel(OutputStream stream) throws SourceException; }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]