From 0b903670eea33d1b16622ef60477e24cc9bcadf7 Mon Sep 17 00:00:00 2001 From: josh Date: Tue, 10 Jun 2008 01:49:17 +0000 Subject: [PATCH] working on DOM git-svn-id: svn://anubis/gvsu@121 45c1a28c-8058-47b2-ae61-ca45b979098e --- cs675/dom_sax/JDOM.java | 83 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 76 insertions(+), 7 deletions(-) diff --git a/cs675/dom_sax/JDOM.java b/cs675/dom_sax/JDOM.java index cb06f9b..3e757a9 100644 --- a/cs675/dom_sax/JDOM.java +++ b/cs675/dom_sax/JDOM.java @@ -1,21 +1,37 @@ import javax.xml.parsers.*; import org.w3c.dom.*; +import java.util.HashMap; +import java.io.*; public class JDOM { - public static int main(String args[]) + public static void main(String args[]) { if (args.length != 1) { System.err.println("Usage: java JDOM "); - return 42; + return; } JDOM j = new JDOM(); - return j.parse(args[0]); + j.parse(args[0]); } - public int parse(String file) + private HashMap m_weekDayNames; + + public JDOM() + { + m_weekDayNames = new HashMap(); + 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; try @@ -25,16 +41,69 @@ public class JDOM doc = db.parse(file); if (doc != null) printHTMLSchedule(doc); - return 0; } catch (Exception e) { 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(""); + out.println(""); + out.println("Daily Planner"); + out.println(""); + out.println(""); + out.println(""); + + printHTMLSchedule( ((Document) n).getDocumentElement() ); + + out.println("
Daily Planner
 AMPM
\n\n"); + 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(""); + out.print(m_weekDayNames.get(name)); + out.println(""); + for (int i = 0; i < children.getLength(); i++) + { + out.print(""); + printAMPM(children.item(i)); + out.print(""); + } + out.println(""); + } + 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) + { + } + } } }