View Javadoc

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