You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by cz...@apache.org on 2002/04/19 12:58:58 UTC

cvs commit: jakarta-avalon-excalibur/all/src/scratchpad/org/apache/excalibur/xmlizer/impl TextXMLizer.java XMLizerImpl.java

cziegeler    02/04/19 03:58:58

  Modified:    all/src/scratchpad/org/apache/excalibur/xmlizer/impl
                        XMLizerImpl.java
  Added:       all/src/scratchpad/org/apache/excalibur/xmlizer/impl
                        TextXMLizer.java
  Log:
  Next version of xmlizer
  
  Revision  Changes    Path
  1.2       +31 -32    jakarta-avalon-excalibur/all/src/scratchpad/org/apache/excalibur/xmlizer/impl/XMLizerImpl.java
  
  Index: XMLizerImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/all/src/scratchpad/org/apache/excalibur/xmlizer/impl/XMLizerImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XMLizerImpl.java	19 Apr 2002 06:59:38 -0000	1.1
  +++ XMLizerImpl.java	19 Apr 2002 10:58:58 -0000	1.2
  @@ -9,44 +9,31 @@
   
   import java.io.InputStream;
   import java.io.IOException;
  -import org.apache.avalon.excalibur.xml.Parser;
   import org.apache.excalibur.xmlizer.XMLizer;
  -import org.apache.avalon.framework.logger.AbstractLogEnabled;
  -import org.apache.avalon.framework.component.Component;
   import org.apache.avalon.framework.component.ComponentException;
  -import org.apache.avalon.framework.component.ComponentManager;
  -import org.apache.avalon.framework.component.Composable;
  +import org.apache.avalon.excalibur.component.DefaultComponentSelector;
   import org.apache.avalon.framework.thread.ThreadSafe;
   import org.xml.sax.ContentHandler;
  -import org.xml.sax.InputSource;
   import org.xml.sax.SAXException;
   
   /**
    * Converter for transforming any input stream with a given mime-type
    * into SAX events.
  - * ATTENTION: This is a hacky, first-shot implementation which always
  - * assumes that the input stream contains text/xml !
  - * This will soon be replaced by a better, configurable approach.
  + * This component acts like a selector. All XMLizer can "register"
  + * themselfes for a given mime-type and this component forwards
  + * the transformation to the registered on.
    *
    * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
  - * @version CVS $Revision: 1.1 $ $Date: 2002/04/19 06:59:38 $
  + * @version CVS $Revision: 1.2 $ $Date: 2002/04/19 10:58:58 $
    */
   
   public class XMLizerImpl
  -    extends AbstractLogEnabled
  -    implements XMLizer, ThreadSafe, Composable
  +    extends DefaultComponentSelector
  +    implements XMLizer, ThreadSafe
   {
   
  -    /** The component manager */
  -    protected ComponentManager manager;
  -
  -    /**
  -     * Composable interface
  -     */
  -    public void compose(ComponentManager manager)
  -    {
  -        this.manager = manager;
  -    }
  +    /** The default mimeType used when no mimeType is given */
  +    protected String defaultMimeType = "text/xml";
   
       /**
        * Generates SAX events from the given input stream
  @@ -64,20 +51,32 @@
                          ContentHandler handler )
           throws SAXException, IOException, ComponentException
       {
  -        final InputSource inputSource = new InputSource( stream );
  -        inputSource.setSystemId( systemID );
  -
  -        Parser parser = null;
  -        try
  -        {
  -            parser = (Parser)this.manager.lookup( Parser.ROLE );
  +        if ( null == stream ) {
  +            throw new ComponentException("Stream must not be null.");
  +        }
  +        if ( null == handler ) {
  +            throw new ComponentException("Handler must not be null.");
  +        }
  +        if ( null == mimeType ) {
  +            if ( this.getLogger().isDebugEnabled() ) {
  +                this.getLogger().debug("No mime-type for xmlizing " + systemID + ", guessing " + this.defaultMimeType);
  +            }
  +            mimeType = this.defaultMimeType;
  +        }
   
  -            parser.parse( inputSource, handler );
  +        if ( !this.hasComponent ( mimeType ) ) {
  +            throw new ComponentException("No XMLizer registered for mimeType " + mimeType);
           }
  -        finally
  +
  +        XMLizer realXMLizer = null;
  +        try
           {
  -            if( parser != null ) this.manager.release( parser );
  +            realXMLizer = (XMLizer) this.select( mimeType );
  +            realXMLizer.toSAX( stream, mimeType, systemID, handler );
  +        } finally {
  +            this.release( realXMLizer );
           }
  +
       }
   
   }
  
  
  
  1.1                  jakarta-avalon-excalibur/all/src/scratchpad/org/apache/excalibur/xmlizer/impl/TextXMLizer.java
  
  Index: TextXMLizer.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.excalibur.xmlizer.impl;
  
  import java.io.InputStream;
  import java.io.IOException;
  import org.apache.avalon.excalibur.xml.Parser;
  import org.apache.excalibur.xmlizer.XMLizer;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.framework.component.Component;
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.component.Composable;
  import org.apache.avalon.framework.thread.ThreadSafe;
  import org.xml.sax.ContentHandler;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  
  /**
   * Converter for transforming an input stream contain text/xml data
   * to SAX events.
   *
   * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/04/19 10:58:58 $
   */
  
  public class TextXMLizer
      extends AbstractLogEnabled
      implements XMLizer, ThreadSafe, Composable
  {
  
      /** The component manager */
      protected ComponentManager manager;
  
      /**
       * Composable interface
       */
      public void compose(ComponentManager manager)
      {
          this.manager = manager;
      }
  
      /**
       * Generates SAX events from the given input stream
       * <b>NOTE</b> : if the implementation can produce lexical events, care should be taken
       * that <code>handler</code> can actually be a {@link XMLConsumer} that accepts such
       * events or directly implements the LexicalHandler interface!
       * @param stream    the data
       * @param mimeType  the mime-type for the data
       * @param systemID  the URI defining the data (this is optional and can be null)
       * @throws ComponentException if no suitable converter is found
       */
      public void toSAX( InputStream    stream,
                         String         mimeType,
                         String         systemID,
                         ContentHandler handler )
          throws SAXException, IOException, ComponentException
      {
          if ( null == stream ) {
              throw new ComponentException("Stream must not be null.");
          }
          if ( null == handler ) {
              throw new ComponentException("Handler must not be null.");
          }
          if ( null == mimeType ) {
              if ( this.getLogger().isDebugEnabled() ) {
                  this.getLogger().debug("No mime-type for xmlizing " + systemID + ", guessing text/xml");
              }
          }
  
  
          final InputSource inputSource = new InputSource( stream );
          inputSource.setSystemId( systemID );
  
          Parser parser = null;
          try
          {
              parser = (Parser)this.manager.lookup( Parser.ROLE );
  
              parser.parse( inputSource, handler );
          }
          finally
          {
              if( parser != null ) this.manager.release( parser );
          }
      }
  
  }
  
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>