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

113 lines
3.7 KiB
Java

package com.dbgen;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
public class Variable extends Naming {
private final static Pattern pattern = Pattern.compile("[_a-zA-Z][_a-zA-Z0-9]*");
private final static Set<String> keywords = new HashSet<>(Arrays.asList("Object", "abstract", "add", "alias", "and",
"and_eq", "as", "ascending", "asm", "assert", "async", "auto", "await", "base", "bitand", "bitor", "bool",
"boolean", "break", "byte", "case", "catch", "char", "checked", "class", "compl", "const", "const_cast",
"continue", "decimal", "default", "delegate", "delete", "descending", "do", "double", "dynamic",
"dynamic_cast", "else", "enum", "event", "explicit", "export", "extends", "extern", "false", "final",
"finally", "fixed", "float", "for", "foreach", "friend", "get", "global", "goto", "group", "if",
"implements", "implicit", "import", "in", "inline", "instanceof", "int", "interface", "internal", "into",
"is", "join", "let", "lock", "long", "mutable", "namespace", "native", "new", "not", "not_eq", "null",
"object", "operator", "or", "or_eq", "orderby", "out", "override", "package", "params", "partial",
"private", "protected", "public", "readonly", "ref", "register", "reinterpret_cast", "remove", "return",
"sbyte", "sealed", "select", "set", "short", "signed", "sizeof", "stackalloc", "static", "static_cast",
"strictfp", "string", "struct", "super", "switch", "synchronized", "template", "this", "throw", "throws",
"transient", "true", "try", "typedef", "typeid", "typename", "typeof", "uint", "ulong", "unchecked",
"union", "unsafe", "unsigned", "ushort", "using", "value", "var", "virtual", "void", "volatile", "wchar_t",
"where", "while", "xor", "xor_eq", "yield",
// c++ class com.dbgen.method names
"marshal", "unmarshal", "trace", "encode", "getType", "process", "destroy", "equals", "com.dbgen.method.MethodEquals"));
public static void verifyName(Naming nm) {
String name = nm.getName();
if (!pattern.matcher(name).matches())
throw new RuntimeException("invalid name \"" + name + "\"");
if (keywords.contains(name))
throw new RuntimeException("variable reserved keyword \"" + name + "\"");
}
private String type;
private String key = "";
private String value = "";
private String comment = "";
private Type varType;
public Variable(Jbean bean, Element self) throws Exception {
super(bean, self);
initialize(self);
}
public Variable(Naming parent, Element self) throws Exception {
super(parent, self);
initialize(self);
}
private void initialize(Element self) {
verifyName(this);
ElementHelper eh = new ElementHelper(self);
type = eh.getString("type");
key = eh.getString("key");
value = eh.getString("value");
comment = extractComment(self);
eh.warnUnused("name");
varType = Type.resolve(this, type, key, value);
}
static String extractComment(Element self) {
String comment = "";
Node c = self.getNextSibling();
if (c != null && Node.TEXT_NODE == c.getNodeType()) {
comment = c.getTextContent().trim().replaceAll("[\r\n]", "");
}
if (!comment.isEmpty())
comment = " // " + comment;
return comment;
}
public Variable(Jbean jbean, Variable oldvar) {
super(jbean, oldvar.getName());
initializeFromOther(oldvar);
}
private void initializeFromOther(Variable self) {
type = self.getTypeString();
key = self.getKey();
value = self.getValue();
}
public Type getType() {
return varType;
}
public String getTypeString() {
return type;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
public String getComment() {
return comment;
}
}