001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.serialization;
018:
019: import java.io.IOException;
020: import java.io.OutputStream;
021:
022: import org.apache.cocoon.components.elementprocessor.ElementProcessor;
023: import org.apache.cocoon.components.elementprocessor.impl.poi.POIFSElementProcessor;
024:
025: import org.apache.poi.poifs.filesystem.POIFSFileSystem;
026: import org.xml.sax.SAXException;
027:
028: /**
029: * An extension of ElementProcessorSerializer with extensions for dealing with
030: * the POIFS filesystem This is an abstract class. Concrete extensions need to
031: * implement the following methods:
032: * <ul>
033: * <li> String getMimeType()</li>
034: * <li> void doLocalPreEndDocument()</li>
035: * <li> void doLocalPostEndDocument()</li>
036: * <li> ElementProcessorFactory getElementProcessorFactory()</li>
037: * </li>
038: * </ul>
039: *
040: * @author Marc Johnson (marc_johnson27591@hotmail.com)
041: * @author Nicola Ken Barozzi (nicolaken@apache.org)
042: * @version CVS $Id: POIFSSerializer.java 433543 2006-08-22 06:22:54Z crossley $
043: */
044: public abstract class POIFSSerializer extends
045: ElementProcessorSerializer {
046: private POIFSFileSystem _filesystem;
047:
048: /**
049: * Constructor
050: */
051:
052: public POIFSSerializer() {
053: super ();
054: _filesystem = new POIFSFileSystem();
055: }
056:
057: /*
058: * ********** START implementation of ContentHandler **********
059: */
060: /**
061: * Receive notification of the end of a document.
062: *
063: *@exception SAXException if there is an error writing the document to the
064: * output stream
065: */
066:
067: public void endDocument() throws SAXException {
068: doLocalPreEndDocument();
069:
070: OutputStream stream = getOutputStream();
071:
072: if (stream != null) {
073: try {
074: _filesystem.writeFilesystem(stream);
075: } catch (IOException e) {
076: throw SAXExceptionFactory(
077: "could not process endDocument event", e);
078: }
079: } else {
080: throw SAXExceptionFactory("no outputstream for writing the document!!");
081: }
082: doLocalPostEndDocument();
083: }
084:
085: /**
086: * Provide access to the filesystem for extending classes
087: *
088: *@return the filesystem
089: */
090:
091: protected POIFSFileSystem getFilesystem() {
092: return _filesystem;
093: }
094:
095: /**
096: * Extending classes should do whatever they need to do prior to writing the
097: * filesystem out
098: */
099:
100: protected abstract void doLocalPreEndDocument();
101:
102: /**
103: * Extending classes should do whatever they need to do after writing the
104: * filesystem out
105: */
106:
107: protected abstract void doLocalPostEndDocument();
108:
109: /**
110: * perform pre-initialization on an element processor
111: *
112: *@param processor the element processor to be iniitialized
113: *@exception SAXException on errors
114: */
115:
116: protected void doPreInitialization(ElementProcessor processor)
117: throws SAXException {
118: try {
119: ((POIFSElementProcessor) processor)
120: .setFilesystem(_filesystem);
121: } catch (ClassCastException e) {
122: throw SAXExceptionFactory(
123: "could not pre-initialize processor", e);
124: }
125: }
126:
127: /*
128: * ********** END implementation of ContentHandler **********
129: */
130: }
131: // end public abstract class POIFSSerializer
|