001package edu.pdx.cs410J.xml;
002
003import org.w3c.dom.*;
004
005/**
006 * This class represents a business whose name, address and phone
007 * number are listed in a phone book.  A <code>Business</code> is
008 * constructed from an XML DOM tree.  If we were doing this for real,
009 * we'd want a way of constructing an empty <code>Business</code> and
010 * filling in its fields.
011 */
012public class Business extends PhoneBookEntry {
013  protected String name;
014
015  /**
016   * Create a new <code>Business</code> from an <code>Element</code>
017   * in a DOM tree.
018   */
019  public Business(Element root) {
020    NodeList elements = root.getChildNodes();
021    for (int i = 0; i < elements.getLength(); i++) {
022      Node node = elements.item(i);
023
024      if (!(node instanceof Element)) {
025        continue;
026      }
027
028      Element element = (Element) node;
029
030      switch (element.getNodeName()) {
031        case "name":
032          Node text = element.getFirstChild();
033          this.name = text.getNodeValue();
034          break;
035
036        case "address":
037          fillInAddress(element);
038          break;
039
040        case "phone":
041          fillInPhone(element);
042          break;
043
044        default:
045          String s = "Unknown element: " + element.getNodeName() + " (" +
046            element.getNodeValue() + ")";
047          throw new IllegalArgumentException(s);
048      }
049    }
050  }
051
052  public String toString() {
053    return this.name + "\n" + super.toString();
054  }
055}