excelconfigutil.java

来自「通过apache 的jxl中件间,实现excel文件的导入导出的小例子」· Java 代码 · 共 103 行

JAVA
103
字号
package cn.capitek.TorchRelay.util;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.digester.Digester;
import org.xml.sax.SAXException;

/**
 * 用于处理系统配置的工具类
 * 它的作用是解析配置文件,生成配置对象
 * 
 * @author cellen
 *
 */
public class ExcelConfigUtil
{
	// 缺省配置文件的名称
	public static final String DEFAULT_CONFI_FILE = "ExcelConfig.xml";

	// 配置对象的实例
	public static final ExcelXmlConfig MESSAGE_CONFIG;

	static
	{
		// 创建Digester对象的实例
		Digester digester = new Digester();
		digester.setValidating(false);

		// 初始化根配置对象
		digester.addObjectCreate("config", "cn.capitek.TorchRelay.util.ExcelXmlConfig");
		digester.addSetProperties("config");

		
          
		//add 

		digester.addObjectCreate("config/excel",
		"cn.capitek.TorchRelay.util.ExcelConfig");
digester.addSetProperties("config/excel");
digester.addSetNext("config/excel", "addExcelConfig",
		"cn.capitek.TorchRelay.util.ExcelConfig");

// 设置ExcelConfig对象的配置参数
digester.addObjectCreate("config/excel/exceltype",
		"cn.capitek.TorchRelay.util.ExcelType");
digester.addSetProperties("config/excel/exceltype");
digester.addSetNext("config/excel/exceltype", "addExcelType",
		"cn.capitek.TorchRelay.util.ExcelType");

// 设置ExcelType对象的配置参数
digester.addObjectCreate("config/excel/exceltype/property",
"cn.capitek.TorchRelay.util.Property");
digester.addSetProperties("config/excel/exceltype/property");
digester.addSetNext("config/excel/exceltype/property", "addProperty",
"cn.capitek.TorchRelay.util.Property");
		//end add
		

		// 读取配置文件
		ClassLoader classLoader = Thread.currentThread()
				.getContextClassLoader();
		InputStream in = classLoader.getResourceAsStream(DEFAULT_CONFI_FILE);
		ExcelXmlConfig config = null;
		try
		{
			// 解析配置文件,生成根配置对象
			if (in != null)
				config = (ExcelXmlConfig) digester.parse(in);
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (SAXException e)
		{
			e.printStackTrace();
		}
		MESSAGE_CONFIG = config;
	}

	/**
	 * 读取根配置对象
	 * 
	 * @return 根配置对象
	 */
	public static ExcelXmlConfig getConfig()
	{
		return ExcelConfigUtil.MESSAGE_CONFIG;
	}

	
	/**
	 * 读取ExcelConfig的配置对象
	 * 
	 * @return ExcelConfig的配置对象
	 */
	public static ExcelConfig getExcelConfig()
	{
		return MESSAGE_CONFIG.getExcelConfig();
	}

	
}

⌨️ 快捷键说明

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