working on DOM
git-svn-id: svn://anubis/gvsu@121 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
aabe876290
commit
0b903670ee
@ -1,21 +1,37 @@
|
|||||||
|
|
||||||
import javax.xml.parsers.*;
|
import javax.xml.parsers.*;
|
||||||
import org.w3c.dom.*;
|
import org.w3c.dom.*;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
public class JDOM
|
public class JDOM
|
||||||
{
|
{
|
||||||
public static int main(String args[])
|
public static void main(String args[])
|
||||||
{
|
{
|
||||||
if (args.length != 1)
|
if (args.length != 1)
|
||||||
{
|
{
|
||||||
System.err.println("Usage: java JDOM <XMLFile>");
|
System.err.println("Usage: java JDOM <XMLFile>");
|
||||||
return 42;
|
return;
|
||||||
}
|
}
|
||||||
JDOM j = new JDOM();
|
JDOM j = new JDOM();
|
||||||
return j.parse(args[0]);
|
j.parse(args[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int parse(String file)
|
private HashMap<String, String> m_weekDayNames;
|
||||||
|
|
||||||
|
public JDOM()
|
||||||
|
{
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void parse(String file)
|
||||||
{
|
{
|
||||||
Document doc = null;
|
Document doc = null;
|
||||||
try
|
try
|
||||||
@ -25,16 +41,69 @@ public class JDOM
|
|||||||
doc = db.parse(file);
|
doc = db.parse(file);
|
||||||
if (doc != null)
|
if (doc != null)
|
||||||
printHTMLSchedule(doc);
|
printHTMLSchedule(doc);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
System.out.println(e.getMessage());
|
System.out.println(e.getMessage());
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void printHTMLSchedule(Document d)
|
private void printHTMLSchedule(Node n)
|
||||||
{
|
{
|
||||||
|
PrintStream out = System.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 { background-color: #FF8; }");
|
||||||
|
out.println("</style>");
|
||||||
|
out.println("</head><body>");
|
||||||
|
out.println("<table><caption>Daily Planner</caption><tr><th> </th><th>AM</th><th>PM</th></tr>");
|
||||||
|
|
||||||
|
printHTMLSchedule( ((Document) n).getDocumentElement() );
|
||||||
|
|
||||||
|
out.println("</table>\n</body>\n</html>");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Node.ELEMENT_NODE:
|
||||||
|
String name = n.getNodeName();
|
||||||
|
NodeList children = n.getChildNodes();
|
||||||
|
if (name.equals("dailyplanner"))
|
||||||
|
{
|
||||||
|
for (int i = 0; i < children.getLength(); i++)
|
||||||
|
printHTMLSchedule(children.item(i));
|
||||||
|
}
|
||||||
|
else if (m_weekDayNames.containsKey(name))
|
||||||
|
{
|
||||||
|
out.print("<tr><td class=\"day\">");
|
||||||
|
out.print(m_weekDayNames.get(name));
|
||||||
|
out.println("</td>");
|
||||||
|
for (int i = 0; i < children.getLength(); i++)
|
||||||
|
{
|
||||||
|
out.print("<td>");
|
||||||
|
printAMPM(children.item(i));
|
||||||
|
out.print("</td>");
|
||||||
|
}
|
||||||
|
out.println("</tr>");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void printAMPM(Node n)
|
||||||
|
{
|
||||||
|
NodeList children = n.getChildren();
|
||||||
|
for (int i = 0; i < children.getLength(); i++)
|
||||||
|
{
|
||||||
|
Node c = children.item(i);
|
||||||
|
if (c.getType() == Node.TEXT_NODE)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user