View Javadoc

1   /*
2    * $Id: BaseLocaleConverter.java,v 1.3 2004/04/04 18:27:44 eelco12 Exp $
3    * $Revision: 1.3 $
4    * $Date: 2004/04/04 18:27:44 $
5    *
6    * ====================================================================
7    * Copyright (c) 2003, Open Edge B.V.
8    * All rights reserved.
9    * Redistribution and use in source and binary forms, with or without 
10   * modification, are permitted provided that the following conditions are met:
11   * Redistributions of source code must retain the above copyright notice, 
12   * this list of conditions and the following disclaimer. Redistributions 
13   * in binary form must reproduce the above copyright notice, this list of 
14   * conditions and the following disclaimer in the documentation and/or other 
15   * materials provided with the distribution. Neither the name of OpenEdge B.V. 
16   * nor the names of its contributors may be used to endorse or promote products 
17   * derived from this software without specific prior written permission.
18   * 
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  
32  package nl.openedge.baritus.converters;
33  
34  import java.text.ParseException;
35  import java.util.Locale;
36  
37  /***
38   * base class for localized converters
39   * @author Eelco Hillenius
40   */
41  public abstract class BaseLocaleConverter implements LocaleConverter, Formatter
42  {
43  
44  	// ----------------------------------------------------- Instance Variables
45  
46  	/*** The locale specified to our Constructor, by default - system locale. */
47  	protected Locale locale = Locale.getDefault();
48  
49  	/*** The default pattern specified to our Constructor, if any. */
50  	protected String pattern = null;
51  
52  	/*** The flag indicating whether the given pattern string is localized or not. */
53  	protected boolean locPattern = false;
54  
55  	// ----------------------------------------------------------- Constructors
56  
57  	/***
58  	 * Create a {@link LocaleConverter} that will throw a {@link ConversionException}
59  	 * if a conversion error occurs.
60  	 * An unlocalized pattern is used for the convertion.
61  	 *
62  	 * @param locale        The locale
63  	 * @param pattern       The convertion pattern
64  	 */
65  	protected BaseLocaleConverter(Locale locale, String pattern)
66  	{
67  		this(locale, pattern, false);
68  	}
69  
70  
71  	/***
72  	 * Create a {@link LocaleConverter} that will throw a {@link ConversionException} 
73  	 * if an conversion error occurs.
74  	 *
75  	 * @param locale        The locale
76  	 * @param pattern       The convertion pattern
77  	 * @param locPattern    Indicate whether the pattern is localized or not
78  	 */
79  	protected BaseLocaleConverter(
80  		Locale locale,
81  		String pattern,
82  		boolean locPattern)
83  	{
84  
85  		if (locale != null)
86  		{
87  			this.locale = locale;
88  		}
89  
90  		this.pattern = pattern;
91  		this.locPattern = locPattern;
92  	}
93  
94  	// --------------------------------------------------------- Methods
95  
96  	/***
97  	 * Convert the specified locale-sensitive input object into an output object of the
98  	 * specified type.
99  	 *
100 	 * @param value The input object to be converted
101 	 * @param pattern The pattern is used for the convertion
102 	 *
103 	 * @exception ConversionException if conversion cannot be performed
104 	 *  successfully
105 	 */
106 
107 	abstract protected Object parse(Object value, String pattern)
108 		throws ParseException;
109 
110 	/***
111 	 * Convert the specified locale-sensitive input object into an output object.
112 	 * The default pattern is used for the convertion.
113 	 *
114 	 * @param value The input object to be converted
115 	 *
116 	 * @exception ConversionException if conversion cannot be performed
117 	 *  successfully
118 	 */
119 	public Object convert(Object value)
120 	
121 	{
122 		return convert(value, null);
123 	}
124 
125 	/***
126 	 * Convert the specified locale-sensitive input object into an output object.
127 	 *
128 	 * @param value The input object to be converted
129 	 * @param pattern The pattern is used for the convertion
130 	 *
131 	 * @exception ConversionException if conversion cannot be performed
132 	 *  successfully
133 	 */
134 	public Object convert(Object value, String pattern)
135 	{
136 		return convert(null, value, pattern);
137 	}
138 
139 	/***
140 	 * Convert the specified locale-sensitive input object into an output object of the
141 	 * specified type. The default pattern is used for the convertion.
142 	 *
143 	 * @param type Data type to which this value should be converted
144 	 * @param value The input object to be converted
145 	 *
146 	 * @exception ConversionException if conversion cannot be performed
147 	 *  successfully
148 	 */
149 	public Object convert(Class type, Object value)
150 	{
151 		return convert(type, value, null);
152 	}
153 
154 //	/***
155 //	 * Convert the specified locale-sensitive input object into an output object of the
156 //	 * specified type.
157 //	 *
158 //	 * @param type Data type to which this value should be converted
159 //	 * @param value The input object to be converted
160 //	 * @param pattern The pattern is used for the convertion
161 //	 *
162 //	 * @exception ConversionException if conversion cannot be performed successfully
163 //	 */
164 //	public Object convert(Class type, Object value, String pattern)
165 //	{
166 //		if (value == null)
167 //		{
168 //			return null;
169 //		}
170 //
171 //		try
172 //		{
173 //			if (pattern != null)
174 //			{
175 //				return parse(value, pattern);
176 //			}
177 //			else
178 //			{
179 //				return parse(value, this.pattern);
180 //			}
181 //		}
182 //		catch (Exception e)
183 //		{
184 //			throw new ConversionException(e);
185 //		}
186 //	}
187 
188 	/***
189 	 * get the locale
190 	 * @return Locale
191 	 */
192 	public Locale getLocale()
193 	{
194 		return locale;
195 	}
196 
197 	/***
198 	 * get the pattern
199 	 * @return String
200 	 */
201 	public String getPattern()
202 	{
203 		return pattern;
204 	}
205 
206 	/***
207 	 * set the locale
208 	 * @param locale
209 	 */
210 	public void setLocale(Locale locale)
211 	{
212 		this.locale = locale;
213 	}
214 
215 	/***
216 	 * set the pattern
217 	 * @param string
218 	 */
219 	public void setPattern(String string)
220 	{
221 		pattern = string;
222 	}
223 
224 }