commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nicola...@apache.org
Subject cvs commit: jakarta-commons-sandbox/morphos/src/java/org/apache/commons/morphos/morphers/poi ElementProcessorMorpher.java HSSFMorpher.java POIFSMorpher.java
Date Thu, 25 Jul 2002 13:19:50 GMT
nicolaken    2002/07/25 06:19:50

  Added:       morphos/src/java/org/apache/commons/morphos/morphers/poi
                        ElementProcessorMorpher.java HSSFMorpher.java
                        POIFSMorpher.java
  Log:
  Initial Morphos codebase, based on the discussions on commons-dev.
  Corrently the Factories and pipelining are not yet done, although there are
  already the interfaces.
  The main class, Morphos, currently uses the HSSFMorpher hardcoded.
  The only Morpher supplied is the HSSFMorpher.
  To try, run
    ant examples
  and look in the build directory.
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/morphos/src/java/org/apache/commons/morphos/morphers/poi/ElementProcessorMorpher.java
  
  Index: ElementProcessorMorpher.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, 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
      apache@apache.org.
  
   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 (INCLU-
   DING, 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 and was  originally created by
   Stefano Mazzocchi  <stefano@apache.org>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.commons.morphos.morphers.poi;
  
  import org.apache.commons.morphos.helpers.elementprocessor.types.Attribute;
  import org.apache.commons.morphos.helpers.elementprocessor.ElementProcessorFactory;
  import org.apache.commons.morphos.helpers.elementprocessor.ElementProcessor;
  import org.apache.commons.morphos.helpers.elementprocessor.CannotCreateElementProcessorException;
  import org.apache.commons.morphos.*;
  
  import org.xml.sax.Attributes;
  import org.xml.sax.Locator;
  import org.xml.sax.SAXException;
  import org.xml.sax.ContentHandler;
  
  import java.io.IOException;
  import java.io.OutputStream;
  
  import java.util.Stack;
  
  /**
   *
   * This is an abstract class. Concrete extensions need to implement
   * the following methods:
   * <ul>
   *    <li>String getMimeType()</li>
   *    <li>void endDocument()</li>
   *    <li>ElementProcessorFactory getElementProcessorFactory()</li>
   *    <li>void doPreInitialization(ElementProcessor processor)</li>
   * </ul>
   *
   * @author Marc Johnson (marc_johnson27591@hotmail.com)
   * @author Nicola Ken Barozzi (nicolaken@apache.org)
   */
  
  public abstract class ElementProcessorMorpher 
          extends AbstractMorpher implements ContentHandler 
  {
      private static final boolean _should_set_content_length = false;
      private OutputStream         _output_stream;    
      private Stack                _open_elements;
      private Locator              _locator;
  
      /**
       * Constructor
       */
  
  
      public ElementProcessorMorpher()
      {
          _output_stream = null;    
          _open_elements = new Stack();
          _locator       = null;
      }
  
     
      /**
       * get the appropriate ElementProcessorFactory
       *
       * @return an ElementProcessorFactory suitable for the file type
       */
  
      protected abstract ElementProcessorFactory getElementProcessorFactory();
  
      /**
       * perform whatever pre-initialization seems good on the
       * ElementProcessor
       *
       * @param processor the processor to be initialized
       *
       * @exception SAXException on errors
       */
  
      protected abstract void doPreInitialization(ElementProcessor processor)
  	throws SAXException;
  
      /**
       * @return the output stream
       */
  
      protected OutputStream getOutputStream()
      {
  	return _output_stream;
      }
      
      /**
       * Set the OutputStream where the requested resource should be
       * serialized.
       *
       * @param output the OutputStream to which the serialized data will
       *            be written
       */
       public void setOutput(OutputStream output)
       {
          _output_stream = output;
       }
       
      /**
       * Create a new SAXException
       *
       * @param message the exception message
       * @param e the underlying exception (may be null)
       *
       * @return new SAXException
       */
  
      protected SAXException SAXExceptionFactory(final String message,
                                                 final Exception e)
      {
          StringBuffer message_buffer = new StringBuffer();
  
          message_buffer.append((message == null) ? ""
                                                  : message);
          if (_locator != null)
          {
              message_buffer.append("; System id: \"");
              message_buffer.append(_locator.getSystemId());
              message_buffer.append("\"; public id: \"");
              message_buffer.append(_locator.getPublicId());
              message_buffer.append("\"; line number: ");
              message_buffer.append(_locator.getLineNumber());
              message_buffer.append("; column number: ");
              message_buffer.append(_locator.getColumnNumber());
          }
          SAXException rval = null;
  
          if (e != null)
          {
              rval = new SAXException(message_buffer.toString(), e);
          }
          else
          {
              rval = new SAXException(message_buffer.toString());
          }
          return rval;
      }
  
      /**
       * Create a SAXException
       *
       * @param message the exception message
       *
       * @return new SAXException
       */
  
      protected SAXException SAXExceptionFactory(final String message)
      {
          return SAXExceptionFactory(message, null);
      }
  
      private ElementProcessor getCurrentElementProcessor()
      {
          return _open_elements.empty() ? null
                                        : ( ElementProcessor ) _open_elements
                                            .peek();
      }
  
      private char [] cleanupArray(final char [] array, final int start,
                                   final int length)
      {
          char[] output = new char[ length ];
  
          System.arraycopy(array, start, output, 0, length);
          return output;
      }
  
      /* ********** START implementation of ContentHandler ********** */
  
      /**
       * Receive notification of character data.
       *
       * @param ch the character array
       * @param start the start index in ch
       * @param length the length of the valid part of ch
       *
       * @exception SAXException if anything goes wrong in processing
       *            the character data
       */
  
      public void characters(final char [] ch, final int start,
                             final int length)
          throws SAXException
      {
          try
          {
              getCurrentElementProcessor().acceptCharacters(cleanupArray(ch,
                      start, length));
          }
          catch (Exception e)
          {
              throw SAXExceptionFactory("could not process characters event",
                                        e);
          }
      }
  
      /**
       * Receive notification of the end of an element.
       *
       * @param ignored_namespaceURI
       * @param ignored_localName
       * @param ignored_qName
       *
       * @exception SAXException on any errors processing the event.
       */
  
      public void endElement(final String ignored_namespaceURI,
                             final String ignored_localName,
                             final String ignored_qName)
          throws SAXException
      {
          try
          {
              getCurrentElementProcessor().endProcessing();
              _open_elements.pop();
          }
          catch (Exception e)
          {
              throw SAXExceptionFactory("could not process endElement event",
                                        e);
          }
      }
  
      /**
       * End the scope of a prefix-URI mapping. We don't really care.
       *
       * @param ignored_prefix
       */
  
      public void endPrefixMapping(final String ignored_prefix)
      {
      }
  
      /**
       * Receive notification of ignorable whitespace in element
       * content.
       *
       * @param ch the character array
       * @param start the start index in ch
       * @param length the length of the valid part of ch
       *
       * @exception SAXException if anything goes wrong in processing
       *            the character data
       */
  
      public void ignorableWhitespace(final char [] ch, final int start,
                                      final int length)
          throws SAXException
      {
          try
          {
              getCurrentElementProcessor()
                  .acceptWhitespaceCharacters(cleanupArray(ch, start, length));
          }
          catch (Exception e)
          {
              throw SAXExceptionFactory(
                  "could not process ignorableWhitespace event", e);
          }
      }
  
      /**
       * Receive notification of a processing instruction. We don't
       * really care.
       *
       * @param ignored_target
       * @param ignored_data
       */
  
      public void processingInstruction(final String ignored_target,
                                        final String ignored_data)
      {
      }
  
      /**
       * Receive an object for locating the origin of SAX document
       * events.
       *
       * @param locator the Locator object
       */
  
      public void setDocumentLocator(final Locator locator)
      {
          _locator = locator;
      }
  
      /**
       * Receive notification of a skipped entity. We don't really care.
       *
       * @param ignored_name
       */
  
      public void skippedEntity(final String ignored_name)
      {
      }
  
      /**
       * Receive notification of the beginning of a document.
       */
  
      public void startDocument()
      {
  
          // nothing to do; should be ready as soon as we were
          // constructed
      }
  
      /**
       * Receive notification of the beginning of an element.
       *
       * @param namespaceURI the namespace this element is in
       * @param localName the local name of the element
       * @param qName the qualified name of the element
       * @param atts the Attributes, if any, of the element
       *
       * @exception SAXException if we cannot create an ElementProcessor
       *            to handle the element
       */
  
      public void startElement(final String namespaceURI,
                               final String localName, final String qName,
                               final Attributes atts)
          throws SAXException
      {
          String name = "";
  
          if ((localName != null) && (localName.length() != 0))
          {
              name = localName;
          }
          else if ((qName != null) && (qName.length() != 0))
          {
              name = qName;
          }
          ElementProcessor processor;
  
          try
          {
              processor =
                  getElementProcessorFactory().createElementProcessor(name);
          }
          catch (CannotCreateElementProcessorException e)
          {
              throw SAXExceptionFactory("could not process startElement event",
                                        e);
          }
  	doPreInitialization(processor);
          Attribute[] attributes = (atts == null) ? new Attribute[ 0 ]
                                                  : new Attribute[ atts.getLength() ];
  
          for (int j = 0; j < attributes.length; j++)
          {
              attributes[ j ] = new Attribute(atts.getQName(j),
                                              atts.getValue(j));
          }
          try
          {
              processor.initialize(attributes, getCurrentElementProcessor());
          }
          catch (IOException e)
          {
              throw SAXExceptionFactory("Exception processing startElement", e);
          }
          _open_elements.push(processor);
      }
  
      /**
       * Begin the scope of a prefix-URI Namespace mapping. We don't
       * really care.
       *
       * @param ignored_prefix
       * @param ignored_uri
       */
  
      public void startPrefixMapping(final String ignored_prefix,
                                     final String ignored_uri)
      {
      }
  
      /* **********  END  implementation of ContentHandler ********** */
  
  }   // end public abstract class ElementProcessorContentHandler
  
  
  
  1.1                  jakarta-commons-sandbox/morphos/src/java/org/apache/commons/morphos/morphers/poi/HSSFMorpher.java
  
  Index: HSSFMorpher.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, 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
      apache@apache.org.
  
   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 (INCLU-
   DING, 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 and was  originally created by
   Stefano Mazzocchi  <stefano@apache.org>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.commons.morphos.morphers.poi;
  
  import org.apache.commons.morphos.morphers.poi.elementprocessor.hssf.HSSFElementProcessorFactory;
  import org.apache.commons.morphos.helpers.elementprocessor.ElementProcessorFactory;
  import java.io.FileOutputStream;
  import java.io.OutputStream;
  import java.io.InputStream;
  
  import javax.xml.parsers.SAXParserFactory;
  
  import org.xml.sax.InputSource;
  import org.xml.sax.XMLReader;
  
  
  /**
   *  Morpher to produce an HSSF stream.
   *
   *@author   Marc Johnson (marc_johnson27591@hotmail.com)
   *@author   Nicola Ken Barozzi (nicolaken@apache.org) 
   *@author svenk@imost.de
   *@created  26 febbraio 2002
   *@version $Revision: 1.1 $ $Date: 2002/07/25 13:19:50 $
   */
  
  public class HSSFMorpher
        extends POIFSMorpher
  {
    private ElementProcessorFactory _element_processor_factory;
    private final static String _mime_type = "vnd.ms-excel";
      
    /**
     *  Constructor
     */
  
    public HSSFMorpher() {
      super();
      _element_processor_factory  = new HSSFElementProcessorFactory();
    }
          
    /**
     *  get the mime type
     *
     *@return    vnd.ms-excel
     */
  
    public String getMimeType() {
      return _mime_type;
    }
  
    /**
     *  get the ElementProcessorFactory
     *
     *@return    the ElementProcessorFactory
     */
  
    protected ElementProcessorFactory getElementProcessorFactory() {
      return _element_processor_factory;
    }
  
    /**
     *  post-processing for endDocument
     */
  
    protected void doLocalPostEndDocument() { }
  
    /**
     *  pre-processing for endDocument
     */
  
    protected void doLocalPreEndDocument() { }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/morphos/src/java/org/apache/commons/morphos/morphers/poi/POIFSMorpher.java
  
  Index: POIFSMorpher.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, 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
      apache@apache.org.
  
   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 (INCLU-
   DING, 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 and was  originally created by
   Stefano Mazzocchi  <stefano@apache.org>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.commons.morphos.morphers.poi;
  
  import java.io.IOException;
  import java.io.OutputStream;
  
  import org.apache.commons.morphos.helpers.elementprocessor.ElementProcessor;
  
  import org.apache.commons.morphos.helpers.elementprocessor.CannotCreateElementProcessorException;
  import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  import org.apache.commons.morphos.morphers.poi.elementprocessor.POIFSElementProcessor;
  
  
  import org.xml.sax.SAXException;
  
  /**
   *  An extension of ElementProcessorContentHandler with extensions for dealing with
   *  the POIFS filesystem This is an abstract class. Concrete extensions need to
   *  implement the following methods:
   *  <ul>
   *    <li> String getMimeType()</li>
   *    <li> void doLocalPreEndDocument()</li>
   *    <li> void doLocalPostEndDocument()</li>
   *    <li> ElementProcessorFactory getElementProcessorFactory()</li>
   *    </li>
   *  </ul>
   *
   *
   *@author   Marc Johnson (marc_johnson27591@hotmail.com)
   *@author   Nicola Ken Barozzi (nicolaken@apache.org) 
   *@created  26 febbraio 2002
   */
  
  public abstract class POIFSMorpher
        extends ElementProcessorMorpher
  {
    private POIFSFileSystem _filesystem;
  
    /**
     *  Constructor
     */
  
    public POIFSMorpher() {
      super();
      _filesystem = new POIFSFileSystem();
    }
  
    /*
     *  ********** START implementation of ContentHandler **********
     */
    /**
     *  Receive notification of the end of a document.
     *
     *@exception  SAXException  if there is an error writing the document to the
     *      output stream
     */
  
    public void endDocument()
      throws SAXException {
      doLocalPreEndDocument();
  
      OutputStream stream = getOutputStream();
  
      if ( stream != null ) {
        try {
          _filesystem.writeFilesystem( stream );
        }
        catch ( IOException e ) {
          throw SAXExceptionFactory(
            "could not process endDocument event", e );
        }
      }
      else {
        throw SAXExceptionFactory(
          "no outputstream for writing the document!!" );
      }
      doLocalPostEndDocument();
    }
  
    /**
     *  Provide access to the filesystem for extending classes
     *
     *@return    the filesystem
     */
  
    protected POIFSFileSystem getFilesystem() {
      return _filesystem;
    }
  
    /**
     *  Extending classes should do whatever they need to do prior to writing the
     *  filesystem out
     */
  
    protected abstract void doLocalPreEndDocument();
  
    /**
     *  Extending classes should do whatever they need to do after writing the
     *  filesystem out
     */
  
    protected abstract void doLocalPostEndDocument();
  
    /**
     *  perform pre-initialization on an element processor
     *
     *@param  processor         the element processor to be iniitialized
     *@exception  SAXException  on errors
     */
  
    protected void doPreInitialization( ElementProcessor processor )
      throws SAXException {
      try {
        ( ( POIFSElementProcessor ) processor ).setFilesystem( _filesystem );
      }
      catch ( ClassCastException e ) {
        throw SAXExceptionFactory( "could not pre-initialize processor",
          e );
      }
    }
  
    /*
     *  **********  END  implementation of ContentHandler **********
     */
  }
  // end public abstract class POIFSSerializer
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message