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