cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Adrian Petru Dimulescu" <adrian.dimule...@free.fr>
Subject RE: SxwGenerator: ZipInputStream gets closed
Date Tue, 13 May 2003 14:21:03 GMT
> Just an idea: make a wrapper around the inputstream that intercepts
the
> close() call.

Just done that, thanks for the idea.

Is there any interest from Cocoon maintainers to include this
SxwGenerator in Cocoon?


Here is the functional java file:


/*
 * Created on 12 mai 2003
 *
 */
package org.apache.cocoon.generation;

import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.avalon.framework.component.ComponentException;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.xml.EmbeddedXMLPipe;
import org.apache.excalibur.xml.sax.SAXParser;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

/**
 * Generates Sax events using an OpenOffice Writer .sxw file as source.
 * <br/>
 * It uses &lt;office:document> as root and pastes in all XML content
that can
 * be found inside the .sxw zip archive.
 * 
 * @author Adrian Petru Dimulescu
 */
public class SxwGenerator extends FileGenerator {

	protected String rootNsUrl 			=
"http://openoffice.org/2000/office";
	protected String rootElementName 	= "document";
	protected String rootPrefix 		= "office";

	/* (non-Javadoc)
	 * @see org.apache.cocoon.generation.Generator#generate()
	 */
	public void generate()
		throws IOException, SAXException, ProcessingException 
	{
		this.xmlConsumer.startDocument();
		this.xmlConsumer.startElement(
			this.rootNsUrl,
			this.rootElementName,
			this.rootPrefix + ":" + this.rootElementName,
			new AttributesImpl());

		NonCloseableZipInputStream zin = null;
		try {
			zin =
				new
NonCloseableZipInputStream(this.inputSource.getInputStream());
			ZipEntry entry;
			 
			EmbeddedXMLPipe startEndFilter = new
EmbeddedXMLPipe(this.xmlConsumer);
			SAXParser parser = (SAXParser)
this.manager.lookup(SAXParser.ROLE);
		
			while ((entry = zin.getNextEntry()) != null) {
				if
(entry.getName().toLowerCase().endsWith(".xml")) {
					InputSource iSrc = new
InputSource(zin);
					parser.parse(iSrc,
startEndFilter);
				}
			}
			
		} catch (ComponentException e) {
			getLogger().error("Could not get parser", e);
			throw new ProcessingException(
				"Exception in "
					+ this.getClass().getName()
					+ ".generate()",
				e);
		} finally {
			this.xmlConsumer.endElement(
				this.rootNsUrl,
				this.rootElementName,
				this.rootPrefix + ":" +
this.rootElementName);
			this.xmlConsumer.endDocument();
			if (zin != null)
				zin.reallyClose();
		}
	}

}

class NonCloseableZipInputStream extends ZipInputStream {
	NonCloseableZipInputStream(InputStream is) {
		super(is);
	}

	public void close() throws IOException {
		// Don't close
	}

	public void reallyClose() throws IOException {
		// really close
		super.close();
	}
}



Mime
View raw message