//UseDOM.java
import java.io.*;
import java.net.*;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class UseDOM {
    private Document outputDoc;
    private Element body;
    private Element html;

    private HashMap authors = new HashMap();
    
    public String toString() {
        if (html != null) {
            return html.toString();
        }
        return super.toString();
    }
    
    public void processWithDOM(String urlString) 
      throws Exception {
        System.out.println(
          "Processing URL " + urlString);
        DocumentBuilderFactory dbf = 
          DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(urlString);
        Element elem = doc.getDocumentElement();
        NodeList nl = 
          elem.getElementsByTagName("author");
        for (int n=0; n<nl.getLength(); n++) 
        {
            Element author = (Element)nl.item(n);
            String id = author.getAttribute("id");
            String fullName = 
              author.getAttribute("fullName");          
            Element h2 = 
              outputDoc.createElement("H2");
            body.appendChild(h2);
            h2.appendChild(outputDoc.createTextNode(
              "by " +  fullName));
            Element list = 
              outputDoc.createElement("OL");
            body.appendChild(list);
            authors.put(id, list);
        }
        NodeList nlTips = 
              elem.getElementsByTagName("tip");
        for (int i=0; i<nlTips.getLength(); i++) 
        {
            Element tip = (Element)nlTips.item(i);
            String title = tip.getAttribute("title");           
            String htmlURL = 
              tip.getAttribute("htmlURL");
            String author = 
              tip.getAttribute("author");
            Node list = (Node) authors.get(author);
            Node item = list.appendChild(
              outputDoc.createElement("LI"));
            Element a = outputDoc.createElement("A");
            item.appendChild(a);
            a.appendChild(
              outputDoc.createTextNode(title));                     
            a.setAttribute("HREF", htmlURL);            
        }
    }
    
    public void createHTMLDoc(String heading) 
      throws ParserConfigurationException  
    {
        DocumentBuilderFactory dbf = 
          DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        outputDoc = db.newDocument();
        html = outputDoc.createElement("HTML");
        outputDoc.appendChild(html);
        body = outputDoc.createElement("BODY"); 
        html.appendChild(body);
        Element h1 = outputDoc.createElement("H1");
        body.appendChild(h1);
        h1.appendChild(
          outputDoc.createTextNode(heading));
    }
    
    public static void main(String[] args) {
        try {
            UseDOM ud = new UseDOM();
            ud.createHTMLDoc("JDC Tech Tips Archive");
            ud.processWithDOM(args[0]);
            String htmlOut = ud.toString();
            System.out.println(
              "Saving result to " + args[1]);
            FileWriter fw = new FileWriter(args[1]);
            fw.write(htmlOut, 0, htmlOut.length());
            fw.flush();
        }
        catch (Throwable t) {
            t.printStackTrace();
        }
    }
}