1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the Internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2007
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: org.eclipse.jdt.ui.prefs 1178 2006-11-06 12:48:02Z dmsmith $
21   */
22  
23  package org.crosswire.common.icu;
24  
25  import java.lang.reflect.InvocationTargetException;
26  import java.text.DateFormat;
27  import java.text.ParseException;
28  import java.text.SimpleDateFormat;
29  import java.util.Date;
30  
31  import org.crosswire.common.util.ClassUtil;
32  import org.crosswire.common.util.ReflectionUtil;
33  
34  /**
35   * DateFormat provides a wrapper of some of DateFormat and SimpleDateFormat
36   * using ICU4J if present, otherwise from core Java. Note, only those methods in
37   * DateFormat that are actually used are here.
38   * 
39   * @see gnu.lgpl.License for license details.<br>
40   *      The copyright to this program is held by it's authors.
41   * @author DM Smith [dmsmith555 at yahoo dot com]
42   */
43  public class DateFormatter {
44      /**
45       * Prevent instantiation.
46       */
47      private DateFormatter() {
48      }
49  
50      /*
51       * (non-Javadoc)
52       * 
53       * @see java.text.DateFormat#getDateInstance(int)
54       */
55      public static DateFormatter getDateInstance(int format) {
56          DateFormatter fmt = new DateFormatter();
57          boolean oops = false;
58          try {
59              fmt.formatterClass = ClassUtil.forName("com.ibm.icu.text.DateFormat");
60              // To call a method taking a type of int, the type has to match but
61              // the object has to be wrapped
62              Class<?>[] instanceTypes = {
63                  int.class
64              };
65              Object[] instanceParams = {
66                  Integer.valueOf(format)
67              };
68              fmt.formatter = ReflectionUtil.invoke(fmt.formatterClass, fmt.formatterClass, "getDateInstance", instanceParams, instanceTypes);
69          } catch (NoSuchMethodException e) {
70              oops = true;
71          } catch (IllegalAccessException e) {
72              oops = true;
73          } catch (InvocationTargetException e) {
74              oops = true;
75          } catch (ClassNotFoundException e) {
76              oops = true;
77          }
78  
79          if (oops) {
80              fmt.formatterClass = DateFormat.class;
81              fmt.formatter = DateFormat.getDateInstance(format);
82          }
83  
84          return fmt;
85      }
86  
87      /*
88       * (non-Javadoc)
89       * 
90       * @see java.text.DateFormat#getDateInstance()
91       */
92      public static DateFormatter getDateInstance() {
93          return getDateInstance(DEFAULT);
94      }
95  
96      /*
97       * (non-Javadoc)
98       * 
99       * @see java.text.DateFormat#getDateInstance(int)
100      */
101     public static DateFormatter getSimpleDateInstance(String format) {
102         DateFormatter fmt = new DateFormatter();
103         boolean oops = false;
104         try {
105             fmt.formatterClass = ClassUtil.forName("com.ibm.icu.text.SimpleDateFormat");
106             fmt.formatter = ReflectionUtil.construct("com.ibm.icu.text.SimpleDateFormat", format);
107         } catch (NoSuchMethodException e) {
108             oops = true;
109         } catch (IllegalAccessException e) {
110             oops = true;
111         } catch (InvocationTargetException e) {
112             oops = true;
113         } catch (ClassNotFoundException e) {
114             oops = true;
115         } catch (InstantiationException e) {
116             oops = true;
117         }
118 
119         if (oops) {
120             fmt.formatterClass = SimpleDateFormat.class;
121             fmt.formatter = new SimpleDateFormat(format);
122         }
123 
124         return fmt;
125     }
126 
127     /*
128      * (non-Javadoc)
129      * 
130      * @see java.text.DateFormat#setLenient(boolean)
131      */
132     public void setLenient(boolean lenient) {
133         try {
134             Class<?>[] lenientTypes = {
135                 boolean.class
136             };
137             Object[] lenientParams = {
138                 Boolean.valueOf(lenient)
139             };
140             ReflectionUtil.invoke(formatterClass, formatter, "setLenient", lenientParams, lenientTypes);
141         } catch (NoSuchMethodException e) {
142             assert false : e;
143         } catch (IllegalAccessException e) {
144             assert false : e;
145         } catch (InvocationTargetException e) {
146             assert false : e;
147         }
148     }
149 
150     /*
151      * (non-Javadoc)
152      * 
153      * @see java.text.DateFormat#format(java.util.Date)
154      */
155     public String format(Date date) {
156         try {
157             return (String) ReflectionUtil.invoke(formatterClass, formatter, "format", date);
158         } catch (Exception e) {
159             assert false : e;
160             return "";
161         }
162     }
163 
164     /*
165      * (non-Javadoc)
166      * 
167      * @see java.text.DateFormat#parse(java.lang.String)
168      */
169     public Date parse(String text) throws ParseException {
170         try {
171             return (Date) ReflectionUtil.invoke(formatterClass, formatter, "parse", text);
172         } catch (Exception e) {
173             if (e instanceof ParseException) {
174                 throw (ParseException) e;
175             }
176 
177             assert false : e;
178             return new Date();
179         }
180     }
181 
182     // Note these values are the same for Java and ICU4J
183     /**
184      * Constant for full style pattern.
185      */
186     public static final int FULL = 0;
187     /**
188      * Constant for long style pattern.
189      */
190     public static final int LONG = 1;
191     /**
192      * Constant for medium style pattern.
193      */
194     public static final int MEDIUM = 2;
195     /**
196      * Constant for short style pattern.
197      */
198     public static final int SHORT = 3;
199     /**
200      * Constant for default style pattern. Its value is MEDIUM.
201      */
202     public static final int DEFAULT = MEDIUM;
203 
204     /** The actual formatter. */
205     private Object formatter;
206 
207     /** The class of the formatter */
208     private Class<?> formatterClass;
209 }
210