1
22
23 package com.liferay.portal.tools;
24
25 import com.liferay.portal.kernel.util.StringMaker;
26 import com.liferay.portal.kernel.util.StringUtil;
27
28 import com.thoughtworks.qdox.JavaDocBuilder;
29 import com.thoughtworks.qdox.model.JavaClass;
30 import com.thoughtworks.qdox.model.JavaMethod;
31 import com.thoughtworks.qdox.model.JavaParameter;
32 import com.thoughtworks.qdox.model.Type;
33
34 import java.io.File;
35 import java.io.IOException;
36
37 import java.util.Iterator;
38 import java.util.LinkedHashSet;
39 import java.util.Set;
40
41 import org.dom4j.Document;
42 import org.dom4j.DocumentException;
43 import org.dom4j.Element;
44 import org.dom4j.io.SAXReader;
45
46
52 public class InstanceWrapperBuilder {
53
54 public static void main(String[] args) {
55 if (args.length == 1) {
56 new InstanceWrapperBuilder(args[0]);
57 }
58 else {
59 throw new IllegalArgumentException();
60 }
61 }
62
63 public InstanceWrapperBuilder(String xml) {
64 try {
65 File file = new File(xml);
66
67 SAXReader reader = new SAXReader();
68
69 Document doc = null;
70
71 try {
72 doc = reader.read(file);
73 }
74 catch (DocumentException de) {
75 de.printStackTrace();
76 }
77
78 Element root = doc.getRootElement();
79
80 Iterator itr = root.elements("instance-wrapper").iterator();
81
82 while (itr.hasNext()) {
83 Element instanceWrapper = (Element)itr.next();
84
85 String parentDir = instanceWrapper.attributeValue("parent-dir");
86 String srcFile = instanceWrapper.attributeValue("src-file");
87
88 _createIW(parentDir, srcFile);
89 }
90 }
91 catch (Exception e) {
92 e.printStackTrace();
93 }
94 }
95
96 private void _createIW(String parentDir, String srcFile)
97 throws IOException {
98
99 JavaClass javaClass = _getJavaClass(parentDir, srcFile);
100
101 JavaMethod[] methods = javaClass.getMethods();
102
103 StringMaker sm = new StringMaker();
104
105
107 sm.append("package " + javaClass.getPackage() + ";");
108
109
111 sm.append("public class " + javaClass.getName() + "_IW {");
112
113
115 sm.append("public static " + javaClass.getName() + "_IW getInstance() {");
116 sm.append("return _instance;");
117 sm.append("}");
118
119 for (int i = 0; i < methods.length; i++) {
120 JavaMethod javaMethod = methods[i];
121
122 String methodName = javaMethod.getName();
123
124 if (javaMethod.isPublic() && javaMethod.isStatic()) {
125 if (methodName.equals("getInstance")) {
126 methodName = "getWrappedInstance";
127 }
128
129 sm.append("public " + javaMethod.getReturns().getValue() + _getDimensions(javaMethod.getReturns()) + " " + methodName + "(");
130
131 JavaParameter[] parameters = javaMethod.getParameters();
132
133 for (int j = 0; j < parameters.length; j++) {
134 JavaParameter javaParameter = parameters[j];
135
136 sm.append(javaParameter.getType().getValue() + _getDimensions(javaParameter.getType()) + " " + javaParameter.getName());
137
138 if ((j + 1) != parameters.length) {
139 sm.append(", ");
140 }
141 }
142
143 sm.append(")");
144
145 Type[] thrownExceptions = javaMethod.getExceptions();
146
147 Set newExceptions = new LinkedHashSet();
148
149 for (int j = 0; j < thrownExceptions.length; j++) {
150 Type thrownException = thrownExceptions[j];
151
152 newExceptions.add(thrownException.getValue());
153 }
154
155 if (newExceptions.size() > 0) {
156 sm.append(" throws ");
157
158 Iterator itr = newExceptions.iterator();
159
160 while (itr.hasNext()) {
161 sm.append(itr.next());
162
163 if (itr.hasNext()) {
164 sm.append(", ");
165 }
166 }
167 }
168
169 sm.append("{");
170
171 if (!javaMethod.getReturns().getValue().equals("void")) {
172 sm.append("return ");
173 }
174
175 sm.append(javaClass.getName() + "." + javaMethod.getName() + "(");
176
177 for (int j = 0; j < parameters.length; j++) {
178 JavaParameter javaParameter = parameters[j];
179
180 sm.append(javaParameter.getName());
181
182 if ((j + 1) != parameters.length) {
183 sm.append(", ");
184 }
185 }
186
187 sm.append(");");
188 sm.append("}");
189 }
190 }
191
192
194 sm.append("private " + javaClass.getName() + "_IW() {");
195 sm.append("}");
196
197
199 sm.append("private static " + javaClass.getName() + "_IW _instance = new " + javaClass.getName() + "_IW();");
200
201
203 sm.append("}");
204
205
207 File file = new File(parentDir + "/" + StringUtil.replace(javaClass.getPackage(), ".", "/") + "/" + javaClass.getName() + "_IW.java");
208
209 ServiceBuilder.writeFile(file, sm.toString());
210 }
211
212 private String _getDimensions(Type type) {
213 String dimensions = "";
214
215 for (int i = 0; i < type.getDimensions(); i++) {
216 dimensions += "[]";
217 }
218
219 return dimensions;
220 }
221
222 private JavaClass _getJavaClass(String parentDir, String srcFile)
223 throws IOException {
224
225 String className = StringUtil.replace(
226 srcFile.substring(0, srcFile.length() - 5), "/", ".");
227
228 JavaDocBuilder builder = new JavaDocBuilder();
229
230 builder.addSource(new File(parentDir + "/" + srcFile));
231
232 return builder.getClassByName(className);
233 }
234
235 }