defaultconfigparser.java

来自「jConfig,JAVA读取XML的开源项目」· Java 代码 · 共 80 行

JAVA
80
字号
/*
 * 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;
    }        
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?