01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.commons.betwixt.introspection;
19:
20: import java.text.DateFormat;
21: import java.text.SimpleDateFormat;
22: import java.util.Date;
23: import java.util.HashMap;
24: import java.util.Locale;
25: import java.util.Map;
26:
27: /**
28: * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
29: * @version $Revision: 438373 $
30: */
31: public class DateFormatterBean {
32:
33: private Map formatsByLocale = new HashMap();
34:
35: //TODO: want to be able to match iterators from maps...
36: // but this mean ammending the descriptors when the property type
37: // is discovered
38: public Map getFormats() {
39: return formatsByLocale;
40: }
41:
42: //TODO: should be able to use another verb eg register
43: //TODO: support for specifying adders in the dot betwixt file
44: //TODO: support for Simple types so that we can use Locale -> String
45: public void addFormat(Locale locale, SimpleDateFormat format) {
46: formatsByLocale.put(locale, format);
47: }
48:
49: public String format(Date date, Locale locale) {
50: String result = "";
51: SimpleDateFormat simpleDateFormat = (SimpleDateFormat) formatsByLocale
52: .get(locale);
53: if (simpleDateFormat == null) {
54: result = DateFormat.getDateInstance(DateFormat.SHORT,
55: locale).format(date);
56: } else {
57: result = simpleDateFormat.format(date);
58: }
59: return result;
60: }
61:
62: }
|