View Javadoc
1   package com.opencsv.bean.customconverter;
2   
3   import com.opencsv.bean.AbstractBeanField;
4   import com.opencsv.exceptions.CsvDataTypeMismatchException;
5   import org.apache.commons.beanutils.ConversionException;
6   import org.apache.commons.beanutils.Converter;
7   import org.apache.commons.beanutils.converters.BooleanConverter;
8   import org.apache.commons.lang3.StringUtils;
9   
10  import java.util.ResourceBundle;
11  
12  /**
13   * A base class for any converter to and from booleans when the string
14   * values have been or should be localized to a specific language.
15   * @param <T> Type of the bean to be manipulated
16   * @param <I> Type of the index into multivalued fields
17   * @author Andrew Rucker Jones
18   */
19  abstract public class ConverterLanguageToBoolean<T, I> extends AbstractBeanField<T, I> {
20  
21      /**
22       * This is the string for "true" in the localized language.
23       * This value will be used on converting from a boolean to a string.
24       *
25       * @return The canonical name of {@code true} in this language
26       */
27      abstract protected String getLocalizedTrue();
28  
29      /**
30       * This is the string for "false" in the localized language.
31       * This value will be used on converting from a boolean to a string.
32       *
33       * @return The canonical name of {@code false} in this language
34       */
35      abstract protected String getLocalizedFalse();
36  
37      /**
38       * This represents a list of all values accepted as "true".
39       * Any language will have more than one way to say "true", such as
40       * "yes", "y", or "1". This array should list all possibilities.
41       * Comparison is done in a case-insensitive fashion.
42       *
43       * @return An array of all "true" strings
44       */
45      abstract protected String[] getAllLocalizedTrueValues();
46  
47      /**
48       * This represents a list of all values accepted as "false".
49       * Any language will have more than one way to say "false", such as
50       * "no", "n", or "0". This array should list all possibilities.
51       * Comparison is done in a case-insensitive fashion.
52       *
53       * @return An array of all "false" strings
54       */
55      abstract protected String[] getAllLocalizedFalseValues();
56  
57      /**
58       * Converts localized text into a {@link Boolean}.
59       * The comparisons are case-insensitive.
60       *
61       * @param value String that should represent a Boolean
62       * @return Boolean
63       * @throws CsvDataTypeMismatchException   If anything other than the
64       *                                        explicitly translated pairs is found
65       */
66      @Override
67      protected Object convert(String value)
68              throws CsvDataTypeMismatchException {
69          if (StringUtils.isEmpty(value)) {
70              return null;
71          }
72          Converter bc = new BooleanConverter(getAllLocalizedTrueValues(), getAllLocalizedFalseValues());
73          try {
74              return bc.convert(Boolean.class, value.trim());
75          } catch (ConversionException e) {
76              CsvDataTypeMismatchException csve = new CsvDataTypeMismatchException(
77                      value, field.getType(), ResourceBundle
78                      .getBundle("convertLanguageToBoolean", errorLocale)
79                      .getString("input.not.boolean"));
80              csve.initCause(e);
81              throw csve;
82          }
83      }
84  
85      /**
86       * This method takes the current value of the field in question in the bean
87       * passed in and converts it to a string.
88       * This implementation returns true/false values in the localized language.
89       *
90       * @return Localized text value for "true" or "false"
91       * @throws CsvDataTypeMismatchException If the field is not a {@code boolean}
92       *   or {@link Boolean}
93       */
94      @Override
95      protected String convertToWrite(Object value)
96              throws CsvDataTypeMismatchException {
97          String result = "";
98          try {
99              if(value != null) {
100                 Boolean b = (Boolean) value;
101                 result = b? getLocalizedTrue() : getLocalizedFalse();
102             }
103         }
104         catch(ClassCastException e) {
105             CsvDataTypeMismatchException csve =
106                     new CsvDataTypeMismatchException(ResourceBundle
107                             .getBundle("convertLanguageToBoolean", errorLocale)
108                             .getString("field.not.boolean"));
109             csve.initCause(e);
110             throw csve;
111         }
112         return result;
113     }
114 }