1 16 17 package org.apache.taglibs.standard.lang.jpath.expression; 18 19 import java.util.Calendar ; 20 21 import javax.servlet.jsp.PageContext ; 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 35 public class RollDateFunction extends SimpleNode { 36 37 44 public RollDateFunction(int id) { 45 super(id); 46 } 47 48 56 public RollDateFunction(Parser p, int id) { 57 super(p, id); 58 } 59 60 70 public String toNormalizedString() { 71 72 boolean first = true; 73 String 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 115 public Object evaluate(PageContext 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 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 150 private int getCalendarConstant(String 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 |