KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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

115     public Object JavaDoc evaluate(PageContext JavaDoc pageContext, IterationContext icontext)
116             throws EvaluationException {
117
118         JSPDate result;
119
120         try {
121             result = Convert.toJSPDate(jjtGetChild(0).evaluate(pageContext,
122                     icontext));
123
124             String JavaDoc arg2 =
125                 Convert.toString(jjtGetChild(1).evaluate(pageContext,
126                     icontext));
127             int arg3 = Convert.toDouble(jjtGetChild(2).evaluate(pageContext,
128                            icontext)).intValue();
129             int field = getCalendarConstant(arg2);
130
131             result.roll(field, arg3);
132         } catch (ConversionException ce) {
133             throw new EvaluationException(this, ce.getMessage());
134         }
135
136         return result;
137     }
138
139     /**
140      * The getCalendarConstant method
141      *
142      *
143      * @param field
144      *
145      * @return
146      *
147      * @throws EvaluationException
148      *
149      */

150     private int getCalendarConstant(String JavaDoc field) throws EvaluationException {
151
152         int result;
153
154         if (field.equals("era")) {
155             result = Calendar.ERA;
156         } else if (field.equals("year")) {
157             result = Calendar.YEAR;
158         } else if (field.equals("month")) {
159             result = Calendar.MONTH;
160         } else if (field.equals("weekOfYear")) {
161             result = Calendar.WEEK_OF_YEAR;
162         } else if (field.equals("date")) {
163             result = Calendar.DATE;
164         } else if (field.equals("dayOfMonth")) {
165             result = Calendar.DAY_OF_MONTH;
166         } else if (field.equals("dayOfYear")) {
167             result = Calendar.DAY_OF_YEAR;
168         } else if (field.equals("dayOfWeek")) {
169             result = Calendar.DAY_OF_WEEK;
170         } else if (field.equals("dayOfWeekInMonth")) {
171             result = Calendar.DAY_OF_WEEK_IN_MONTH;
172         } else if (field.equals("amPm")) {
173             result = Calendar.AM_PM;
174         } else if (field.equals("hour")) {
175             result = Calendar.HOUR;
176         } else if (field.equals("hourOfDay")) {
177             result = Calendar.HOUR_OF_DAY;
178         } else if (field.equals("minute")) {
179             result = Calendar.MINUTE;
180         } else if (field.equals("second")) {
181             result = Calendar.SECOND;
182         } else if (field.equals("millisecond")) {
183             result = Calendar.MILLISECOND;
184         } else {
185             throw new EvaluationException(this,
186                     "An invalid date field was supplied");
187         }
188
189         return result;
190     }
191 }
192
Popular Tags