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