1    package geekblog;
2    
3    
4    import java.text.*;
5    import java.util.*;
6    import javax.servlet.*;
7    import javax.servlet.http.*;
8    import javax.servlet.jsp.*;
9    
10   
11   public class Utils
12   {
13     /**
14      * Figure out what the root URL of the weblog is. Can't just use the name of
15      * the servlet context, as we might be co-located deep inside another servlet
16      * context.
17      */
18     public static String getRootURL(HttpServletRequest request)
19     {
20       String requestURL = request.getRequestURL().toString();
21       return requestURL.substring(0, requestURL.lastIndexOf("/"));
22     }
23   
24   
25     /**
26      * Scrub passed string (assumed to be HTML) for malicious tags; simply escape
27      * the tag in the event it doesn't fit into the allowed set.
28      *
29      * TODO: In the future, allow the set to be user-configurable?
30      */
31     public static String scrubHTML(String html)
32     {
33       return html;
34     }
35   
36   
37     /**
38      * Generate the HTML calendar text for the side panel.
39      */
40     private static final String placeholder = "<td align\"right\" class=\"calendar\"> </td>";
41     private static final String dayPrefix = "<td align=\"right\" class=\"calendar\">";
42     private static final String todayPrefix = "<td align\"right\" class=\"calendarcurrent\">";
43     private static final String daySuffix = "</td>";
44     public static String getCalendar(Calendar workingDate, DataManager dataMgr)
45     {
46       GregorianCalendar date = (GregorianCalendar)workingDate.clone();
47       SimpleDateFormat datef = new SimpleDateFormat("yyyyMMdd");
48       GregorianCalendar today = new GregorianCalendar();
49       today.setTime(new Date());
50   
51       java.io.StringWriter writer = new java.io.StringWriter();
52       java.io.PrintWriter out = new java.io.PrintWriter(writer);
53   
54       // Generate header
55       //
56       out.println("<center><span class=\"calendarhead\">" + 
57                   "<a href=\"index.jsp?month=" + new SimpleDateFormat("yyyyMM").format(workingDate.getTime()) + "\">" + 
58                     new SimpleDateFormat("MMMM yyyy").format(workingDate.getTime()) + "</a>" +
59                   "</span></center>");
60       out.println("<table border=\"0\" bgcolor=\"#FFFFFF\" cellpadding=\"2\" cellspacing=\"2\" class=\"border\">");
61       out.println("<tr>");
62       out.println("<th align=right class=\"calendarhead\">Sun</th>");
63       out.println("<th align=right class=\"calendarhead\">Mon</th>");
64       out.println("<th align=right class=\"calendarhead\">Tue</th>");
65       out.println("<th align=right class=\"calendarhead\">Wed</th>");
66       out.println("<th align=right class=\"calendarhead\">Thu</th>");
67       out.println("<th align=right class=\"calendarhead\">Fri</th>");
68       out.println("<th align=right class=\"calendarhead\">Sat</th>");
69       out.println("</tr>");
70   
71       ArrayList weeksInMonth = new ArrayList();
72       for (int i=1; i<=date.getActualMaximum(Calendar.WEEK_OF_MONTH); i++)
73         weeksInMonth.add(new String[] { new String(placeholder), new String(placeholder), new String(placeholder), new String(placeholder), new String(placeholder), new String(placeholder), new String(placeholder) } );
74   
75       String todayStr = datef.format(today.getTime());
76       int day = 1;
77       do
78       {
79         date.set(Calendar.DAY_OF_MONTH, day++);
80         int week = date.get(Calendar.WEEK_OF_MONTH);
81         int dayOfWeek = date.get(Calendar.DAY_OF_WEEK);
82         String dayStr = datef.format(date.getTime());
83   
84         String[] weekDays = (String[])weeksInMonth.get(week - 1);
85   
86         weekDays[dayOfWeek - 1] = 
87           (dayStr.equals(todayStr) ? todayPrefix : dayPrefix) +
88           ((dataMgr.getEntryCountForDate(date.getTime()) > 0) ? "<a href=\"index.jsp?date=" + datef.format(date.getTime()) + "\">" + date.get(Calendar.DAY_OF_MONTH) + "</a>" : "" + date.get(Calendar.DAY_OF_MONTH)) +
89           daySuffix;
90   
91       } while (day <= date.getActualMaximum(Calendar.DAY_OF_MONTH));
92       
93   
94       for (Iterator iter = weeksInMonth.iterator(); iter.hasNext(); )
95       {
96         out.println("<tr>");
97         String[] weekDays = (String[])iter.next();
98         for (int j=0; j<7; j++)
99           out.println(weekDays[j]);
100        out.println("</tr>");
101      }
102  
103      out.println("</table>");
104  
105      return (writer.toString());
106    }
107  }
108