001package edu.pdx.cs410J.xml;
002
003import org.w3c.dom.*;
004
005import javax.xml.parsers.DocumentBuilder;
006import javax.xml.parsers.DocumentBuilderFactory;
007import javax.xml.parsers.ParserConfigurationException;
008import javax.xml.transform.*;
009import javax.xml.transform.dom.DOMSource;
010import javax.xml.transform.stream.StreamResult;
011import java.io.File;
012import java.io.PrintStream;
013import java.net.MalformedURLException;
014
015/**
016 * This program builds a DOM tree that represents a phone book and
017 * writes it to a file.
018 */
019public class BuildPhonebook {
020  private static final PrintStream err = System.err;
021
022  public static void main(String[] args) {
023    Document doc = null;
024
025    String publicID = null;  // Who cares?
026    String systemID = null;
027    try {
028      File dtd = new File("phonebook.dtd");
029      systemID = dtd.toURL().toString();
030
031    } catch (MalformedURLException ex) {
032      err.println("** Bad URL: " + ex);
033      System.exit(1);
034    }
035
036    // Create an empty Document
037    try {
038      DocumentBuilderFactory factory =
039        DocumentBuilderFactory.newInstance();
040      factory.setValidating(true);
041
042      DocumentBuilder builder = factory.newDocumentBuilder();
043
044      DOMImplementation dom =
045        builder.getDOMImplementation();
046      DocumentType docType = 
047        dom.createDocumentType("phonebook", publicID, systemID);
048      doc = dom.createDocument(null, "phonebook", docType);
049
050    } catch (ParserConfigurationException ex) {
051      ex.printStackTrace(System.err);
052      System.exit(1);
053
054    } catch (DOMException ex) {
055      // Eep, this is bad
056      ex.printStackTrace(System.err);
057      System.exit(1);
058    }
059    
060    // Construct the DOM tree
061    try {
062      Element root = doc.getDocumentElement();
063
064      Element biz = doc.createElement("business");
065      root.appendChild(biz);
066
067      Element name = doc.createElement("name");
068      biz.appendChild(name);
069      String br = "Tripwire, Inc.";
070      name.appendChild(doc.createTextNode(br));
071      
072      Element address = doc.createElement("address");
073      biz.appendChild(address);
074
075      Element street1 = doc.createElement("street");
076      address.appendChild(street1);
077      String st1 = "308 SW 2nd Ave";
078      street1.appendChild(doc.createTextNode(st1));
079      
080      Element street2 = doc.createElement("street");
081      address.appendChild(street2);
082      String st2 = "Suite 400";
083      street2.appendChild(doc.createTextNode(st2));
084
085      Element city = doc.createElement("city");
086      address.appendChild(city);
087      city.appendChild(doc.createTextNode("Portland"));
088
089      Element state = doc.createElement("state");
090      address.appendChild(state);
091      state.appendChild(doc.createTextNode("OR"));
092
093      Element zip = doc.createElement("zip");
094      address.appendChild(zip);
095      zip.appendChild(doc.createTextNode("97205"));
096
097      Element phone = doc.createElement("phone");
098      biz.appendChild(phone);
099      phone.setAttribute("areacode", "503");
100      phone.setAttribute("number", "276-7500");
101
102    } catch (DOMException ex) {
103      ex.printStackTrace(err);
104      System.exit(1);
105    }
106
107    // Write the XML document to the console
108    try {
109      Source src = new DOMSource(doc);
110      Result res = new StreamResult(System.out);
111
112      TransformerFactory xFactory = TransformerFactory.newInstance();
113      Transformer xform = xFactory.newTransformer();
114      xform.setOutputProperty(OutputKeys.INDENT, "yes");
115      xform.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemID);
116      xform.transform(src, res);
117
118    } catch (TransformerException ex) {
119      ex.printStackTrace(System.err);
120      System.exit(1);
121    }
122  }
123
124}