InitParamsParser.java

This class parses an XML file to retrieve parameter information. The parser used is the Xerces SAX parser from Apache.org.
package cpa.app.vip;

import java.io.File;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.Properties;
import java.io.IOException;
import org.xml.sax.XMLReader;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;

/************************************************************
 * This class provides methods for obtaining initialization
 * parameter values.
 *
 * @author Paul McKinney
 ************************************************************/
class InitParamsParser extends DefaultHandler
{

   private String xmlFile = null;
   private String parserName = null;
   private String section = null;
   private String paramName = null;
   private String paramValue = null;

   private boolean isAtSectionName = false;
   private boolean isAtParamName = false;
   private boolean isAtParamValue = false;
   private boolean foundSectionName = false;
   private boolean foundParamName = false;
   private boolean foundParamValue = false;

   public String getInitParam(String section, String paramName)
   {
      String xmlFileUrl = null;
      InitParamsParser ipp = new InitParamsParser();
      File file = null;
      URL url = null;

      ipp.section = section;
      ipp.paramName = paramName;
      foundSectionName = false;
      foundParamName = false;
      foundParamValue = false;

      xmlFile = System.getProperty("cpa.app.vip.initParamsXMLFileLoc");
      file = new File(xmlFile);
      try
      {
         url = file.toURL();
      }
      catch (MalformedURLException ex)
      {
         System.err.println("Error:InitParamsParser:getInitParam");
      }

      xmlFileUrl = url.toExternalForm();
      parserName = "org.apache.xerces.parsers.SAXParser";

      try
      {
         XMLReader parser = XMLReaderFactory.createXMLReader(parserName);
         parser.setContentHandler(ipp);
         parser.parse(xmlFileUrl);
      }
      catch (SAXException ex)
      {
         ex.printStackTrace();
      }
      catch (IOException ex)
      {
         ex.printStackTrace();
      }

      return ipp.paramValue;
   }


   public void startElement(String namespaceURI,
                       String localName,
                       String qName,
                       Attributes attrs)
                              throws SAXException
   {
      if (localName.equals("section-name"))
         isAtSectionName = true;
      if (localName.equals("param-name"))
         isAtParamName = true;
      if (localName.equals("param-value"))
         isAtParamValue = true;
   }


   public void endElement(String namespaceURI,
                          String localName,
                          String qName)
                              throws SAXException
   {
      isAtSectionName = false;
      isAtParamName = false;
      isAtParamValue = false;
   }


   public void characters(char buf[], int offset, int len)
                              throws SAXException
   {
      String elementValue = null;
      elementValue = new String(buf, offset, len);

      if (isAtSectionName && elementValue.equals(section))
      {
         foundSectionName = true;
         return;
      }

      if (isAtParamName && foundSectionName && elementValue.equals(paramName))
      {
         foundParamName = true;
         return;
      }

      if (isAtParamValue && foundSectionName && foundParamName && !foundParamValue)
      {
         paramValue = elementValue;
         foundParamValue = true;
      }
   }

}