KickJava   Java API By Example, From Geeks To Geeks.

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

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

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