001package edu.pdx.cs410J.xml; 002 003import org.w3c.dom.*; 004 005/** 006 * This class represents a resident (person) whose first and last 007 * name, as well as whose middle initial, is listing in a phone book. 008 * A <code>Resident</code> is constructed from an XML DOM tree. If we 009 * were doing this for real, we'd want a way of constructing an empty 010 * <code>Resident</code> and filling in its fields. 011 */ 012public class Resident extends PhoneBookEntry { 013 private String firstName; 014 private String middleInitial; 015 private String lastName; 016 private boolean unlisted = false; 017 018 /** 019 * Create a new <code>Resident</code> from a <code>Element</code> in 020 * a DOM tree. 021 */ 022 public Resident(Element root) { 023 NodeList elements = root.getChildNodes(); 024 for (int i = 0; i < elements.getLength(); i++) { 025 Node node = elements.item(i); 026 027 if (!(node instanceof Element)) { 028 continue; 029 } 030 031 Element element = (Element) node; 032 033 switch (element.getNodeName()) { 034 case "first-name": { 035 Node name = element.getFirstChild(); 036 Node text = name.getFirstChild(); 037 this.firstName = text.getNodeValue(); 038 break; 039 } 040 041 case "initial": { 042 Node text = element.getFirstChild(); 043 this.middleInitial = text.getNodeValue(); 044 break; 045 } 046 047 case "last-name": { 048 Node name = element.getFirstChild(); 049 Node text = name.getFirstChild(); 050 this.lastName = text.getNodeValue(); 051 break; 052 } 053 054 case "address": 055 fillInAddress(element); 056 break; 057 058 case "phone": 059 fillInPhone(element); 060 break; 061 } 062 } 063 064 // Check for the "unlisted" attribute 065 if (root.getAttribute("unlisted").equals("true")) { 066 this.unlisted = true; 067 } 068 } 069 070 public String toString() { 071 return this.firstName + " " + this.middleInitial + " " + 072 this.lastName + "\n" + 073 super.toString(); 074 } 075}