KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > datatype > convertor > Icu4jDateConvertor


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.forms.datatype.convertor;
17
18 import java.text.ParseException JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import org.apache.cocoon.forms.FormsConstants;
23 import org.apache.cocoon.xml.AttributesImpl;
24 import org.xml.sax.ContentHandler JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26
27 import com.ibm.icu.text.DateFormat;
28 import com.ibm.icu.text.SimpleDateFormat;
29
30 /**
31  * A Convertor for {@link java.util.Date Date} objects backed by ICU4J's
32  * {@link com.ibm.icu.text.SimpleDateFormat} class.
33  *
34  * <p>It can be configured to use one of three <strong>variants</strong>: date,
35  * time or datetime and one of four <strong>styles</strong>: long, full, medium or short.
36  *
37  * <p>Alternatively, a <strong>formatting pattern</strong> can be used. This can either be a locale-dependent
38  * or locale-independent formatting pattern. When looking up a formatting pattern, a mechansim
39  * similar to resource bundle lookup is used. Suppose the locale is nl-BE, then first a formatting
40  * pattern for nl-BE will be sought, then one for nl, and if that is not
41  * found, finally the locale-independent formatting pattern will be used.
42  *
43  * @version $Id: Icu4jDateConvertor.java 326838 2005-10-20 06:26:53Z sylvain $
44  */

45 public class Icu4jDateConvertor implements Convertor {
46     //FIXME: the only difference of this class with FormattingDateConvertor is the use of com.ibm.icu.text.SimpleDateFormat
47
// --> refactor to have it extend FormattingDateConvertor
48

49     /** See {@link #setStyle}. */
50     private int style;
51     /** See {@link #setVariant}. */
52     private String JavaDoc variant;
53     /** Locale-specific formatting patterns. */
54     private LocaleMap localizedPatterns;
55     /** Non-locale specific formatting pattern. */
56     private String JavaDoc nonLocalizedPattern;
57
58     public static final String JavaDoc DATE = "date";
59     public static final String JavaDoc TIME = "time";
60     public static final String JavaDoc DATE_TIME = "datetime";
61
62     public Icu4jDateConvertor() {
63         this.style = DateFormat.SHORT;
64         this.variant = DATE;
65         this.localizedPatterns = new LocaleMap();
66     }
67
68     public ConversionResult convertFromString(String JavaDoc value, Locale JavaDoc locale, Convertor.FormatCache formatCache) {
69         SimpleDateFormat dateFormat = getDateFormat(locale, formatCache);
70         try {
71             return new ConversionResult(dateFormat.parse(value));
72         } catch (ParseException JavaDoc e) {
73             return ConversionResult.create("date." + this.variant);
74         }
75     }
76
77     public String JavaDoc convertToString(Object JavaDoc value, Locale JavaDoc locale, Convertor.FormatCache formatCache) {
78         SimpleDateFormat dateFormat = getDateFormat(locale, formatCache);
79         return dateFormat.format((Date JavaDoc)value);
80     }
81
82     private final SimpleDateFormat getDateFormat(Locale JavaDoc locale, Convertor.FormatCache formatCache) {
83         SimpleDateFormat dateFormat = null;
84         if (formatCache != null)
85             dateFormat = (SimpleDateFormat)formatCache.get();
86         if (dateFormat == null) {
87             dateFormat = getDateFormat(locale);
88             if (formatCache != null)
89                 formatCache.store(dateFormat);
90         }
91         return dateFormat;
92     }
93
94     protected SimpleDateFormat getDateFormat(Locale JavaDoc locale) {
95         SimpleDateFormat dateFormat = null;
96
97         if (this.variant.equals(DATE)) {
98             //dateFormat = I18nSupport.getInstance().getDateFormat(style, locale);
99
dateFormat = (SimpleDateFormat)DateFormat.getDateInstance(style, locale);
100         } else if (this.variant.equals(TIME)) {
101             //dateFormat = I18nSupport.getInstance().getTimeFormat(style, locale);
102
dateFormat = (SimpleDateFormat)DateFormat.getTimeInstance(style, locale);
103         } else if (this.variant.equals(DATE_TIME)) {
104             //dateFormat = I18nSupport.getInstance().getDateTimeFormat(style, style, locale);
105
dateFormat = (SimpleDateFormat)DateFormat.getDateTimeInstance(style, style, locale);
106         }
107
108         String JavaDoc pattern = (String JavaDoc)localizedPatterns.get(locale);
109
110         if (pattern != null)
111             // Note: this was previously using applyLocalizedPattern() which allows to use
112
// a locale-specific pattern syntax, e.g. in french "j" (jour) for "d" and
113
// "a" (annee) for "y". But the localized pattern syntax is very little known and thus
114
// led to some weird pattern syntax error messages.
115
dateFormat.applyPattern(pattern);
116         else if (nonLocalizedPattern != null)
117             dateFormat.applyPattern(nonLocalizedPattern);
118
119         return dateFormat;
120     }
121
122     public Class JavaDoc getTypeClass() {
123         return Date JavaDoc.class;
124     }
125
126     /**
127      *
128      * @param style one of the constants FULL, LONG, MEDIUM or SHORT defined in the {@link Date} class.
129      */

130     public void setStyle(int style) {
131         this.style = style;
132     }
133
134     public void setVariant(String JavaDoc variant) {
135         if (DATE.equals(variant) || TIME.equals(variant) || DATE_TIME.equals(variant)) {
136             this.variant = variant;
137         } else {
138             throw new IllegalArgumentException JavaDoc("Invalid value for variant parameter.");
139         }
140     }
141
142     public void addFormattingPattern(Locale JavaDoc locale, String JavaDoc pattern) {
143         localizedPatterns.put(locale, pattern);
144     }
145
146     public void setNonLocalizedPattern(String JavaDoc pattern) {
147         this.nonLocalizedPattern = pattern;
148     }
149
150     private static final String JavaDoc CONVERTOR_EL = "convertor";
151
152     public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
153         String JavaDoc pattern = getDateFormat(locale).toPattern();
154
155         if (pattern != null) {
156             AttributesImpl attrs = new AttributesImpl();
157             attrs.addCDATAAttribute("pattern", pattern);
158             attrs.addCDATAAttribute("variant", this.variant);
159             contentHandler.startElement(FormsConstants.INSTANCE_NS, CONVERTOR_EL, FormsConstants.INSTANCE_PREFIX_COLON + CONVERTOR_EL, attrs);
160             contentHandler.endElement(FormsConstants.INSTANCE_NS, CONVERTOR_EL, FormsConstants.INSTANCE_PREFIX_COLON + CONVERTOR_EL);
161         }
162     }
163 }
164
Popular Tags