package com.dy.common.util;
|
|
import org.jdom2.Document;
|
import org.jdom2.Element;
|
import org.jdom2.input.SAXBuilder;
|
import org.jdom2.output.Format;
|
import org.jdom2.output.XMLOutputter;
|
|
import java.io.FileWriter;
|
import java.net.URL;
|
import java.util.List;
|
|
@SuppressWarnings("unused")
|
public class ConfigXml {
|
|
|
/**
|
* 创建Document对象
|
* @param clazz 参考类
|
* @param filePath 配置文件路径
|
* @return 返回结果 返回结果返回 doc对象
|
* @throws Exception 抛出异常 抛出异常
|
*/
|
public Document createDom(Class<?> clazz, String filePath) throws Exception {
|
if(clazz == null){
|
throw new Exception("class对象为空!");
|
}
|
if(filePath == null || filePath.equals("")){
|
throw new Exception("配置文件路径名称为空!");
|
}
|
if(!filePath.startsWith("/")){
|
filePath = "/" + filePath ;
|
}
|
URL configFileURL = clazz.getResource(filePath);
|
if (configFileURL == null) {
|
throw new Exception("没有得到" + filePath + "配置!");
|
}
|
return this.createDom(configFileURL) ;
|
}
|
|
/**
|
* 创建Document对象
|
* @param configFileURL 配置文件路径
|
* @return 返回结果 返回结果返回 doc对象
|
* @throws Exception 抛出异常 抛出异常
|
*/
|
public Document createDom(URL configFileURL) throws Exception {
|
if (configFileURL == null) {
|
throw new Exception("没有得到配置文件!", null);
|
}
|
Document doc;
|
try {
|
SAXBuilder sb = new SAXBuilder();
|
doc = sb.build(configFileURL);
|
if (doc == null) {
|
throw new Exception("没有生成配置文件的DOM对象!", null);
|
}
|
} catch (Exception e) {
|
throw new Exception("生成配置文件的DOM对象失败!", e);
|
}
|
return doc;
|
}
|
|
|
/**
|
* 保存xml文件
|
* @param clazz 参考类
|
* @param doc doc对象 doc对象
|
* @param filePath 配置文件路径
|
* @throws Exception 抛出异常 抛出异常
|
*/
|
public void saveXML(Class<?> clazz, Document doc, String filePath) throws Exception {
|
if(doc == null){
|
throw new Exception("Document对象为空!");
|
}
|
if(filePath == null || filePath.equals("")){
|
throw new Exception("配置文件路径名称为空!");
|
}
|
if(!filePath.startsWith("/")){
|
filePath = "/" + filePath ;
|
}
|
// 将doc对象输出到文件
|
FileWriter writer = null ;
|
try {
|
// 创建xml文件输出流
|
XMLOutputter xmlopt = new XMLOutputter();
|
// 创建文件输出流
|
URL configFileURL = clazz.getResource(filePath);
|
if (configFileURL == null) {
|
throw new Exception("没有得到" + filePath + "配置!");
|
}
|
writer = new FileWriter(configFileURL.getPath());
|
// 指定文档格式
|
Format fm = Format.getPrettyFormat();
|
fm.setEncoding("UTF-8");
|
xmlopt.setFormat(fm);
|
// 将doc写入到指定的文件中
|
xmlopt.output(doc, writer);
|
} catch (Exception e) {
|
throw new Exception("保存xml文件失败!", e);
|
} finally {
|
if(writer != null){
|
writer.close();
|
}
|
}
|
}
|
|
/**
|
* 检查元素是否存在
|
* @param doc doc对象
|
* @param elementName 元素名称
|
* @return 返回结果 返回结果
|
*/
|
public Element getElement(Document doc, String elementName){
|
if(doc == null){
|
return null ;
|
}
|
if(elementName == null){
|
return null ;
|
}
|
elementName = elementName.trim() ;
|
if(elementName.equals("")){
|
return null ;
|
}
|
return this.get_element(doc, elementName) ;
|
}
|
|
/**
|
* 检查元素是否存在
|
* @param doc doc对象
|
* @param elementName 元素名称
|
* @return 返回结果 返回结果
|
*/
|
public boolean existElement(Document doc, String elementName){
|
if(doc == null){
|
return false ;
|
}
|
if(elementName == null){
|
return false ;
|
}
|
elementName = elementName.trim() ;
|
if(elementName.equals("")){
|
return false ;
|
}
|
Element e = this.get_element(doc, elementName) ;
|
return e != null;
|
}
|
|
|
/**
|
* 得到并设置元素的属性的布尔值
|
* @param doc doc对象
|
* @param elementName 元素名称
|
* @param attrName 属性名称
|
* @param attPropertyNameFix 属性名称前缀
|
* @param setValue 同时设置值
|
* @return 返回结果
|
* @throws Exception 抛出异常
|
*/
|
public boolean getSetAttrBoolean(Document doc, String elementName, String attrName, String attPropertyNameFix, String setValue) throws Exception{
|
String txt = this.getSetAttrTxt(doc, elementName, attrName, attPropertyNameFix, false, setValue) ;
|
return txt.equalsIgnoreCase("true");
|
}
|
|
/**
|
* 得到并设置元素的属性的布尔值
|
* @param e 元素对象
|
* @param attrName 属性名称
|
* @param attPropertyNameFix 属性名称前缀
|
* @param setValue 同时设置值
|
* @return 返回结果
|
* @throws Exception 抛出异常
|
*/
|
public boolean getSetAttrBoolean(Element e, String attrName, String attPropertyNameFix, String setValue) throws Exception{
|
String txt = this.getSetAttrTxt(e, attrName, attPropertyNameFix, false, setValue) ;
|
return txt.equalsIgnoreCase("true");
|
}
|
|
/**
|
* 得到并设置元素的属性的字符串值
|
* @param doc doc对象
|
* @param elementName 元素名称
|
* @param attrName 属性名称
|
* @param attPropertyNameFix 属性名称前缀
|
* @param canBlank 是否可为空
|
* @param setValue 同时设置值
|
* @return 返回结果
|
* @throws Exception 抛出异常
|
*/
|
public String getSetAttrTxt(Document doc, String elementName, String attrName, String attPropertyNameFix, Boolean canBlank, String setValue) throws Exception{
|
Element e = this.check_and_get_element(doc, elementName);
|
return this.getSetAttrTxt(e, attrName, attPropertyNameFix, canBlank, setValue) ;
|
}
|
|
/**
|
* 得到并设置元素的属性的字符串值
|
* @param e 元素对象
|
* @param attrName 属性名称
|
* @param attPropertyNameFix 属性名称前缀
|
* @param canBlank 是否可为空
|
* @param setValue 同时设置值
|
* @return 返回结果
|
* @throws Exception 抛出异常
|
*/
|
public String getSetAttrTxt(Element e, String attrName, String attPropertyNameFix, Boolean canBlank, String setValue) throws Exception{
|
String txt = this.check_get_set_Attribute(e, e.getName(), attrName, attPropertyNameFix, setValue) ;
|
if(!canBlank){
|
if(txt.trim().equals("")){
|
throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值为空字符串!");
|
}
|
}
|
return txt.trim() ;
|
}
|
/**
|
* 得到并设置元素的属性的正整数值
|
* @param doc doc对象
|
* @param elementName 元素名称
|
* @param attrName 属性名称
|
* @param attPropertyNameFix 属性名称前缀
|
* @param min 最小值
|
* @param max 最大值
|
* @param setValue 同时设置值
|
* @return 返回结果
|
* @throws Exception 抛出异常
|
*/
|
public Integer getSetAttrPlusInt(Document doc, String elementName, String attrName, String attPropertyNameFix, Integer min, Integer max, String setValue) throws Exception{
|
Element e = this.check_and_get_element(doc, elementName);
|
return this.getSetAttrPlusInt(e, attrName, attPropertyNameFix, min, max, setValue) ;
|
}
|
|
/**
|
* 得到并设置元素的属性的正整数值
|
* @param e 元素对象
|
* @param attrName 属性名称
|
* @param attPropertyNameFix 属性名称前缀
|
* @param min 最小值
|
* @param max 最大值
|
* @param setValue 同时设置值
|
* @return 返回结果
|
* @throws Exception 抛出异常
|
*/
|
public Integer getSetAttrPlusInt(Element e, String attrName, String attPropertyNameFix, Integer min, Integer max, String setValue) throws Exception{
|
int v ;
|
String txt = this.check_get_set_Attribute(e, e.getName(), attrName, attPropertyNameFix, setValue) ;
|
if(!NumUtil.isPlusIntNumber(txt)){
|
throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值不是正整数!");
|
}
|
v = Integer.parseInt(txt);
|
if(min != null && v < min){
|
throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值小于" + min + "!");
|
}
|
if(max != null && v > max){
|
throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值大于" + max + "!");
|
}
|
return v ;
|
}
|
|
/**
|
* 得到并设置元素的属性的整数值
|
* @param doc doc对象
|
* @param elementName 元素名称
|
* @param attrName 属性名称
|
* @param attPropertyNameFix 属性名称前缀
|
* @param min 最小值
|
* @param max 最大值
|
* @param setValue 同时设置值
|
* @return 返回结果
|
* @throws Exception 抛出异常
|
*/
|
public Integer getSetAttrInt(Document doc, String elementName, String attrName, String attPropertyNameFix, Integer min, Integer max, String setValue) throws Exception{
|
Element e = this.check_and_get_element(doc, elementName);
|
return this.getSetAttrInt(e, attrName, attPropertyNameFix, min, max, setValue) ;
|
}
|
|
/**
|
* 得到并设置元素的属性的整数值
|
* @param e 元素对象
|
* @param attrName 属性名称
|
* @param attPropertyNameFix 属性名称前缀
|
* @param min 最小值
|
* @param max 最大值
|
* @param setValue 同时设置值
|
* @return 返回结果
|
* @throws Exception 抛出异常
|
*/
|
public Integer getSetAttrInt(Element e, String attrName, String attPropertyNameFix, Integer min, Integer max, String setValue) throws Exception{
|
int v ;
|
String txt = this.check_get_set_Attribute(e, e.getName(), attrName, attPropertyNameFix, setValue) ;
|
if(!NumUtil.isIntNumber(txt)){
|
throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值不是正整数!");
|
}
|
v = Integer.parseInt(txt) ;
|
if(min != null && v < min){
|
throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值小于" + min + "!");
|
}
|
if(max != null && v > max){
|
throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值大于" + max + "!");
|
}
|
return v ;
|
}
|
|
/**
|
* 得到并设置元素的属性的浮点数值
|
* @param doc doc对象
|
* @param elementName 元素名称
|
* @param attrName 属性名称
|
* @param min 最小值
|
* @param max 最大值
|
* @param setValue 同时设置值
|
* @return 返回结果
|
* @throws Exception 抛出异常
|
*/
|
public Double getSetAttrPlusDouble(Document doc, String elementName, String attrName, String attPropertyNameFix, Double min, Double max, String setValue) throws Exception{
|
Element e = this.check_and_get_element(doc, elementName);
|
return this.getSetAttrPlusDouble(e, attrName, attPropertyNameFix, min, max, setValue) ;
|
}
|
|
/**
|
* 得到并设置元素的属性的正浮点数值
|
* @param e 元素对象
|
* @param attrName 属性名称
|
* @param attPropertyNameFix 属性名称前缀
|
* @param min 最小值
|
* @param max 最大值
|
* @param setValue 同时设置值
|
* @return 返回结果
|
* @throws Exception 抛出异常
|
*/
|
public Double getSetAttrPlusDouble(Element e, String attrName, String attPropertyNameFix, Double min, Double max, String setValue) throws Exception{
|
double v ;
|
String txt = this.check_get_set_Attribute(e, e.getName(), attrName, attPropertyNameFix, setValue) ;
|
if(!NumUtil.isPlusDoubleNumber(txt)){
|
throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值不是正浮点数!");
|
}
|
v = Double.parseDouble(txt) ;
|
if(min != null && v < min){
|
throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值小于" + min + "!");
|
}
|
if(max != null && v > max){
|
throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值大于" + max + "!");
|
}
|
return v ;
|
}
|
|
/**
|
* 得到并设置元素的属性的浮点数值
|
* @param doc doc对象
|
* @param elementName 元素名称
|
* @param attrName 属性名称
|
* @param attPropertyNameFix 属性名称前缀
|
* @param min 最小值
|
* @param max 最大值
|
* @param setValue 同时设置值
|
* @return 返回结果
|
* @throws Exception 抛出异常
|
*/
|
public Double getSetAttrDouble(Document doc, String elementName, String attrName, String attPropertyNameFix, Double min, Double max, String setValue) throws Exception{
|
Element e = this.check_and_get_element(doc, elementName);
|
return this.getSetAttrDouble(e, attrName, attPropertyNameFix, min, max, setValue) ;
|
}
|
|
/**
|
* 得到并设置元素的属性的浮点数值
|
* @param e 元素对象
|
* @param attrName 属性名称
|
* @param attPropertyNameFix 属性名称前缀
|
* @param min 最小值
|
* @param max 最大值
|
* @param setValue 同时设置值
|
* @return 返回结果
|
* @throws Exception 抛出异常
|
*/
|
public Double getSetAttrDouble(Element e, String attrName, String attPropertyNameFix, Double min, Double max, String setValue) throws Exception{
|
double v ;
|
String txt = this.check_get_set_Attribute(e, e.getName(), attrName, attPropertyNameFix, setValue) ;
|
if(!NumUtil.isDoubleNumber(txt)){
|
throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值不是正浮点数!");
|
}
|
v = Double.parseDouble(txt) ;
|
if(min != null && v < min){
|
throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值小于" + min + "!");
|
}
|
if(max != null && v > max){
|
throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值大于" + max + "!");
|
}
|
return v ;
|
}
|
/**
|
* 得到元素
|
* @param doc doc对象
|
* @param elementName 元素名称 元素名,形如"config.base.test"
|
* @return 返回结果
|
*/
|
private Element get_element(Document doc, String elementName){
|
Element e = null ;
|
Element root ;
|
String nm ;
|
String[] eNames = elementName.split("\\.") ;
|
if(eNames.length > 0){
|
root = doc.getRootElement() ;
|
nm = root.getName() ;
|
if(nm.equals(eNames[0].trim())){
|
if(eNames.length == 1){
|
e = root ;
|
}else{
|
e = this.get_next_element(root, eNames, 1) ;
|
}
|
}
|
}
|
return e ;
|
}
|
|
/**
|
* 得到子元素
|
* @param base 上级元素对象
|
* @param eNames 元素名称
|
* @param index 数组下标
|
* @return 返回结果
|
*/
|
private Element get_next_element(Element base, String[] eNames, int index){
|
Element e = null ;
|
String nm ;
|
List<Element> list = base.getChildren() ;
|
if(list != null && list.size() >0){
|
for(Element el: list){
|
nm = el.getName() ;
|
if(nm.equals(eNames[index].trim())){
|
if(eNames.length == index + 1){
|
e = el ;
|
}else{
|
e = this.get_next_element(el, eNames, index + 1) ;
|
}
|
break ;
|
}
|
}
|
}
|
return e ;
|
}
|
/**
|
* 检查并获元素
|
* @param doc doc对象
|
* @param elementName 元素名称
|
* @return 返回结果
|
* @throws Exception 抛出异常
|
*/
|
private Element check_and_get_element(Document doc, String elementName)throws Exception{
|
if(doc == null){
|
throw new Exception("配置文件的DOM对象为空!");
|
}
|
if(elementName == null){
|
throw new Exception("元素名为空!");
|
}
|
elementName = elementName.trim() ;
|
if(elementName.equals("")){
|
throw new Exception("元素名为空!");
|
}
|
Element e = this.get_element(doc, elementName) ;
|
if(e == null){
|
throw new Exception("未得到名称为" + elementName + "的元素!");
|
}
|
return e ;
|
}
|
|
/**
|
* 检查并获得并设置属性值
|
* @param e 元素对象
|
* @param elementName 元素名称
|
* @param attrName 属性名称
|
* @param attPropertyNameFix 属性名称前缀
|
* @return 返回结果
|
* @throws Exception 抛出异常
|
*/
|
private String check_get_set_Attribute(Element e, String elementName, String attrName, String attPropertyNameFix, String setValue)throws Exception{
|
if(attrName == null){
|
throw new Exception("属性名为空!");
|
}
|
attrName = attrName.trim() ;
|
if(attrName.equals("")){
|
throw new Exception("属性名为空!");
|
}
|
String txt = this.get_set_AttributTxt(e, attrName, attPropertyNameFix, setValue) ;
|
if(txt == null){
|
throw new Exception("未得到元素" + elementName + "的属性" + attrName + "的值!");
|
}
|
return txt ;
|
}
|
/**
|
* 得到并设置属性配置值
|
* @param e 元素对象
|
* @param attName 属性名称
|
* @param attPropertyNameFix 属性名称前缀
|
* @param setValue 同时设置值
|
* @return 返回结果
|
*/
|
private String get_set_AttributTxt(Element e, String attName, String attPropertyNameFix, String setValue){
|
String txt = null ;
|
if(e != null && attName != null){
|
attName = attName.trim() ;
|
if(!attName.equals("")){
|
txt = e.getAttributeValue(attName) ;
|
if(setValue != null){
|
e.setAttribute(attName, setValue) ;
|
}
|
}
|
}
|
if(txt != null && txt.startsWith("${") && txt.endsWith("}")){
|
txt = txt.substring(2, txt.length() - 1) ;
|
if(attPropertyNameFix != null){
|
txt = txt + attPropertyNameFix ;
|
}
|
txt = ConfigProperties.getConfig(txt) ;
|
}
|
return txt ;
|
}
|
|
}
|