1   /*
2    * Copyright 2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
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: 155402 $
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.get(locale);
52          if (simpleDateFormat == null)
53          {
54               result = DateFormat.getDateInstance(DateFormat.SHORT, locale).format(date);
55          }
56          else
57          {
58              result = simpleDateFormat.format(date);
59          }
60          return result;
61      }
62      
63  }