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.Locale;
19
20 import net.sf.morph.Defaults;
21 import net.sf.morph.transform.Converter;
22 import net.sf.morph.transform.DecoratedConverter;
23 import net.sf.morph.transform.ImpreciseTransformer;
24 import net.sf.morph.transform.TransformationException;
25 import net.sf.morph.transform.transformers.BaseTransformer;
26 import net.sf.morph.util.TransformerUtils;
27
28 /**
29 * Converts boolean values to text values. Subclasses can build in support for
30 * locale-sensitive translations of true and false to text values by overriding
31 * {@link BooleanToTextConverter#getTrueString(Locale)} and
32 * {@link BooleanToTextConverter#getFalseString(Locale)}.
33 *
34 * @author Matt Sgarlata
35 * @since Jan 9, 2005
36 */
37 public class BooleanToTextConverter extends BaseTransformer implements DecoratedConverter, ImpreciseTransformer {
38
39 private static final Class[] SOURCE_TYPES = { Boolean.class, boolean.class };
40
41 private Converter textConverter;
42
43 /**
44 * {@inheritDoc}
45 */
46 protected Object convertImpl(Class destinationClass, Object source,
47 Locale locale) throws Exception {
48
49 String string;
50 if (source.equals(Boolean.TRUE)) {
51 string = getTrueString(locale);
52 }
53 else if (source.equals(Boolean.FALSE)) {
54 string = getFalseString(locale);
55 }
56 else {
57 throw new TransformationException(destinationClass, source);
58 }
59
60 return getTextConverter().convert(destinationClass, string, locale);
61 }
62
63 /**
64 * {@inheritDoc}
65 */
66 protected Class[] getSourceClassesImpl() throws Exception {
67 return SOURCE_TYPES;
68 }
69
70 /**
71 * {@inheritDoc}
72 */
73 protected Class[] getDestinationClassesImpl() throws Exception {
74 return getTextConverter().getDestinationClasses();
75 }
76
77 /**
78 * Get the string representation of <code>true</code>.
79 * @param locale
80 * @return String
81 */
82 public String getTrueString(Locale locale) {
83 return Boolean.TRUE.toString();
84 }
85
86 /**
87 * Get the string representation of <code>false</code>.
88 * @param locale
89 * @return String
90 */
91 public String getFalseString(Locale locale) {
92 return Boolean.FALSE.toString();
93 }
94
95 /**
96 * {@inheritDoc}
97 */
98 protected boolean isImpreciseTransformationImpl(Class destinationClass, Class sourceClass) {
99 return TransformerUtils.isImpreciseTransformation(getTextConverter(), destinationClass, String.class);
100 }
101
102 /**
103 * {@inheritDoc}
104 */
105 protected boolean isWrappingRuntimeExceptions() {
106 return true;
107 }
108
109 /**
110 * Get the text converter used by this BaseToPrettyTextConverter.
111 * @return Converter
112 */
113 public Converter getTextConverter() {
114 if (textConverter == null) {
115 setTextConverter(Defaults.createTextConverter());
116 }
117 return textConverter;
118 }
119
120 /**
121 * Set the text converter used by this BaseToPrettyTextConverter.
122 * @param textConverter
123 */
124 public void setTextConverter(Converter textConverter) {
125 this.textConverter = textConverter;
126 }
127 }