FileDocCategorySizeDatePackage
ConfigSet.javaAPI DocExample3651Sun Sep 11 17:34:14 BST 2005com.oreilly.jent.people.util

ConfigSet.java

package com.oreilly.jent.people.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.StringTokenizer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * A ConfigReader is used to read configuration information from an XML file
 * and provide an easy way for an application to query for specific elements
 * in the XML document that represent configuration information.
 * 
 * @author Jim Farley
 *
 */
public class ConfigSet {
  
  private Document mXMLConfig = null;

  /**
   * Constructor for ConfigReader with a resource reference to the XML config
   * file.
   */
  public ConfigSet(String fileSpec) throws IllegalArgumentException {
    InputStream xmlStr = getClass().getResourceAsStream(fileSpec);
    if (xmlStr != null) {
      try {
        DocumentBuilder docBuilder = 
          DocumentBuilderFactory.newInstance().newDocumentBuilder();
        mXMLConfig = docBuilder.parse(xmlStr);
      }
      catch (ParserConfigurationException pce) {
        throw new IllegalArgumentException("Failed parsing XML config file: " +
                                           pce.getMessage());
      }
      catch (SAXException se) {
        throw new IllegalArgumentException("Parsing error processing XML file: "
                                           + se.getMessage());
      }
      catch (IOException ioe) {
        throw new IllegalArgumentException("Failed loading XML config file: " +
                                           ioe.getMessage());
      }
    }
  }

  /** Constructor taking a pre-parsed Document as the source of the config
   *  data.
   * @param paramSpec
   * @return String
   */
  public ConfigSet(Document data) {
    mXMLConfig = data;
  }
  
  /** Find a named parameter in the loaded config file.  The 
   * <code>paramSpec</code> argument uses a dot-delimited format to indicate
   * the desired element from the XML config file.  For example, if the config 
   * file contains "<app-config><params><name>foobar</name></params>. . .", 
   * then the application can request the "name" element like so:
   * <pre>
   * 
   *     ConfigReader config = new ConfigReader(...);
   *     String name = config.getParameter("app-config.params.name");
   * 
   * </pre>
   * Note that this method is fairly limited in its capabilities.  It does not
   * support multiple paths through the document that match the given element.
   * Instead, it simply returns the value of first matching element that it 
   * finds in the document.  It also only supports element values that can be
   * converted directly to String values.  
   */
  public String getParameter(String paramSpec) {
    String paramVal = null;
    Element currNode = mXMLConfig.getDocumentElement();
    StringTokenizer tokenizer = new StringTokenizer(paramSpec, ".");
    boolean firstOne = true;
    while (tokenizer.hasMoreTokens()) {
      String elem = tokenizer.nextToken();
      if (firstOne) {
        firstOne = false;
        if (currNode.getNodeName().equals(elem)) {
          continue;
        }
        else {
          break;
        }
      }
      NodeList nodes = currNode.getElementsByTagName(elem);
      currNode = (Element)nodes.item(0);
    }
    if (currNode != null) {
      paramVal = currNode.getChildNodes().item(0).getNodeValue();
    }
    return paramVal;
  }
}