KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > jpath > expression > FormatDateFunction


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.standard.lang.jpath.expression;
18
19 import java.text.DateFormat JavaDoc;
20 import java.text.SimpleDateFormat JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24
25 import javax.servlet.jsp.PageContext JavaDoc;
26
27 import org.apache.taglibs.standard.lang.jpath.adapter.ConversionException;
28 import org.apache.taglibs.standard.lang.jpath.adapter.Convert;
29 import org.apache.taglibs.standard.lang.jpath.adapter.IterationContext;
30 import org.apache.taglibs.standard.lang.jpath.adapter.JSPDate;
31
32 /**
33  * The FormatDateFunction class
34  *
35  *
36  * @author <a HREF='mailto:scott.hasse@isthmusgroup.com'>Scott Hasse</a>
37  * @version
38  */

39 public class FormatDateFunction extends SimpleNode {
40
41     /**
42      * Used to create an instance of the FormatDateFunction class
43      *
44      *
45      * @param id
46      *
47      */

48     public FormatDateFunction(int id) {
49         super(id);
50     }
51
52     /**
53      * Used to create an instance of the FormatDateFunction class
54      *
55      *
56      * @param p
57      * @param id
58      *
59      */

60     public FormatDateFunction(Parser p, int id) {
61         super(p, id);
62     }
63
64     /**
65      * Provides a method to print a normalized version of the original
66      * expression. The normalized version has standardized spacing and
67      * parenthesis, and can be used to compare expressions formatted
68      * in different ways to see if they are actually the same expression.
69      *
70      *
71      * @return The normalized version of the original expression
72      *
73      */

74     public String JavaDoc toNormalizedString() {
75
76         String JavaDoc normalized = "";
77
78         normalized = "format-date(" + jjtGetChild(0).toNormalizedString()
79                 + jjtGetChild(1).toNormalizedString() + "," + ")";
80
81         return normalized;
82     }
83
84     /**
85      * This method evaluates this node of the expression and all child nodes.
86      * It returns the result of the
87      * evaluation as an <tt>Object</tt>. If any problems are encountered
88      * during the evaluation, an <tt>EvaluationException</tt> is thrown.
89      *
90      *
91      * @param pageContext the current JSP PageContext
92      *
93      * @param icontext the Iteration Context of the expression. If there is
94      * no interation context, this should be null.
95      *
96      * @return the result of the expression evaluation as an object
97      *
98      * @throws EvaluationException if a problem is encountered during the
99      * evaluation
100      */

101     public Object JavaDoc evaluate(PageContext JavaDoc pageContext, IterationContext icontext)
102             throws EvaluationException {
103
104         String JavaDoc formatted;
105
106         try {
107             JSPDate arg =
108                 Convert.toJSPDate(jjtGetChild(0).evaluate(pageContext,
109                     icontext));
110             String JavaDoc pattern =
111                 Convert.toString(jjtGetChild(1).evaluate(pageContext,
112                     icontext));
113             DateFormat JavaDoc form;
114
115             if (jjtGetNumChildren() > 2) {
116                 String JavaDoc arg3 =
117                     Convert.toString(jjtGetChild(2).evaluate(pageContext,
118                         icontext));
119                 Locale JavaDoc locale = getLocale(arg3);
120
121                 form = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
122             } else {
123                 form = DateFormat.getInstance();
124             }
125
126             try {
127                 ((SimpleDateFormat JavaDoc) form).applyPattern(pattern);
128
129                 formatted = form.format(new Date JavaDoc(arg.getTime().longValue()));
130             } catch (IllegalArgumentException JavaDoc iae) {
131                 formatted = new String JavaDoc("");
132             }
133         } catch (ConversionException ce) {
134             throw new EvaluationException(this, ce.getMessage());
135         }
136
137         return formatted;
138     }
139
140     /**
141      * The getLocale method
142      *
143      *
144      * @param arg
145      *
146      * @return
147      *
148      */

149     private Locale JavaDoc getLocale(String JavaDoc arg) {
150
151         Locale JavaDoc result;
152         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(arg, "_");
153         String JavaDoc language = null;
154         String JavaDoc country = null;
155         String JavaDoc variant = null;
156
157         if (st.hasMoreTokens()) {
158             language = st.nextToken();
159
160             if (st.hasMoreTokens()) {
161                 country = st.nextToken();
162
163                 if (st.hasMoreTokens()) {
164                     variant = st.nextToken();
165                     result = new Locale JavaDoc(language, country, variant);
166                 } else {
167                     result = new Locale JavaDoc(language, country);
168                 }
169             } else {
170                 result = new Locale JavaDoc(language, "");
171             }
172         } else {
173             result = Locale.getDefault();
174         }
175
176         return result;
177     }
178 }
179
Popular Tags