📄 xmlconfig.java
字号:
package dev.trade.common.xml;
import org.dom4j.*;
import java.util.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import org.apache.log4j.*;
import java.net.*;
/**
* <p>Title: XML工具</p>
*
* <p>Description: XML配置文件工具类</p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: Newland</p>
*
* @author ZhengYanNan
* @version 1.0
*/
public class XMLConfig{
private static Logger log = Logger.getLogger(XMLConfig.class);
private String configFile = null;
private Document doc = null;
public XMLConfig(){
}
public XMLConfig(String fileName){
setConfigFile(fileName);
}
/**
* 是否已初始化
* @return boolean
*/
public boolean isInit(){
return (doc!=null);
}
/**
* 创建一个默认的配置document
* @return boolean
*/
public void initConfig(){
doc = XMLUtils.buildDocument();
}
/**
* 从默认设置的XML文件载入配置信息
* @return boolean
*/
public boolean loadConfig(){
if(configFile==null)
return false;
else
return loadConfig(configFile);
}
/**
* 从XML文件载入配置
* @param fileName String
* @return boolean
*/
public boolean loadConfig(String fileName){
try{
setConfigFile(fileName);
doc = XMLUtils.buildDocFromInputStream(getResourceAsStream(fileName));
return (doc!=null);
} catch(Exception ex){
doc = null;
log.error("无法载入配置文件:" + fileName, ex);
return false;
}
}
/**
* 重新载入配置文件(只支持已从loadConfig(fileName)文件读取过配置的情况)
* @return boolean
*/
public boolean reloadConfig(){
return loadConfig();
}
/**
* 保存配置信息到默认设置的XML文件
* @return boolean
*/
public boolean saveConfig(){
return saveConfig(configFile);
}
/**
* 保存配置信息到指定的XML文件
* @param fileName String
* @return boolean
*/
public boolean saveConfig(String fileName){
URL url= XMLConfig.class.getClassLoader().getResource(fileName);
boolean ret = XMLUtils.writeXML(doc, url.getFile());
if(!ret)
ret = XMLUtils.writeXML(doc, fileName);
return ret;
}
/**
* 设置配置文件名
* @param fileName String
*/
public void setConfigFile(String fileName){
this.configFile = fileName;
}
/**
* 获取配置文件名
* @return String
*/
public String getConfigFile(){
return this.configFile;
}
/**
* 从Document对象载入配置
* @param doc Document
* @return boolean
*/
public boolean setConfigDocument(Document doc){
this.doc = doc;
return(this.doc != null);
}
/**
* 获取配置的Document对象
* @return Document
*/
public Document getConfigDocment(){
return this.doc;
}
/**
* 获取指定路径结点的指定属性值, 节点不存在或属性不存在时返回null;
* @param path String
* @param attrName String
* @return String
*/
public String getAttribute(String path, String attrName){
return getAttribute(path, attrName, null);
}
/**
* 获取指定路径结点的指定属性值, 节点不存在或属性不存在时返回默认值
* @param path String 节点路径
* @param attrName String 属性名称
* @param defValue String 默认值
*/
public String getAttribute(String path, String attrName, String defValue){
try{
Object obj = doc.selectSingleNode(path);
if(obj != null && obj instanceof Element){
Element ele = (Element)obj;
Attribute attr = ele.attribute(attrName);
return attr==null?defValue:attr.getValue();
} else{
return defValue;
}
} catch(Exception ex){
return defValue;
}
}
/**
* 设置指定路径结点的指定属性值
* @param path String 节点路径
* @param attrName String 属性名
* @param value String 属性值
*/
public void setAttribute(String path, String attrName, String value){
Node node = findOrCreateNode(path);
if(node != null && node instanceof Element){
((Element)node).addAttribute(attrName,value);
}
}
/**
* 得到配置属性的字符串值
* @param path 配置属性的xpath
* @return 配置属性的值,如果配置属性没有设置值,则返回null
*/
public String getString(String path){
return getString(path, null);
}
/**
* 得到配置属性的值
* @param path 配置属性的xpath
* @param defValue 默认值
* @return 配置属性的值
*/
public String getString(String path, String defValue){
try{
Object obj = doc.selectSingleNode(path);
if(obj != null && obj instanceof Element){
Element ele = (Element)obj;
return ele.getText();
} else{
return defValue;
}
} catch(Exception ex){
return defValue;
}
}
/**
* 设置指定路径节点的值(如果不存在则创建)
* @param path String 用"/"分隔的简单路径,如:/config/system/version
* @param value String
*/
public void setString(String path, String value){
Node node = findOrCreateNode(path);
if(node != null){
node.setText(value);
}
}
/**
* 获取指定XPath下所有子节点构成的字符串数组
* @param path String
* @return String[]
*/
public String[] getStrings(String path){
return getStrings(path, null);
}
/**
* 获取指定XPath下所有子节点构成的字符串数组
* @param path String
* @param defValue String[]
* @return String[]
*/
public String[] getStrings(String path, String[] defValue){
try{
Object obj = doc.selectSingleNode(path);
if(obj != null && obj instanceof Element){
Element ele = (Element)obj;
List list = new ArrayList();
Iterator it = ele.elementIterator();
while(it.hasNext()){
Element e = (Element)it.next();
list.add(e.getText());
}
String[] tmp = new String[list.size()];
list.toArray(tmp);
return tmp;
}else{
return defValue;
}
} catch(Exception ex){
return defValue;
}
}
/**
* 在指定XPath下构建指定names中各项值的多个子节点, 并设置相应的值
* @param path String
* @param names String[] 名称列表
* @param values String[] 值列表
*/
public void setStrings(String path, String[] names, String[] values){
Node node = findOrCreateNode(path);
if(node != null && node instanceof Element){
Element ele = (Element)node;
String value = "";
for(int i = 0; i < names.length; i++){
value = (i < values.length ? values[i] : "");
ele.addElement(names[i]).setText(value);
}
}
}
/**
* 在指定XPath下构建指定names中各项值的多个子节点, 并设置相应的值
* @param path String
* @param nameList List
* @param values String[]
*/
public void setStrings(String path, List nameList, String[] values){
String[] names = new String[nameList.size()];
nameList.toArray(names);
setStrings(path, names, values);
}
/**
* 在指定XPath下构建指定字符串数组值的多个子节点
* @param path String
* @param name String
* @param values String[]
*/
public void setStrings(String path, String name, String[] values){
Node node = findOrCreateNode(path);
if(node != null && node instanceof Element){
Element ele = (Element)node;
for(int i = 0; i < values.length; i++){
ele.addElement(name).setText(values[i]);
}
}
}
/**
* 获取指定xpath下的子节点构成的 XmlNodePair 列表
* @param path String
* @return List
*/
public PairList getPairList(String path){
try{
Object obj = doc.selectSingleNode(path);
if(obj != null && obj instanceof Element){
return XMLUtils.getChildToPairList((Element)obj);
} else{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -