KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > datetime > DateFormatSymbolsTag


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
17 package org.apache.taglibs.datetime;
18
19 import java.util.Locale JavaDoc;
20 import java.util.ResourceBundle JavaDoc;
21 import java.util.MissingResourceException JavaDoc;
22 import java.text.DateFormatSymbols JavaDoc;
23 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
24 import javax.servlet.jsp.PageContext JavaDoc;
25 import javax.servlet.jsp.JspException JavaDoc;
26
27 /**
28  * <p>
29  * This class implements an empty tag that allows you to use a custom symbols
30  * for date formating.</p>
31  * <p>
32  * Must be specified attribute baseName that point to property file.
33  * </p>
34  * <p>
35  * Property file contains key\value pair in such rule<br>
36  * # Month symbols change on custom<br>
37  * month=true<br>
38  * month0=January<br>
39  * month1=February<br>
40  * # Era symbols change on custom<br>
41  * era=true<br>
42  * era0=before<br>
43  * era1=after<br>
44  *
45  * </p>
46  *
47  * @author <a HREF="mailto:wigor@bigmir.net">Kozlov Igor</a>
48  */

49 public class DateFormatSymbolsTag extends TagSupport JavaDoc {
50
51     private int scope = PageContext.PAGE_SCOPE;
52     private String JavaDoc localeRef = null;
53     private boolean locale_flag = false;
54     private String JavaDoc baseName = null;
55
56     private ResourceBundle JavaDoc bundle;
57     private DateFormatSymbols JavaDoc dateFormatSymbols;
58
59     public void setScope(String JavaDoc scope) {
60         if (scope.toLowerCase().equals("application")) {
61             this.scope = PageContext.APPLICATION_SCOPE;
62         } else if (scope.toLowerCase().equals("session")) {
63             this.scope = PageContext.SESSION_SCOPE;
64         } else if (scope.toLowerCase().equals("request")) {
65             this.scope = PageContext.REQUEST_SCOPE;
66         } else if (scope.toLowerCase().equals("page")) {
67             this.scope = PageContext.PAGE_SCOPE;
68         }
69     }
70
71     public void setBaseName(String JavaDoc baseName) {
72         this.baseName = baseName;
73     }
74
75     public void setLocale(boolean locale) {
76         this.locale_flag = locale;
77     }
78
79     public void setLocaleRef(String JavaDoc localeRef) {
80         this.localeRef = localeRef;
81     }
82
83     public int doStartTag() throws JspException JavaDoc {
84         Locale JavaDoc locale = null;
85         if (localeRef != null) {
86             locale = (Locale JavaDoc) pageContext.findAttribute(localeRef);
87         } else if (locale_flag) {
88             locale = pageContext.getRequest().getLocale();
89         } else {
90             dateFormatSymbols = new DateFormatSymbols JavaDoc();
91         }
92         findBundle(locale);
93         buildDateFormatSymbols(locale);
94         if (this.getId() != null) {
95             pageContext.setAttribute(this.getId(), dateFormatSymbols, scope);
96         }
97         return SKIP_BODY;
98     }
99
100     public void release() {
101         super.release();
102         scope = PageContext.PAGE_SCOPE;
103         localeRef = null;
104         locale_flag = false;
105         baseName = null;
106     }
107
108     private void buildDateFormatSymbols(Locale JavaDoc locale) {
109         if (locale != null) {
110             dateFormatSymbols = new DateFormatSymbols JavaDoc(locale);
111         } else {
112             dateFormatSymbols = new DateFormatSymbols JavaDoc();
113         }
114         // Set ampm string
115
try {
116             String JavaDoc customAmPm = bundle.getString("ampm");
117             if ("true".equals(customAmPm)) {
118                 String JavaDoc ampm[] = new String JavaDoc[2];
119                 for (int i = 0; i < 2; i++) {
120                     ampm[i] = bundle.getString("ampm" + i);
121                 }
122                 dateFormatSymbols.setAmPmStrings(ampm);
123             }
124         } catch (MissingResourceException JavaDoc e) {
125         }
126
127         // Set era strings
128
try {
129             String JavaDoc customEras = bundle.getString("eras");
130             if ("true".equals(customEras)) {
131                 String JavaDoc eras[] = new String JavaDoc[2];
132                 for (int i = 0; i < 2; i++) {
133                     eras[i] = bundle.getString("eras" + i);
134                 }
135                 dateFormatSymbols.setEras(eras);
136             }
137         } catch (MissingResourceException JavaDoc e) {
138         }
139
140         // Set short month strings
141
try {
142             String JavaDoc customShortMonth = bundle.getString("shortmonth");
143             if ("true".equals(customShortMonth)) {
144                 String JavaDoc shortmonth[] = new String JavaDoc[12];
145                 for (int i = 0; i < 12; i++) {
146                     shortmonth[i] = bundle.getString("shortmonth" + i);
147                 }
148                 dateFormatSymbols.setShortMonths(shortmonth);
149             }
150         } catch (MissingResourceException JavaDoc e) {
151         }
152
153         // Set month strings
154
try {
155             String JavaDoc customMonth = bundle.getString("month");
156             if ("true".equals(customMonth)) {
157                 String JavaDoc month[] = new String JavaDoc[12];
158                 for (int i = 0; i < 12; i++) {
159                     month[i] = bundle.getString("month" + i);
160                 }
161                 dateFormatSymbols.setMonths(month);
162             }
163         } catch (MissingResourceException JavaDoc e) {
164         }
165
166         // Set weekdays strings
167
try {
168             String JavaDoc customWeekdays = bundle.getString("weekdays");
169             if ("true".equals(customWeekdays)) {
170                 String JavaDoc weekdays[] = new String JavaDoc[7];
171                 for (int i = 0; i < 7; i++) {
172                     weekdays[i] = bundle.getString("weekdays" + i);
173                 }
174                 dateFormatSymbols.setWeekdays(weekdays);
175             }
176         } catch (MissingResourceException JavaDoc e) {
177         }
178
179         // Set short weekdays strings
180
try {
181             String JavaDoc customShortWeekdays = bundle.getString("shortweekdays");
182             if ("true".equals(customShortWeekdays)) {
183                 String JavaDoc shortweekdays[] = new String JavaDoc[7];
184                 for (int i = 0; i < 7; i++) {
185                     shortweekdays[i] = bundle.getString("shortweekdays" + i);
186                 }
187                 dateFormatSymbols.setShortWeekdays(shortweekdays);
188             }
189         } catch (MissingResourceException JavaDoc e) {
190         }
191     }
192
193     private void findBundle(Locale JavaDoc locale) {
194         if (locale != null) {
195             ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
196             bundle = ResourceBundle.getBundle(baseName, locale, cl);
197         } else {
198             bundle = ResourceBundle.getBundle(baseName);
199         }
200     }
201
202 }
203
Popular Tags