读取xml类Configuration

package com.thinkive.base.config;

 

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

 

import org.apache.log4j.Logger;

import org.dom4j.Document;

import org.dom4j.Element;

 

import com.thinkive.base.util.StringHelper;

import com.thinkive.base.util.XMLHelper;

 

 

public class Configuration

{

 

private static Logger logger = Logger.getLogger(Configuration.class);

 

private static Map items = new HashMap();

 

private static String CONFIG_FILE_NAME = "configuration.xml";

 

static

{

loadConfig();

}

 

/**

* 读入配置文件

*/

private static void loadConfig()

{

try

{

 

Document document = XMLHelper.getDocument(Configuration.class, CONFIG_FILE_NAME);

if (document != null)

{

Element systemElement = document.getRootElement();

List catList = systemElement.elements("category");

for (Iterator catIter = catList.iterator(); catIter.hasNext();)

{

Element catElement = (Element) catIter.next();

String catName = catElement.attributeValue("name");

if (StringHelper.isEmpty(catName))

{

continue;

}

 

List itemList = catElement.elements("item");

for (Iterator itemIter = itemList.iterator(); itemIter.hasNext();)

{

Element itemElement = (Element) itemIter.next();

String itemName = itemElement.attributeValue("name");

String value = itemElement.attributeValue("value");

if (!StringHelper.isEmpty(itemName))

{

items.put(catName + "." + itemName, value);

}

}

}

}

}

catch (Exception ex)

{

logger.error("", ex);

}

finally

{

}

 

}

 

/**

* 获得字串配置值

*

* @param name

* @return

*/

public static String getString(String name)

{

String value = (String) items.get(name);

return (value == null) ? "" : value;

}

 

/**

* 获得字串配置值,若为空,则返回缺省值

*

* @param name

* @param defaultValue

* @return

*/

public static String getString(String name, String defaultValue)

{

String value = (String) items.get(name);

if (value != null && value.length() > 0)

return value;

else

return defaultValue;

}

 

/**

* 获得整型配置值

*

* @param name

* @return

*/

public static int getInt(String name)

{

String value = getString(name);

try

{

return Integer.parseInt(value);

}

catch (NumberFormatException ex)

{

return 0;

}

}

 

/**

* 获得整型配置值

*

* @param name

* @return

*/

public static int getInt(String name, int defaultValue)

{

String value = getString(name);

try

{

return Integer.parseInt(value);

}

catch (NumberFormatException ex)

{

}

return defaultValue;

}

 

/**

* 获得布尔型配置值

*

* @param name

* @return

*/

public static boolean getBoolean(String name)

{

String value = getString(name);

return Boolean.valueOf(value).booleanValue();

}

 

/**

* 获得双精度浮点数配置值

* @param name

* @return

*/

public static double getDouble(String name, double defaultValue)

{

String value = getString(name);

try

{

return Double.parseDouble(value);

}

catch (NumberFormatException ex)

{

}

return defaultValue;

}

 

public static Map getItems()

{

return items;

}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值