📄 configuration.java
字号:
package Chapter12;
import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.*;
public class Configuration {
/* 为配置文件相对路径 */
private static String relativeMosticConfigUrl = "MosticConfig.xml";
/*为配置文件绝对路径 */
private static String MosticConfigUrl = null;
/*对应配置文件中的根元素 */
private static Element elemConfiguration = null;
/**保存所有的配置分类 */
private static HashMap mapConfigType = new HashMap();
/*保存所有的配置项 */
private static HashMap mapConfigItem = new HashMap();
/*用于同步 */
private static Object synchronizeObj = new Object();
private static XMLOutputter outputter = new XMLOutputter(Format
.getPrettyFormat());
/*配置管理类构造函数,从配置文件中读取配置信息,并保存在内存的缓冲区中*/
public Configuration() {
synchronized (synchronizeObj) {
//从配置文件中读取配置信息,只读取一次
if (elemConfiguration == null) {
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(Configuration.class
.getResourceAsStream(relativeMosticConfigUrl));
elemConfiguration = doc.getRootElement();
//读取配置分类
List listConfigType = elemConfiguration.getChildren();
for (int i = 0; i < listConfigType.size(); i++) {
Element elemConfigType = (Element) listConfigType
.get(i);
String typeName = elemConfigType
.getAttributeValue("name");
//读取配置分类中的配置项
List listItems = elemConfigType.getChildren();
for (int j = 0; j < listItems.size(); j++) {
Element elemItem = (Element) listItems.get(j);
String itemName = elemItem
.getAttributeValue("name");
mapConfigItem.put(typeName + "&" + itemName, null);
}
mapConfigType.put(typeName, null);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
/**
* 增加新的配置分类: 配置分类名称\配置分类描述
* @return true为建立成功,false失败,原因可能是存在同名分类
*/
public boolean addConfigType(String name, String description) {
synchronized (synchronizeObj) {
if (mapConfigType.containsKey(name))
return false;
elemConfiguration.addContent(configTypeToElement(name, description));
try {
//保存更改到配置文件
outputter.output(elemConfiguration, new FileOutputStream(
MosticConfigUrl));
} catch (Exception e) {
return false;
}
mapConfigType.put(name, null);
}
return true;
}
/*根据name属性获取相应的Element
* @return 返回相应的子Element
*/
private Element getElementByNameAttr(Element father, String nameAttr) {
List listChildren = father.getChildren();
for (Iterator iter = listChildren.iterator(); iter.hasNext();) {
Element element = (Element) iter.next();
if (element.getAttributeValue("name").equals(nameAttr))
return element;
}
return null;
}
private Element configTypeToElement(String name, String description) {
Element e = new Element("ConfigType");
e.setAttribute("name", name);
e.setAttribute("description", description);
return e;
}
private Element configItemToElement(String name, String value,
String description) {
Element e = new Element("Item");
e.setAttribute("name", name);
e.setAttribute("value", value);
e.setAttribute("description", description);
return e;
}
/**
* 修改配置分类
* @return true更新成功,false失败
*/
public boolean updateConfigType(String oldName, String newName,
String newDescription) {
synchronized (synchronizeObj) {
//不存在oldName的分类,返回false
if (mapConfigType.containsKey(oldName) == false)
return false;
if (oldName.equals(newName) == false) {
mapConfigType.remove(oldName);
mapConfigType.put(newName, null);
Vector vKeys = new Vector();
for (Iterator iter = mapConfigItem.keySet().iterator(); iter
.hasNext();) {
String key = (String) iter.next();
if (key.startsWith(oldName + "&"))
vKeys.add(new String(key));
}
for (Iterator iter = vKeys.iterator(); iter.hasNext();) {
String key = (String) iter.next();
String newKey = newName + key.substring(key.indexOf("&"));
mapConfigItem.remove(key);
mapConfigItem.put(newKey, null);
}
}
//更改elemConfiguration中的配置分类
Element elemConfigType = getElementByNameAttr(elemConfiguration,
oldName);
elemConfigType.setAttribute("name", newName);
elemConfigType.setAttribute("description", newDescription);
try {
//保存更改到配置文件
outputter.output(elemConfiguration, new FileOutputStream(
MosticConfigUrl));
} catch (Exception e) {
return false;
}
}
return true;
}
/**
* 删除配置分类
* @return true删除成功,false失败
*/
public boolean delConfigType(String name) {
synchronized (synchronizeObj) {
if (mapConfigType.containsKey(name) == false)
return false;
Vector vKeys = new Vector();
for (Iterator iter = mapConfigItem.keySet().iterator(); iter
.hasNext();) {
String key = (String) iter.next();
if (key.startsWith(name + "&"))
vKeys.add(new String(key));
}
for (Iterator iter = vKeys.iterator(); iter.hasNext();) {
String key = (String) iter.next();
mapConfigItem.remove(key);
}
Element elemConfigType = getElementByNameAttr(elemConfiguration,
name);
elemConfiguration.removeContent(elemConfigType);
try {
//保存更改到配置文件
outputter.output(elemConfiguration, new FileOutputStream(
MosticConfigUrl));
} catch (Exception e) {
return false;
}
}
return true;
}
/**
* 增加配置信息项
* @return true增加成功,false失败
*/
public boolean addItem(String configTypeName, String itemName,
String itemValue, String itemDescription) {
synchronized (synchronizeObj) {
//如果配置分类不存在或者该配置项已经存在,返回false
if (mapConfigType.containsKey(configTypeName) == false
|| mapConfigItem.containsKey(configTypeName + "&"
+ itemName))
return false;
//在mapConfigItem中增加记录
mapConfigItem.put(configTypeName + "&" + itemName, null);
//更改XML的Element
Element elemConfigType = getElementByNameAttr(elemConfiguration,
configTypeName);
elemConfigType.addContent(configItemToElement(itemName, itemValue,
itemDescription));
try {
//保存更改到配置文件
outputter.output(elemConfiguration, new FileOutputStream(
MosticConfigUrl));
} catch (Exception e) {
return false;
}
}
return true;
}
/**
* 更新配置信息项
* @return true更新成功,false失败
*/
public boolean updateItem(String configTypeName, String itemName,
String itemValue, String itemDescription) {
synchronized (synchronizeObj) {
//如果配置分类不存在或者该配置项不存在,返回false
if (mapConfigType.containsKey(configTypeName) == false
|| mapConfigItem.containsKey(configTypeName + "&"
+ itemName) == false)
return false;
//更改XML的Element
Element elemConfigType = getElementByNameAttr(elemConfiguration,
configTypeName);
Element elemConfigItem = getElementByNameAttr(elemConfigType,
itemName);
elemConfigType.removeContent(elemConfigItem);
elemConfigType.addContent(configItemToElement(itemName, itemValue,
itemDescription));
try {
//保存更改到配置文件
outputter.output(elemConfiguration, new FileOutputStream(
MosticConfigUrl));
} catch (Exception e) {
return false;
}
}
return true;
}
/**
* 删除配置信息项
* @return true删除成功,false失败
*/
public boolean delItem(String configTypeName, String itemName) {
synchronized (synchronizeObj) {
//如果配置分类不存在或者该配置项不存在,返回false
if (mapConfigType.containsKey(configTypeName) == false
|| mapConfigItem.containsKey(configTypeName + "&"
+ itemName) == false)
return false;
//更改XML的Element
Element elemConfigType = getElementByNameAttr(elemConfiguration,
configTypeName);
Element elemConfigItem = getElementByNameAttr(elemConfigType,
itemName);
elemConfigType.removeContent(elemConfigItem);
try {
//保存更改到配置文件
outputter.output(elemConfiguration, new FileOutputStream(
MosticConfigUrl));
} catch (Exception e) {
return false;
}
}
return true;
}
/**
* 获取配置信息项的值
* @return 该信息项的值,如果该项不存在返回null
*/
public String getItemValue(String configTypeName, String itemName) {
synchronized (synchronizeObj) {
//如果该配置项不存在,返回false
if (mapConfigItem.containsKey(configTypeName + "&" + itemName)) {
Element elemConfigType = getElementByNameAttr(
elemConfiguration, configTypeName);
Element elemConfigItem = getElementByNameAttr(elemConfigType,
itemName);
return elemConfigItem.getAttributeValue("value");
}
}
return null;
}
/**
* 获取所有的配置分类
*/
public Vector getConfigTypes() {
Vector vConfigType = new Vector();
synchronized (synchronizeObj) {
List listConfigType = elemConfiguration.getChildren();
for (Iterator iter = listConfigType.iterator(); iter.hasNext();) {
Element element = (Element) iter.next();
String name = element.getAttributeValue("name");
String desc = element.getAttributeValue("description");
vConfigType.add(new ConfigType(name, desc));
}
}
return vConfigType;
}
/**
* 获取某分类下的配置项
*/
public Vector getConfigItems(String configTypeName) {
Vector vConfigItem = new Vector();
synchronized (synchronizeObj) {
Element elemConfigType = getElementByNameAttr(elemConfiguration,
configTypeName);
if (elemConfigType != null) {
List listConfigItem = elemConfigType.getChildren();
for (Iterator iter = listConfigItem.iterator(); iter.hasNext();) {
Element element = (Element) iter.next();
String name = element.getAttributeValue("name");
String value = element.getAttributeValue("value");
String desc = element.getAttributeValue("description");
vConfigItem.add(new ConfigItem(name, value, desc));
}
}
}
return vConfigItem;
}
public static void main(String[] args) {
Configuration sigsitconfig = new Configuration();
System.out.println(sigsitconfig.getItemValue("DBConnection",
"url"));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -