1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.morph.transform.converters;
17
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Locale;
22
23 /**
24 * <p>
25 * Converts a container to a textual representation (String or StringBuffer only).
26 * The string representation is comprised of a prefix, a textual representation
27 * of the array contents, and a suffix. The textual representation of the array
28 * contents is in turn made up of the string representation of each of the
29 * elements in the array separated by a separator character. Conversions to
30 * characters will only succeed if the result of the conversion is a single
31 * character in length.
32 * </p>
33 *
34 * <p>
35 * For example, if the prefix is <code>{</code>, the suffix is <code>}</code>,
36 * the separator is <code>,</code> and the contents of the array are the
37 * Integers <code>1</code>,<code>2</code> and <code>3</code>, the array
38 * will be converted to the text <code>{1,2,3}</code>.
39 * </p>
40 *
41 * @author Matt Sgarlata
42 * @since Dec 25, 2004
43 */
44 public class ContainerToPrettyTextConverter extends BaseToPrettyTextConverter {
45 /** Default prefix */
46 public static final String DEFAULT_PREFIX = "{";
47 /** Default suffix */
48 public static final String DEFAULT_SUFFIX = "}";
49 /** Default separator */
50 public static final String DEFAULT_SEPARATOR = ",";
51
52 /**
53 * Create a new ContainerToPrettyTextConverter.
54 */
55 public ContainerToPrettyTextConverter() {
56 setPrefix(DEFAULT_PREFIX);
57 setSuffix(DEFAULT_SUFFIX);
58 setSeparator(DEFAULT_SEPARATOR);
59 }
60
61 /**
62 * {@inheritDoc}
63 */
64 protected Object convertImpl(Class destinationClass, Object source,
65 Locale locale) throws Exception {
66
67 boolean endsInSeparator = false;
68 StringBuffer buffer = new StringBuffer();
69 if (getPrefix() != null) {
70 buffer.append(getPrefix());
71 }
72 Iterator iterator = getContainerReflector().getIterator(source);
73 while (iterator != null && iterator.hasNext()) {
74 Object next = iterator.next();
75 if (next != null || isShowNullValues()) {
76 String text = (String) getToTextConverter().convert(String.class,
77 next, locale);
78 buffer.append(text);
79 buffer.append(getSeparator());
80 endsInSeparator = true;
81 }
82 }
83 if (endsInSeparator) {
84 buffer.delete(buffer.length() - getSeparator().length(), buffer.length());
85 }
86 if (getSuffix() != null) {
87 buffer.append(getSuffix());
88 }
89 return getTextConverter().convert(destinationClass, buffer, locale);
90 }
91
92 /**
93 * {@inheritDoc}
94 */
95 protected Class[] getSourceClassesImpl() throws Exception {
96
97
98
99
100 List list = null;
101 Class[] reflectableClasses = getContainerReflector().getReflectableClasses();
102 for (int i = 0; i < reflectableClasses.length; i++) {
103 if (reflectableClasses[i] == Object.class) {
104 if (list == null) {
105 list = new ArrayList(reflectableClasses.length - 1);
106 for (int j = 0; j < i; j++) {
107 list.add(j, reflectableClasses[j]);
108 }
109 }
110 } else if (list != null) {
111 list.add(reflectableClasses[i]);
112 }
113 }
114 return list == null ? reflectableClasses : (Class[]) list.toArray(new Class[list.size()]);
115 }
116
117 }