unico       2003/10/27 01:31:28

  Modified:    
src/blocks/repository/java/org/apache/cocoon/components/source/impl
                        JPEGSourceInspector.java GIFSourceInspector.java
  Added:       
src/blocks/repository/java/org/apache/cocoon/components/source/impl
                        AbstractImageSourceInspector.java
  Log:
  factor out common code and use same namspace uri for jpeg and gif properties 
(hope everybody approves)
  
  Revision  Changes    Path
  1.3       +19 -58    
cocoon-2.1/src/blocks/repository/java/org/apache/cocoon/components/source/impl/JPEGSourceInspector.java
  
  Index: JPEGSourceInspector.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/blocks/repository/java/org/apache/cocoon/components/source/impl/JPEGSourceInspector.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- JPEGSourceInspector.java  23 Oct 2003 16:57:17 -0000      1.2
  +++ JPEGSourceInspector.java  27 Oct 2003 09:31:28 -0000      1.3
  @@ -52,10 +52,7 @@
   import java.io.BufferedInputStream;
   import java.io.IOException;
   
  -import org.apache.avalon.framework.logger.AbstractLogEnabled;
   import org.apache.avalon.framework.thread.ThreadSafe;
  -import org.apache.cocoon.components.source.SourceInspector;
  -import org.apache.cocoon.components.source.helpers.SourceProperty;
   import org.apache.excalibur.source.Source;
   import org.apache.excalibur.source.SourceException;
   
  @@ -66,61 +63,29 @@
    * @author <a href="mailto:[EMAIL PROTECTED]">Donald A. Ball Jr.</a>
    * @version CVS $Id$
    */
  -public class JPEGSourceInspector extends AbstractLogEnabled implements 
  -    SourceInspector, ThreadSafe {
  +public class JPEGSourceInspector extends AbstractImageSourceInspector 
implements ThreadSafe {
   
  -    /**
  -     * The namespace uri of the properties exposed by this SourceInspector.
  -     * The value is <code>http://apache.org/cocoon/inspector/jpeg/1.0</code> 
.
  -     */
  -    private static final String PROPERTY_NS = 
"http://apache.org/cocoon/inspector/jpeg/1.0";;
  +
  +    public JPEGSourceInspector() {
  +    }
       
       /**
  -     * <code>width</code> property name.
  +     * Checks the source uri for the .jp(e)g extension.
        */
  -    private static final String IMAGE_WIDTH_PROPERTY_NAME = "width";
  +    protected final boolean isImageMimeType(Source source) {
  +        final String uri = source.getURI();
  +        final int index = uri.lastIndexOf('.');
  +        if (index != -1) {
  +            String extension = uri.substring(index);
  +            return extension.equalsIgnoreCase(".jpg") || 
extension.equalsIgnoreCase(".JPEG");
  +        }
  +        return false;
  +    }
       
       /**
  -     * <code>height</code> property name.
  +     * Checks that this is in fact a jpeg file.
        */
  -    private static final String IMAGE_HEIGHT_PROPERTY_NAME = "height";
  -    
  -    private static final String[] EXPOSED_PROPERTIES = new String[] {
  -        PROPERTY_NS + "#" + IMAGE_WIDTH_PROPERTY_NAME,
  -        PROPERTY_NS + "#" + IMAGE_HEIGHT_PROPERTY_NAME
  -    };
  -    
  -    
  -    public SourceProperty getSourceProperty(Source source, String namespace, 
String name) 
  -        throws SourceException {
  -
  -        if ((namespace.equals(PROPERTY_NS)) && 
  -            (source.getURI().endsWith(".jpg")) && (isJPEGFile(source))) {
  -
  -            if (name.equals(IMAGE_WIDTH_PROPERTY_NAME))
  -                return new SourceProperty(PROPERTY_NS, 
IMAGE_WIDTH_PROPERTY_NAME, 
  -                                          
String.valueOf(getJpegSize(source)[0]));
  -            if (name.equals(IMAGE_HEIGHT_PROPERTY_NAME))
  -                return new SourceProperty(PROPERTY_NS, 
IMAGE_HEIGHT_PROPERTY_NAME,
  -                                          
String.valueOf(getJpegSize(source)[1]));
  -        }
  -        return null;  
  -    }
  -
  -    public SourceProperty[] getSourceProperties(Source source) throws 
SourceException {
  -
  -        if ((source.getURI().endsWith(".jpg")) &&
  -            (isJPEGFile(source))) {
  -            int[] size = getJpegSize(source);
  -            return new SourceProperty[] {
  -                new SourceProperty(PROPERTY_NS, IMAGE_WIDTH_PROPERTY_NAME, 
String.valueOf(size[0])),
  -                new SourceProperty(PROPERTY_NS, IMAGE_HEIGHT_PROPERTY_NAME, 
String.valueOf(size[1]))
  -            };
  -        }
  -        return null;
  -    }
  -
  -    private boolean isJPEGFile(Source source) throws SourceException {
  +    protected final boolean isImageFileType(Source source) throws 
SourceException {
           BufferedInputStream in = null;
           try {
               in = new BufferedInputStream(source.getInputStream());
  @@ -142,11 +107,11 @@
           }
           return false;
       }
  -
  +    
       /**
        * returns width as first element, height as second
        */
  -    private int[] getJpegSize(Source source) throws SourceException {
  +    protected final int[] getImageSize(Source source) throws SourceException 
{
           BufferedInputStream in = null;
           try {
               in = new BufferedInputStream(source.getInputStream());
  @@ -206,10 +171,6 @@
                       in.close(); 
                   } catch (Exception e) {}
           }
  -    }
  -    
  -    public String[] getExposedSourcePropertyTypes() {
  -        return EXPOSED_PROPERTIES;
       }
   
   }
  
  
  
  1.3       +18 -60    
cocoon-2.1/src/blocks/repository/java/org/apache/cocoon/components/source/impl/GIFSourceInspector.java
  
  Index: GIFSourceInspector.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/blocks/repository/java/org/apache/cocoon/components/source/impl/GIFSourceInspector.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- GIFSourceInspector.java   23 Oct 2003 16:56:51 -0000      1.2
  +++ GIFSourceInspector.java   27 Oct 2003 09:31:28 -0000      1.3
  @@ -52,12 +52,7 @@
   import java.io.BufferedInputStream;
   import java.io.IOException;
   
  -import org.apache.avalon.framework.logger.AbstractLogEnabled;
   import org.apache.avalon.framework.thread.ThreadSafe;
  -
  -import org.apache.cocoon.components.source.SourceInspector;
  -import org.apache.cocoon.components.source.helpers.SourceProperty;
  -
   import org.apache.excalibur.source.Source;
   import org.apache.excalibur.source.SourceException;
   
  @@ -68,62 +63,29 @@
    * @author <a href="mailto:[EMAIL PROTECTED]">Donald A. Ball Jr.</a>
    * @version CVS $Id$
    */
  -public class GIFSourceInspector extends AbstractLogEnabled implements 
  -    SourceInspector, ThreadSafe {
  +public class GIFSourceInspector extends AbstractImageSourceInspector 
implements ThreadSafe {
       
  -    /** 
  -     * The namespace uri of the properties exposed by this SourceInspector.
  -     * The value is <code>http://apache.org/cocoon/inspector/gif/1.0</code> .
  -     */
  -    public static final String PROPERTY_NS = 
"http://apache.org/cocoon/inspector/gif/1.0";;
       
  -    /**
  -     * <code>width</code> property name.
  -     */
  -    public static final String IMAGE_WIDTH_PROPERTY_NAME = "width";
  +    public GIFSourceInspector() {
  +    }
       
       /**
  -     * <code>height</code> property name.
  +     * Checks the source uri for the .gif extension.
        */
  -    public static final String IMAGE_HEIGHT_PROPERTY_NAME = "height";
  -    
  -    private static String[] EXPOSED_PROPERTIES = new String[] {
  -        PROPERTY_NS + "#" + IMAGE_HEIGHT_PROPERTY_NAME,
  -        PROPERTY_NS + "#" + IMAGE_WIDTH_PROPERTY_NAME
  -    };
  -
  -    public SourceProperty getSourceProperty(Source source, String namespace, 
String name) 
  -        throws SourceException {
  -
  -        if ((namespace.equals(PROPERTY_NS)) && 
  -            ((name.equals(IMAGE_WIDTH_PROPERTY_NAME)) || 
(name.equals(IMAGE_HEIGHT_PROPERTY_NAME))) && 
  -            (source.getURI().endsWith(".gif")) && (isGIFFile(source))) {
  -
  -            if (name.equals(IMAGE_WIDTH_PROPERTY_NAME))
  -                return new SourceProperty(PROPERTY_NS, 
IMAGE_WIDTH_PROPERTY_NAME, 
  -                                          
String.valueOf(getGifSize(source)[0]));
  -            if (name.equals(IMAGE_HEIGHT_PROPERTY_NAME))
  -                return new SourceProperty(PROPERTY_NS, 
IMAGE_HEIGHT_PROPERTY_NAME,
  -                                          
String.valueOf(getGifSize(source)[1]));
  -        }
  -        return null;  
  -    }
  -
  -    public SourceProperty[] getSourceProperties(Source source) throws 
SourceException {
  -
  -        if ((source.getURI().endsWith(".gif")) &&
  -            (isGIFFile(source))) {
  -
  -            int[] size = getGifSize(source);
  -            return new SourceProperty[] {
  -                new SourceProperty(PROPERTY_NS, IMAGE_WIDTH_PROPERTY_NAME, 
String.valueOf(size[0])),
  -                new SourceProperty(PROPERTY_NS, IMAGE_HEIGHT_PROPERTY_NAME, 
String.valueOf(size[1]))
  -            };
  +    protected final boolean isImageMimeType(Source source) {
  +        final String uri = source.getURI();
  +        final int index = uri.lastIndexOf('.');
  +        if (index != -1) {
  +            String extension = uri.substring(index);
  +            return extension.equalsIgnoreCase(".gif");
           }
  -        return null;
  +        return false;
       }
  -
  -    private boolean isGIFFile(Source source) throws SourceException {
  +    
  +    /**
  +     * Checks that this is in fact a gif file.
  +     */
  +    protected final boolean isImageFileType(Source source) throws 
SourceException {
           BufferedInputStream in = null;
           try {
               in = new BufferedInputStream(source.getInputStream());
  @@ -148,7 +110,7 @@
       /**
        * Returns width as first element, height as second
        */
  -    private int[] getGifSize(Source source) throws SourceException {
  +    protected final int[] getImageSize(Source source) throws SourceException 
{
           BufferedInputStream in = null;
           try {
               in = new BufferedInputStream(source.getInputStream());
  @@ -178,10 +140,6 @@
                       in.close(); 
                   } catch (Exception e) {}
           }
  -    }
  -
  -    public String[] getExposedSourcePropertyTypes() {
  -        return EXPOSED_PROPERTIES;
       }
   
   }
  
  
  
  1.1                  
cocoon-2.1/src/blocks/repository/java/org/apache/cocoon/components/source/impl/AbstractImageSourceInspector.java
  
  Index: AbstractImageSourceInspector.java
  ===================================================================
  /*
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 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
      [EMAIL PROTECTED]
  
   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  <[EMAIL PROTECTED]>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.components.source.impl;
  
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.cocoon.components.source.SourceInspector;
  import org.apache.cocoon.components.source.helpers.SourceProperty;
  import org.apache.excalibur.source.Source;
  import org.apache.excalibur.source.SourceException;
  
  /**
   * Abstract base class for inspectors that can calculate 
   * the size of an image of a particular type.
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]">Unico Hommes</a>
   */
  public abstract class AbstractImageSourceInspector
      extends AbstractLogEnabled
      implements SourceInspector {
  
      /** 
       * The namespace uri of the properties exposed by this SourceInspector.
       * <p>
       * The value is <code>http://apache.org/cocoon/inspector/image/1.0</code>.
       * </p>
       */
      public static final String PROPERTY_NS = 
"http://apache.org/cocoon/inspector/image/1.0";;
      
      /**
       * <code>width</code> property name.
       */
      public static final String IMAGE_WIDTH_PROPERTY_NAME = "width";
      
      /**
       * <code>height</code> property name.
       */
      public static final String IMAGE_HEIGHT_PROPERTY_NAME = "height";
      
      private static final String[] HANDLED_PROPERTIES = new String[] {
          PROPERTY_NS + "#" + IMAGE_HEIGHT_PROPERTY_NAME,
          PROPERTY_NS + "#" + IMAGE_WIDTH_PROPERTY_NAME
      };
      
      
      public AbstractImageSourceInspector() {
      }
      
      public final SourceProperty getSourceProperty(Source source, String 
namespace, String name)
          throws SourceException {
  
          if (handlesProperty(namespace,name) && isImageMimeType(source) && 
isImageFileType(source)) {
              if (name.equals(IMAGE_WIDTH_PROPERTY_NAME))
                  return new SourceProperty(PROPERTY_NS, 
IMAGE_WIDTH_PROPERTY_NAME, 
                                            
String.valueOf(getImageSize(source)[0]));
              if (name.equals(IMAGE_HEIGHT_PROPERTY_NAME))
                  return new SourceProperty(PROPERTY_NS, 
IMAGE_HEIGHT_PROPERTY_NAME,
                                            
String.valueOf(getImageSize(source)[1]));
          }
          return null;
      }
      
      public final SourceProperty[] getSourceProperties(Source source) throws 
SourceException {
          if (isImageMimeType(source) && isImageFileType(source)) {
              int[] size = getImageSize(source);
              return new SourceProperty[] {
                  new SourceProperty(PROPERTY_NS, IMAGE_WIDTH_PROPERTY_NAME, 
String.valueOf(size[0])),
                  new SourceProperty(PROPERTY_NS, IMAGE_HEIGHT_PROPERTY_NAME, 
String.valueOf(size[1]))
              };
          }
          return null;
      }
      
      public final String[] getHandledPropertyTypes() {
          return HANDLED_PROPERTIES;
      }
      
      /**
       * Check whether this inspector handles properties of the given kind.
       */
      private final boolean handlesProperty(String namespace, String name) {
          return namespace.equals(PROPERTY_NS) && 
                     (name.equals(IMAGE_WIDTH_PROPERTY_NAME) || 
                     name.equals(IMAGE_HEIGHT_PROPERTY_NAME));
      }
      
      /**
       * Checks whether the mime mapping yields the type this inspector
       * handles.
       * 
       * @param source  the Source to test
       */
      protected abstract boolean isImageMimeType(Source source);
      
      /**
       * Inspects the input stream to verify this is in fact a file
       * of the type that this inspector handles.
       * 
       * @param source  the Source to test
       */
      protected abstract boolean isImageFileType(Source source) throws 
SourceException;
      
      /**
       * Calculate the width and the height of the image represented by source.
       * 
       * @param source  the Source to inspect.
       * @return  array carrying the calculated width parameter
       * in its 0 index, the height under index 1.
       */
      protected abstract int[] getImageSize(Source source) throws 
SourceException;
  
  }
  
  
  

Reply via email to