From 211e14059733c33e98c531449298225393cbaf44 Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Fri, 8 Nov 2002 10:05:29 +0000 Subject: [PATCH] Improved registration of ImageReader implementations. line ending correction ArrayList --> List git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/fop-0_20_2-maintain@195450 13f79535-47bb-0310-9956-ffa450edef68 --- .../image/analyser/ImageReaderFactory.java | 65 ++++++++++++++----- 1 file changed, 49 insertions(+), 16 deletions(-) diff --git a/src/org/apache/fop/image/analyser/ImageReaderFactory.java b/src/org/apache/fop/image/analyser/ImageReaderFactory.java index 271f83a81..3d6896ed0 100644 --- a/src/org/apache/fop/image/analyser/ImageReaderFactory.java +++ b/src/org/apache/fop/image/analyser/ImageReaderFactory.java @@ -1,6 +1,6 @@ /* * $Id$ - * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * 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. */ @@ -11,7 +11,7 @@ package org.apache.fop.image.analyser; import java.io.InputStream; import java.io.BufferedInputStream; import java.io.IOException; -import java.util.ArrayList; +import java.util.List; // FOP import org.apache.fop.image.FopImageException; @@ -19,13 +19,52 @@ import org.apache.fop.image.FopImageException; /** * Factory for ImageReader objects. * @author Pankaj Narula - * @version 1.0 */ public class ImageReaderFactory { - static protected ArrayList formats = null; + static protected List formats = null; + + static { + /**@todo make configurable one day...*/ + formats = new java.util.Vector(); + try { + registerImageReader("org.apache.fop.image.analyser.JPEGReader"); + registerImageReader("org.apache.fop.image.analyser.BMPReader"); + registerImageReader("org.apache.fop.image.analyser.GIFReader"); + registerImageReader("org.apache.fop.image.analyser.PNGReader"); + registerImageReader("org.apache.fop.image.analyser.TIFFReader"); + registerImageReader("org.apache.fop.image.analyser.EPSReader"); + registerImageReader("org.apache.fop.image.analyser.SVGReader"); + } catch (ClassNotFoundException cnfe) { + throw new RuntimeException("One of the default ImageReader implementations is not available: "+cnfe.getMessage()); + } + } + + /** + * Registers a new ImageReader implementation. + * + * @param classname The fully qualified classname + * @throws ClassNotFoundException In case the given class cannot be found + */ + public static void registerImageReader(String classname) throws ClassNotFoundException { + Class clazz = Class.forName(classname); + registerImageReader(clazz); + } + + + /** + * Registers a new ImageReader implementation. + * + * @param clazz The ImageReader implementation class. + */ + public static void registerImageReader(Class clazz) { + if (!ImageReader.class.isAssignableFrom(clazz)) throw new RuntimeException("This class does not implement the ImageReader interface: "+clazz.getName()); + formats.add(clazz); + } /** * ImageReader maker. + * + * @param uri URI of the image * @param in image input stream * @return ImageReader object * @exception FopImageException an error occured during creation or @@ -34,22 +73,16 @@ public class ImageReaderFactory { static public ImageReader Make(String uri, InputStream in) throws FopImageException { - // need to use a config file and remove static methods - formats = new ArrayList(); - formats.add(new JPEGReader()); - formats.add(new BMPReader()); - formats.add(new GIFReader()); - formats.add(new PNGReader()); - formats.add(new TIFFReader()); - formats.add(new EPSReader()); - formats.add(new SVGReader()); - // - ImageReader reader; BufferedInputStream bis = new BufferedInputStream(in); try { for (int i = 0; i< formats.size(); i++) { - reader = (ImageReader)formats.get(i); + Class clazz = (Class)formats.get(i); + try { + reader = (ImageReader)clazz.newInstance(); + } catch (Exception e) { + throw new FopImageException("ImageReader implementation cannot be instantiated: "+e.getMessage()); + } if (reader.verifySignature(uri, bis)) { return reader; } -- 2.39.5