KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > file > transformers > FileToString


1 /*
2  * $Id: FileToString.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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 java.io.File JavaDoc;
14 import java.io.UnsupportedEncodingException JavaDoc;
15
16 import org.mule.umo.transformer.TransformerException;
17
18 /**
19  * <code>FileToString</code> reads file contents into a string.
20  */

21 public class FileToString extends FileToByteArray
22 {
23     /**
24      * Serial version
25      */

26     private static final long serialVersionUID = -5376852963195290731L;
27
28     public FileToString()
29     {
30         registerSourceType(File JavaDoc.class);
31         registerSourceType(byte[].class);
32         setReturnClass(String JavaDoc.class);
33     }
34
35     /**
36      * Simple implementation which relies on {@link FileToByteArray} to get a
37      * <code>byte[]</code> from the file beeing parsed and then transform it to a
38      * String with the correct encoding. If the encoding isn't supported simply throw
39      * an exception, good tranformation or no trasfromation at all. NOTE: if a
40      * <code>byte[]</code> is passed in as a source object this transformer accept
41      * it an try the usual transformation.
42      */

43     public Object JavaDoc doTransform(Object JavaDoc src, String JavaDoc encoding) throws TransformerException
44     {
45         byte[] bytes;
46
47         if (src instanceof byte[])
48         {
49             bytes = (byte[])src;
50         }
51         else
52         {
53             bytes = (byte[])super.doTransform(src, encoding);
54         }
55
56         try
57         {
58             return new String JavaDoc(bytes, encoding);
59         }
60         catch (UnsupportedEncodingException JavaDoc uee)
61         {
62             throw new TransformerException(this, uee);
63         }
64     }
65 }
66
Popular Tags