📄 smsconfig.java
字号:
package com.dom4j;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class SMSConfig {
private static SMSConfig config;
private static Map<String,ActionDTO> actionMap;
private static Map<String,String> datasourceMap;
public static SMSConfig getSMSConfig(){
if(config==null){
config=new SMSConfig();
}
return config;
}
private SMSConfig(){
actionMap=new HashMap<String,ActionDTO>();
datasourceMap=new HashMap<String,String>();
}
public void writeXMl(String path ){
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("SMSConfig");
Element datasource = root.addElement("datasource");
Element dirver = datasource.addElement("driver");
dirver.setText("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Element url = datasource.addElement("url");
url.setText("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=SMS");
Element username = datasource.addElement("username");
username.setText("sa");
Element password = datasource.addElement("password");
password.setText("123");
Element maxActive = datasource.addElement("maxActive");
maxActive.setText("5");
Element actionMap = root.addElement("actionMapping");
Element action=actionMap.addElement("action");
action.addAttribute("path","hello");
action.addAttribute("name","");
action.addAttribute("redirect","");
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer;
try {
writer = new XMLWriter(new FileOutputStream(path),format);
writer.write(doc);
writer.close();
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void readXML(String path){
SAXReader reader = new SAXReader();
try {
Document document = reader.read(new File(path));
Element root = document.getRootElement();
Iterator its = root.elementIterator();
while(its.hasNext()){
Element element =(Element) its.next();
if(element.getName().equalsIgnoreCase("datasource")){
for(Iterator i = element.elementIterator();i.hasNext();){
Element result =(Element)i.next();
String name=result.getName();
System.out.println(name);
String text=result.getText();
System.out.println(text);
config.getDatasourceMap().put(name,text);
}
}else if(element.getName().equalsIgnoreCase("actionMapping")){
for(Iterator j = element.elementIterator();j.hasNext();){
Element result =(Element)j.next();
String attpath=result.attributeValue("path");
String name=result.attributeValue("name");
String redirect=result.attributeValue("redirect");
ActionDTO action=new ActionDTO(attpath,name,redirect);
config.getActionMap().put(attpath,action);
}
}
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Map<String, ActionDTO> getActionMap() {
return actionMap;
}
public Map<String, String> getDatasourceMap() {
return datasourceMap;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -