jeremias 2002/11/08 02:05:29
Modified: src/org/apache/fop/image/analyser Tag: fop-0_20_2-maintain
ImageReaderFactory.java
Log:
Improved registration of ImageReader implementations.
line ending correction
ArrayList --> List
Revision Changes Path
No revision
No revision
1.6.2.3 +97 -64
xml-fop/src/org/apache/fop/image/analyser/ImageReaderFactory.java
Index: ImageReaderFactory.java
===================================================================
RCS file:
/home/cvs/xml-fop/src/org/apache/fop/image/analyser/ImageReaderFactory.java,v
retrieving revision 1.6.2.2
retrieving revision 1.6.2.3
diff -u -r1.6.2.2 -r1.6.2.3
--- ImageReaderFactory.java 2 Aug 2002 20:28:52 -0000 1.6.2.2
+++ ImageReaderFactory.java 8 Nov 2002 10:05:29 -0000 1.6.2.3
@@ -1,64 +1,97 @@
-/*
- * $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.analyser;
-
-// Java
-import java.io.InputStream;
-import java.io.BufferedInputStream;
-import java.io.IOException;
-import java.util.ArrayList;
-
-// FOP
-import org.apache.fop.image.FopImageException;
-
-/**
- * Factory for ImageReader objects.
- * @author Pankaj Narula
- * @version 1.0
- */
-public class ImageReaderFactory {
- static protected ArrayList formats = null;
-
- /**
- * ImageReader maker.
- * @param in image input stream
- * @return ImageReader object
- * @exception FopImageException an error occured during creation or
- * image type is not supported
- */
- 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);
- if (reader.verifySignature(uri, bis)) {
- return reader;
- }
- }
- } catch (IOException ex) {
- throw new FopImageException(ex.getMessage());
- }
- return null;
- }
-
-}
-
+/*
+ * $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.analyser;
+
+// Java
+import java.io.InputStream;
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.util.List;
+
+// FOP
+import org.apache.fop.image.FopImageException;
+
+/**
+ * Factory for ImageReader objects.
+ * @author Pankaj Narula
+ */
+public class ImageReaderFactory {
+ 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
+ * image type is not supported
+ */
+ static public ImageReader Make(String uri,
+ InputStream in) throws FopImageException {
+
+ ImageReader reader;
+ BufferedInputStream bis = new BufferedInputStream(in);
+ try {
+ for (int i = 0; i< formats.size(); 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;
+ }
+ }
+ } catch (IOException ex) {
+ throw new FopImageException(ex.getMessage());
+ }
+ return null;
+ }
+
+}
+
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]