jeremias    2002/11/08 02:11:06

  Modified:    src/org/apache/fop/image Tag: fop-0_20_2-maintain
                        GifImage.java FopImageFactory.java
                        AbstractFopImage.java
  Log:
  Fixed multi-threading bugs (NPEs)
  Submitted by: Joachim Unger <[EMAIL PROTECTED]>
  
  line ending correction
  ArrayList --> List, HashMap --> Map
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.1   +2 -1      xml-fop/src/org/apache/fop/image/GifImage.java
  
  Index: GifImage.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/image/GifImage.java,v
  retrieving revision 1.1
  retrieving revision 1.1.2.1
  diff -u -r1.1 -r1.1.2.1
  --- GifImage.java     18 Sep 2001 08:17:07 -0000      1.1
  +++ GifImage.java     8 Nov 2002 10:11:06 -0000       1.1.2.1
  @@ -36,6 +36,7 @@
       }
   
       protected void loadImage() throws FopImageException {
  +        
//org.apache.fop.messaging.MessageHandler.debug(getClass().getName()+".loadImage(): 
"+this.m_href);
           int[] tmpMap = null;
           try {
               ImageProducer ip = (ImageProducer)this.m_href.getContent();
  
  
  
  1.25.2.5  +205 -207  xml-fop/src/org/apache/fop/image/Attic/FopImageFactory.java
  
  Index: FopImageFactory.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/image/Attic/FopImageFactory.java,v
  retrieving revision 1.25.2.4
  retrieving revision 1.25.2.5
  diff -u -r1.25.2.4 -r1.25.2.5
  --- FopImageFactory.java      17 Sep 2002 20:19:29 -0000      1.25.2.4
  +++ FopImageFactory.java      8 Nov 2002 10:11:06 -0000       1.25.2.5
  @@ -1,207 +1,205 @@
  -/*
  - * $Id$
  - * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  - * For details on use and redistribution please refer to the
  - * LICENSE file included with these sources.
  - */
  -
  -package org.apache.fop.image;
  -
  -// FOP
  -import org.apache.fop.image.analyser.ImageReaderFactory;
  -import org.apache.fop.image.analyser.ImageReader;
  -import org.apache.fop.configuration.Configuration;
  -
  -// Java
  -import java.io.IOException;
  -import java.io.InputStream;
  -import java.io.File;
  -import java.net.URL;
  -import java.net.MalformedURLException;
  -import java.lang.reflect.Constructor;
  -import java.util.HashMap;
  -
  -/**
  - * create FopImage objects (with a configuration file - not yet implemented).
  - * @author Eric SCHAEFFER
  - */
  -public class FopImageFactory {
  -
  -    private static HashMap m_urlMap = new HashMap();
  -
  -    /**
  -     * 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 synchronized FopImage Make(String href)
  -            throws MalformedURLException, FopImageException {
  -
  -        /*
  -         * According to section 5.11 a <uri-specification> is:
  -         * "url(" + URI + ")"
  -         * according to 7.28.7 a <uri-specification> is:
  -         * URI
  -         * So handle both.
  -         */
  -        // Get the absolute URL
  -        URL absoluteURL = null;
  -        InputStream imgIS = null;
  -        href = href.trim();
  -        if(href.startsWith("url(") && (href.indexOf(")") != -1)) {
  -            href = href.substring(4, href.indexOf(")")).trim();
  -            if(href.startsWith("'") && href.endsWith("'")) {
  -                href = href.substring(1, href.length() - 1);
  -            } else if(href.startsWith("\"") && href.endsWith("\"")) {
  -                href = href.substring(1, href.length() - 1);
  -            }
  -        }
  -
  -        // check if already created
  -        FopImage imageObject = (FopImage)m_urlMap.get(href);
  -        if (imageObject != null)
  -            return imageObject;
  -
  -        try {
  -            // try url as complete first, this can cause
  -            // a problem with relative uri's if there is an
  -            // image relative to where fop is run and relative
  -            // to the base dir of the document
  -            try {
  -                absoluteURL = new URL(href);
  -            } catch (MalformedURLException mue) {
  -                // if the href contains onl a path then file is assumed
  -                absoluteURL = new URL("file:" + href);
  -            }
  -            imgIS = absoluteURL.openStream();
  -        } catch (MalformedURLException e_context) {
  -            throw new FopImageException("Error with image URL: "
  -                                        + e_context.getMessage());
  -        } catch (Exception e) {
  -            // maybe relative
  -            URL context_url = null;
  -            String base = Configuration.getStringValue("baseDir");
  -
  -            if (base == null) {
  -                throw new FopImageException("Error with image URL: "
  -                                             + e.getMessage()
  -                                             + " and no base directory is 
specified");
  -            }
  -
  -            try {
  -                absoluteURL = new URL(Configuration.getStringValue("baseDir")
  -                                      + absoluteURL.getFile());
  -            } catch (MalformedURLException e_context) {
  -                // pb context url
  -                throw new FopImageException("Invalid Image URL - error on relative 
URL : "
  -                                            + e_context.getMessage());
  -            }
  -        }
  -
  -        // If not, check image type
  -        ImageReader imgReader = null;
  -        try {
  -            if (imgIS == null) {
  -                imgIS = absoluteURL.openStream();
  -            }
  -            imgReader = ImageReaderFactory.Make(absoluteURL.toExternalForm(),
  -                                                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.GifImage";
  -            // imgClassName = "org.apache.fop.image.JAIImage";
  -        } else if ("image/jpeg".equals(imgMimeType)) {
  -            imgClassName = "org.apache.fop.image.JpegImage";
  -            // 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/eps".equals(imgMimeType)) {
  -            imgClassName = "org.apache.fop.image.EPSImage";
  -        } 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) {
  -                msg = t.getMessage();
  -            } else {
  -                msg = ex.getMessage();
  -            }
  -            throw new FopImageException("Error creating FopImage object ("
  -                                        + absoluteURL.toString() + ") : "
  -                                        + msg);
  -        } catch (Exception ex) {
  -            throw new FopImageException("Error creating FopImage object ("
  -                                        + "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");
  -        }
  -        m_urlMap.put(href, imageInstance);
  -        return (FopImage)imageInstance;
  -    }
  -
  -    /**
  -     * Clear the image cache.
  -     */
  -    public static synchronized void resetCache() {
  -        m_urlMap.clear();
  -    }
  -}
  -
  +/*
  + * $Id$
  + * Copyright (C) 2001-2002 The Apache Software Foundation. All rights reserved.
  + * For details on use and redistribution please refer to the
  + * LICENSE file included with these sources.
  + */
  +
  +package org.apache.fop.image;
  +
  +// FOP
  +import org.apache.fop.image.analyser.ImageReaderFactory;
  +import org.apache.fop.image.analyser.ImageReader;
  +import org.apache.fop.configuration.Configuration;
  +
  +// Java
  +import java.io.IOException;
  +import java.io.InputStream;
  +import java.io.File;
  +import java.net.URL;
  +import java.net.MalformedURLException;
  +import java.lang.reflect.Constructor;
  +import java.util.Map;
  +
  +/**
  + * create FopImage objects (with a configuration file - not yet implemented).
  + * @author Eric SCHAEFFER
  + */
  +public class FopImageFactory {
  +
  +    private static Map m_urlMap = new java.util.HashMap();
  +
  +    /**
  +     * 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 synchronized FopImage Make(String href)
  +            throws MalformedURLException, FopImageException {
  +
  +        /*
  +         * According to section 5.11 a <uri-specification> is:
  +         * "url(" + URI + ")"
  +         * according to 7.28.7 a <uri-specification> is:
  +         * URI
  +         * So handle both.
  +         */
  +        // Get the absolute URL
  +        URL absoluteURL = null;
  +        InputStream imgIS = null;
  +        href = href.trim();
  +        if(href.startsWith("url(") && (href.indexOf(")") != -1)) {
  +            href = href.substring(4, href.indexOf(")")).trim();
  +            if(href.startsWith("'") && href.endsWith("'")) {
  +                href = href.substring(1, href.length() - 1);
  +            } else if(href.startsWith("\"") && href.endsWith("\"")) {
  +                href = href.substring(1, href.length() - 1);
  +            }
  +        }
  +
  +        // check if already created
  +        FopImage imageObject = (FopImage)m_urlMap.get(href);
  +        if (imageObject != null)
  +            return imageObject;
  +
  +        try {
  +            // try url as complete first, this can cause
  +            // a problem with relative uri's if there is an
  +            // image relative to where fop is run and relative
  +            // to the base dir of the document
  +            try {
  +                absoluteURL = new URL(href);
  +            } catch (MalformedURLException mue) {
  +                // if the href contains onl a path then file is assumed
  +                absoluteURL = new URL("file:" + href);
  +            }
  +            imgIS = absoluteURL.openStream();
  +        } catch (MalformedURLException e_context) {
  +            throw new FopImageException("Error with image URL: "
  +                                        + e_context.getMessage());
  +        } catch (Exception e) {
  +            // maybe relative
  +            URL baseURL = Configuration.getBaseURL();
  +
  +            if (baseURL == null) {
  +                throw new FopImageException("Error with image URL: "
  +                                             + e.getMessage()
  +                                             + " and no base URL is specified");
  +            }
  +
  +            try {
  +                absoluteURL = new URL(baseURL, absoluteURL.getFile());
  +            } catch (MalformedURLException e_context) {
  +                // pb context url
  +                throw new FopImageException("Invalid Image URL - error on relative 
URL : "
  +                                            + e_context.getMessage());
  +            }
  +        }
  +
  +        // If not, check image type
  +        ImageReader imgReader = null;
  +        try {
  +            if (imgIS == null) {
  +                imgIS = absoluteURL.openStream();
  +            }
  +            imgReader = ImageReaderFactory.Make(absoluteURL.toExternalForm(),
  +                                                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.GifImage";
  +            // imgClassName = "org.apache.fop.image.JAIImage";
  +        } else if ("image/jpeg".equals(imgMimeType)) {
  +            imgClassName = "org.apache.fop.image.JpegImage";
  +            // 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/eps".equals(imgMimeType)) {
  +            imgClassName = "org.apache.fop.image.EPSImage";
  +        } 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) {
  +                msg = t.getMessage();
  +            } else {
  +                msg = ex.getMessage();
  +            }
  +            throw new FopImageException("Error creating FopImage object ("
  +                                        + absoluteURL.toString() + ") : "
  +                                        + msg);
  +        } catch (Exception ex) {
  +            throw new FopImageException("Error creating FopImage object ("
  +                                        + "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");
  +        }
  +        m_urlMap.put(href, imageInstance);
  +        return (FopImage)imageInstance;
  +    }
  +
  +    /**
  +     * Clear the image cache.
  +     */
  +    public static synchronized void resetCache() {
  +        m_urlMap.clear();
  +    }
  +}
  +
  
  
  
  1.5.2.5   +42 -24    xml-fop/src/org/apache/fop/image/AbstractFopImage.java
  
  Index: AbstractFopImage.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/image/AbstractFopImage.java,v
  retrieving revision 1.5.2.4
  retrieving revision 1.5.2.5
  diff -u -r1.5.2.4 -r1.5.2.5
  --- AbstractFopImage.java     5 Nov 2002 07:53:48 -0000       1.5.2.4
  +++ AbstractFopImage.java     8 Nov 2002 10:11:06 -0000       1.5.2.5
  @@ -137,6 +137,7 @@
        */
       abstract protected void loadImage() throws FopImageException;
   
  +
       /**
       * If true, image data are inverted
       */
  @@ -158,8 +159,10 @@
        * @exception FopImageException an error occured during property retriaval
        */
       public int getWidth() throws FopImageException {
  -        if (this.m_width == 0)
  -            this.loadImage();
  +        synchronized(this) {
  +            if (this.m_width == 0)
  +                this.loadImage();
  +        }
   
           return this.m_width;
       }
  @@ -170,8 +173,10 @@
        * @exception FopImageException an error occured during property retriaval
        */
       public int getHeight() throws FopImageException {
  -        if (this.m_height == 0)
  -            this.loadImage();
  +        synchronized(this) {
  +            if (this.m_height == 0)
  +                this.loadImage();
  +        }
   
           return this.m_height;
       }
  @@ -182,8 +187,10 @@
        * @exception FopImageException an error occured during property retriaval
        */
       public ColorSpace getColorSpace() throws FopImageException {
  -        if (this.m_colorSpace == null)
  -            this.loadImage();
  +        synchronized(this) {
  +            if (this.m_colorSpace == null)
  +                this.loadImage();
  +        }
   
           return this.m_colorSpace;
       }
  @@ -194,8 +201,10 @@
        * @exception FopImageException an error occured during property retriaval
        */
       public int getBitsPerPixel() throws FopImageException {
  -        if (this.m_bitsPerPixel == 0)
  -            this.loadImage();
  +        synchronized(this) {
  +            if (this.m_bitsPerPixel == 0)
  +                this.loadImage();
  +        }
   
           return this.m_bitsPerPixel;
       }
  @@ -224,8 +233,10 @@
        * @exception FopImageException an error occured during loading
        */
       public byte[] getBitmaps() throws FopImageException {
  -        if (this.m_bitmaps == null)
  -            this.loadImage();
  +        synchronized(this) {
  +            if (this.m_bitmaps == null)
  +                this.loadImage();
  +        }
   
           return this.m_bitmaps;
       }
  @@ -236,8 +247,10 @@
        * @exception FopImageException an error occured during loading
        */
       public int getBitmapsSize() throws FopImageException {
  -        if (this.m_bitmapsSize == 0)
  -            this.loadImage();
  +        synchronized(this) {
  +            if (this.m_bitmapsSize == 0)
  +                this.loadImage();
  +        }
   
           return this.m_bitmapsSize;
       }
  @@ -272,8 +285,10 @@
            * Using the bitsPerPixel var as our flag since many imges will
            * have a null m_compressionType even after being loaded
            */
  -        if (this.m_bitsPerPixel == 0)
  -            this.loadImage();
  +        synchronized(this) {
  +            if (this.m_bitsPerPixel == 0)
  +                this.loadImage();
  +        }
   
           return m_compressionType;
       }
  @@ -282,21 +297,24 @@
        * Free all ressource.
        */
       public void close() {
  +        
//org.apache.fop.messaging.MessageHandler.debug(getClass().getName()+".close(): 
"+this.m_href);
           /*
            * For the moment, only release the bitmaps (image areas
            * can share the same FopImage object)
            * Thus, even if it had been called, other properties
            * are still available.
            */
  -        // this.m_width = 0;
  -        // this.m_height = 0;
  -        // this.m_href = null;
  -        // this.m_colorSpace = null;
  -        // this.m_bitsPerPixel = 0;
  -        this.m_bitmaps = null;
  -        this.m_bitmapsSize = 0;
  -        // this.m_isTransparent = false;
  -        // this.m_transparentColor = null;
  +        synchronized(this) {
  +            // this.m_width = 0;
  +            // this.m_height = 0;
  +            // this.m_href = null;
  +            // this.m_colorSpace = null;
  +            // this.m_bitsPerPixel = 0;
  +            this.m_bitmaps = null;
  +            this.m_bitmapsSize = 0;
  +            // this.m_isTransparent = false;
  +            // this.m_transparentColor = null;
  +        }
       }
   
   }
  
  
  

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

Reply via email to