001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.beanutils.locale;
019:
020: import org.apache.commons.beanutils.ConversionException;
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023:
024: import java.text.ParseException;
025: import java.util.Locale;
026:
027: /**
028: * <p>The base class for all standart type locale-sensitive converters.
029: * It has {@link LocaleConverter} and {@link org.apache.commons.beanutils.Converter} implementations,
030: * that convert an incoming locale-sensitive Object into an object of correspond type,
031: * optionally using a default value or throwing a {@link ConversionException}
032: * if a conversion error occurs.</p>
033: *
034: * @author Yauheny Mikulski
035: */
036:
037: public abstract class BaseLocaleConverter implements LocaleConverter {
038:
039: // ----------------------------------------------------- Instance Variables
040:
041: /** All logging goes through this logger */
042: private Log log = LogFactory.getLog(BaseLocaleConverter.class);
043:
044: /** The default value specified to our Constructor, if any. */
045: private Object defaultValue = null;
046:
047: /** Should we return the default value on conversion errors? */
048: protected boolean useDefault = false;
049:
050: /** The locale specified to our Constructor, by default - system locale. */
051: protected Locale locale = Locale.getDefault();
052:
053: /** The default pattern specified to our Constructor, if any. */
054: protected String pattern = null;
055:
056: /** The flag indicating whether the given pattern string is localized or not. */
057: protected boolean locPattern = false;
058:
059: // ----------------------------------------------------------- Constructors
060:
061: /**
062: * Create a {@link LocaleConverter} that will throw a {@link ConversionException}
063: * if a conversion error occurs.
064: * An unlocalized pattern is used for the convertion.
065: *
066: * @param locale The locale
067: * @param pattern The convertion pattern
068: */
069: protected BaseLocaleConverter(Locale locale, String pattern) {
070:
071: this (null, locale, pattern, false, false);
072: }
073:
074: /**
075: * Create a {@link LocaleConverter} that will throw a {@link ConversionException}
076: * if a conversion error occurs.
077: *
078: * @param locale The locale
079: * @param pattern The convertion pattern
080: * @param locPattern Indicate whether the pattern is localized or not
081: */
082: protected BaseLocaleConverter(Locale locale, String pattern,
083: boolean locPattern) {
084:
085: this (null, locale, pattern, false, locPattern);
086: }
087:
088: /**
089: * Create a {@link LocaleConverter} that will return the specified default value
090: * if a conversion error occurs.
091: * An unlocalized pattern is used for the convertion.
092: *
093: * @param defaultValue The default value to be returned
094: * @param locale The locale
095: * @param pattern The convertion pattern
096: */
097: protected BaseLocaleConverter(Object defaultValue, Locale locale,
098: String pattern) {
099:
100: this (defaultValue, locale, pattern, false);
101: }
102:
103: /**
104: * Create a {@link LocaleConverter} that will return the specified default value
105: * if a conversion error occurs.
106: *
107: * @param defaultValue The default value to be returned
108: * @param locale The locale
109: * @param pattern The convertion pattern
110: * @param locPattern Indicate whether the pattern is localized or not
111: */
112: protected BaseLocaleConverter(Object defaultValue, Locale locale,
113: String pattern, boolean locPattern) {
114:
115: this (defaultValue, locale, pattern, true, locPattern);
116: }
117:
118: /**
119: * Create a {@link LocaleConverter} that will return the specified default value
120: * or throw a {@link ConversionException} if a conversion error occurs.
121: *
122: * @param defaultValue The default value to be returned
123: * @param locale The locale
124: * @param pattern The convertion pattern
125: * @param useDefault Indicate whether the default value is used or not
126: * @param locPattern Indicate whether the pattern is localized or not
127: */
128: private BaseLocaleConverter(Object defaultValue, Locale locale,
129: String pattern, boolean useDefault, boolean locPattern) {
130:
131: if (useDefault) {
132: this .defaultValue = defaultValue;
133: this .useDefault = true;
134: }
135:
136: if (locale != null) {
137: this .locale = locale;
138: }
139:
140: this .pattern = pattern;
141: this .locPattern = locPattern;
142: }
143:
144: // --------------------------------------------------------- Methods
145:
146: /**
147: * Convert the specified locale-sensitive input object into an output object of the
148: * specified type.
149: *
150: * @param value The input object to be converted
151: * @param pattern The pattern is used for the convertion
152: * @return The converted value
153: *
154: * @exception ParseException if conversion cannot be performed
155: * successfully
156: */
157:
158: abstract protected Object parse(Object value, String pattern)
159: throws ParseException;
160:
161: /**
162: * Convert the specified locale-sensitive input object into an output object.
163: * The default pattern is used for the convertion.
164: *
165: * @param value The input object to be converted
166: * @return The converted value
167: *
168: * @exception ConversionException if conversion cannot be performed
169: * successfully
170: */
171: public Object convert(Object value) {
172: return convert(value, null);
173: }
174:
175: /**
176: * Convert the specified locale-sensitive input object into an output object.
177: *
178: * @param value The input object to be converted
179: * @param pattern The pattern is used for the convertion
180: * @return The converted value
181: *
182: * @exception ConversionException if conversion cannot be performed
183: * successfully
184: */
185: public Object convert(Object value, String pattern) {
186: return convert(null, value, pattern);
187: }
188:
189: /**
190: * Convert the specified locale-sensitive input object into an output object of the
191: * specified type. The default pattern is used for the convertion.
192: *
193: * @param type Data type to which this value should be converted
194: * @param value The input object to be converted
195: * @return The converted value
196: *
197: * @exception ConversionException if conversion cannot be performed
198: * successfully
199: */
200: public Object convert(Class type, Object value) {
201: return convert(type, value, null);
202: }
203:
204: /**
205: * Convert the specified locale-sensitive input object into an output object of the
206: * specified type.
207: *
208: * @param type Data is type to which this value should be converted
209: * @param value is the input object to be converted
210: * @param pattern is the pattern is used for the conversion; if null is
211: * passed then the default pattern associated with the converter object
212: * will be used.
213: * @return The converted value
214: *
215: * @exception ConversionException if conversion cannot be performed
216: * successfully
217: */
218: public Object convert(Class type, Object value, String pattern) {
219: if (value == null) {
220: if (useDefault) {
221: return (defaultValue);
222: } else {
223: // symmetric beanutils function allows null
224: // so do not: throw new ConversionException("No value specified");
225: log
226: .debug("Null value specified for conversion, returing null");
227: return null;
228: }
229: }
230:
231: try {
232: if (pattern != null) {
233: return parse(value, pattern);
234: } else {
235: return parse(value, this .pattern);
236: }
237: } catch (Exception e) {
238: if (useDefault) {
239: return (defaultValue);
240: } else {
241: if (e instanceof ConversionException) {
242: throw (ConversionException) e;
243: }
244: throw new ConversionException(e);
245: }
246: }
247: }
248: }
|