| 1 |
package com.jidesoft.converter; |
| 2 |
|
| 3 |
public class StringArrayConverter implements ObjectConverter { |
| 4 |
public static final ConverterContext CONTEXT = new ConverterContext("StringArrayConverter"); |
| 5 |
private String _separator = ";"; |
| 6 |
|
| 7 |
/** |
| 8 |
* Creates a StringArrayConverter using default constructor. The semicolon (";") will be used as the separator when |
| 9 |
* converting from string to array and vise versa. |
| 10 |
*/ |
| 11 |
public StringArrayConverter() { |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Creates a StringArrayConverter with a specified separator. Please make sure the separator is not used in the |
| 16 |
* character set used in each string element. For example, you want to use space as separator, then each string in |
| 17 |
* the string array must not use space. |
| 18 |
* |
| 19 |
* @param separator the separator used to separate string to an array. |
| 20 |
*/ |
| 21 |
public StringArrayConverter(String separator) { |
| 22 |
_separator = separator; |
| 23 |
} |
| 24 |
|
| 25 |
public String toString(Object object, ConverterContext context) { |
| 26 |
if (object == null) { |
| 27 |
return null; |
| 28 |
} |
| 29 |
if (object.getClass().isArray()) { |
| 30 |
String[] array = (String[]) object; |
| 31 |
StringBuffer b = new StringBuffer(); |
| 32 |
for (int i = 0; i < array.length; i++) { |
| 33 |
if (i > 0) b.append(_separator); |
| 34 |
b.append(array[i]); |
| 35 |
} |
| 36 |
return b.toString(); |
| 37 |
} |
| 38 |
return null; |
| 39 |
} |
| 40 |
|
| 41 |
public boolean supportToString(Object object, ConverterContext context) { |
| 42 |
return true; |
| 43 |
} |
| 44 |
|
| 45 |
public Object fromString(String string, ConverterContext context) { |
| 46 |
if (string.length() == 0) { |
| 47 |
return new String[0]; |
| 48 |
} |
| 49 |
return string.split(_separator); |
| 50 |
} |
| 51 |
|
| 52 |
public boolean supportFromString(String string, ConverterContext context) { |
| 53 |
return true; |
| 54 |
} |
| 55 |
} |