donaldp     2002/08/03 21:19:59

  Modified:    xmlutil/src/java/org/apache/excalibur/xmlizer/impl
                        TextXMLizer.java HTMLXMLizer.java
  Added:       xmlutil/src/java/org/apache/excalibur/xmlizer/impl
                        AbstractXMLizer.java
  Log:
  Add a Abstract base class for XMLizer objects so
  that the subclasses only have to implement the actual
  conversion.
  
  Revision  Changes    Path
  1.6       +9 -67     
jakarta-avalon-excalibur/xmlutil/src/java/org/apache/excalibur/xmlizer/impl/TextXMLizer.java
  
  Index: TextXMLizer.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-excalibur/xmlutil/src/java/org/apache/excalibur/xmlizer/impl/TextXMLizer.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TextXMLizer.java  10 Jul 2002 09:34:59 -0000      1.5
  +++ TextXMLizer.java  4 Aug 2002 04:19:58 -0000       1.6
  @@ -9,13 +9,6 @@
   
   import java.io.IOException;
   import java.io.InputStream;
  -import org.apache.avalon.excalibur.xml.Parser;
  -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.logger.AbstractLogEnabled;
  -import org.apache.avalon.framework.thread.ThreadSafe;
  -import org.apache.excalibur.xmlizer.XMLizer;
   import org.xml.sax.ContentHandler;
   import org.xml.sax.InputSource;
   import org.xml.sax.SAXException;
  @@ -28,77 +21,26 @@
    * @version CVS $Revision$ $Date$
    */
   public final class TextXMLizer
  -    extends AbstractLogEnabled
  -    implements XMLizer, ThreadSafe, Composable
  +    extends AbstractXMLizer
   {
       private static final String XML_MIME_TYPE = "text/xml";
   
  -    /**
  -     * The parser to use.
  -     */
  -    private Parser m_parser;
  -
  -    public void compose( final ComponentManager manager )
  -        throws ComponentException
  +    public TextXMLizer()
       {
  -        m_parser = (Parser)manager.lookup( Parser.ROLE );
  +        super( XML_MIME_TYPE );
       }
   
  -    /**
  -     * 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 org.apache.avalon.excalibur.xml.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( final InputStream stream,
  -                       final String mimeType,
  -                       final String systemID,
  -                       final ContentHandler handler )
  -        throws SAXException, IOException, ComponentException
  +    protected void toSAX( final InputStream stream,
  +                          final String systemID,
  +                          final ContentHandler handler )
  +        throws SAXException, IOException
       {
  -        if( null == stream )
  -        {
  -            final String message = "Stream must not be null.";
  -            throw new ComponentException( message );
  -        }
  -        if( null == handler )
  -        {
  -            final String message = "Handler must not be null.";
  -            throw new ComponentException( message );
  -        }
  -
  -        if( null == mimeType )
  -        {
  -            if( getLogger().isDebugEnabled() )
  -            {
  -                final String message =
  -                    "No mime-type for xmlizing " + systemID +
  -                    ", guessing text/xml";
  -                getLogger().debug( message );
  -            }
  -        }
  -        else if( !mimeType.equalsIgnoreCase( XML_MIME_TYPE ) )
  -        {
  -            if( getLogger().isDebugEnabled() )
  -            {
  -                final String message = "Mime-type " + mimeType +
  -                    "not supported for xmlizing " + systemID +
  -                    ", guessing text/xml";
  -                getLogger().debug( message );
  -            }
  -        }
  -
           final InputSource inputSource = new InputSource( stream );
           if( null != systemID )
           {
               inputSource.setSystemId( systemID );
           }
   
  -        m_parser.parse( inputSource, handler );
  +        getParser().parse( inputSource, handler );
       }
   }
  
  
  
  1.11      +12 -69    
jakarta-avalon-excalibur/xmlutil/src/java/org/apache/excalibur/xmlizer/impl/HTMLXMLizer.java
  
  Index: HTMLXMLizer.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-excalibur/xmlutil/src/java/org/apache/excalibur/xmlizer/impl/HTMLXMLizer.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- HTMLXMLizer.java  4 Aug 2002 03:20:33 -0000       1.10
  +++ HTMLXMLizer.java  4 Aug 2002 04:19:58 -0000       1.11
  @@ -9,8 +9,8 @@
   
   import java.io.IOException;
   import java.io.InputStream;
  -import java.io.StringWriter;
   import java.io.StringReader;
  +import java.io.StringWriter;
   import java.util.Properties;
   import javax.xml.transform.OutputKeys;
   import javax.xml.transform.Transformer;
  @@ -18,13 +18,6 @@
   import javax.xml.transform.TransformerFactory;
   import javax.xml.transform.dom.DOMSource;
   import javax.xml.transform.stream.StreamResult;
  -import org.apache.avalon.excalibur.xml.Parser;
  -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.logger.AbstractLogEnabled;
  -import org.apache.avalon.framework.thread.ThreadSafe;
  -import org.apache.excalibur.xmlizer.XMLizer;
   import org.w3c.tidy.Tidy;
   import org.xml.sax.ContentHandler;
   import org.xml.sax.InputSource;
  @@ -39,75 +32,23 @@
    * @version CVS $Revision$ $Date$
    */
   public final class HTMLXMLizer
  -    extends AbstractLogEnabled
  -    implements XMLizer, ThreadSafe, Composable
  +    extends AbstractXMLizer
   {
       private static final String HTML_MIME_TYPE = "text/html";
   
       /** Used for converting DOM -> SAX */
       private static final Properties c_format = createFormatProperties();
   
  -    /** The parser used by {@link XMLizer} */
  -    private Parser m_parser;
  -
  -    public void compose( final ComponentManager manager )
  -        throws ComponentException
  +    public HTMLXMLizer()
       {
  -        m_parser = (Parser)manager.lookup( Parser.ROLE );
  +        super( HTML_MIME_TYPE );
       }
   
  -    /**
  -     * 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 org.apache.avalon.excalibur.xml.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( final InputStream stream,
  -                       final String mimeType,
  -                       final String systemID,
  -                       final ContentHandler handler )
  -        throws SAXException, IOException, ComponentException
  +    protected void toSAX( final InputStream stream,
  +                          final String systemID,
  +                          final ContentHandler handler )
  +        throws SAXException, IOException
       {
  -        if( null == stream )
  -        {
  -            final String message = "Stream must not be null.";
  -            throw new ComponentException( message );
  -        }
  -
  -        if( null == handler )
  -        {
  -            final String message = "Handler must not be null.";
  -            throw new ComponentException( message );
  -        }
  -
  -        if( null == mimeType )
  -        {
  -            if( getLogger().isDebugEnabled() )
  -            {
  -                final String message =
  -                    "No mime-type for xmlizing " + systemID +
  -                    ", guessing text/html";
  -                getLogger().debug( message );
  -            }
  -        }
  -        else if( !mimeType.equalsIgnoreCase( HTML_MIME_TYPE ) )
  -        {
  -            if( getLogger().isDebugEnabled() )
  -            {
  -                final String message = "Mime-type " + mimeType +
  -                    "not supported for xmlizing " + systemID +
  -                    ", guessing text/html";
  -                getLogger().debug( message );
  -            }
  -        }
  -
           final Tidy xhtmlconvert = new Tidy();
           xhtmlconvert.setXmlOut( true );
           xhtmlconvert.setXHTML( true );
  @@ -131,9 +72,11 @@
           final InputSource inputSource =
               new InputSource( new StringReader( writer.toString() ) );
           if( null != systemID )
  +        {
               inputSource.setSystemId( systemID );
  +        }
   
  -        m_parser.parse( inputSource, handler );
  +        getParser().parse( inputSource, handler );
       }
   
       /**
  
  
  
  1.1                  
jakarta-avalon-excalibur/xmlutil/src/java/org/apache/excalibur/xmlizer/impl/AbstractXMLizer.java
  
  Index: AbstractXMLizer.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.IOException;
  import java.io.InputStream;
  import org.apache.avalon.excalibur.xml.Parser;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.framework.service.ServiceException;
  import org.apache.avalon.framework.service.ServiceManager;
  import org.apache.avalon.framework.service.Serviceable;
  import org.apache.avalon.framework.thread.ThreadSafe;
  import org.apache.excalibur.xmlizer.XMLizer;
  import org.xml.sax.ContentHandler;
  import org.xml.sax.SAXException;
  
  /**
   * An abstract XMLizer that can be extended to actually
   * perform some conversion. This class validates that the
   * mimetype is valid and then delegates to implementation
   * defined in subclass.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Carsten Ziegeler</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/08/04 04:19:58 $
   */
  public abstract class AbstractXMLizer
      extends AbstractLogEnabled
      implements XMLizer, ThreadSafe, Serviceable
  {
      /**
       * The mime type handled by this XMLizer.
       */
      private final String m_mimeType;
  
      /** The parser used by {@link XMLizer} */
      private Parser m_parser;
  
      protected AbstractXMLizer( final String mimeType )
      {
          m_mimeType = mimeType;
      }
  
      public void service( final ServiceManager manager )
          throws ServiceException
      {
          m_parser = (Parser)manager.lookup( Parser.ROLE );
      }
  
      /**
       * 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 org.apache.avalon.excalibur.xml.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)
       */
      public void toSAX( final InputStream stream,
                         final String mimeType,
                         final String systemID,
                         final ContentHandler handler )
          throws SAXException, IOException
      {
          if( null == stream )
          {
              throw new NullPointerException( "stream" );
          }
          if( null == handler )
          {
              throw new NullPointerException( "handler" );
          }
  
          if( null == mimeType )
          {
              if( getLogger().isDebugEnabled() )
              {
                  final String message =
                      "No mime-type for xmlizing " + systemID +
                      ", guessing text/html";
                  getLogger().debug( message );
              }
          }
          else if( !mimeType.equalsIgnoreCase( m_mimeType ) )
          {
              if( getLogger().isDebugEnabled() )
              {
                  final String message = "Mime-type " + mimeType +
                      "not supported for xmlizing " + systemID +
                      ", guessing text/html";
                  getLogger().debug( message );
              }
          }
  
          toSAX( stream, systemID, handler );
      }
  
      protected abstract void toSAX( final InputStream stream,
                                     final String systemID,
                                     final ContentHandler handler )
          throws SAXException, IOException;
  
      protected Parser getParser()
      {
          return m_parser;
      }
  }
  
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to