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.DataType;
10  import org.mule.api.transformer.DiscoverableTransformer;
11  import org.mule.api.transformer.TransformerException;
12  import org.mule.config.i18n.CoreMessages;
13  import org.mule.transformer.AbstractTransformer;
14  import org.mule.transformer.types.DataTypeFactory;
15  import org.mule.transformer.types.SimpleDataType;
16  
17  /**
18   * <code>ByteArrayToSerializable</code> converts a serialized object to its object
19   * representation
20   */
21  public class StringToBoolean extends AbstractTransformer implements DiscoverableTransformer
22  {
23  
24      /**
25       * Give core transformers a slighty higher priority
26       */
27      private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING + 1;
28  
29      public StringToBoolean()
30      {
31          registerSourceType(new SimpleDataType<Object>(String.class));
32          setReturnDataType(DataTypeFactory.create(Boolean.class));
33      }
34  
35      @Override
36      public Object doTransform(Object src, String encoding) throws TransformerException
37      {
38          if (src == null)
39          {
40              if (isAllowNullReturn())
41              {
42                  return null;
43              }
44              else
45              {
46                  throw new TransformerException(
47                      CoreMessages.createStaticMessage("Unable to transform null to a primitive"));
48              }
49          }
50          else
51          {
52              return Boolean.valueOf((String) src);
53          }
54      }
55  
56      @Override
57      public void setReturnDataType(DataType<?> type)
58      {
59          if (!Boolean.class.isAssignableFrom(type.getType()))
60          {
61              throw new IllegalArgumentException("This transformer only supports Boolean return types.");
62          }
63          else
64          {
65              super.setReturnDataType(type);
66          }
67      }
68  
69      public int getPriorityWeighting()
70      {
71          return priorityWeighting;
72      }
73  
74      public void setPriorityWeighting(int priorityWeighting)
75      {
76          this.priorityWeighting = priorityWeighting;
77      }
78  
79  }