You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by es...@locus.apache.org on 2000/12/12 10:38:53 UTC

cvs commit: xml-fop/src/org/apache/fop/image FopImageFactory.java

eschaeffer    00/12/12 01:38:53

  Modified:    src/org/apache/fop/image FopImageFactory.java
  Log:
  Patch from Kelly Campbell. Fix NullPointerException...
  
  Revision  Changes    Path
  1.16      +132 -130  xml-fop/src/org/apache/fop/image/FopImageFactory.java
  
  Index: FopImageFactory.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/image/FopImageFactory.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- FopImageFactory.java	2000/11/28 16:06:14	1.15
  +++ FopImageFactory.java	2000/12/12 09:38:52	1.16
  @@ -1,27 +1,27 @@
  -/*-- $Id: FopImageFactory.java,v 1.15 2000/11/28 16:06:14 eschaeffer Exp $ --
  +/*-- $Id: FopImageFactory.java,v 1.16 2000/12/12 09:38:52 eschaeffer Exp $ --
    ============================================================================
  -                   The Apache Software License, Version 1.1
  +									 The Apache Software License, Version 1.1
    ============================================================================
  -    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
  +		Copyright (C) 1999 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.
  +		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.
  +		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.
  +		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 "Fop" 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.
  +		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.
  +		"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
  @@ -59,117 +59,119 @@
    */
   public class FopImageFactory {
   
  -  private static Hashtable m_urlMap = new Hashtable();
  +	private static Hashtable m_urlMap = new Hashtable();
   
  -  /**
  -   * create an FopImage objects.
  -   * @param href image URL as a String
  -   * @return a new FopImage object
  -   * @exception java.net.MalformedURLException bad URL
  -   * @exception FopImageException an error occured during construction
  -   */
  -  public static FopImage Make(String href)
  -    throws MalformedURLException, FopImageException {
  -
  -    // Get the absolute URL
  -    URL absoluteURL = null;
  -    //		try {
  -    absoluteURL = new URL(href);
  -    /*
  -    		}
  -    		catch (MalformedURLException e) {
  -    			// maybe relative
  -    			URL context_url = null;
  -    			try {
  -    				context_url = new URL(); // how to get the context URL ?
  -    				try {
  -    					absoluteURL = new URL(context_url, ref);
  -    				}
  -    				catch (MalformedURLException e_abs) {
  -    					// not found
  -    					MessageHandler.errorln(
  -                "Invalid Image URL : " +
  -                e_abs.getMessage() +
  -                "(base URL " + context_url.toString() + ")"
  -                );
  -    					return null;
  -    				}
  -    			}
  -    			catch (MalformedURLException e_context) {
  -    				// pb context url
  -    				MessageHandler.errorln("Invalid Image URL - error on relative URL : " + e_context.getMessage());
  -    				return null;
  -    			}
  -    		}
  -    */
  -
  -    // check if already created
  -    FopImage imageObject =
  -      (FopImage) m_urlMap.get(absoluteURL.toString());
  -    if (imageObject != null)
  -      return imageObject;
  +	/**
  +	 * create an FopImage objects.
  +	 * @param href image URL as a String
  +	 * @return a new FopImage object
  +	 * @exception java.net.MalformedURLException bad URL
  +	 * @exception FopImageException an error occured during construction
  +	 */
  +	public static FopImage Make(String href)
  +		throws MalformedURLException, FopImageException {
  +
  +		// Get the absolute URL
  +		URL absoluteURL = null;
  +		//		try {
  +		absoluteURL = new URL(href);
  +		/*
  +				}
  +				catch (MalformedURLException e) {
  +					// maybe relative
  +					URL context_url = null;
  +					try {
  +						context_url = new URL(); // how to get the context URL ?
  +						try {
  +							absoluteURL = new URL(context_url, ref);
  +						}
  +						catch (MalformedURLException e_abs) {
  +							// not found
  +							MessageHandler.errorln(
  +								"Invalid Image URL : " +
  +								e_abs.getMessage() +
  +								"(base URL " + context_url.toString() + ")"
  +								);
  +							return null;
  +						}
  +					}
  +					catch (MalformedURLException e_context) {
  +						// pb context url
  +						MessageHandler.errorln("Invalid Image URL - error on relative URL : " + e_context.getMessage());
  +						return null;
  +					}
  +				}
  +		*/
  +
  +		// check if already created
  +		FopImage imageObject =
  +			(FopImage) m_urlMap.get(absoluteURL.toString());
  +		if (imageObject != null)
  +			return imageObject;
   
  -    // If not, check image type
  -    ImageReader imgReader = null;
  +		// If not, check image type
  +		ImageReader imgReader = null;
   		InputStream imgIS = null;
  -    try {
  +		try {
   			imgIS = absoluteURL.openStream();
  -      imgReader = ImageReaderFactory.Make(imgIS);
  -    } catch (Exception e) {
  -      throw new FopImageException(
  -        "Error while recovering Image Informations (" +
  -        absoluteURL.toString() + ") : " + e.getMessage());
  -    } finally {
  -			try {imgIS.close();} catch (IOException e) {}
  +			imgReader = ImageReaderFactory.Make(imgIS);
  +		} catch (Exception e) {
  +			throw new FopImageException(
  +				"Error while recovering Image Informations (" +
  +				absoluteURL.toString() + ") : " + e.getMessage());
  +		} finally {
  +			if (imgIS != null) {
  +				try {imgIS.close();} catch (IOException e) {}
  +			}
   		}
  -    if (imgReader == null)
  -      throw new FopImageException("No ImageReader for this type of image (" +
  -                                  absoluteURL.toString() + ")");
  -    // Associate mime-type to FopImage class
  -    String imgMimeType = imgReader.getMimeType();
  -    String imgClassName = null;
  -    if ("image/gif".equals(imgMimeType)) {
  -      imgClassName = "org.apache.fop.image.GifJpegImage";
  -//      imgClassName = "org.apache.fop.image.JAIImage";
  -    } else if ("image/jpeg".equals(imgMimeType)) {
  -      imgClassName = "org.apache.fop.image.GifJpegImage";
  -//      imgClassName = "org.apache.fop.image.JAIImage";
  -    } else if ("image/bmp".equals(imgMimeType)) {
  -      imgClassName = "org.apache.fop.image.BmpImage";
  -//      imgClassName = "org.apache.fop.image.JAIImage";
  -    } else if ("image/png".equals(imgMimeType)) {
  -      imgClassName = "org.apache.fop.image.JimiImage";
  -//      imgClassName = "org.apache.fop.image.JAIImage";
  -    } else if ("image/tga".equals(imgMimeType)) {
  -      imgClassName = "org.apache.fop.image.JimiImage";
  -//      imgClassName = "org.apache.fop.image.JAIImage";
  -    } else if ("image/tiff".equals(imgMimeType)) {
  -      imgClassName = "org.apache.fop.image.JimiImage";
  -//      imgClassName = "org.apache.fop.image.JAIImage";
  -    } else if ("image/svg-xml".equals(imgMimeType)) {
  -      imgClassName = "org.apache.fop.image.SVGImage";
  -    }
  -    if (imgClassName == null)
  -      throw new FopImageException("Unsupported image type (" +
  -                                  absoluteURL.toString() + ") : " + imgMimeType);
  -
  -    // load the right image class
  -    // return new <FopImage implementing class>
  -    Object imageInstance = null;
  -    Class imageClass = null;
  -    try {
  -      imageClass = Class.forName(imgClassName);
  -      Class[] imageConstructorParameters = new Class[2];
  -      imageConstructorParameters[0] = Class.forName("java.net.URL");
  -      imageConstructorParameters[1] = Class.forName("org.apache.fop.image.analyser.ImageReader");
  -      Constructor imageConstructor =
  -        imageClass.getDeclaredConstructor(
  -          imageConstructorParameters);
  -      Object[] initArgs = new Object[2];
  -      initArgs[0] = absoluteURL;
  -      initArgs[1] = imgReader;
  -      imageInstance = imageConstructor.newInstance(initArgs);
  -    } catch (java.lang.reflect.InvocationTargetException ex) {
  +		if (imgReader == null)
  +			throw new FopImageException("No ImageReader for this type of image (" +
  +																	absoluteURL.toString() + ")");
  +		// Associate mime-type to FopImage class
  +		String imgMimeType = imgReader.getMimeType();
  +		String imgClassName = null;
  +		if ("image/gif".equals(imgMimeType)) {
  +			imgClassName = "org.apache.fop.image.GifJpegImage";
  +//      imgClassName = "org.apache.fop.image.JAIImage";
  +		} else if ("image/jpeg".equals(imgMimeType)) {
  +			imgClassName = "org.apache.fop.image.GifJpegImage";
  +//      imgClassName = "org.apache.fop.image.JAIImage";
  +		} else if ("image/bmp".equals(imgMimeType)) {
  +			imgClassName = "org.apache.fop.image.BmpImage";
  +//      imgClassName = "org.apache.fop.image.JAIImage";
  +		} else if ("image/png".equals(imgMimeType)) {
  +			imgClassName = "org.apache.fop.image.JimiImage";
  +//      imgClassName = "org.apache.fop.image.JAIImage";
  +		} else if ("image/tga".equals(imgMimeType)) {
  +			imgClassName = "org.apache.fop.image.JimiImage";
  +//      imgClassName = "org.apache.fop.image.JAIImage";
  +		} else if ("image/tiff".equals(imgMimeType)) {
  +			imgClassName = "org.apache.fop.image.JimiImage";
  +//      imgClassName = "org.apache.fop.image.JAIImage";
  +		} else if ("image/svg-xml".equals(imgMimeType)) {
  +			imgClassName = "org.apache.fop.image.SVGImage";
  +		}
  +		if (imgClassName == null)
  +			throw new FopImageException("Unsupported image type (" +
  +																	absoluteURL.toString() + ") : " + imgMimeType);
  +
  +		// load the right image class
  +		// return new <FopImage implementing class>
  +		Object imageInstance = null;
  +		Class imageClass = null;
  +		try {
  +			imageClass = Class.forName(imgClassName);
  +			Class[] imageConstructorParameters = new Class[2];
  +			imageConstructorParameters[0] = Class.forName("java.net.URL");
  +			imageConstructorParameters[1] = Class.forName("org.apache.fop.image.analyser.ImageReader");
  +			Constructor imageConstructor =
  +				imageClass.getDeclaredConstructor(
  +					imageConstructorParameters);
  +			Object[] initArgs = new Object[2];
  +			initArgs[0] = absoluteURL;
  +			initArgs[1] = imgReader;
  +			imageInstance = imageConstructor.newInstance(initArgs);
  +		} catch (java.lang.reflect.InvocationTargetException ex) {
   			Throwable t = ex.getTargetException();
   			String msg;
   			if (t != null) {
  @@ -188,15 +190,15 @@
   				"Error creating FopImage object (" +
   				absoluteURL.toString() + ") : " + ex.getMessage()
   				);
  +		}
  +		if (! (imageInstance instanceof org.apache.fop.image.FopImage)) {
  +			throw new FopImageException(
  +				"Error creating FopImage object (" +
  +				absoluteURL.toString() + ") : " + "class " +
  +				imageClass.getName() + " doesn't implement org.apache.fop.image.FopImage interface");
   		}
  -    if (! (imageInstance instanceof org.apache.fop.image.FopImage)) {
  -      throw new FopImageException(
  -        "Error creating FopImage object (" +
  -        absoluteURL.toString() + ") : " + "class " +
  -        imageClass.getName() + " doesn't implement org.apache.fop.image.FopImage interface");
  -    }
  -    m_urlMap.put(absoluteURL.toString(), imageInstance);
  -    return (FopImage) imageInstance;
  -  }
  +		m_urlMap.put(absoluteURL.toString(), imageInstance);
  +		return (FopImage) imageInstance;
  +	}
   }