java 代码
-
package
org.wanwei.test.xmlreader; -
import
java.io.InputStream; -
import
java.util.HashMap; -
import
java.util.Iterator; -
import
java.util.List; -
import
java.util.Map; -
import
java.util.Set; -
import
org.apache.log4j.Logger; -
import
org.jdom.Document; -
import
org.jdom.Element; -
import
org.jdom.input.SAXBuilder; -
/**
-
* 读取系统配置文件systemConfig.xml.
-
* 只支持读取简单的XML配置:
-
* eg:<assocication>
-
* <bj_comp_eff_date>2007-06-20</bj_comp_eff_date>
-
* </assocication>
-
* 不支持带属性的XML读取:
-
* <Controler type=”1″ factoryClass=”com.elinksoft.bsp.action.ActionBeanFactory” />
-
* @author wanwei
-
* @since 2007-7-4
-
*/
-
public
class
SystemConfig - {
-
private
static
final
Logger logger = Logger.getLogger(SystemConfig.
class
); -
private
static
final
String DEFAULT_SPLIT =
“/”
; -
private
static
final
Map cacheMap =
new
HashMap(); -
private
static
boolean
initialize =
false
; -
/**
-
* 从systemConfig.xml文件中读取相应配置
-
* note:第一次读取需要初始化,以后都从缓存中读取.
-
* @param configPath eg:/config/assocication/bj_comp_eff_date
-
* @return String not null or empty
-
* @throws Exception “unmatching configPath” will throw if no matching text found
-
*/
-
public
static
String getConfigTextValue(String configPath)
throws
Exception - {
-
if
( !initialize ) - {
- initialize();
- }
- String value = (String) cacheMap.get(configPath);
-
if
( value ==
null
|| value.length() ==
0
) - {
-
getConfigTextValue(configPath,
true
); - }
-
return
value; - }
-
/**
-
* 从systemConfig.xml文件中读取相应配置
-
* @param configPath eg:/config/assocication/bj_comp_eff_date
-
* @param reinitialize if true ,system will read the xml files again.
-
* @return String not null or empty
-
* @throws Exception Exception “unmatching configPath” will throw if no matching text found
-
*/
-
public
static
final
String getConfigTextValue(String configPath,
boolean
reinitialize) -
throws
Exception - {
-
if
( reinitialize ) - {
- initialize();
- }
- String value = (String) cacheMap.get(configPath);
-
if
( value ==
null
|| value.length() ==
0
) -
throw
new
Exception(
“unmatching configPath:”
+ configPath); -
return
value; - }
-
/**
-
* read xml files and cache them.
-
* @throws Exception
-
*/
-
private
static
void
initialize()
throws
Exception - {
-
SAXBuilder sb =
new
SAXBuilder(); -
InputStream is = ClassLoader.getSystemResourceAsStream(
“org/wanwei/test/xmlreader/systemconfig.xml”
); -
Document doc = sb.build(is);
// 构造文档对象
-
Element root = doc.getRootElement();
// 获取根元素
-
recursiveReadElement(root,
“”
);
// 递归读取xml元素
-
initialize =
true
; -
// logger the cacheMap.
-
if
( logger.isInfoEnabled() ) -
logger.info(
“initialize system config:\n”
+mapToString(cacheMap)); -
//System.out.println(mapToString(cacheMap));
- }
-
private
static
void
recursiveReadElement(Element root, String parentName) - {
- List list = root.getChildren();
- String text = root.getTextTrim();
- String url = parentName + DEFAULT_SPLIT + root.getName();
-
if
( text !=
null
&& text.length() >
0
) - {
- cacheMap.put(url, text);
- }
-
if
( list !=
null
&& list.size() >=
0
) - {
-
for
(
int
i =
0
; i < list.size(); i++ ) - {
-
if
( list.get(i)
instanceof
Element ) - {
- recursiveReadElement((Element) list.get(i), url);
- }
- }
- }
- }
-
private
static
String mapToString(Map map) - {
-
StringBuffer mapBuffer =
new
StringBuffer(); -
if
( map ==
null
|| map.isEmpty() ) -
return
“”
; - Set KeySet = map.keySet();
- Iterator iterator = KeySet.iterator();
-
while
( iterator.hasNext() ) - {
- String key = (String) iterator.next();
- String value = (String) map.get(key);
-
mapBuffer.append(key +
“=”
+ value); -
if
( iterator.hasNext() ) -
mapBuffer.append(
“;\n”
); - }
-
return
mapBuffer.toString(); - }
-
public
static
void
main(String[] arg0) - {
-
try
- {
-
System.out.println(SystemConfig.getConfigTextValue(
“/config/assocication/bj_comp_eff_date”
)); -
System.out.println(
“OK”
); -
}
catch
( Exception e ) - {
- e.printStackTrace();
-
System.out.println(
“ERROR”
); - }
- }
- }