miduo_server/jieling-dbgen/src/main/java/com/dbgen/ElementHelper.java

66 lines
1.5 KiB
Java

package com.dbgen;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public final class ElementHelper {
private Element self;
private final Map<String, String> attrs = new HashMap<>();
private static Properties properties;
public static void setProperties(Properties proper) {
properties = proper;
}
public ElementHelper(Element ele) {
self = ele;
NamedNodeMap as = self.getAttributes();
for (int i = 0; i < as.getLength(); ++i) {
Attr attr = (Attr) as.item(i);
attrs.put(attr.getName(), attr.getValue());
}
}
public String getTag() {
return self.getTagName();
}
public String getString(String name) {
String value = attrs.remove(name);
if (value == null)
value = "";
if (value.length() > 2 && value.startsWith("$") && value.endsWith("$")) {
value = value.substring(1, value.length() - 1);
String[] vs = value.split(":");
if (vs.length > 2)
throw new RuntimeException("bad property \"" + value + "\"");
String key = vs[0];
value = getProperty(key);
if (null == value) {
if (vs.length > 1)
value = vs[1];
else
throw new RuntimeException("lost property \"" + key + "\"");
}
}
return value;
}
private String getProperty(String key) {
String value = System.getProperty(key);
return value != null ? value : properties != null ? properties.getProperty(key) : null;
}
public void warnUnused(String... except) {
}
}