View Javadoc

1   /*
2    * $Id: PropertyEditorTextToValueTransformer.java 20320 2010-11-24 15:03:31Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.DiscoverableTransformer;
14  import org.mule.api.transformer.TransformerException;
15  import org.mule.transformer.AbstractTransformer;
16  import org.mule.transformer.types.DataTypeFactory;
17  import org.mule.transformer.types.SimpleDataType;
18  
19  import java.beans.PropertyEditor;
20  
21  /**
22   * <code>PropertyEditorTextToValueTransformer</code> adapts a {@link PropertyEditor}
23   * instance allowing it to be used to transform from a String to another type in Mule
24   */
25  public class PropertyEditorTextToValueTransformer extends AbstractTransformer
26      implements DiscoverableTransformer
27  {
28  
29      private PropertyEditor propertyEditor;
30  
31      /**
32       * Give core transformers a slighty higher priority
33       */
34      private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING + 1;
35  
36      public PropertyEditorTextToValueTransformer(PropertyEditor propertyEditor, Class<?> clazz)
37      {
38          this.propertyEditor = propertyEditor;
39          registerSourceType(DataTypeFactory.STRING);
40          setReturnDataType(new SimpleDataType<Object>(clazz));
41      }
42  
43      @Override
44      public Object doTransform(Object src, String encoding) throws TransformerException
45      {
46          synchronized (propertyEditor)
47          {
48              propertyEditor.setAsText((String) src);
49              return propertyEditor.getValue();
50          }
51      }
52  
53      public int getPriorityWeighting()
54      {
55          return priorityWeighting;
56      }
57  
58      public void setPriorityWeighting(int priorityWeighting)
59      {
60          this.priorityWeighting = priorityWeighting;
61      }
62  
63  }