001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * ImageFactory.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.resourceloader;
030:
031: import java.awt.Image;
032: import java.awt.Toolkit;
033: import java.io.IOException;
034: import java.io.InputStream;
035: import java.net.URL;
036: import java.net.URLConnection;
037: import java.util.ArrayList;
038:
039: import org.jfree.io.IOUtils;
040: import org.jfree.util.Log;
041: import org.jfree.util.ObjectUtilities;
042: import org.jfree.report.util.MemoryByteArrayOutputStream;
043:
044: public class ImageFactory {
045: private static ImageFactory singleton;
046: private ArrayList factoryModules;
047:
048: public static synchronized ImageFactory getInstance() {
049: if (singleton == null) {
050: singleton = new ImageFactory();
051: }
052: return singleton;
053: }
054:
055: private ImageFactory() {
056: factoryModules = new ArrayList();
057: }
058:
059: public boolean registerModule(final String className) {
060: try {
061: final Class c = ObjectUtilities.getClassLoader(getClass())
062: .loadClass(className);
063: registerModule((ImageFactoryModule) c.newInstance());
064: return true;
065: } catch (Exception e) {
066: return false;
067: }
068: }
069:
070: public synchronized void registerModule(
071: final ImageFactoryModule module) {
072: if (factoryModules.contains(module) == false) {
073: factoryModules.add(module);
074: }
075: }
076:
077: public Image createImage(final URL url) throws IOException {
078: final InputStream in = url.openStream();
079: final URLConnection uc = url.openConnection();
080: final Image image = createImage(uc.getInputStream(), url
081: .getFile(), uc.getContentType());
082: in.close();
083: return image;
084: }
085:
086: public Image createImage(final InputStream in,
087: final String fileName, final String mimeType)
088: throws IOException {
089: final MemoryByteArrayOutputStream bout = new MemoryByteArrayOutputStream(
090: 32 * 1024, 64 * 1024);
091: IOUtils.getInstance().copyStreams(in, bout, 16 * 1024);
092: return createImage(bout.toByteArray(), fileName, mimeType);
093: }
094:
095: public synchronized Image createImage(final byte[] data,
096: final String fileName, final String mimeType)
097: throws IOException {
098: // first pass: Search by content
099: // this is the safest method to identify the image data
100: // as names might be invalid and mimetypes might be forged ..
101: for (int i = 0; i < factoryModules.size(); i++) {
102: try {
103: final ImageFactoryModule module = (ImageFactoryModule) factoryModules
104: .get(i);
105: if (module.getHeaderFingerprintSize() > 0
106: && data.length >= module
107: .getHeaderFingerprintSize()) {
108: if (module.canHandleResourceByContent(data)) {
109: return module.createImage(data, fileName,
110: mimeType);
111: }
112: }
113: } catch (IOException ioe) {
114: // first try failed ..
115: Log
116: .debug(
117: "Failed to load image: Trying harder ..",
118: ioe);
119: }
120: }
121:
122: // second pass: Search by mime type
123: // this is the second safest method to identify the image data
124: // as names might be invalid and mimetypes might be forged ..
125: if (mimeType != null && "".equals(mimeType) == false) {
126: for (int i = 0; i < factoryModules.size(); i++) {
127: try {
128: final ImageFactoryModule module = (ImageFactoryModule) factoryModules
129: .get(i);
130: if (module.canHandleResourceByMimeType(mimeType)) {
131: return module.createImage(data, fileName,
132: mimeType);
133: }
134: } catch (IOException ioe) {
135: // first try failed ..
136: Log.debug("Failed to load image: Trying harder ..",
137: ioe);
138: }
139: }
140: }
141:
142: // third pass: Search by mime type
143: // this is the final method to identify the image data
144: // as names might be invalid and mimetypes might be forged ..
145: if (mimeType != null && "".equals(mimeType) == false) {
146: for (int i = 0; i < factoryModules.size(); i++) {
147: try {
148: final ImageFactoryModule module = (ImageFactoryModule) factoryModules
149: .get(i);
150: if (module.canHandleResourceByName(fileName)) {
151: return module.createImage(data, fileName,
152: mimeType);
153: }
154: } catch (IOException ioe) {
155: // first try failed ..
156: Log.debug("Failed to load image: Trying harder ..",
157: ioe);
158: }
159: }
160: }
161:
162: Log
163: .debug("Failed to find suitable factory for image: Using the AWT as fallback ..");
164: // default failback ..
165: // the JDK implementation might be able to handle some more modules
166: return Toolkit.getDefaultToolkit().createImage(data);
167: }
168: }
|