KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.jsp.PageContext JavaDoc;
20
21 import org.apache.taglibs.standard.lang.jpath.adapter.ConversionException;
22 import org.apache.taglibs.standard.lang.jpath.adapter.Convert;
23 import org.apache.taglibs.standard.lang.jpath.adapter.GregorianCalendarAdapter;
24 import org.apache.taglibs.standard.lang.jpath.adapter.IterationContext;
25 import org.apache.taglibs.standard.lang.jpath.adapter.JSPDate;
26
27 /**
28  * The DateFunction class
29  *
30  *
31  * @author <a HREF='mailto:scott.hasse@isthmusgroup.com'>Scott Hasse</a>
32  * @version
33  */

34 public class DateFunction extends SimpleNode {
35
36     /**
37      * Used to create an instance of the DateFunction class
38      *
39      *
40      * @param id
41      *
42      */

43     public DateFunction(int id) {
44         super(id);
45     }
46
47     /**
48      * Used to create an instance of the DateFunction class
49      *
50      *
51      * @param p
52      * @param id
53      *
54      */

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

69     public String JavaDoc toNormalizedString() {
70
71         boolean first = true;
72         String JavaDoc normalized;
73
74         normalized = "date(";
75
76         if (children != null) {
77             for (int i = 0; i < children.length; ++i) {
78                 if (!first) {
79                     normalized = normalized + ",";
80                 }
81
82                 first = false;
83
84                 SimpleNode n = (SimpleNode) children[i];
85
86                 if (n != null) {
87                     normalized = normalized + n.toNormalizedString();
88                 }
89             }
90         }
91
92         normalized = normalized + ")";
93
94         return normalized;
95     }
96
97     /**
98      * This method evaluates this node of the expression and all child nodes.
99      * It returns the result of the
100      * evaluation as an <tt>Object</tt>. If any problems are encountered
101      * during the evaluation, an <tt>EvaluationException</tt> is thrown.
102      *
103      *
104      * @param pageContext the current JSP PageContext
105      *
106      * @param icontext the Iteration Context of the expression. If there is
107      * no interation context, this should be null.
108      *
109      * @return the result of the expression evaluation as an object
110      *
111      * @throws EvaluationException if a problem is encountered during the
112      * evaluation
113      */

114     public Object JavaDoc evaluate(PageContext JavaDoc pageContext, IterationContext icontext)
115             throws EvaluationException {
116
117         JSPDate d;
118
119         try {
120             if (jjtGetNumChildren() == 3) {
121                 int year =
122                     Convert.toDouble(jjtGetChild(0).evaluate(pageContext,
123                         icontext)).intValue();
124                 int month =
125                     Convert.toDouble(jjtGetChild(1)
126                     .evaluate(pageContext, icontext)).intValue() - 1;
127                 int date =
128                     Convert.toDouble(jjtGetChild(2).evaluate(pageContext,
129                         icontext)).intValue();
130
131                 d = new GregorianCalendarAdapter(year, month, date);
132             } else if (jjtGetNumChildren() == 5) {
133                 int year =
134                     Convert.toDouble(jjtGetChild(0).evaluate(pageContext,
135                         icontext)).intValue();
136                 int month =
137                     Convert.toDouble(jjtGetChild(1)
138                     .evaluate(pageContext, icontext)).intValue() - 1;
139                 int date =
140                     Convert.toDouble(jjtGetChild(2).evaluate(pageContext,
141                         icontext)).intValue();
142                 int hour =
143                     Convert.toDouble(jjtGetChild(3).evaluate(pageContext,
144                         icontext)).intValue();
145                 int minute =
146                     Convert.toDouble(jjtGetChild(4).evaluate(pageContext,
147                         icontext)).intValue();
148
149                 d = new GregorianCalendarAdapter(year, month, date, hour,
150                         minute);
151             } else if (jjtGetNumChildren() == 6) {
152                 int year =
153                     Convert.toDouble(jjtGetChild(0).evaluate(pageContext,
154                         icontext)).intValue();
155                 int month =
156                     Convert.toDouble(jjtGetChild(1)
157                     .evaluate(pageContext, icontext)).intValue() - 1;
158                 int date =
159                     Convert.toDouble(jjtGetChild(2).evaluate(pageContext,
160                         icontext)).intValue();
161                 int hour =
162                     Convert.toDouble(jjtGetChild(3).evaluate(pageContext,
163                         icontext)).intValue();
164                 int minute =
165                     Convert.toDouble(jjtGetChild(4).evaluate(pageContext,
166                         icontext)).intValue();
167                 int second =
168                     Convert.toDouble(jjtGetChild(5).evaluate(pageContext,
169                         icontext)).intValue();
170
171                 d = new GregorianCalendarAdapter(year, month, date, hour,
172                         minute, second);
173             } else {
174                 throw new EvaluationException(this,
175                         "date does not have the proper"
176                         + "number of arguments");
177             }
178         } catch (ConversionException ce) {
179             throw new EvaluationException(this, ce.getMessage());
180         }
181
182         return d;
183     }
184 }
185
Popular Tags