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: * RTFImageCache.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.output.table.rtf.helper;
030:
031: import java.io.BufferedInputStream;
032: import java.io.IOException;
033: import java.io.InputStream;
034: import java.net.URL;
035: import java.util.HashMap;
036:
037: import com.lowagie.text.BadElementException;
038: import com.lowagie.text.DocumentException;
039: import com.lowagie.text.Image;
040: import org.jfree.io.IOUtils;
041: import org.jfree.report.ImageContainer;
042: import org.jfree.report.LocalImageContainer;
043: import org.jfree.report.URLImageContainer;
044: import org.jfree.report.layout.output.RenderUtility;
045: import org.jfree.report.resourceloader.ImageFactory;
046: import org.jfree.report.util.MemoryByteArrayOutputStream;
047: import org.jfree.util.Log;
048: import org.jfree.util.StringUtils;
049: import org.jfree.util.WaitingImageObserver;
050:
051: /**
052: * Todo: Document me!
053: *
054: * @author : Thomas Morgner
055: */
056: public class RTFImageCache {
057: private HashMap cachedImages;
058:
059: public RTFImageCache() {
060: this .cachedImages = new HashMap();
061: }
062:
063: private boolean isSupportedFormat(final URL sourceURL) {
064: final String file = sourceURL.getFile();
065: if (StringUtils.endsWithIgnoreCase(file, ".png")) {
066: return true;
067: }
068: if (StringUtils.endsWithIgnoreCase(file, ".jpg")
069: || StringUtils.endsWithIgnoreCase(file, ".jpeg")) {
070: return true;
071: }
072: if (StringUtils.endsWithIgnoreCase(file, ".bmp")
073: || StringUtils.endsWithIgnoreCase(file, ".ico")) {
074: return true;
075: }
076: return false;
077: }
078:
079: /**
080: * Helperfunction to extract an image from an imagereference. If the image is contained
081: * as java.awt.Image object only, the image is recoded into an PNG-Image.
082: *
083: * @param reference the image reference.
084: * @return an image.
085: * @throws com.lowagie.text.DocumentException
086: * if no PDFImageElement could be created using the given
087: * ImageReference.
088: * @throws java.io.IOException if the image could not be read.
089: */
090: public Image getImage(final ImageContainer reference)
091: throws DocumentException, IOException {
092: Object identity = null;
093: java.awt.Image image = null;
094: if (reference instanceof URLImageContainer) {
095: final URLImageContainer urlImageContainer = (URLImageContainer) reference;
096: final URL url = urlImageContainer.getSourceURL();
097: if (url != null && urlImageContainer.isLoadable()) {
098: try {
099: identity = String.valueOf(url);
100: final Image cached = (Image) cachedImages
101: .get(identity);
102: if (cached != null) {
103: return cached;
104: }
105:
106: if (isSupportedFormat(url) == false) {
107: // This is a unsupported image format.
108: if (reference instanceof LocalImageContainer) {
109: final LocalImageContainer li = (LocalImageContainer) reference;
110: image = li.getImage();
111: }
112: if (image == null) {
113: image = ImageFactory.getInstance()
114: .createImage(url);
115: }
116: } else {
117: final MemoryByteArrayOutputStream bout = new MemoryByteArrayOutputStream();
118: final InputStream urlIn = new BufferedInputStream(
119: url.openStream());
120: try {
121: IOUtils.getInstance().copyStreams(urlIn,
122: bout);
123: } finally {
124: bout.close();
125: urlIn.close();
126: }
127:
128: final byte[] data = bout.toByteArray();
129: final Image itextimage = Image
130: .getInstance(data);
131: cachedImages.put(identity, itextimage);
132: return itextimage;
133: }
134: } catch (BadElementException be) {
135: Log
136: .info(
137: "Caught illegal Image, will recode to PNG instead",
138: be);
139: } catch (IOException ioe) {
140: Log
141: .info(
142: "Unable to read the raw-data, will try to recode image-data.",
143: ioe);
144: }
145: }
146: }
147:
148: if (reference instanceof LocalImageContainer && image == null) {
149: final LocalImageContainer localImageContainer = (LocalImageContainer) reference;
150: image = localImageContainer.getImage();
151: if (image != null) {
152: // check, if the content was cached ...
153: identity = localImageContainer.getIdentity();
154: if (identity != null) {
155: final Image cachedImage = (Image) cachedImages
156: .get(identity);
157: if (cachedImage != null) {
158: return cachedImage;
159: }
160: }
161:
162: }
163: }
164: if (image == null) {
165: return null;
166: }
167:
168: final WaitingImageObserver obs = new WaitingImageObserver(image);
169: obs.waitImageLoaded();
170:
171: final byte[] data = RenderUtility.encodeImage(image);
172: final Image itextimage = Image.getInstance(data);
173: if (identity != null) {
174: cachedImages.put(identity, itextimage);
175: }
176: return itextimage;
177: }
178:
179: }
|