Package org.jconfig.parser

Source Code of org.jconfig.parser.DefaultConfigParser

/*
* XMLConfigParser.java
*
* Created on 12. Dezember 2002, 22:02
*/

package org.jconfig.parser;

import org.jconfig.Configuration;
import org.jconfig.DefaultConfiguration;
import org.jconfig.error.ErrorReporter;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
*
* @author  Andreas Mecky andreas.mecky@xcom.de
* @author  Terry Dye terry.dye@xcom.de
*/
public class DefaultConfigParser extends AbstractConfigParser {
   
    public DefaultConfigParser() {
    }
   
    /**
     *  Description of the Method
     *
     *@param  doc  Description of the Parameter
     */
    public Configuration parse(Document doc,String configName) {
        Configuration configuration = new DefaultConfiguration(configName);
        String currentCategory;
        getBaseConfigName(doc, configuration);
        getVariables(doc,configuration);
        getIncludeProperties(doc, configuration);
        // first we get all nodes where the element is category
        NodeList nl = doc.getElementsByTagName("category");
        for (int i = 0; i < nl.getLength(); i++) {
            // now we get every node from the list
            Node n = nl.item(i);
            // and get the name attribute for this category
            NamedNodeMap curAtt = n.getAttributes();
            Node curNode = curAtt.getNamedItem("name");
            currentCategory = curNode.getNodeValue();
//            System.err.println("processing for currentCategory "+currentCategory);
            configuration.setCategory(currentCategory);
            // now we process all children for this category
            for (Node child = n.getFirstChild(); child != null; child = child.getNextSibling()) {
                if (child.getNodeName().equals("property")) {
                    // we have found a property element and now we grab the name and value
                    // attributes
                    NamedNodeMap myAtt = child.getAttributes();
                    Node myNode = myAtt.getNamedItem("name");
                    if ( myNode != null ) {
                      String name = myNode.getNodeValue();
                      myNode = myAtt.getNamedItem("value");
                      if ( myNode != null ) {
                        String value = myNode.getNodeValue();
                        // if we have both then lets store it
                        if (name != null && value != null) {
                            // the key is always category/name
                            // e.g. general/congig_file
                            configuration.setProperty(name,value,currentCategory);
                        }
                      }
                      else {
                        ErrorReporter.getErrorHandler().reportError("No attribute 'value' found for property - ignoring entry");
                      }
                    }
                    else {
                      ErrorReporter.getErrorHandler().reportError("No attribute 'name' found for property - ignoring entry");
                    }
                }
            }
        }
        return configuration;
    }       
}
TOP

Related Classes of org.jconfig.parser.DefaultConfigParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.