File Source: CalendarTag.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * 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. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */
18
19 package org.apache.roller.weblogger.ui.core.tags.calendar;
20
21 import org.apache.commons.beanutils.PropertyUtils;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.roller.util.DateUtil;
25
26 import java.io.PrintWriter;
27 import java.io.StringWriter;
28 import java.text.SimpleDateFormat;
29 import java.util.Calendar;
30 import java.util.Date;
31 import java.util.Locale;
32 import java.util.ResourceBundle;
33 import java.util.StringTokenizer;
34
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.jsp.JspException;
37 import javax.servlet.jsp.tagext.Tag;
38 import javax.servlet.jsp.tagext.TagSupport;
39
40
41 /**
42 * Calendar tag.
43 * @jsp.tag name="Calendar"
44 */
45 public class CalendarTag extends TagSupport {
/*
P/P * Method: org.apache.roller.weblogger.ui.core.tags.calendar.CalendarTag__static_init
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getFactory(...)@46 != null
*
* Postconditions:
* init'ed(mLogger)
*/
46 private static Log mLogger =
47 LogFactory.getFactory().getInstance(CalendarTag.class);
48
49 // JSP Attributes
50
51 /** @jsp.attribute required="true" */
/*
P/P * Method: String getName()
*
* Preconditions:
* init'ed(this.mName)
*
* Postconditions:
* return_value == this.mName
* init'ed(return_value)
*/
52 public String getName() { return mName; }
/*
P/P * Method: void setName(String)
*
* Postconditions:
* this.mName == name
* init'ed(this.mName)
*/
53 public void setName( String name ) { mName = name; }
54 private String mName = null;
55
56 /* @jsp.attribute description="Date in yyyyMMdd format"
57 public String getDate() { return mDate; }
58 public void setDate( String s ) { mDate = s; }
59 private String mDate = null;
60 */
61
62 /** @jsp.attribute */
/*
P/P * Method: String getModel()
*
* Preconditions:
* init'ed(this.mModelName)
*
* Postconditions:
* return_value == this.mModelName
* init'ed(return_value)
*/
63 public String getModel() { return mModelName; }
/*
P/P * Method: void setModel(String)
*
* Postconditions:
* this.mModelName == s
* init'ed(this.mModelName)
*/
64 public void setModel( String s ) { mModelName= s; }
65 private String mModelName = null;
66
67 /** @jsp.attribute */
/*
P/P * Method: String getClassSuffix()
*
* Preconditions:
* init'ed(this.mClassSuffix)
*
* Postconditions:
* return_value == this.mClassSuffix
* init'ed(return_value)
*/
68 public String getClassSuffix() { return mClassSuffix; }
/*
P/P * Method: void setClassSuffix(String)
*
* Postconditions:
* this.mClassSuffix == s
* init'ed(this.mClassSuffix)
*/
69 public void setClassSuffix( String s ) { mClassSuffix= s; }
70 private String mClassSuffix = "";
71
72 // not a tag attribute
73 public void setLocale(Locale locale) {
/*
P/P * Method: void setLocale(Locale)
*
* Postconditions:
* this.mLocale == One-of{old this.mLocale, locale}
*
* Test Vectors:
* locale: Addr_Set{null}, Inverse{null}
*/
74 if (locale != null)
75 mLocale = locale;
76 }
77 private Locale mLocale = Locale.getDefault();
78
79 // not a tag attribute
80 /*
81 private TimeZone mTimeZone = TimeZone.getDefault();
82 public void setTimeZone(TimeZone zone) {
83 if (zone != null)
84 mTimeZone = zone;
85 }
86 private TimeZone getTimeZone()
87 {
88 // I've seen TimeZone.getDefault() return null. -Lance
89 if (mTimeZone == null)
90 mTimeZone = TimeZone.getTimeZone("America/New_York");
91 return mTimeZone;
92 }
93 */
94
95 private String[] mDayNames = null;
96
/*
P/P * Method: void org.apache.roller.weblogger.ui.core.tags.calendar.CalendarTag()
*
* Postconditions:
* this.mClassSuffix == &""
* this.mDayNames == null
* this.mModelName == null
* this.mName == null
* init'ed(this.mLocale)
*/
97 public CalendarTag() {
98 /*
99 * Empty constructor.
100 *
101 * Used to build the day names, but the correct locale
102 * was not set at this stage. Day-name-building has moved to the
103 * doStartTag() method.
104 */
105 }
106
107 //------------------------------------------------------------------------
108 /**
109 * Write to a PrintWriter so that tag may be used from Velocity
110 */
111 public int doStartTag( PrintWriter pw ) throws JspException {
112 try {
113 // build week day names
/*
P/P * Method: int doStartTag(PrintWriter)
*
* Preconditions:
* (soft) mLogger != null
* (soft) init'ed(this.mDayNames[...])
* (soft) pw != null
* (soft) init'ed(this.mClassSuffix)
* (soft) init'ed(this.mLocale)
* (soft) init'ed(this.mModelName)
* (soft) this.pageContext != null
*
* Presumptions:
* java.util.ResourceBundle:getBundle(...)@155 != null
* model.calendar@142 != null
* model.calendar@145 != null
* model.calendar@158 != null
* model.day@132 != null
* ...
*
* Postconditions:
* return_value == 0
* this.mDayNames == One-of{&new String[](buildDayNames#1), old this.mDayNames}
* (soft) init'ed(this.mDayNames[...])
* new String[](buildDayNames#1) num objects <= 1
* new String[](buildDayNames#1).length == 7
* (soft) init'ed(new String[](buildDayNames#1)[...])
*
* Test Vectors:
* java.util.StringTokenizer:hasMoreTokens(...)@129: {0}, {1}
* model.nextMonth@169: Addr_Set{null}, Inverse{null}
* model.prevMonth@169: Addr_Set{null}, Inverse{null}
*/
114 this.buildDayNames();
115
116 Date day=null; // day to be displayed
117 Calendar dayCal; // set to day to be displayed
118 Calendar cal; // for iterating through days of month
119 Calendar todayCal; // for iterating through days of month
120 CalendarModel model; // the calendar model
121
122 // ---------------------------------
123 // --- initialize date variables ---
124 // ---------------------------------
125
126 // check for parameter map and target url
127 StringTokenizer toker = new StringTokenizer(mModelName,".");
128 String tok1 = toker.nextToken();
129 if (toker.hasMoreTokens()) {
130 String tok2 = toker.nextToken();
131 Object bean = pageContext.findAttribute(tok1);
132 model = (CalendarModel)PropertyUtils.getProperty(bean, tok2);
133 } else {
134 model = (CalendarModel)pageContext.findAttribute( mModelName );
135 }
136
137 // no model specified, nothing to generate
138 if (model == null) {
139 return SKIP_BODY;
140 }
141
142 day = model.getDay();
143
144 // ceate object to represent today
145 todayCal = model.getCalendar();
146 todayCal.setTime( new Date() );
147
148 // formatter Month-Year title of calendar
149 SimpleDateFormat formatTitle = new SimpleDateFormat("MMMM yyyy", mLocale);
150
+ 151 HttpServletRequest request =
152 (HttpServletRequest)pageContext.getRequest();
153
154 // get Resource Bundle
155 ResourceBundle bundle = ResourceBundle.getBundle("ApplicationResources", mLocale);
156
157 // go back to first day in month
158 cal = model.getCalendar();
159 day = DateUtil.getNoonOfDay(day, cal);
160 cal.set( Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DAY_OF_MONTH) );
161
162 // Go back to first day of week before that (Sunday in US, Monday in France, e.g.)
163 // in the calendar
164 while ( cal.get( Calendar.DAY_OF_WEEK ) != cal.getFirstDayOfWeek() ) {
165 cal.add( Calendar.DATE, -1 );
166 }
167
168 // create table of 5 weeks, 7 days per row
169 dayCal = model.getCalendar();
170 dayCal.setTime( day );
171
172 // -------------------------
173 // --- draw the calendar ---
174 // -------------------------
175 pw.print("<table cellspacing=\"0\" border=\"0\" ");
176 pw.print(" summary=\""
177 +bundle.getString("calendar.summary")
178 +"\" class=\"hCalendarTable"
179 +mClassSuffix+"\">");
180 pw.print("<tr>");
181 pw.print("<td colspan=\"7\" align=\"center\" "+
182 "class=\"hCalendarMonthYearRow"+mClassSuffix+"\">");
183 if (model.getPrevMonth() != null) {
184 pw.print("<a href=\"" + model.computePrevMonthUrl()
185 + "\" title=\"" + bundle.getString("calendar.prev")
186 + "\" class=\"hCalendarNavBar\">«</a> ");
187 }
188 pw.print( formatTitle.format(day) );
189 if (model.getNextMonth() != null) {
190 pw.print(" <a href=\"" + model.computeNextMonthUrl()
191 + "\" title=\"" + bundle.getString("calendar.next")
192 + "\" class=\"hCalendarNavBar\">»</a>");
193 }
194 pw.print("</td></tr>");
195
196 // emit the HTML calendar
197 for ( int w=-1; w<6; w++ ) {
198 pw.print("<tr>");
199 for ( int d=0; d<7; d++ ) {
200 if ( w == -1 ) {
201 pw.print(
202 "<th class=\"hCalendarDayNameRow"
203 +mClassSuffix+"\" align=\"center\">");
204 pw.print( mDayNames[d] );
205 pw.print("</th>");
206 continue;
207 }
208
209 // determine URL for this calendar day
210 Date tddate = cal.getTime();
211 String url = model.computeUrl(tddate, false, false);
212 String content = model.getContent( tddate );
213
214 if // day is in calendar month
215 ((cal.get(Calendar.MONTH) == dayCal.get(Calendar.MONTH))
216 && (cal.get(Calendar.YEAR) == dayCal.get(Calendar.YEAR))) {
217 if // day is today then use today style
218 (( cal.get(Calendar.DAY_OF_MONTH)
219 == todayCal.get(Calendar.DAY_OF_MONTH))
220 && ( cal.get(Calendar.MONTH)
221 == todayCal.get(Calendar.MONTH))
222 && ( cal.get(Calendar.YEAR)
223 == todayCal.get(Calendar.YEAR))) {
224 printToday(pw, cal, url, content);
225 } else {
226 printDayInThisMonth(pw, cal, url, content);
227 }
228 } else // apply day-not-in-month style ;-)
229 {
230 printDayNotInMonth(pw, cal);
231 }
232
233 // increment calendar by one day
234 cal.add( Calendar.DATE, 1 );
235 }
236 pw.print("</tr>");
237 }
238
239 pw.print("<tr class=\"hCalendarNextPrev"
240 +mClassSuffix+"\">");
241 pw.print("<td colspan=\"7\" align=\"center\">");
242
243 pw.print("<a href=\""+model.computeTodayMonthUrl()
244 +"\" class=\"hCalendarNavBar\">"
245 +bundle.getString("calendar.today")
246 +"</a>");
247
248 pw.print("</td>");
249 pw.print("</tr>");
250
251 pw.print("</table>");
252 } catch (Exception e) {
253 pw.print("<span class=\"error\">");
254 pw.print("<p><b>An ERROR has occured CalendarTag</b></p>");
255 pw.print("</span>");
256 mLogger.error("Calendar tag exception",e);
257 }
258 return Tag.SKIP_BODY;
259 }
260
261 private void printDayNotInMonth(PrintWriter pw, Calendar cal) {
/*
P/P * Method: void printDayNotInMonth(PrintWriter, Calendar)
*
* Preconditions:
* pw != null
* init'ed(this.mClassSuffix)
*/
262 pw.print("<td class=\"hCalendarDayNotInMonth"+mClassSuffix+"\">");
263 //pw.print(cal.get(Calendar.DAY_OF_MONTH));
264 pw.print(" ");
265 pw.print("</td>");
266 }
267
268 private void printDayInThisMonth(PrintWriter pw, Calendar cal, String url, String content) {
/*
P/P * Method: void printDayInThisMonth(PrintWriter, Calendar, String, String)
*
* Preconditions:
* pw != null
* init'ed(this.mClassSuffix)
* (soft) cal != null
*
* Test Vectors:
* content: Addr_Set{null}, Inverse{null}
* url: Addr_Set{null}, Inverse{null}
*/
269 if ( content!=null ) {
270 pw.print("<td class=\"hCalendarDayCurrent"
271 +mClassSuffix+"\">");
272 pw.print( content );
273 pw.print("</td>");
274 } else if (url!=null) {
275 pw.print("<td class=\"hCalendarDayLinked"
276 +mClassSuffix+"\">");
277 pw.print("<div class=\"hCalendarDayTitle"
278 +mClassSuffix+"\">");
279 pw.print("<a href=\""+url+"\">");
280 pw.print(cal.get(Calendar.DAY_OF_MONTH));
281 pw.print("</a></div>");
282 pw.print("</td>");
283 } else {
284 pw.print("<td class=\"hCalendarDay"
285 +mClassSuffix+"\">");
286 pw.print("<div class=\"hCalendarDayTitle"
287 +mClassSuffix+"\">");
288 pw.print(cal.get(Calendar.DAY_OF_MONTH));
289 pw.print("</div>");
290 pw.print("</td>");
291 }
292 }
293
294 private void printToday(PrintWriter pw, Calendar cal, String url, String content) {
/*
P/P * Method: void printToday(PrintWriter, Calendar, String, String)
*
* Preconditions:
* pw != null
* init'ed(this.mClassSuffix)
* (soft) cal != null
*
* Test Vectors:
* content: Addr_Set{null}, Inverse{null}
* url: Addr_Set{null}, Inverse{null}
*/
295 if ( content!=null ) {
296 pw.print("<td class=\"hCalendarDayCurrent"
297 +mClassSuffix+"\">");
298 pw.print( content );
299 pw.print("</td>");
300 } else if (url!=null) {
301 pw.print("<td class=\"hCalendarDayCurrent"
302 +mClassSuffix+"\">");
303 pw.print("<a href=\""+url+"\" "
304 +"class=\"hCalendarDayTitle"+mClassSuffix+"\">");
305 pw.print(cal.get(Calendar.DAY_OF_MONTH));
306 pw.print("</a>");
307 pw.print("</td>");
308 } else {
309 pw.print("<td class=\"hCalendarDayCurrent"
310 +mClassSuffix+"\">");
311 pw.print("<div class=\"hCalendarDayTitle"
312 +mClassSuffix+"\">");
313 pw.print(cal.get(Calendar.DAY_OF_MONTH));
314 pw.print("</div></td>");
315 }
316 }
317
318 /**
319 * Helper method to build the names of the weekdays. This
320 * used to take place in the <code>CalendarTag</code> constructor,
321 * but there, <code>mLocale</code> doesn't have the correct value yet.
322 */
323 private void buildDayNames() {
324 // build array of names of days of week
/*
P/P * Method: void buildDayNames()
*
* Preconditions:
* init'ed(this.mLocale)
*
* Presumptions:
* java.util.Calendar:getInstance(...)@326 != null
*
* Postconditions:
* this.mDayNames == &new String[](buildDayNames#1)
* possibly_updated(this.mDayNames[...])
* new String[](buildDayNames#1) num objects == 1
* this.mDayNames.length == 7
* init'ed(this.mDayNames[...])
*/
325 mDayNames = new String[7];
326 Calendar dayNameCal = Calendar.getInstance(mLocale);
327 SimpleDateFormat dayFormatter = new SimpleDateFormat("EEE", mLocale);
328 dayNameCal.set(Calendar.DAY_OF_WEEK, dayNameCal.getFirstDayOfWeek());
329 for (int dnum = 0; dnum < 7; dnum++) {
330 mDayNames[dnum] = dayFormatter.format(dayNameCal.getTime());
331 dayNameCal.add(Calendar.DATE, 1);
332 }
333 }
334
335
336 public String toString() {
/*
P/P * Method: String toString()
*
* Preconditions:
* (soft) mLogger != null
* (soft) init'ed(this.mDayNames[...])
* (soft) init'ed(this.mClassSuffix)
* (soft) init'ed(this.mLocale)
* (soft) init'ed(this.mModelName)
* (soft) this.pageContext != null
*
* Postconditions:
* java.io.StringWriter:toString(...)._tainted == 0
* return_value == One-of{&java.io.StringWriter:toString(...), &"Exception in tag"}
* return_value in Addr_Set{&java.io.StringWriter:toString(...),&"Exception in tag"}
* this.mDayNames == One-of{&new String[](buildDayNames#1), old this.mDayNames}
* (soft) init'ed(this.mDayNames[...])
* new String[](buildDayNames#1) num objects <= 1
* new String[](buildDayNames#1).length == 7
* init'ed(new String[](buildDayNames#1)[...])
*/
337 String ret = null;
338 try {
339 StringWriter sw = new StringWriter();
340 doStartTag( new PrintWriter( sw, true ));
341 // See, design precludes contents
342 doEndTag( new PrintWriter( sw, true ));
343 ret = sw.toString();
344 } catch (Exception e) {
345 ret = "Exception in tag";
346 mLogger.error(ret,e);
347 }
348 return ret;
349 }
350
351 public String emit() {
/*
P/P * Method: String emit()
*
* Preconditions:
* (soft) mLogger != null
* (soft) init'ed(this.mDayNames[...])
* (soft) init'ed(this.mClassSuffix)
* (soft) init'ed(this.mLocale)
* (soft) init'ed(this.mModelName)
* (soft) this.pageContext != null
*
* Postconditions:
* java.io.StringWriter:toString(...)._tainted == 0
* return_value == One-of{&java.io.StringWriter:toString(...), &"Exception in tag"}
* return_value in Addr_Set{&java.io.StringWriter:toString(...),&"Exception in tag"}
* this.mDayNames == One-of{&new String[](buildDayNames#1), old this.mDayNames}
* (soft) init'ed(this.mDayNames[...])
* new String[](buildDayNames#1) num objects <= 1
* new String[](buildDayNames#1).length == 7
* init'ed(new String[](buildDayNames#1)[...])
*/
352 return toString();
353 }
354
355 public int doStartTag() throws JspException {
/*
P/P * Method: int doStartTag()
*
* Preconditions:
* this.pageContext != null
* (soft) mLogger != null
* (soft) init'ed(this.mDayNames[...])
* (soft) init'ed(this.mClassSuffix)
* (soft) init'ed(this.mLocale)
* (soft) init'ed(this.mModelName)
*
* Postconditions:
* return_value == 0
* this.mDayNames == &new String[](buildDayNames#1)
* (soft) init'ed(this.mDayNames[...])
* new String[](buildDayNames#1) num objects == 1
* this.mDayNames.length == 7
* init'ed(this.mDayNames[...])
*/
356 return doStartTag( new PrintWriter( pageContext.getOut(), true) );
357 }
358
359
360 public int doEndTag() throws JspException {
/*
P/P * Method: int doEndTag()
*
* Preconditions:
* this.pageContext != null
*
* Postconditions:
* return_value == 6
*/
361 return doEndTag( new PrintWriter( pageContext.getOut(), true) );
362 }
363
364 /** Default processing of the end tag returning EVAL_PAGE. */
365 public int doEndTag( PrintWriter pw ) throws JspException {
/*
P/P * Method: int doEndTag(PrintWriter)
*
* Postconditions:
* return_value == 6
*/
366 return EVAL_PAGE;
367 }
368
369 }
370
SofCheck Inspector Build Version : 2.18479
| CalendarTag.java |
2009-Jan-02 14:25:02 |
| CalendarTag.class |
2009-Sep-04 03:12:44 |