1
2
3
4
5
6
7
8
9
10
11 package org.mule.transformer.simple;
12
13 import org.mule.api.transformer.TransformerException;
14 import org.mule.transformer.AbstractTransformer;
15 import org.mule.util.IOUtils;
16 import org.mule.util.StringUtils;
17
18 import java.io.InputStream;
19
20 public class StringAppendTransformer extends AbstractTransformer
21 {
22
23 private String message = StringUtils.EMPTY;
24
25 public StringAppendTransformer()
26 {
27 this(StringUtils.EMPTY);
28 }
29
30 public StringAppendTransformer(String message)
31 {
32 this.message = message;
33 setReturnClass(String.class);
34 registerSourceType(String.class);
35 registerSourceType(byte[].class);
36 registerSourceType(InputStream.class);
37 }
38
39 protected Object doTransform(Object src, String encoding) throws TransformerException
40 {
41 String string;
42 if (src instanceof byte[])
43 {
44 string = new String((byte[]) src);
45 }
46 else if (src instanceof InputStream)
47 {
48 InputStream input = (InputStream) src;
49 try
50 {
51 string = IOUtils.toString(input);
52 }
53 finally
54 {
55 IOUtils.closeQuietly(input);
56 }
57 }
58 else
59 {
60 string = (String) src;
61 }
62
63 return append(message, string);
64 }
65
66 public static String append(String append, String msg)
67 {
68 return msg + append;
69 }
70
71 public String getMessage()
72 {
73 return message;
74 }
75
76 public void setMessage(String message)
77 {
78 this.message = message;
79 }
80
81 }