001    /*
002     * Created on Jun 8, 2007
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005     * in compliance with the License. You may obtain a copy of the License at
006     *
007     * http://www.apache.org/licenses/LICENSE-2.0
008     *
009     * Unless required by applicable law or agreed to in writing, software distributed under the License
010     * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011     * or implied. See the License for the specific language governing permissions and limitations under
012     * the License.
013     *
014     * Copyright @2007-2009 the original author or authors.
015     */
016    package org.fest.swing.junit.ant;
017    
018    import static java.io.File.separator;
019    import static java.util.logging.Level.SEVERE;
020    import static org.fest.util.Strings.isEmpty;
021    
022    import java.awt.image.BufferedImage;
023    import java.util.logging.Logger;
024    
025    import org.fest.swing.image.ImageFileWriter;
026    
027    /**
028     * Understands base64 encoding and decoding of an image.
029     *
030     * @author Alex Ruiz
031     */
032    public final class ImageHandler {
033    
034      private static final String EMPTY_STRING = "";
035    
036      private static Logger logger = Logger.getAnonymousLogger();
037    
038      private static ImageEncoder imageEncoder = new ImageEncoder();
039      private static ImageDecoder imageDecoder = new ImageDecoder();
040      private static ImageFileWriter imageFileWriter = new ImageFileWriter();
041    
042      /**
043       * Encodes the given image using the base64 algorithm. Failures in encoding an image are simply logged, no exceptions
044       * are thrown.
045       * @param image the image to encode.
046       * @return base64 characters.
047       */
048      public static String encodeBase64(BufferedImage image) {
049        return encodeBase64(image, imageEncoder);
050      }
051    
052      // makes testing easier
053      static String encodeBase64(BufferedImage image, ImageEncoder encoder) {
054        try {
055          return encoder.encodeBase64(image);
056        } catch (Exception e) {
057          logger.log(SEVERE, "Unable to encode image", e);
058          return null;
059        }
060      }
061    
062      /**
063       * Decodes the given base64 characters into an image. Failures in decoding base64 characters are simply logged, no
064       * exceptions are thrown.
065       * @param encoded the given base64 characters.
066       * @return the decoded image.
067       */
068      public static BufferedImage decodeBase64(String encoded) {
069        return decodeBase64(encoded, imageDecoder);
070      }
071    
072      // makes testing easier
073      static BufferedImage decodeBase64(String encoded, ImageDecoder decoder) {
074        try {
075          return decoder.decodeBase64(encoded);
076        } catch (Exception e) {
077          logger.log(SEVERE, "Unable to encode image", e);
078          return null;
079        }
080      }
081    
082      /**
083       * Decodes the given base64 characters into an image, and saves the decoded image as a file using the given path.
084       * Failures in decoding or saving the image as a file are simply logged, no exceptions are thrown.
085       * @param encoded the given base64 characters.
086       * @param path the path where to save the image file.
087       * @return empty <code>String</code>. This method is used by this extensions XSL stylesheets to decode the image in
088       * the XML report.
089       */
090      public static String decodeBase64AndSaveAsPng(String encoded, String path) {
091        return decodeBase64AndSaveAsPng(encoded, path, imageDecoder, imageFileWriter);
092      }
093    
094      // makes testing easier
095      static String decodeBase64AndSaveAsPng(String encoded, String path, ImageDecoder decoder, ImageFileWriter writer) {
096        if (isEmpty(encoded)) return EMPTY_STRING;
097        if (isEmpty(path)) return EMPTY_STRING;
098        String realPath = path.replace("/", separator);
099        BufferedImage image = decodeBase64(encoded, decoder);
100        try {
101          writer.writeAsPng(image, realPath);
102        } catch (Exception ignored) {
103          logger.log(SEVERE, ignored.getMessage());
104        }
105        return EMPTY_STRING;
106      }
107    
108      private ImageHandler() {}
109    }