📄 xmlproperties.java
字号:
package cn.js.fan.util;import java.io.*;import java.util.*;import org.jdom.*;import org.jdom.input.*;import org.jdom.output.*;public class XMLProperties { private File file; private Document doc; private Map propertyCache = new HashMap(); public XMLProperties(String file) { this.file = new File(file); try { SAXBuilder builder = new SAXBuilder(); DataUnformatFilter format = new DataUnformatFilter(); builder.setXMLFilter(format); doc = builder.build(new File(file)); } catch (Exception e) { System.err.println("Error creating XML parser in " + "PropertyManager.java"); e.printStackTrace(); } } public XMLProperties(File file, Document doc) { this.file = file; this.doc = doc; } public String getProperty(String name) { if (propertyCache.containsKey(name)) { return (String)propertyCache.get(name); } String[] propName = parsePropertyName(name); Element element = doc.getRootElement(); for (int i = 0; i < propName.length; i++) { element = element.getChild(propName[i]); if (element == null) { return null; } } String value = element.getText(); if ("".equals(value)) { return null; } else { if (value!=null) value = value.trim(); propertyCache.put(name, value); return value; } } public String getProperty(String name, String childAttributeName, String childAttributeValue) { String visualName = name + "_-_" + childAttributeName + "_-_" + childAttributeValue; if (propertyCache.containsKey(visualName)) { return (String)propertyCache.get(visualName); } String[] propName = parsePropertyName(name); Element element = doc.getRootElement(); for (int i = 0; i < propName.length; i++) { element = element.getChild(propName[i]); if (element == null) { return null; } } String value = null; List list = element.getChildren(); if (list==null) return null; Iterator ir = list.iterator(); while (ir.hasNext()) { Element child = (Element)ir.next(); String attrValue = child.getAttributeValue(childAttributeName); if (attrValue!=null) { if (attrValue.equals(childAttributeValue)) { value = child.getText(); break; } } } if ("".equals(value)) { return null; } else { if (value!=null) value = value.trim(); propertyCache.put(visualName, value); return value; } } public String getProperty(String name, String childAttributeName, String childAttributeValue, String subChildName) { String visualName = name + "_-_" + childAttributeName + "_-_" + childAttributeValue + "_-_" + subChildName; if (propertyCache.containsKey(visualName)) { return (String)propertyCache.get(visualName); } String[] propName = parsePropertyName(name); Element element = doc.getRootElement(); for (int i = 0; i < propName.length; i++) { element = element.getChild(propName[i]); if (element == null) { return null; } } String value = ""; List list = element.getChildren(); if (list==null) return null; Iterator ir = list.iterator(); while (ir.hasNext()) { Element child = (Element)ir.next(); String attrValue = child.getAttributeValue(childAttributeName); if (attrValue!=null) { if (attrValue.equals(childAttributeValue)) { value = child.getChildText(subChildName); break; } } } if ("".equals(value)) { return null; } else { value = value.trim(); propertyCache.put(visualName, value); return value; } } public String [] getChildrenProperties(String parent) { String[] propName = parsePropertyName(parent); Element element = doc.getRootElement(); for (int i = 0; i < propName.length; i++) { element = element.getChild(propName[i]); if (element == null) { return new String [] { }; } } List children = element.getChildren(); int childCount = children.size(); String [] childrenNames = new String[childCount]; for (int i=0; i<childCount; i++) { childrenNames[i] = ((Element)children.get(i)).getName(); } return childrenNames; } public void setProperty(String name, String value) { propertyCache.put(name, value); String[] propName = parsePropertyName(name); Element element = doc.getRootElement(); for (int i=0; i<propName.length; i++) { if (element.getChild(propName[i]) == null) { element.addContent(new Element(propName[i])); } element = element.getChild(propName[i]); } element.setText(value); saveProperties(); } public void setProperty(String name, String childAttributeName, String childAttributeValue, String value) { String visualName = name + "_-_" + childAttributeName + "_-_" + childAttributeValue; propertyCache.put(visualName, value); String[] propName = parsePropertyName(name); Element element = doc.getRootElement(); for (int i=0; i<propName.length; i++) { element = element.getChild(propName[i]); if (element==null) return; } List list = element.getChildren(); if (list==null) return; Iterator ir = list.iterator(); while (ir.hasNext()) { Element child = (Element)ir.next(); String attrValue = child.getAttributeValue(childAttributeName); if (attrValue!=null) { if (attrValue.equals(childAttributeValue)) { child.setText(value); break; } } } saveProperties(); } public void setProperty(String name, String childAttributeName, String childAttributeValue, String subChildName, String value) { String visualName = name + "_-_" + childAttributeName + "_-_" + childAttributeValue + "_-_" + subChildName; propertyCache.put(visualName, value); String[] propName = parsePropertyName(name); Element element = doc.getRootElement(); for (int i=0; i<propName.length; i++) { element = element.getChild(propName[i]); if (element==null) return; } List list = element.getChildren(); if (list==null) return; Iterator ir = list.iterator(); while (ir.hasNext()) { Element child = (Element)ir.next(); String attrValue = child.getAttributeValue(childAttributeName); if (attrValue!=null) { if (attrValue.equals(childAttributeValue)) { child.getChild(subChildName).setText(value); break; } } } saveProperties(); } public void deleteProperty(String name) { String[] propName = parsePropertyName(name); Element element = doc.getRootElement(); for (int i=0; i<propName.length-1; i++) { element = element.getChild(propName[i]); if (element == null) { return; } } element.removeChild(propName[propName.length-1]); saveProperties(); } private synchronized void saveProperties() { OutputStream out = null; boolean error = false; File tempFile = null; try { tempFile = new File(file.getParentFile(), file.getName() + ".tmp"); String indent = " "; org.jdom.output.Format format = org.jdom.output.Format.getPrettyFormat(); format.setIndent(indent); format.setEncoding("utf-8"); XMLOutputter outp = new XMLOutputter(format); out = new BufferedOutputStream(new FileOutputStream(tempFile)); outp.output(doc, out); } catch (Exception e) { e.printStackTrace(); error = true; } finally { try { out.close(); } catch (Exception e) { e.printStackTrace(); error = true; } } if (!error) { file.delete(); tempFile.renameTo(file); } } private String[] parsePropertyName(String name) { int size = 1; for (int i=0; i<name.length(); i++) { if (name.charAt(i) == '.') { size++; } } String[] propName = new String[size]; StringTokenizer tokenizer = new StringTokenizer(name, "."); int i = 0; while (tokenizer.hasMoreTokens()) { propName[i] = tokenizer.nextToken(); i++; } return propName; } public void refresh() { propertyCache.clear(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -