public int doStartTag() throws JspException {
HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
Blog blog = (Blog)request.getAttribute(Constants.BLOG_KEY);
Month month = (Month)request.getAttribute(Constants.MONTHLY_BLOG);
Day today = blog.getBlogForToday();
Calendar now = blog.getCalendar();
if (month == null) {
month = today.getMonth();
}
Calendar firstDayOfMonth = blog.getCalendar();
firstDayOfMonth.setTime(month.getBlogForDay(1).getDate());
SimpleDateFormat monthAndYearFormatter = new SimpleDateFormat("MMMM yyyy", blog.getLocale());
monthAndYearFormatter.setTimeZone(blog.getTimeZone());
SimpleDateFormat monthFormatter = new SimpleDateFormat("MMM", blog.getLocale());
monthFormatter.setTimeZone(blog.getTimeZone());
NumberFormat numberFormatter = NumberFormat.getIntegerInstance(blog.getLocale());
Month firstMonth = blog.getBlogForFirstMonth();
try {
JspWriter out = pageContext.getOut();
out.write("<div class=\"calendar\">");
out.write("<table width=\"100%\">");
out.write("<tr>");
out.write("<td colspan=\"7\" align=\"center\">");
if (month.before(firstMonth)) {
out.write("<b>");
out.write(monthAndYearFormatter.format(month.getDate()));
out.write("</b>");
} else {
out.write("<b><a href=\"");
out.write(UrlRewriter.doRewrite(month.getPermalink()));
out.write("\">");
out.write(monthAndYearFormatter.format(month.getDate()));
out.write("</a></b>");
}
out.write("</td>");
out.write("</tr>");
int firstDayOfWeek = now.getFirstDayOfWeek();
// write out the calendar header
DateFormatSymbols symbols = new DateFormatSymbols(blog.getLocale());
String[] days = symbols.getShortWeekdays();
out.write("<tr>");
for (int i = firstDayOfWeek; i <= 7; i++) {
out.write("<td class=\"calendarDayHeader\" width=\"14%\">" + days[i] + "</td>");
}
for (int i = 1; i < firstDayOfWeek; i++) {
out.write("<td class=\"calendarDayHeader\">" + days[i] + "</td>");
}
out.write("</tr>");
// write out the body of the calendar
Iterator it = getDatesForCompleteWeeks(blog, month).iterator();
Calendar cal;
int count = 0;
while (it.hasNext()) {
cal = (Calendar)it.next();
Day daily = blog.getBlogForDay(cal.getTime());
String formattedNumber = numberFormatter.format(cal.get(Calendar.DAY_OF_MONTH));
if (formattedNumber.length() == 1) {
formattedNumber = " " + formattedNumber;
}
if (count % 7 == 0) {
out.write("<tr>");
}
// output padding if the date to display isn't in the month
if (cal.get(Calendar.MONTH) != firstDayOfMonth.get(Calendar.MONTH)) {
out.write("<td class=\"calendarDay\"> ");
} else if (now.get(Calendar.YEAR) == cal.get(Calendar.YEAR) &&
now.get(Calendar.MONTH) == cal.get(Calendar.MONTH) &&
now.get(Calendar.DAY_OF_MONTH) == cal.get(Calendar.DAY_OF_MONTH)) {
out.write("<td class=\"calendarToday\">");
if (daily.hasBlogEntries()) {
out.write(" <a href=\"" + UrlRewriter.doRewrite(daily.getPermalink()) + "\">" + formattedNumber + "</a> ");
} else {
out.write(" " + formattedNumber + " ");
}
} else {
if (daily.hasBlogEntries()) {
out.write("<td class=\"calendarDayWithEntries\">");
out.write(" <a href=\"" + UrlRewriter.doRewrite(daily.getPermalink()) + "\">" + formattedNumber + "</a> ");
} else {
out.write("<td class=\"calendarDay\">");
out.write(" " + formattedNumber + " ");
}
}