📄 idltype.java
字号:
package com.corba.mnq.tool.idl;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.corba.mnq.tool.BNFTool;
import com.corba.mnq.tool.Warehouse;
import com.corba.mnq.tool.idl.type.CTArray;
import com.corba.mnq.tool.idl.type.CTBasic;
import com.corba.mnq.tool.idl.type.CTDeclaration;
import com.corba.mnq.tool.idl.type.CTDefaultValue;
import com.corba.mnq.tool.idl.type.CTEnum;
import com.corba.mnq.tool.idl.type.CTSequence;
import com.corba.mnq.tool.idl.type.CTStruct;
import com.corba.mnq.tool.idl.type.CTUnion;
import com.corba.mnq.tool.idl.type.TypeBase;
public class IdlType extends IdlBase {
public IdlType() {
super();
// TODO Auto-generated constructor stub
}
// this attribute type
public String type;
// union's switch type
public String stype;
// sequence's included type
public String itype;
// array's length
public int len = 0;
public void recurContent() {
// TODO Auto-generated method stub
prefix = getPrefix();
}
public String getIconName() {
return "type.gif";
}
private TypeBase findArrayElementType() {
TypeBase ret = null;
// Handle sequence type
if (type.equalsIgnoreCase("sequence")) {
ret = getSequenceType();
return ret;
}
ret = getDeclaratorType();
return ret;
}
public TypeBase toType() {
TypeBase ret = null;
// Handle array type
if (len > 0) {
CTArray at = new CTArray();
at.dn = cName;
at.rdn = sName;
at.id = id();
at.len = len;
at.withAliasName = true;
at.t_in = findArrayElementType();
return at;
}
// Handle struct type
if (type.equalsIgnoreCase("struct")) {
CTStruct stt = new CTStruct();
stt.dn = cName;
stt.rdn = sName;
stt.id = id();
getStructMems(stt.mems, stt.memType);
return stt;
}
// Handle union type
if (type.equalsIgnoreCase("union")) {
CTUnion ut = new CTUnion();
ut.dn = cName;
ut.rdn = sName;
ut.id = id();
getUnionMems(ut.disType, ut.mems, ut.memType);
return ut;
}
// Handle enum type
if (type.equalsIgnoreCase("enum")) {
CTEnum et = new CTEnum();
et.rdn = sName;
et.dn = cName;
et.id = id();
getEnumMems(et.mems);
return et;
}
// Handle sequence type
if (type.equalsIgnoreCase("sequence")) {
CTSequence st = new CTSequence();
st.rdn = sName;
st.dn = cName;
st.id = id();
st.t_in = getSequenceType();
return st;
}
CTDeclaration dt = new CTDeclaration();
dt.rdn = sName;
dt.dn = cName;
dt.id = id();
dt.t_in = getDeclaratorType();
ret = dt;
return ret;
}
private void getUnionMems(List di, List m, List mt) {
String param_type_spec = BNFTool.param_type_spec;
// group 1--label, group 2--memType, group 3--memName, group 4--length
String strmem = "(?>case)" + "\\s+(?>')?(\\S+)(?>')?\\s*:\\s*" + "("
+ param_type_spec + ")" + "\\s+" + BNFTool.declarator + "\\s*;";
Pattern pType = Pattern.compile(strmem, Pattern.DOTALL
+ Pattern.MULTILINE);
Matcher mType = pType.matcher(val);
while (mType.find()) {
String name = mType.group(3);
String type = mType.group(2).replaceAll("\\s+", "").replaceAll(
"::_", "::");
String label = mType.group(1);
String alen = mType.group(4);
int len = BNFTool.parsePositiveIntConst(alen);
if (len > 0) {
m.add(name);
CTArray at = new CTArray();
at.dn = "ArrayWithoutAliasName";
at.rdn = "ArrayWithoutAliasName";
at.id = "";
at.len = len;
at.withAliasName = false;
at.t_in = getIncludedType(type);
mt.add(at);
} else {
m.add(name);
mt.add(getIncludedType(type));
}
di.add(getCaseInstance(label));
}
// group 1--memType, group 2--memName, group 3--length
strmem = "(?>default)" + "\\s*:\\s*" + "(" + param_type_spec + ")"
+ "\\s+" + BNFTool.declarator + ";";
pType = Pattern.compile(strmem, Pattern.DOTALL + Pattern.MULTILINE);
mType = pType.matcher(val);
while (mType.find()) {
String name = mType.group(2);
String type = mType.group(1).replaceAll("\\s+", "").replaceAll(
"::_", "::");
String alen = mType.group(3);
int len = BNFTool.parsePositiveIntConst(alen);
if (len > 0) {
m.add(name);
CTArray at = new CTArray();
at.dn = "ArrayWithoutAliasName";
at.rdn = "ArrayWithoutAliasName";
at.id = "";
at.len = len;
at.withAliasName = false;
at.t_in = getIncludedType(type);
mt.add(at);
} else {
m.add(name);
mt.add(getIncludedType(type));
}
di.add(getCaseInstance("default"));
break;
}
}
private TypeBase getCaseInstance(String lab) {
TypeBase ret = null;
ret = getIncludedType(stype);
if (lab.equalsIgnoreCase("default")) {
ret.val = new CTDefaultValue();
return ret;
}
if (ret instanceof CTBasic) {
CTBasic bt = (CTBasic) ret;
if (bt.rdn.equalsIgnoreCase("boolean")) {
if (lab.equalsIgnoreCase("true")) {
bt.val = Boolean.TRUE;
} else {
bt.val = Boolean.FALSE;
}
return bt;
} else if (bt.rdn.equalsIgnoreCase("char")) {
bt.val = new Character(lab.charAt(0));
}
if (bt.rdn.equalsIgnoreCase("short")) {
bt.val = Short.valueOf(lab);
} else if (bt.rdn.equalsIgnoreCase("long")) {
bt.val = Integer.valueOf(lab);
} else if (bt.rdn.equalsIgnoreCase("long long")
|| bt.rdn.equalsIgnoreCase("longlong")) {
bt.val = Long.valueOf(lab);
} else if (bt.rdn.equalsIgnoreCase("unsigned short")
|| bt.rdn.equalsIgnoreCase("unsignedshort")) {
bt.val = Short.valueOf(lab);
} else if (bt.rdn.equalsIgnoreCase("unsigned long")
|| bt.rdn.equalsIgnoreCase("unsignedlong")) {
bt.val = Integer.valueOf(lab);
} else if (bt.rdn.equalsIgnoreCase("unsigned long long")
|| bt.rdn.equalsIgnoreCase("unsignedlonglong")) {
bt.val = Long.valueOf(lab);
}
} else if (ret instanceof CTEnum) {
CTEnum et = (CTEnum) ret;
for (int i = 0; i < et.mems.size(); i++) {
if (lab.equalsIgnoreCase((String) et.mems.get(i))) {
et.val = new Integer(i);
break;
}
}
return et;
}
return ret;
}
private void getStructMems(List m, List mt) {
String param_type_spec = BNFTool.param_type_spec;
// group 1--memType, group 2--memName
// String strmem = "(" + param_type_spec + ")" + "\\s+" + "("
// + BNFTool.declarators + ")";
// remove can't correctly parse unsigned long long ddd; struct member error
String strmem = "(" + param_type_spec + ")" + "\\s+" + "("
+ BNFTool.declarators + ")" + "\\s*;";
Pattern pType = Pattern.compile(strmem, Pattern.DOTALL
+ Pattern.MULTILINE);
Matcher mType = pType.matcher(val);
while (mType.find()) {
String declarators = mType.group(2);
String type = mType.group(1).replaceAll("\\s+", "").replaceAll(
"::_", "::");
// group 1--name, group 2--arraylength
Pattern pDeclarator = Pattern.compile(BNFTool.declarator,
Pattern.DOTALL + Pattern.MULTILINE);
Matcher mDeclarator = pDeclarator.matcher(declarators);
while (mDeclarator.find()) {
String name = mDeclarator.group(1);
String alen = mDeclarator.group(2);
int len = BNFTool.parsePositiveIntConst(alen);
if (len > 0) {
m.add(name);
CTArray at = new CTArray();
at.dn = "ArrayWithoutAliasName";
at.rdn = "ArrayWithoutAliasName";
at.id = "";
at.len = len;
at.withAliasName = false;
at.t_in = getIncludedType(type);
mt.add(at);
} else {
m.add(name);
mt.add(getIncludedType(type));
}
}
}
}
private void getEnumMems(List l) {
Pattern pType = Pattern.compile(BNFTool.identifier_catch,
Pattern.DOTALL + Pattern.MULTILINE);
Matcher mType = pType.matcher(val);
while (mType.find()) {
l.add(mType.group(1));
}
}
private TypeBase getSequenceType() {
TypeBase ret = null;
ret = getIncludedType(itype);
return ret;
}
private TypeBase getDeclaratorType() {
TypeBase ret = null;
ret = getIncludedType(type);
return ret;
}
private TypeBase getIncludedType(String t) {
String tmp;
Object node = null;
if (t.indexOf("::") < 0) {
String val;
val = cName;
do {
val = val.substring(0, val.lastIndexOf("::"));
tmp = val + "::" + t;
node = Warehouse.cname2node.get(tmp.replaceAll("::_", "::"));
if (node != null)
break;
if (val.equalsIgnoreCase(""))
break;
} while (true);
} else {
if (t.startsWith("::")) {
tmp = t;
} else {
tmp = "::" + t;
}
node = Warehouse.cname2node.get(tmp.replaceAll("::_", "::"));
}
if (node != null) {
if (node instanceof IdlType) {
return ((IdlType) node).toType();
} else if (node instanceof IdlInterface) {
return ((IdlInterface) node).toType();
} else {
System.out.println("Wrong in getIncludedType() of IdlType: "
+ ((IdlBase) node).content);
}
}
CTBasic bt = new CTBasic();
if (t.equalsIgnoreCase("float"))
bt.rdn = "float";
else if (t.equalsIgnoreCase("double"))
bt.rdn = "double";
else if (t.equalsIgnoreCase("long double")
|| t.equalsIgnoreCase("longdouble"))
bt.rdn = "long double";
else if (t.equalsIgnoreCase("short"))
bt.rdn = "short";
else if (t.equalsIgnoreCase("long"))
bt.rdn = "long";
else if (t.equalsIgnoreCase("long long")
|| t.equalsIgnoreCase("longlong"))
bt.rdn = "long long";
else if (t.equalsIgnoreCase("unsigned short")
|| t.equalsIgnoreCase("unsignedshort"))
bt.rdn = "unsigned short";
else if (t.equalsIgnoreCase("unsigned long")
|| t.equalsIgnoreCase("unsignedlong"))
bt.rdn = "unsigned long";
else if (t.equalsIgnoreCase("unsigned long long")
|| t.equalsIgnoreCase("unsignedlonglong"))
bt.rdn = "unsigned long long";
else if (t.equalsIgnoreCase("char"))
bt.rdn = "char";
else if (t.equalsIgnoreCase("wchar"))
bt.rdn = "wchar";
else if (t.equalsIgnoreCase("boolean"))
bt.rdn = "boolean";
else if (t.equalsIgnoreCase("octet"))
bt.rdn = "octet";
else if (t.equalsIgnoreCase("any"))
bt.rdn = "any";
else if (t.equalsIgnoreCase("string"))
bt.rdn = "string";
else if (t.equalsIgnoreCase("wstring"))
bt.rdn = "wstring";
else if (t.equalsIgnoreCase("void"))
bt.rdn = "void";
else {
bt.rdn = "string";
}
return bt;
}
/**
* @param args
*/
public static void main_(String[] args) {
// TODO Auto-generated method stub
}
}
/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -