📄 oxmledconfig.java
字号:
/**
* @author 沈东良 Edward Shen<a href="mailto:shendl_s@hotmail.com">shendl_s@hotmail.com</a>
* 2007-8-8 下午02:20:10
*/
package net.sf.oxmled.use.example;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.tree.DefaultElement;
import net.sf.oxmled.common.config.IConfig;
import net.sf.oxmled.common.util.ClassLoaderUtil;
import net.sf.oxmled.model.INode;
import net.sf.oxmled.model.Node;
import net.sf.oxmled.service.INodeService;
import net.sf.oxmled.service.NodeService;
/**
* @author 沈东良 Edward Shen<a href="mailto:shendl_s@hotmail.com">shendl_s@hotmail.com</a>
* 2007-8-8 下午02:20:10
* 这是一个使用OXmlEd类库的例子,实现和net.sf.oxmled.common.config.Config类一样的功能。
*
*/
public class OXmlEdConfig implements IConfig {
/**
* 配置文件的路径
*/
private String configFile=IConfig.defaultPath;
/**
* 使用OXmlEd类库提供的助手类
*/
private INodeService nodeService=null;
/**
* 读取map的东西
*/
private Map<String, String> map = new Hashtable<String, String>();
/**
* 这个Map中的值是集合,也就是说里面有可重复的数据
*/
private Map<String, List<String>> multiMap = new Hashtable<String, List<String>>();
/**
* 使用单例
*/
private static IConfig instance = null;
/**
* 代表配置文件的节点
*/
private INode node=null;
/* (non-Javadoc)
* @see net.sf.oxmled.use.example.IConfig#put(java.lang.String, java.lang.String)
*/
public void put(String key,String value) throws Exception{
/**
* 1,修改Map对象
* 2,新增,或者修改Node对象
* 3,保存到文件中。
*
*/
//如果包含这个元素,就修改值
if(this.getMap().containsKey(key)){
Iterator<INode> itMap=this.getNode().getNodes("map").iterator();
while(itMap.hasNext()){
INode tmp=itMap.next();
String keyTmp=tmp.getChildNodes("key").get(0).getText();
if(key.equals(keyTmp)){
tmp.getChildNodes("value").get(0).setText(value);
}
}
}else{
INode mapsNode=this.getNode().getNodes("maps").get(0);
INode mapNode=new Node("map");
mapsNode.getChildNodes().add(mapNode);
INode keyNode=new Node("key");
keyNode.setText(key);
mapNode.getChildNodes().add(keyNode);
INode valueNode=new Node("value");
valueNode.setText(value);
mapNode.getChildNodes().add(valueNode);
}
this.getMap().put(key, value);
//写入文件
this.getNodeService().writeToPrettyPrint(node, this.getConfigFile());
}
/* (non-Javadoc)
* @see net.sf.oxmled.use.example.IConfig#put(java.lang.String, java.util.List)
*/
public void put(String key,List<String> values) throws Exception{
//修改Node
if(this.getMultiMap().containsKey(key)){
Iterator<INode> itMap=this.getNode().getNodes("map").iterator();
while(itMap.hasNext()){
INode tmp=itMap.next();
String keyTmp=tmp.getChildNodes("key").get(0).getText();
if(key.equals(keyTmp)){
/**
* 1,删除原来的values
* 2,创建新的节点。
*/
//tmp.getChildNodes("value").get(0).setText(value);
tmp.getChildNodes().removeAll(tmp.getChildNodes("values"));
Iterator<String> itValues=values.iterator();
while(itValues.hasNext()){
INode tmpValuesNode=new Node("values");
tmpValuesNode.setText(itValues.next());
tmp.getChildNodes().add(tmpValuesNode);
}
}
}
}else{
//新增Node
INode mapsNode=this.getNode().getNodes("maps").get(0);
INode newMap=new Node("map");
mapsNode.getChildNodes().add(newMap);
INode newKey=new Node("key");
newKey.setText(key);
newMap.getChildNodes().add(newKey);
Iterator<String> itValues=values.iterator();
while(itValues.hasNext()){
INode newValuesTmp=new Node("values");
newValuesTmp.setText(itValues.next());
newMap.getChildNodes().add(newValuesTmp);
}
}
this.getMultiMap().put(key, values);
//写入文件
this.getNodeService().writeToPrettyPrint(node, this.getConfigFile());
}
/* (non-Javadoc)
* @see net.sf.oxmled.use.example.IConfig#get(java.lang.String)
*/
public String get(String key){
return this.getMap().get(key);
}
/* (non-Javadoc)
* @see net.sf.oxmled.use.example.IConfig#gets(java.lang.String)
*/
public List<String>gets(String key){
return this.getMultiMap().get(key);
}
/* (non-Javadoc)
* @see net.sf.oxmled.use.example.IConfig#getNode()
*/
public INode getNode() throws Exception {
synchronized(this){
if(this.node==null){
this.node=this.getNodeService().loadXmlFile(ClassLoaderUtil.getResource(this.getConfigFile()));
}
}
return node;
}
/* (non-Javadoc)
* @see net.sf.oxmled.use.example.IConfig#setNode(net.sf.oxmled.model.INode)
*/
public void setNode(INode node) {
this.node = node;
}
/**
* 返回单例的对象----在多线程中可能出错,请使用getNewInstance
*
* @return
* @throws Exception
*/
public static IConfig getInstance(String configFile) throws Exception {
/**
* 如果没有实现单例
*
*/
synchronized(OXmlEdConfig.class){
if (OXmlEdConfig.instance == null) {
OXmlEdConfig.instance = new OXmlEdConfig();
OXmlEdConfig.instance.setConfigFile(configFile);
OXmlEdConfig.instance.parseConfigXml();
}
}
return OXmlEdConfig.instance;
}
/**
* 返回单例的对象----在多线程中可能出错,请使用getNewInstance
*
* @return
* @throws Exception
*/
public static IConfig getInstance() throws Exception {
/**
* 如果没有实现单例
*
*/
synchronized(OXmlEdConfig.class){
if (OXmlEdConfig.instance == null) {
OXmlEdConfig.instance = new OXmlEdConfig();
OXmlEdConfig.instance.parseConfigXml();
}
}
return OXmlEdConfig.instance;
}
/**
* 解析xml配置文件
* @throws Exception
*/
public void parseConfigXml() throws Exception {
/*
*1,解析文件,得到Node,然后存放
*this.getNode();
*得到了完整的对象树。
* 然后,我们需要得到map名字的所有节点
*
*/
//this.setNode(this.getNodeService().loadXmlFile(ClassLoaderUtil.getResource(Config.defaultPath)));
Iterator<INode> itMap=this.getNode().getNodes("map").iterator();
while(itMap.hasNext()){
INode tmp=itMap.next();
/**
* 1,如果它有子节点,叫做 value的,就把它加入到map中;
* 2, 如果有叫作values的,就把它加入到multiMap中。
*
*/
String key=tmp.getChildNodes("key").get(0).getText();
if (!key.equals("")) {
if(tmp.childContains("value")){
String value=tmp.getChildNodes("value").get(0).getText();
this.getMap().put(key, value);
}
if(tmp.childContains("values")){
List<String> valuesList=new ArrayList<String>();
Iterator<INode> it=tmp.getChildNodes("values").iterator();
while(it.hasNext()){
INode tmpValues=it.next();
valuesList.add(tmpValues.getText());
}
this.getMultiMap().put(key, valuesList);
}
}
}
}
//OXmlEdConfig.instance.setConfigFile(configFile);
/**
* 得到多例
*
* @return
* @throws Exception
*/
public static IConfig getNewInstance(String configFile) throws Exception {
IConfig instance = new OXmlEdConfig();
instance.setConfigFile(configFile);
instance.parseConfigXml();
return instance;
}
/**
* 得到多例
*
* @return
* @throws Exception
*/
public static IConfig getNewInstance() throws Exception {
IConfig instance = new OXmlEdConfig();
instance.parseConfigXml();
return instance;
}
/* (non-Javadoc)
* @see net.sf.oxmled.use.example.IConfig#getMap()
*/
public Map<String, String> getMap() {
return map;
}
/* (non-Javadoc)
* @see net.sf.oxmled.use.example.IConfig#setMap(java.util.Map)
*/
public void setMap(Map<String, String> map) {
this.map = map;
}
/* (non-Javadoc)
* @see net.sf.oxmled.use.example.IConfig#getMultiMap()
*/
public Map<String, List<String>> getMultiMap() {
return multiMap;
}
/* (non-Javadoc)
* @see net.sf.oxmled.use.example.IConfig#setMultiMap(java.util.Map)
*/
public void setMultiMap(Map<String, List<String>> multiMap) {
this.multiMap = multiMap;
}
/* (non-Javadoc)
* @see net.sf.oxmled.use.example.IConfig#getNodeService()
*/
public INodeService getNodeService() {
synchronized(this){
this.nodeService=new NodeService();
}
return nodeService;
}
/* (non-Javadoc)
* @see net.sf.oxmled.use.example.IConfig#setNodeService(net.sf.oxmled.service.INodeService)
*/
public void setNodeService(INodeService nodeService) {
this.nodeService = nodeService;
}
/**
*
*/
private OXmlEdConfig() {
/*
*
*/
}
/**
* @param args
*/
public static void main(String[] args) {
/*
*
*/
try {
IConfig instance=OXmlEdConfig.getInstance();
/*
instance.put("a", "搜索斯b");
List<String> values=new ArrayList<String>();
values.add("我们");
values.add("dsdfsfsdfsfsss");
instance.put("b", values);
Element e=new DefaultElement("a");
e.elements();
System.out.println(instance.getNode().toString());
*/
INode map=new Node("map");
INode key=new Node("key");
key.setText("a");
INode value=new Node("value");
value.setText("搜索斯b");
map.getChildNodes().add(key);
map.getChildNodes().add(value);
INode map2=new Node("map");
INode key2=new Node("key");
key2.setText("a");
INode value2=new Node("value");
value2.setText("搜索斯b ");
map2.getChildNodes().add(key2);
map2.getChildNodes().add(value2);
System.out.println(map.equals(map2));
} catch (DocumentException e) {
/*
*
*/
e.printStackTrace();
} catch (Exception e) {
/*
*
*/
e.printStackTrace();
}
}
/**
* @return the configFile
*/
public String getConfigFile() {
return configFile;
}
/**
* @param configFile the configFile to set
*/
public void setConfigFile(String configFile) {
this.configFile = configFile;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -