📄 cfg.java
字号:
package com.huawei.insa2.util;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.StringTokenizer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class Cfg {
public Cfg(String url) throws IOException {
this(url, false);
}
public Cfg(String url, boolean create) throws IOException {
if (url == null)
throw new IllegalArgumentException("url is null");
if (url.indexOf(':') > 1)
file = url;
else
file = (new File(url)).toURL().toString();
new URL(file);
try {
load();
} catch (FileNotFoundException ex) {
if (!create) {
throw ex;
} else {
loadXMLParser();
doc = builder.newDocument();
root = doc.createElement("config");
doc.appendChild(root);
isDirty = true;
flush();
return;
}
}
}
public Args getArgs(String key) {
Map args = new HashMap();
String children[] = childrenNames(key);
for (int i = 0; i < children.length; i++)
args.put(children[i], get(String.valueOf(String
.valueOf((new StringBuffer(String.valueOf(String
.valueOf(key)))).append('/').append(children[i]))),
null));
return new Args(args);
}
private static void writeIndent(PrintWriter pw, int level) {
for (int i = 0; i < level; i++)
pw.print(indent);
}
private static void writeNode(Node node, PrintWriter pw, int deep) {
switch (node.getNodeType()) {
case 8: // '\b'
writeIndent(pw, deep);
pw.print("<!--");
pw.print(node.getNodeValue());
pw.println("-->");
return;
case 3: // '\003'
String value = node.getNodeValue().trim();
if (value.length() == 0)
return;
writeIndent(pw, deep);
for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
switch (c) {
case 60: // '<'
pw.print("<");
break;
case 62: // '>'
pw.print("<");
break;
case 38: // '&'
pw.print("&");
break;
case 39: // '\''
pw.print("'");
break;
case 34: // '"'
pw.print(""");
break;
default:
pw.print(c);
break;
}
}
pw.println();
return;
case 1: // '\001'
if (!node.hasChildNodes())
return;
for (int i = 0; i < deep; i++)
pw.print(indent);
String nodeName = node.getNodeName();
pw.print('<');
pw.print(nodeName);
NamedNodeMap nnm = node.getAttributes();
if (nnm != null) {
for (int i = 0; i < nnm.getLength(); i++) {
Node attr = nnm.item(i);
pw.print(' ');
pw.print(attr.getNodeName());
pw.print("=\"");
pw.print(attr.getNodeValue());
pw.print('"');
}
}
if (node.hasChildNodes()) {
NodeList children = node.getChildNodes();
if (children.getLength() == 0) {
pw.print('<');
pw.print(nodeName);
pw.println("/>");
return;
}
if (children.getLength() == 1) {
Node n = children.item(0);
if (n.getNodeType() == 3) {
String v = n.getNodeValue();
if (v != null)
v = v.trim();
if (v == null || v.length() == 0) {
pw.println(" />");
return;
} else {
pw.print('>');
pw.print(v);
pw.print("</");
pw.print(nodeName);
pw.println('>');
return;
}
}
}
pw.println(">");
for (int i = 0; i < children.getLength(); i++)
writeNode(children.item(i), pw, deep + 1);
for (int i = 0; i < deep; i++)
pw.print(indent);
pw.print("</");
pw.print(nodeName);
pw.println(">");
} else {
pw.println("/>");
}
return;
case 9: // '\t'
pw.println(XML_HEAD);
NodeList nl = node.getChildNodes();
for (int i = 0; i < nl.getLength(); i++)
writeNode(nl.item(i), pw, 0);
return;
case 2: // '\002'
case 4: // '\004'
case 5: // '\005'
case 6: // '\006'
case 7: // '\007'
default:
return;
}
}
private Node findNode(String key) {
Node ancestor = root;
StringTokenizer st = new StringTokenizer(key, "/");
label0: do {
if (st.hasMoreTokens()) {
String nodeName = st.nextToken();
NodeList nl = ancestor.getChildNodes();
int i = 0;
do {
if (i >= nl.getLength())
continue label0;
Node n = nl.item(i);
if (nodeName.equals(n.getNodeName())) {
ancestor = n;
if (!st.hasMoreTokens())
return n;
continue label0;
}
i++;
} while (true);
}
return null;
} while (true);
}
private Node createNode(String key) {
Node ancestor = root;
StringTokenizer st = new StringTokenizer(key, "/");
label0: do
if (st.hasMoreTokens()) {
String nodeName = st.nextToken();
NodeList nl = ancestor.getChildNodes();
int i = 0;
do {
if (i >= nl.getLength())
break;
Node n = nl.item(i);
if (nodeName.equals(n.getNodeName())) {
ancestor = n;
if (!st.hasMoreTokens())
return ancestor;
continue label0;
}
i++;
} while (true);
do {
Node n = doc.createElement(nodeName);
ancestor.appendChild(n);
ancestor = n;
if (!st.hasMoreTokens())
return ancestor;
nodeName = st.nextToken();
} while (true);
} else {
return null;
}
while (true);
}
private Node createNode(Node ancestor, String key) {
StringTokenizer st = new StringTokenizer(key, "/");
label0: do {
if (st.hasMoreTokens()) {
String nodeName = st.nextToken();
NodeList nl = ancestor.getChildNodes();
int i = 0;
do {
if (i >= nl.getLength())
break;
if (nodeName.equals(nl.item(i).getNodeName())) {
ancestor = nl.item(i);
continue label0;
}
i++;
} while (true);
return null;
}
return ancestor;
} while (true);
}
public String get(String key, String def) {
if (key == null)
throw new NullPointerException("parameter key is null");
Node node = findNode(key);
if (node == null)
return def;
NodeList nl = node.getChildNodes();
for (int i = 0; i < nl.getLength(); i++)
if (nl.item(i).getNodeType() == 3)
return nl.item(i).getNodeValue().trim();
node.appendChild(doc.createTextNode(def));
return def;
}
public void put(String key, String value) {
if (key == null)
throw new NullPointerException("parameter key is null");
if (value == null)
throw new NullPointerException("parameter value is null");
value = value.trim();
Node node = createNode(key);
NodeList nl = node.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node child = nl.item(i);
if (child.getNodeType() != 3)
continue;
String childValue = child.getNodeValue().trim();
if (childValue.length() == 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -