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>PropertyEditorTextToValueTransformer</code> adapts a {@link PropertyEditor}
19   * instance allowing it to be used to transform from a String to another type in Mule
20   */
21  public class PropertyEditorTextToValueTransformer extends AbstractTransformer
22      implements DiscoverableTransformer
23  {
24  
25      private PropertyEditor propertyEditor;
26  
27      /**
28       * Give core transformers a slighty higher priority
29       */
30      private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING + 1;
31  
32      public PropertyEditorTextToValueTransformer(PropertyEditor propertyEditor, Class<?> clazz)
33      {
34          this.propertyEditor = propertyEditor;
35          registerSourceType(DataTypeFactory.STRING);
36          setReturnDataType(new SimpleDataType<Object>(clazz));
37      }
38  
39      @Override
40      public Object doTransform(Object src, String encoding) throws TransformerException
41      {
42          synchronized (propertyEditor)
43          {
44              propertyEditor.setAsText((String) src);
45              return propertyEditor.getValue();
46          }
47      }
48  
49      public int getPriorityWeighting()
50      {
51          return priorityWeighting;
52      }
53  
54      public void setPriorityWeighting(int priorityWeighting)
55      {
56          this.priorityWeighting = priorityWeighting;
57      }
58  
59  }