
import javax.xml.parsers.*;
import org.xml.sax.helpers.*;
import org.xml.sax.*;
import java.util.HashMap;
import java.io.*;

public class JSAX extends DefaultHandler
{
    public static void main(String args[])
    {
        if (args.length != 1)
        {
            System.err.println("Usage: java JSAX <XMLFile>");
            return;
        }
        JSAX j = new JSAX();
        j.parse(args[0]);
    }

    private HashMap<String, String> m_weekDayNames;
    private boolean m_displayText;

    public JSAX()
    {
        m_weekDayNames = new HashMap<String, String>();
        m_weekDayNames.put("sunday", "Sunday");
        m_weekDayNames.put("monday", "Monday");
        m_weekDayNames.put("tuesday", "Tuesday");
        m_weekDayNames.put("wednesday", "Wednesday");
        m_weekDayNames.put("thursday", "Thursday");
        m_weekDayNames.put("friday", "Friday");
        m_weekDayNames.put("saturday", "Saturday");
        m_displayText = false;
    }

    public void parse(String file)
    {
        printPre(System.out);
        try
        {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            sp.parse(file, this);
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
        printPost(System.out);
    }

    private void printPre(PrintStream out)
    {
        out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"");
        out.println("\t\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
        out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">");
        out.println("<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>Daily Planner</title>");
        out.println("<style type=\"text/css\">");
        out.println("  .day, th { background-color: #FF8; }");
        out.println("  table, td, th { border: solid #000 1px; padding: 3px; }");
        out.println("</style>");
        out.println("</head><body>");
        out.println("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><caption>Daily Planner</caption>\n<tr><th>&nbsp;</th><th>AM</th><th>PM</th></tr>");
    }

    private void printPost(PrintStream out)
    {
        out.println("</table>\n</body>\n</html>");
    }

    public void startElement(String uri, String localName,
            String qName, Attributes attrs)
    {
//        System.out.println("In " + qName);
        if (m_weekDayNames.containsKey(qName))
        {
            System.out.print("<tr><td class=\"day\">");
            System.out.print(m_weekDayNames.get(qName));
            System.out.print("</td>");
        }
        else if (qName.equals("am") || qName.equals("pm"))
        {
            System.out.print("<td>");
            m_displayText = true;
        }
    }

    public void endElement(String uri, String localname, String qName)
    {
        if (m_weekDayNames.containsKey(qName))
        {
            System.out.println("</tr>");
        }
        else if (qName.equals("am") || qName.equals("pm"))
        {
            System.out.print("</td>");
            m_displayText = false;
        }
    }

    public void characters(char ch[], int start, int length)
    {
        if (m_displayText)
        {
            String content = new String(ch, start, length).trim();
            System.out.print(content);
        }
    }
}
