View Javadoc

1   /*
2    * $Id: HexStringToByteArray.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.transformers.simple;
12  
13  import org.mule.transformers.AbstractTransformer;
14  import org.mule.umo.transformer.TransformerException;
15  import org.mule.util.ArrayUtils;
16  import org.mule.util.StringUtils;
17  
18  /**
19   * Converts a Hex String to a Byte array
20   */
21  public class HexStringToByteArray extends AbstractTransformer
22  {
23  
24      public HexStringToByteArray()
25      {
26          registerSourceType(String.class);
27          setReturnClass(byte[].class);
28      }
29  
30      protected Object doTransform(Object src, String encoding) throws TransformerException
31      {
32          if (src == null)
33          {
34              return ArrayUtils.EMPTY_BYTE_ARRAY;
35          }
36  
37          try
38          {
39              return StringUtils.hexStringToByteArray((String) src);
40          }
41          catch (Exception ex)
42          {
43              throw new TransformerException(this, ex);
44          }
45      }
46  
47  }