KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > datatype > convertor > FormattingDateConvertor


1 /*
2  * Copyright 1999-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 package org.apache.cocoon.woody.datatype.convertor;
17
18 import org.outerj.i18n.DateFormat;
19 import org.outerj.i18n.I18nSupport;
20
21 import java.util.Locale JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.text.ParseException JavaDoc;
24
25 /**
26  * A Convertor for {@link java.util.Date Date} objects backed by the
27  * {@link java.text.SimpleDateFormat SimpleDateFormat} class.
28  *
29  * <p>It can be configured to use one of three <strong>variants</strong>: date,
30  * time or datetime and one of four <strong>styles</strong>: long, full, medium or short.
31  *
32  * <p>Alternatively, a <strong>formatting pattern</strong> can be used. This can either be a locale-dependent
33  * or locale-independent formatting pattern. When looking up a formatting pattern, a mechansim
34  * similar to resource bundle lookup is used. Suppose the locale is nl-BE, then first a formatting
35  * pattern for nl-BE will be sought, then one for nl, and if that is not
36  * found, finally the locale-independent formatting pattern will be used.
37  *
38  * <p><strong>NOTE:</strong> the earlier statement about the fact that this class uses java.text.SimpleDateFormat
39  * is not entirely correct. In fact, it uses a small wrapper class that will either delegate to
40  * java.text.SimpleDateFormat or com.ibm.icu.text.SimpleDateFormat. The com.ibm version will automatically
41  * be used if it is present on the classpath, otherwise the java.text version will be used.
42  *
43  * @version CVS $Id: FormattingDateConvertor.java 30932 2004-07-29 17:35:38Z vgritsenko $
44  */

45 public class FormattingDateConvertor implements Convertor {
46     /** See {@link #setStyle}. */
47     private int style;
48     /** See {@link #setVariant}. */
49     private int variant;
50     /** Locale-specific formatting patterns. */
51     private LocaleMap localizedPatterns;
52     /** Non-locale specific formatting pattern. */
53     private String JavaDoc nonLocalizedPattern;
54
55     public static final int DATE = 1;
56     public static final int TIME = 2;
57     public static final int DATE_TIME = 3;
58
59     public FormattingDateConvertor() {
60         this.style = java.text.DateFormat.SHORT;
61         this.variant = DATE;
62         this.localizedPatterns = new LocaleMap();
63     }
64
65     public Object JavaDoc convertFromString(String JavaDoc value, Locale JavaDoc locale, Convertor.FormatCache formatCache) {
66         DateFormat dateFormat = getDateFormat(locale, formatCache);
67         try {
68             return dateFormat.parse(value);
69         } catch (ParseException JavaDoc e) {
70             return null;
71         }
72     }
73
74     public String JavaDoc convertToString(Object JavaDoc value, Locale JavaDoc locale, Convertor.FormatCache formatCache) {
75         DateFormat dateFormat = getDateFormat(locale, formatCache);
76         return dateFormat.format((Date JavaDoc)value);
77     }
78
79     private final DateFormat getDateFormat(Locale JavaDoc locale, Convertor.FormatCache formatCache) {
80         DateFormat dateFormat = null;
81         if (formatCache != null)
82             dateFormat = (DateFormat)formatCache.get();
83         if (dateFormat == null) {
84             dateFormat = getDateFormat(locale);
85             if (formatCache != null)
86                 formatCache.store(dateFormat);
87         }
88         return dateFormat;
89     }
90
91     protected DateFormat getDateFormat(Locale JavaDoc locale) {
92         DateFormat dateFormat = null;
93
94         switch (variant) {
95             case DATE:
96                 dateFormat = I18nSupport.getInstance().getDateFormat(style, locale);
97                 break;
98             case TIME:
99                 dateFormat = I18nSupport.getInstance().getTimeFormat(style, locale);
100                 break;
101             case DATE_TIME:
102                 dateFormat = I18nSupport.getInstance().getDateTimeFormat(style, style, locale);
103                 break;
104         }
105
106         String JavaDoc pattern = (String JavaDoc)localizedPatterns.get(locale);
107
108         if (pattern != null)
109             dateFormat.applyLocalizedPattern(pattern);
110         else if (nonLocalizedPattern != null)
111             dateFormat.applyPattern(nonLocalizedPattern);
112
113         return dateFormat;
114     }
115
116     public Class JavaDoc getTypeClass() {
117         return Date JavaDoc.class;
118     }
119
120     /**
121      *
122      * @param style one of the constants FULL, LONG, MEDIUM or SHORT defined in the {@link Date} class.
123      */

124     public void setStyle(int style) {
125         this.style = style;
126     }
127
128     public void setVariant(int variant) {
129         if (variant != DATE && variant != TIME && variant != DATE_TIME)
130             throw new IllegalArgumentException JavaDoc("Invalid value for variant parameter.");
131         this.variant = variant;
132     }
133
134     public void addFormattingPattern(Locale JavaDoc locale, String JavaDoc pattern) {
135         localizedPatterns.put(locale, pattern);
136     }
137
138     public void setNonLocalizedPattern(String JavaDoc pattern) {
139         this.nonLocalizedPattern = pattern;
140     }
141 }
142
Popular Tags