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