Line | Hits | Source |
---|---|---|
1 | /* | |
2 | Chrysalis Web Framework [http://chrysalis.sourceforge.net] | |
3 | Copyright (c) 2002, 2003, 2004, Paul Strack | |
4 | ||
5 | All rights reserved. | |
6 | ||
7 | Redistribution and use in source and binary forms, with or without | |
8 | modification, are permitted provided that the following conditions are met: | |
9 | ||
10 | 1. Redistributions of source code must retain the above copyright notice, this | |
11 | list of conditions and the following disclaimer. | |
12 | ||
13 | 2. Redistributions in binary form must reproduce the above copyright notice, | |
14 | this list of conditions and the following disclaimer in the documentation | |
15 | and/or other materials provided with the distribution. | |
16 | ||
17 | 3. Neither the name of the copyright holder nor the names of its contributors | |
18 | may be used to endorse or promote products derived from this software without | |
19 | specific prior written permission. | |
20 | ||
21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
28 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | */ | |
32 | ||
33 | package org.chwf.i18n; | |
34 | ||
35 | import java.text.DateFormat; | |
36 | import java.text.MessageFormat; | |
37 | import java.text.NumberFormat; | |
38 | import java.text.ParseException; | |
39 | import java.util.Date; | |
40 | ||
41 | /** | |
42 | * Class with static localizer methods. They derive the locale from | |
43 | * {@link UserLocale#getLocale()}. | |
44 | * | |
45 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
46 | */ | |
47 | public abstract class Localizer { | |
48 | ||
49 | /** Private constructor to prevent creation. */ | |
50 | 0 | private Localizer() { |
51 | 0 | } |
52 | ||
53 | /** | |
54 | * Format message pattern using arguments.<p> | |
55 | * | |
56 | * @param pattern The message pattern. | |
57 | * @param args The arguments. | |
58 | * @return The pattern with arguments inserted. | |
59 | */ | |
60 | public static String format(String pattern, Object[] args) { | |
61 | 59 | if ((args.length > 0) && (pattern.indexOf('{') >= 0)) { |
62 | 46 | MessageFormat formatter = getMessageFormat(pattern); |
63 | 46 | formatter.setLocale(UserLocale.getLocale()); |
64 | 46 | return formatter.format(args); |
65 | } else { | |
66 | 13 | return pattern; |
67 | } | |
68 | } | |
69 | ||
70 | /** | |
71 | * Parse the string value as a number.<p> | |
72 | * | |
73 | * @param value The value. | |
74 | * @return The number. | |
75 | * @throws ParseException If parsing fails. | |
76 | */ | |
77 | public static Number parseNumber(String value) throws ParseException { | |
78 | 123 | return getNumberFormat().parse(stripWhitespace(value)); |
79 | } | |
80 | ||
81 | /** | |
82 | * Format number as string.<p> | |
83 | * | |
84 | * @param number The number. | |
85 | * @return The string value or "" for null. | |
86 | */ | |
87 | public static String formatNumber(Number number) { | |
88 | 62 | if (number == null) { |
89 | 2 | return ""; |
90 | } | |
91 | 60 | return getNumberFormat().format(number); |
92 | } | |
93 | ||
94 | /** | |
95 | * Parse the string value as a currency.<p> | |
96 | * | |
97 | * @param value The value. | |
98 | * @return The number. | |
99 | * @throws ParseException If parsing fails. | |
100 | */ | |
101 | public static Number parseCurrency(String value) throws ParseException { | |
102 | 3 | return getCurrencyFormat().parse(stripWhitespace(value)); |
103 | } | |
104 | ||
105 | /** | |
106 | * Format currency as string.<p> | |
107 | * | |
108 | * @param number The number. | |
109 | * @return The string value or "" for null. | |
110 | */ | |
111 | public static String formatCurrency(Number number) { | |
112 | 4 | if (number == null) { |
113 | 1 | return ""; |
114 | } | |
115 | 3 | return getCurrencyFormat().format(number); |
116 | } | |
117 | ||
118 | /** | |
119 | * Parse the string value as a date.<p> | |
120 | * | |
121 | * @param value The value. | |
122 | * @return The date. | |
123 | * @throws ParseException If parsing fails. | |
124 | */ | |
125 | public static Date parseDate(String value) throws ParseException { | |
126 | 11 | return getDateFormat().parse(value); |
127 | } | |
128 | ||
129 | /** | |
130 | * Format date as string.<p> | |
131 | * | |
132 | * @param date The date. | |
133 | * @return The string value or "" for null. | |
134 | */ | |
135 | public static String formatDate(Date date) { | |
136 | 6 | if (date == null) { |
137 | 1 | return ""; |
138 | } | |
139 | 5 | return getDateFormat().format(date); |
140 | } | |
141 | ||
142 | /** | |
143 | * Parse the string value as a time.<p> | |
144 | * | |
145 | * @param value The value. | |
146 | * @return The date. | |
147 | * @throws ParseException If parsing fails. | |
148 | */ | |
149 | public static Date parseTime(String value) throws ParseException { | |
150 | 4 | return getTimeFormat().parse(value); |
151 | } | |
152 | ||
153 | /** | |
154 | * Format time as string.<p> | |
155 | * | |
156 | * @param date The date. | |
157 | * @return The string value or "" for null. | |
158 | */ | |
159 | public static String formatTime(Date date) { | |
160 | 4 | if (date == null) { |
161 | 1 | return ""; |
162 | } | |
163 | 3 | return getTimeFormat().format(date); |
164 | } | |
165 | ||
166 | /** | |
167 | * Parse the string value as a date-time.<p> | |
168 | * | |
169 | * @param value The value. | |
170 | * @return The date. | |
171 | * @throws ParseException If parsing fails. | |
172 | */ | |
173 | public static Date parseDateTime(String value) throws ParseException { | |
174 | 4 | return getDateTimeFormat().parse(value); |
175 | } | |
176 | ||
177 | /** | |
178 | * Format date-time as string.<p> | |
179 | * | |
180 | * @param date The date. | |
181 | * @return The string value or "" for null. | |
182 | */ | |
183 | public static String formatDateTime(Date date) { | |
184 | 4 | if (date == null) { |
185 | 1 | return ""; |
186 | } | |
187 | 3 | return getDateTimeFormat().format(date); |
188 | } | |
189 | ||
190 | /** | |
191 | * Format date as string.<p> | |
192 | * | |
193 | * @param date The date. | |
194 | * @return The string value or "" for null. | |
195 | */ | |
196 | public static String formatLongDate(Date date) { | |
197 | 5 | if (date == null) { |
198 | 1 | return ""; |
199 | } | |
200 | 4 | return getLongDateFormat().format(date); |
201 | } | |
202 | ||
203 | /** | |
204 | * Get a number format for the user locale. | |
205 | * | |
206 | * @return Number format. | |
207 | */ | |
208 | private static NumberFormat getNumberFormat() { | |
209 | 183 | return NumberFormat.getInstance(UserLocale.getLocale()); |
210 | } | |
211 | ||
212 | /** | |
213 | * Get a currency format for the user locale. | |
214 | * | |
215 | * @return Currency format. | |
216 | */ | |
217 | private static NumberFormat getCurrencyFormat() { | |
218 | 6 | return NumberFormat.getCurrencyInstance(UserLocale.DEFAULT_LOCALE); |
219 | } | |
220 | ||
221 | /** | |
222 | * Get a date format for the user locale. | |
223 | * | |
224 | * @return Date format. | |
225 | */ | |
226 | private static DateFormat getDateFormat() { | |
227 | 16 | return DateFormat.getDateInstance(DateFormat.SHORT, UserLocale.getLocale()); |
228 | } | |
229 | ||
230 | /** | |
231 | * Get a time format for the user locale. | |
232 | * | |
233 | * @return Date format. | |
234 | */ | |
235 | private static DateFormat getTimeFormat() { | |
236 | 7 | return DateFormat.getTimeInstance(DateFormat.SHORT, UserLocale.getLocale()); |
237 | } | |
238 | ||
239 | /** | |
240 | * Get a time format for the user locale. | |
241 | * | |
242 | * @return Date format. | |
243 | */ | |
244 | private static DateFormat getDateTimeFormat() { | |
245 | 7 | return DateFormat.getDateTimeInstance( |
246 | DateFormat.SHORT, | |
247 | DateFormat.SHORT, | |
248 | UserLocale.getLocale()); | |
249 | } | |
250 | ||
251 | /** | |
252 | * Get a date format for the user locale. | |
253 | * | |
254 | * @return Date format. | |
255 | */ | |
256 | private static DateFormat getLongDateFormat() { | |
257 | 4 | return DateFormat.getDateInstance(DateFormat.LONG, UserLocale.getLocale()); |
258 | } | |
259 | ||
260 | /** | |
261 | * Get a message format for the pattern. | |
262 | * | |
263 | * @param pattern The pattern. | |
264 | * @return Message format. | |
265 | */ | |
266 | private static MessageFormat getMessageFormat(String pattern) { | |
267 | 46 | return new MessageFormat(pattern); |
268 | } | |
269 | ||
270 | /** | |
271 | * Strip all whitespace from a string. | |
272 | * | |
273 | * @param value The string. | |
274 | * @return The stripped string. | |
275 | */ | |
276 | private static String stripWhitespace(String value) { | |
277 | 126 | if (value == null) { |
278 | 0 | return null; |
279 | } | |
280 | 126 | StringBuffer buffer = new StringBuffer(); |
281 | 126 | char[] chars = value.toCharArray(); |
282 | 452 | for (int i = 0; i < chars.length; i++) { |
283 | 326 | if (!Character.isWhitespace(chars[i])) { |
284 | 321 | buffer.append(chars[i]); |
285 | } | |
286 | } | |
287 | 126 | return buffer.toString(); |
288 | } | |
289 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |