View Javadoc

1   /*
2    * Copyright 2004-2005, 2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.morph.transform.converters;
17  
18  import java.util.Locale;
19  
20  import net.sf.composite.util.ObjectUtils;
21  import net.sf.morph.Defaults;
22  import net.sf.morph.transform.Converter;
23  import net.sf.morph.transform.DecoratedConverter;
24  import net.sf.morph.transform.TransformationException;
25  import net.sf.morph.transform.transformers.BaseTransformer;
26  import net.sf.morph.util.ContainerUtils;
27  
28  /**
29   * Converts text values to Booleans.  Text values include Characters, Strings and
30   * StringBuffers.
31   * 
32   * @author Matt Sgarlata
33   * @since Dec 31, 2004
34   */
35  public class TextToBooleanConverter extends BaseTransformer implements DecoratedConverter {
36  	private Converter textConverter;
37  
38  	private static final Class[] DESTINATION_TYPES = { Boolean.class,
39  		boolean.class };	
40  
41  	/**
42  	 * Default values for the <code>trueText</code> attribute.
43  	 */
44  	public static final String[] DEFAULT_TRUE_TEXT = { "true", "t", "yes", "y" };
45  
46  	/**
47  	 * Default values for the <code>falseText</code> attribute.
48  	 */
49  	public static final String[] DEFAULT_FALSE_TEXT = { "false", "f", "no", "n" };
50  
51  	/**
52  	 * Defines the String, StringBuffer, and Character values that will be
53  	 * converted to <code>true</code>. The strings are not case-sensitive.
54  	 */
55  	private String[] trueText;
56  	/**
57  	 * Defines the String, StringBuffer, and Character values that will be
58  	 * converted to <code>false</code>. The strings are not case-sensitive.
59  	 */
60  	private String[] falseText;
61  
62  	/**
63  	 * {@inheritDoc}
64  	 */
65  	protected Object convertImpl(Class destinationClass, Object source,
66  		Locale locale) throws Exception {
67  
68  		String str = (String) getTextConverter().convert(String.class, source, locale);
69  		if (str != null) {
70  			str = str.toLowerCase();
71  		}
72  		if (ContainerUtils.contains(getTrueText(), str)) {
73  			return Boolean.TRUE;
74  		}
75  		if (ContainerUtils.contains(getFalseText(), str)) {
76  			return Boolean.FALSE;
77  		}
78  		if (ObjectUtils.isEmpty(str) && !destinationClass.isPrimitive()) {
79  			return null;
80  		}
81  		throw new TransformationException(destinationClass, source);
82  	}
83  
84  	/**
85  	 * {@inheritDoc}
86  	 */
87  	protected Class[] getSourceClassesImpl() throws Exception {
88  		return getTextConverter().getSourceClasses();
89  	}
90  
91  	/**
92  	 * {@inheritDoc}
93  	 */
94  	protected Class[] getDestinationClassesImpl() throws Exception {
95  		return DESTINATION_TYPES;
96  	}
97  
98  	/**
99  	 * Returns the String, StringBuffer, and Character values that will be
100 	 * converted to <code>false</code>. The strings are not case-sensitive.
101 	 * 
102 	 * @return the String, StringBuffer, and Character values that will be
103 	 *         converted to <code>false</code>. The strings are not
104 	 *         case-sensitive.
105 	 */
106 	public String[] getFalseText() {
107 		if (ObjectUtils.isEmpty(falseText)) {
108 			setFalseText(DEFAULT_FALSE_TEXT);
109 		}
110 		return falseText;
111 	}
112 
113 	/**
114 	 * Sets the values that will be converted to <code>false</code>. The
115 	 * strings are not case-sensitive.
116 	 * 
117 	 * @param falseText
118 	 *            the values that will be converted to <code>false</code>.
119 	 *            The strings are not case-sensitive.
120 	 */
121 	public void setFalseText(String[] falseStrings) {
122 		falseStrings = changeToLowerCase(falseStrings);
123 		this.falseText = falseStrings;
124 	}
125 
126 	/**
127 	 * Returns the String, StringBuffer, and Character values that will be
128 	 * converted to <code>true</code>. The strings are not case-sensitive.
129 	 * 
130 	 * @return the String, StringBuffer, and Character values that will be
131 	 *         converted to <code>true</code>. The strings are not
132 	 *         case-sensitive.
133 	 */
134 	public String[] getTrueText() {
135 		if (ObjectUtils.isEmpty(trueText)) {
136 			setTrueText(DEFAULT_TRUE_TEXT);
137 		}
138 		return trueText;
139 	}
140 
141 	/**
142 	 * Sets the values that will be converted to <code>true</code>. The
143 	 * strings are not case-sensitive.
144 	 * 
145 	 * @param trueText
146 	 *            the values that will be converted to <code>true</code>. The
147 	 *            strings are not case-sensitive.
148 	 */
149 	public void setTrueText(String[] trueStrings) {
150 		trueStrings = changeToLowerCase(trueStrings);
151 		this.trueText = trueStrings;
152 	}
153 
154 	/**
155 	 * {@inheritDoc}
156 	 */
157 	protected boolean isAutomaticallyHandlingNulls() {
158 		return false;
159 	}
160 
161 	/**
162 	 * {@inheritDoc}
163 	 */
164 	protected boolean isWrappingRuntimeExceptions() {
165 	    return true;
166     }
167 
168 	/**
169 	 * Get an array of Strings obtained by converting contents of <code>array</code> to lowercase.
170 	 * @param array
171 	 * @return String[]
172 	 */
173 	private String[] changeToLowerCase(String[] array) {
174 		if (array == null) {
175 			return null;
176 		}
177 
178 		String[] lowerCase = new String[array.length];
179 		for (int i = 0; i < array.length; i++) {
180 			lowerCase[i] = array[i].toLowerCase();
181 		}
182 		return lowerCase;
183 	}
184 
185 	/**
186 	 * Get the text converter of this TextToBooleanConverter.
187 	 * @return Converter
188 	 */
189 	public Converter getTextConverter() {
190 		if (textConverter == null) {
191 			setTextConverter(Defaults.createTextConverter());
192 		}
193 		return textConverter;
194 	}
195 
196 	/**
197 	 * Set the text converter of this TextToBooleanConverter.
198 	 * @param textConverter the Converter to set
199 	 */
200 	public void setTextConverter(Converter textConverter) {
201 		this.textConverter = textConverter;
202 	}
203 
204 }