Source code for XMLPhoneEntries.java

package PhoneApp.XML;

import java.util.Stack;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.apache.xerces.parsers.DOMParser;

/**
 * Parses phone entries from an XML file, and allows
 * for access to the parsed entries by extending the
 * Stack class. This class is very weakly implemented,
 * providing very little validation, and assuming a very
 * strict conformance to the expected structure. Each
 * phone entry must have seven fields in the correct
 * order.
 *
 * @version $Date: 2001/06/25 $
 * @author  Steve Franklin
 * @since   jdk 1.3.1
 */


public class XMLPhoneEntries extends Stack {

 /**
  * Accepts a filename, and initializes the object
  * based on the contents of the filename.
  */
  public XMLPhoneEntries(String fileName) {
    initialize(fileName);
  }

 /**
  * This method attempts to load and parse the file
  * specified by the variable <b>fileName</b>. If the
  * file does not exist, then the initialization will
  * not continue.
  */
  private void initialize(String fileName) {
    parser = new DOMParser();
    try {
      parser.parse(fileName);
      Document phoneList =  parser.getDocument();
      processRoot(phoneList);
    } catch ( Exception e ) {
      System.out.println("Unable to parse " + fileName);
    }
  }
  
 /**
  * If this method is passed a non-null node, it will attempt
  * to find child elements within the document that are named
  * "PhonebookEntry". Any such elements are passed
  * to another function for subsequent processing. Other elements
  * are disregarded, as entries must be enclosed in the proper
  * element.
  */
  private void processRoot(Node node) {
    if ( node == null ) {
      return;
    } else {
      Node doc = ((Document)node).getDocumentElement();
      NodeList children = doc.getChildNodes();
      for(int i=0; i< children.getLength(); i++) {
        Node item = children.item(i);
        String elementName = "PhonebookEntry";
        if ( elementName.equals(item.getNodeName())) {
          parseEntries(children.item(i));
        }
      }
    }
  }

 /**
  * Navigates a "PhonebookEntry" element looking for
  * all fields that make up a phonebook entry. This code is
  * very weak and hardly qualifies as usable or proper coding
  * practice.
  */
  private void parseEntries(Node node) {
    String[] container = new String[7];
    if ( node == null ) {
      return;
    } else {
      NodeList children = node.getChildNodes();
      for(int i=0; i< children.getLength(); i++) {
        int fieldNumber = i/2;
        Node item = children.item(i);
        if ( item.getNodeType() == Node.ELEMENT_NODE ) {
          NodeList values = item.getChildNodes();
          String nodeValue = "";
          for(int j=0; j< values.getLength() ; j++) {
            Node value = values.item(j);
            nodeValue = value.getNodeValue();
          }
          container[fieldNumber] = nodeValue;
        }
      }
    }
    push(container);
  }

  /** Provides the parsing engine for ingesting XML files */
  private DOMParser parser;

}