View Javadoc
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.transformer.simple;
8   
9   import org.mule.api.transformer.DiscoverableTransformer;
10  import org.mule.api.transformer.TransformerException;
11  import org.mule.transformer.AbstractTransformer;
12  import org.mule.transformer.types.DataTypeFactory;
13  import org.mule.transformer.types.SimpleDataType;
14  
15  import java.beans.PropertyEditor;
16  
17  /**
18   * <code>PropertyEditorValueToTextTransformer</code> adapts a {@link PropertyEditor}
19   * instance allowing it to be used to transform from a specific type to a String.
20   */
21  public class PropertyEditorValueToTextTransformer extends AbstractTransformer implements DiscoverableTransformer
22  {
23  
24      private PropertyEditor propertyEditor;
25  
26      /**
27       * Give core transformers a slighty higher priority
28       */
29      private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING + 1;
30  
31      public PropertyEditorValueToTextTransformer(PropertyEditor propertyEditor, Class<?> clazz)
32      {
33          registerSourceType(new SimpleDataType<Object>(clazz));
34          setReturnDataType(DataTypeFactory.STRING);
35          this.propertyEditor = propertyEditor;
36      }
37  
38      @Override
39      public Object doTransform(Object src, String encoding) throws TransformerException
40      {
41          synchronized (propertyEditor)
42          {
43              propertyEditor.setValue(src);
44              return propertyEditor.getAsText();
45          }
46      }
47  
48      public int getPriorityWeighting()
49      {
50          return priorityWeighting;
51      }
52  
53      public void setPriorityWeighting(int priorityWeighting)
54      {
55          this.priorityWeighting = priorityWeighting;
56      }
57  
58  }