Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
StringToObjectArray |
|
| 1.75;1.75 |
1 | /* | |
2 | * $Id: StringToObjectArray.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.StringUtils; | |
16 | ||
17 | /** | |
18 | * <code>StringToObjectArray</code> converts a String into an object array. This | |
19 | * is useful in certain situations, as when a string needs to be converted into | |
20 | * an Object[] in order to be passed to a SOAP service. The input String is parsed | |
21 | * into the array based on a configurable delimiter - default is a space. | |
22 | */ | |
23 | ||
24 | public class StringToObjectArray extends AbstractTransformer | |
25 | { | |
26 | 10 | private String delimiter = null; |
27 | private static final String DEFAULT_DELIMITER = " "; | |
28 | ||
29 | public StringToObjectArray() | |
30 | 10 | { |
31 | 16 | registerSourceType(String.class); |
32 | 10 | registerSourceType(byte[].class); |
33 | 10 | setReturnClass(Object[].class); |
34 | 10 | } |
35 | ||
36 | public Object doTransform(Object src, String encoding) throws TransformerException | |
37 | { | |
38 | String in; | |
39 | ||
40 | 6 | if (src instanceof byte[]) |
41 | { | |
42 | 0 | in = new String((byte[])src); |
43 | } | |
44 | else | |
45 | { | |
46 | 6 | in = (String)src; |
47 | } | |
48 | ||
49 | 6 | String[] out = StringUtils.splitAndTrim(in, getDelimiter()); |
50 | 6 | return out; |
51 | } | |
52 | ||
53 | /** | |
54 | * @return the delimiter | |
55 | */ | |
56 | public String getDelimiter() | |
57 | { | |
58 | 8 | if (delimiter == null) |
59 | 8 | return DEFAULT_DELIMITER; |
60 | else | |
61 | 0 | return delimiter; |
62 | } | |
63 | ||
64 | /** | |
65 | * @param delimiter the delimiter | |
66 | */ | |
67 | public void setDelimiter(String delimiter) | |
68 | { | |
69 | 2 | this.delimiter = delimiter; |
70 | 2 | } |
71 | ||
72 | } |