FileDocCategorySizeDatePackage
ConfigSet.javaAPI DocExample4853Thu Dec 15 21:41:04 GMT 2005com.oreilly.jent.people.util

ConfigSet

public class ConfigSet extends Object
A ConfigSet 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

Fields Summary
private Document
mXMLConfig
Constructors Summary
public ConfigSet(String fileSpec)
Constructor for ConfigReader with a resource reference to the XML config file.


                 
       
    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());
      }
    }
  
public ConfigSet(Document data)
Constructor taking a pre-parsed Document as the source of the config data.

param
paramSpec
return
String

    mXMLConfig = data;
  
Methods Summary
public java.lang.StringgetParameter(java.lang.String paramSpec)
Find a named parameter in the loaded config file. The paramSpec argument uses a dot-delimited format to indicate the desired element from the XML config file. For example, if the config file contains "foobar. . .", then the application can request the "name" element like so:

ConfigReader config = new ConfigReader(...);
String name = config.getParameter("app-config.params.name");

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.

    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;