com.aricojf.util.ConfigParser.java Source code

Java tutorial

Introduction

Here is the source code for com.aricojf.util.ConfigParser.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aricojf.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 *
 * @author  <1829201968113469592826@163.com>
 */
public class ConfigParser {

    private static final String FILE_SEPARATOR = "/";

    private static String value = "";

    /**
     * ??
     * ?String "/?/?"
     * ep: "config/conn/user"
     * @return
     */
    public static String parseConfigParmByPath(String configFilePath, String nodes) {
        value = "";
        FileInputStream fs = null;
        File file = new File(configFilePath);
        if (file.exists()) {
            file = null;
            try {
                fs = new FileInputStream(configFilePath);
                SAXReader reader = new SAXReader();
                org.dom4j.Document document = reader.read(fs);
                org.dom4j.Element element = document.getRootElement();
                String[] nod = nodes.trim().split(FILE_SEPARATOR);
                for (int i = 1; i < nod.length; i++) {
                    List<Element> elements = element.elements(nod[i]);
                    /**
                     * ???????
                     * ??xml?????
                     */
                    for (Element ele : elements) {
                        element = ele;
                        value = ele.getText();
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                closeConn(fs);
            }
        }
        return null == value ? "" : value;
    }

    /**
     * ?
     * @param fs
     */
    public static void closeConn(InputStream fs) {
        if (null != fs) {
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}