View Javadoc

1   /*
2    * $Id: StringAppendTransformer.java 11316 2008-03-11 15:50:38Z 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.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  }