1 /* 2 * $Id: FileToString.java 7976 2007-08-21 14:26:13Z dirk.olmes $ 3 * -------------------------------------------------------------------------------------- 4 * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com 5 * 6 * The software in this package is published under the terms of the CPAL v1.0 7 * license, a copy of which has been included with this distribution in the 8 * LICENSE.txt file. 9 */ 10 11 package org.mule.providers.file.transformers; 12 13 import org.mule.umo.transformer.TransformerException; 14 15 import java.io.File; 16 import java.io.UnsupportedEncodingException; 17 18 /** 19 * <code>FileToString</code> reads file contents into a string. 20 */ 21 public class FileToString extends FileToByteArray 22 { 23 24 public FileToString() 25 { 26 registerSourceType(File.class); 27 registerSourceType(byte[].class); 28 setReturnClass(String.class); 29 } 30 31 /** 32 * Simple implementation which relies on {@link FileToByteArray} to get a 33 * <code>byte[]</code> from the file beeing parsed and then transform it to a 34 * String with the correct encoding. If the encoding isn't supported simply throw 35 * an exception, good tranformation or no transformation at all. NOTE: if a 36 * <code>byte[]</code> is passed in as a source object this transformer accepts 37 * it and tries the usual transformation. 38 */ 39 // @Override 40 public Object doTransform(Object src, String encoding) throws TransformerException 41 { 42 byte[] bytes; 43 44 if (src instanceof byte[]) 45 { 46 bytes = (byte[])src; 47 } 48 else 49 { 50 bytes = (byte[])super.doTransform(src, encoding); 51 } 52 53 try 54 { 55 return new String(bytes, encoding); 56 } 57 catch (UnsupportedEncodingException uee) 58 { 59 throw new TransformerException(this, uee); 60 } 61 } 62 63 }