View Javadoc

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