working on SAX
git-svn-id: svn://anubis/gvsu@124 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
10f6e8ea4a
commit
437e070896
@ -48,26 +48,36 @@ public class JDOM
|
||||
}
|
||||
}
|
||||
|
||||
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> </th><th>AM</th><th>PM</th></tr>");
|
||||
}
|
||||
|
||||
private void printPost(PrintStream out)
|
||||
{
|
||||
out.println("</table>\n</body>\n</html>");
|
||||
}
|
||||
|
||||
private void printHTMLSchedule(Node n, PrintStream out)
|
||||
{
|
||||
int type = n.getNodeType();
|
||||
switch (type)
|
||||
{
|
||||
case Node.DOCUMENT_NODE:
|
||||
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> </th><th>AM</th><th>PM</th></tr>");
|
||||
printPre(out);
|
||||
|
||||
printHTMLSchedule( ((Document) n).getDocumentElement(), out );
|
||||
|
||||
out.println("</table>\n</body>\n</html>");
|
||||
printPost(out);
|
||||
break;
|
||||
|
||||
case Node.ELEMENT_NODE:
|
||||
|
@ -1,7 +1,97 @@
|
||||
|
||||
public class JSAX
|
||||
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> </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"))
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public void endElement(String uri, String localname, String qName)
|
||||
{
|
||||
if (
|
||||
}
|
||||
|
||||
public void characters(char ch[], int start, int length)
|
||||
{
|
||||
String content = new String(ch, start, length).trim();
|
||||
System.out.println("characters: " + content);
|
||||
}
|
||||
}
|
||||
|
@ -4,4 +4,4 @@ all:
|
||||
|
||||
run:
|
||||
java JDOM test-sched.xml > test-sched-dom.html
|
||||
|
||||
java JSAX test-sched.xml > test-sched-sax.html
|
||||
|
Loading…
x
Reference in New Issue
Block a user