1: /**
2: *
3: */
4: package com.suprasoft.utils.flex;
5:
6: /**
7: * @作者 Charles <chio.yang@gmail.com> Suprasoft Inc.,
8: *
9: * 2008-12-25
10: */
11:
12: import java.io.BufferedWriter;
13: import java.io.File;
14: import java.io.FileWriter;
15: import java.io.IOException;
16: import java.lang.reflect.Field;
17:
18: import org.apache.commons.lang.StringUtils;
19:
20: public class POJO2VO {
21: public POJO2VO() {
22: }
23:
24: public static String getClassType(Class c) {
25: String typeName = c.getSimpleName();
26: // System.out.println("typename " + typeName);
27: // 数值型的都转成Number
28: if (typeName.equals("BigDecimal") || typeName.equals("Decimal")
29: || typeName.equals("Double") || typeName.equals("int")
30: || typeName.equals("Integer") || typeName.equals("Long")
31: || typeName.equals("long")) {
32: return "Number";
33: } else if (typeName.equals("Boolean")) {
34: return "boolean";
35: } else if (typeName.equals("Set")) {
36: return "Array";
37: } else {
38: if (isBasicType(typeName)) {
39: return typeName;// 其它类型的设置为未知类型
40: } else {
41: return typeName + "VO";// 自定义类型的,加上VO结尾
42: }
43: }
44:
45: }
46:
47: // 是否基本的数据类型
48: public static boolean isBasicType(String type) {
49: return type.equals("String") || type.equals("BigDecimal")
50: || type.equals("Decimal") || type.equals("Double")
51: || type.equals("int") || type.equals("Integer")
52: || type.equals("Boolean") || type.equals("Set")
53: || type.equals("Long") || type.equals("long")
54: || type.equals("Date");
55: }
56:
57: public static String repeat(String c, int count) {
58: String temp = "";
59: for (int i = 0; i < count; i++) {
60: temp += c;
61: }
62:
63: return temp;
64: }
65:
66: public static void makeDirByPackageName(String rootFolder, String packageName) {
67:
68: File folder = new File(rootFolder + "\\" + packageName.replace(".", "\\"));
69: if(!folder.exists()) {
70: folder.mkdirs();
71: }
72: }
73:
74: public static void generateAsFiles(String pojoQualifiedName ){
75:
76: }
77:
78: /**
79: * 生成as文件
80: *
81: * @param pojoQualifiedName
82: * @param voPackageName
83: * @param folders
84: * @throws ClassNotFoundException
85: * @throws IOException
86: */
87: public static void generateAsFile(String pojoQualifiedName, String voPackageRootFolder, String voPackageName) throws ClassNotFoundException, IOException {
88: //pojoName必须带包名的Qualified Name
89: Class c = Class.forName(pojoQualifiedName);
90: Field[] fields = c.getDeclaredFields();
91:
92: POJO2VO.makeDirByPackageName(voPackageRootFolder, voPackageName);
93: // as的vo对象名称结尾加上VO标志
94: File f = new File(voPackageRootFolder + "\\"
95: + voPackageName.replace(".", "\\") + "\\"
96: + c.getSimpleName().replace("BO", "VO") + ".as");
97:
98: BufferedWriter bw = new BufferedWriter(new FileWriter(f));
99: // 包名,没有设置包名就取和java pojo一样的包名
100: if (StringUtils.isEmpty(voPackageName)) {
101: bw.write("package " + c.getPackage().getName() + "\n{\n");
102: } else {
103: bw.write("package " + voPackageName + "\n{\n");
104: }
105:
106: // 写类
107: bw.write(repeat(" ", 4) + "public class " + c.getSimpleName().replace("BO", "VO") + "\n");
108: bw.write(repeat(" ", 4) + "{\n");
109:
110: // 写属性
111: for (int i = 0; i < fields.length; i++) {
112: Class fieldType = fields[i].getType();
113: String typeName = getClassType(fieldType);
114: bw.write(repeat(" ", 8) + "private var _" + fields[i].getName()
115: + ":" + typeName + ";\n");
116: }
117:
118: bw.write("\n\n\n");
119: // 写空的构造函数
120: bw.write(repeat(" ", 8) + "public function " + c.getSimpleName().replace("BO", "VO")
121: + "(){}\n\n");
122:
123: // 写 setter/getter 方法
124: for (int i = 0; i < fields.length; i++) {
125: Class fieldType = fields[i].getType();
126: String typeName = getClassType(fieldType);
127: // setter
128: bw.write(repeat(" ", 8) + "public function set "
129: + fields[i].getName() + "(value:" + typeName + "):void{\n");
130: bw.write(repeat(" ", 12) + "this._" + fields[i].getName()
131: + " = value;\n");
132: bw.write(repeat(" ", 8) + "}\n\n");
133: // getter
134: bw.write(repeat(" ", 8) + "public function get "
135: + fields[i].getName() + "():" + typeName + "{\n");
136: bw.write(repeat(" ", 12) + "return this._" + fields[i].getName()
137: + ";\n");
138: bw.write(repeat(" ", 8) + "}\n\n\n");
139:
140: }
141: bw.write(repeat(" ", 4) + "}\n");
142: bw.write("}");
143: bw.close();
144: }
145:
146: public static void generateAsFileBatch(String pojoPackageRootFolder, String pojoPackageName, String voPackageRootFolder, String voPackageName) throws ClassNotFoundException, IOException {
147: String packageFolder = pojoPackageRootFolder + "\\" + pojoPackageName.replace(".", "\\");
148: File dir = new File(packageFolder);
149: File[] files = dir.listFiles();
150: for(File file : files){
151: if(file.isDirectory()) continue;
152:
153: if(file.getName().endsWith("BO.java")){
154: String pojoName = pojoPackageName + "." + file.getName().replace(".java", "");
155: POJO2VO.generateAsFile(pojoName, voPackageRootFolder, voPackageName);
156: }
157: }
158: }
159:
160:
161: /*
162: * 测试
163: */
164: public static void main(String[] args) throws ClassNotFoundException, IOException {
165:
166: // String[] pojos = { "com.bankcomm.icms.domain.migrate.RawCollateralBasicBO" };
167: // for (int i = 0; i < pojos.length; i++) {
168: // POJO2VO.generateAsFile(pojos[i], "C:", "com.bankcomm.icms.domain");
169: // }
170:
171: POJO2VO.generateAsFileBatch("D:\\ICMS\\eclipse\\workspace\\Utils\\src\\",
172: "com.bankcomm.icms.domain.migrate",
173: "C:\\Documents and Settings\\Administrator\\My Documents\\Flex Builder 3\\Temp\\src",
174: "com.bankcomm.icms.domain");
175: }
176: }