|
@@ -24,21 +24,23 @@ import org.dom4j.Element;
|
|
|
import org.dom4j.io.OutputFormat;
|
|
import org.dom4j.io.OutputFormat;
|
|
|
import org.dom4j.io.SAXReader;
|
|
import org.dom4j.io.SAXReader;
|
|
|
import org.dom4j.io.XMLWriter;
|
|
import org.dom4j.io.XMLWriter;
|
|
|
-import org.leanwo.management.exception.ConfigFileException;
|
|
|
|
|
|
|
|
|
|
import com.leanwo.management.model.ApplicationSetting;
|
|
import com.leanwo.management.model.ApplicationSetting;
|
|
|
import com.leanwo.management.model.XmlConfigPath;
|
|
import com.leanwo.management.model.XmlConfigPath;
|
|
|
|
|
|
|
|
public class XmlConfigPathService {
|
|
public class XmlConfigPathService {
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 从文件中加载ApplicationSetting中的
|
|
* 从文件中加载ApplicationSetting中的
|
|
|
* 从config/config.properteis文件中加载portNo,token属性
|
|
* 从config/config.properteis文件中加载portNo,token属性
|
|
|
* 从xml配中获取node 的数据
|
|
* 从xml配中获取node 的数据
|
|
|
* ,contents等属性
|
|
* ,contents等属性
|
|
|
* @param setting
|
|
* @param setting
|
|
|
- * @throws ConfigFileException
|
|
|
|
|
*/
|
|
*/
|
|
|
- public void load(ApplicationSetting setting) throws ConfigFileException {
|
|
|
|
|
|
|
+ public void load(ApplicationSetting setting) {
|
|
|
Map<String, String> contentMap = new HashMap<String, String>();
|
|
Map<String, String> contentMap = new HashMap<String, String>();
|
|
|
XmlConfigPath xmlConfigPath = setting.getXmlConfigPath();
|
|
XmlConfigPath xmlConfigPath = setting.getXmlConfigPath();
|
|
|
String xmlFilePath = setting.getInstallPath() + "\\" + xmlConfigPath.getXmlFileName();
|
|
String xmlFilePath = setting.getInstallPath() + "\\" + xmlConfigPath.getXmlFileName();
|
|
@@ -79,7 +81,7 @@ public class XmlConfigPathService {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
}
|
|
}
|
|
|
}else{
|
|
}else{
|
|
|
- throw new ConfigFileException("文件:"+xmlFilePath+",不存在");
|
|
|
|
|
|
|
+ throw new RuntimeException("文件:"+xmlFilePath+",不存在");
|
|
|
}
|
|
}
|
|
|
//读取propertis信息
|
|
//读取propertis信息
|
|
|
String propertiesPath = setting.getInstallPath()+"/config/config.properties";
|
|
String propertiesPath = setting.getInstallPath()+"/config/config.properties";
|
|
@@ -117,14 +119,6 @@ public class XmlConfigPathService {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private static String getContent(Element element) {
|
|
|
|
|
- StringBuilder builder = new StringBuilder();
|
|
|
|
|
- for (Iterator<Element> i = element.elementIterator(); i.hasNext();) {
|
|
|
|
|
- Element e = i.next();
|
|
|
|
|
- builder.append(e.asXML());
|
|
|
|
|
- }
|
|
|
|
|
- return builder.toString();
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 获得X属性结果是X值的整个标签
|
|
* 获得X属性结果是X值的整个标签
|
|
@@ -155,8 +149,9 @@ public class XmlConfigPathService {
|
|
|
public void save(ApplicationSetting setting) {
|
|
public void save(ApplicationSetting setting) {
|
|
|
XmlConfigPath xmlConfigPath = setting.getXmlConfigPath();
|
|
XmlConfigPath xmlConfigPath = setting.getXmlConfigPath();
|
|
|
Map<String, String> contents = xmlConfigPath.getContents();
|
|
Map<String, String> contents = xmlConfigPath.getContents();
|
|
|
- //Collection<String> values = contents.values();
|
|
|
|
|
|
|
+ //目标xml文件路径
|
|
|
String xmlFilePath = setting.getInstallPath() + "/" + xmlConfigPath.getXmlFileName();
|
|
String xmlFilePath = setting.getInstallPath() + "/" + xmlConfigPath.getXmlFileName();
|
|
|
|
|
+ //目标文件对象
|
|
|
File file = new File(xmlFilePath);
|
|
File file = new File(xmlFilePath);
|
|
|
XMLWriter writer = null;// 声明写XML的对象
|
|
XMLWriter writer = null;// 声明写XML的对象
|
|
|
if (file.exists()) {
|
|
if (file.exists()) {
|
|
@@ -174,40 +169,38 @@ public class XmlConfigPathService {
|
|
|
}
|
|
}
|
|
|
//清空bean节点
|
|
//清空bean节点
|
|
|
dataSource.clearContent();
|
|
dataSource.clearContent();
|
|
|
- /*List<Element> dataSources = dataSource.elements();
|
|
|
|
|
- for (Element e : dataSources) {
|
|
|
|
|
- dataSource.remove(e);
|
|
|
|
|
- }*/
|
|
|
|
|
String xmlText = contents.get(s).replaceAll("\n|\t", "");
|
|
String xmlText = contents.get(s).replaceAll("\n|\t", "");
|
|
|
//将xml文本转换为document对象
|
|
//将xml文本转换为document对象
|
|
|
Document parseText = DocumentHelper.parseText("<myroot>" + xmlText.trim() + "</myroot>");
|
|
Document parseText = DocumentHelper.parseText("<myroot>" + xmlText.trim() + "</myroot>");
|
|
|
Element rootElement = parseText.getRootElement();
|
|
Element rootElement = parseText.getRootElement();
|
|
|
|
|
+ //将文本节点写入目标xml的节点下
|
|
|
dataSource.setContent(rootElement.content());
|
|
dataSource.setContent(rootElement.content());
|
|
|
|
|
+ writer = new XMLWriter(new FileWriter(file), format);
|
|
|
|
|
+ writer.write(document);
|
|
|
|
|
+ /*List<Element> dataSources = dataSource.elements();
|
|
|
|
|
+ for (Element e : dataSources) {
|
|
|
|
|
+ dataSource.remove(e);
|
|
|
|
|
+ }*/
|
|
|
/*List<Element> elements = rootElement.elements();
|
|
/*List<Element> elements = rootElement.elements();
|
|
|
for (Element e : elements) {
|
|
for (Element e : elements) {
|
|
|
dataSource.addElement("property");
|
|
dataSource.addElement("property");
|
|
|
|
|
|
|
|
}*/
|
|
}*/
|
|
|
-
|
|
|
|
|
//dataSource.addCDATA(contents.get(s));
|
|
//dataSource.addCDATA(contents.get(s));
|
|
|
//dataSource.addText(contents.get(s));
|
|
//dataSource.addText(contents.get(s));
|
|
|
//dataSource.setText("<![CDATA["+contents.get(s)+"]]>");
|
|
//dataSource.setText("<![CDATA["+contents.get(s)+"]]>");
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
- writer = new XMLWriter(new FileWriter(file), format);
|
|
|
|
|
- writer.write(document);
|
|
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
} catch (DocumentException e) {
|
|
} catch (DocumentException e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
|
- // TODO Auto-generated catch block
|
|
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
}finally {
|
|
}finally {
|
|
|
if (writer != null){
|
|
if (writer != null){
|
|
|
try {
|
|
try {
|
|
|
writer.close();
|
|
writer.close();
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
|
- // TODO Auto-generated catch block
|
|
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -233,6 +226,13 @@ public class XmlConfigPathService {
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
}finally {
|
|
}finally {
|
|
|
|
|
+ if (writer != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ writer.close();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
if(out != null){
|
|
if(out != null){
|
|
|
try {
|
|
try {
|
|
|
out.close();
|
|
out.close();
|
|
@@ -244,7 +244,10 @@ public class XmlConfigPathService {
|
|
|
}else{
|
|
}else{
|
|
|
throw new RuntimeException("文件:"+propertiesPath+",不存在");
|
|
throw new RuntimeException("文件:"+propertiesPath+",不存在");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
}
|
|
}
|