1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.tools;
16  
17  import com.liferay.portal.kernel.util.StringUtil;
18  import com.liferay.portal.kernel.xml.Document;
19  import com.liferay.portal.kernel.xml.DocumentException;
20  import com.liferay.portal.kernel.xml.Element;
21  import com.liferay.portal.kernel.xml.SAXReaderUtil;
22  import com.liferay.portal.tools.servicebuilder.ServiceBuilder;
23  import com.liferay.portal.util.InitUtil;
24  
25  import com.thoughtworks.qdox.JavaDocBuilder;
26  import com.thoughtworks.qdox.model.DocletTag;
27  import com.thoughtworks.qdox.model.JavaClass;
28  import com.thoughtworks.qdox.model.JavaMethod;
29  import com.thoughtworks.qdox.model.JavaParameter;
30  import com.thoughtworks.qdox.model.Type;
31  
32  import java.io.File;
33  import java.io.IOException;
34  
35  import java.util.Iterator;
36  import java.util.LinkedHashSet;
37  import java.util.Set;
38  
39  /**
40   * <a href="InstanceWrapperBuilder.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   */
44  public class InstanceWrapperBuilder {
45  
46      public static void main(String[] args) {
47          InitUtil.initWithSpring();
48  
49          if (args.length == 1) {
50              new InstanceWrapperBuilder(args[0]);
51          }
52          else {
53              throw new IllegalArgumentException();
54          }
55      }
56  
57      public InstanceWrapperBuilder(String xml) {
58          try {
59              File file = new File(xml);
60  
61              Document doc = null;
62  
63              try {
64                  doc = SAXReaderUtil.read(file);
65              }
66              catch (DocumentException de) {
67                  de.printStackTrace();
68              }
69  
70              Element root = doc.getRootElement();
71  
72              Iterator<Element> itr = root.elements(
73                  "instance-wrapper").iterator();
74  
75              while (itr.hasNext()) {
76                  Element instanceWrapper = itr.next();
77  
78                  String parentDir = instanceWrapper.attributeValue("parent-dir");
79                  String srcFile = instanceWrapper.attributeValue("src-file");
80  
81                  _createIW(parentDir, srcFile);
82              }
83          }
84          catch (Exception e) {
85              e.printStackTrace();
86          }
87      }
88  
89      private void _createIW(String parentDir, String srcFile)
90          throws IOException {
91  
92          JavaClass javaClass = _getJavaClass(parentDir, srcFile);
93  
94          JavaMethod[] methods = javaClass.getMethods();
95  
96          StringBuilder sb = new StringBuilder();
97  
98          // Package
99  
100         sb.append("package " + javaClass.getPackage().getName() + ";");
101 
102         // Class declaration
103 
104         sb.append("public class " + javaClass.getName() + "_IW {");
105 
106         // Methods
107 
108         sb.append("public static " + javaClass.getName() + "_IW getInstance() {");
109         sb.append("return _instance;");
110         sb.append("}\n");
111 
112         for (int i = 0; i < methods.length; i++) {
113             JavaMethod javaMethod = methods[i];
114 
115             String methodName = javaMethod.getName();
116 
117             if (!javaMethod.isPublic() || !javaMethod.isStatic()) {
118                 continue;
119             }
120 
121             if (methodName.equals("getInstance")) {
122                 methodName = "getWrappedInstance";
123             }
124 
125             DocletTag[] docletTags = javaMethod.getTagsByName("deprecated");
126 
127             if ((docletTags != null) && (docletTags.length > 0)) {
128                 sb.append("\t/**\n");
129                 sb.append("\t * @deprecated\n");
130                 sb.append("\t */\n");
131             }
132 
133             sb.append("public " + _getTypeGenericsName(javaMethod.getReturns()) + " " + methodName + "(");
134 
135             JavaParameter[] parameters = javaMethod.getParameters();
136 
137             for (int j = 0; j < parameters.length; j++) {
138                 JavaParameter javaParameter = parameters[j];
139 
140                 sb.append(_getTypeGenericsName(javaParameter.getType()));
141 
142                 if (javaParameter.isVarArgs()) {
143                     sb.append("...");
144                 }
145 
146                 sb.append(" " + javaParameter.getName());
147 
148                 if ((j + 1) != parameters.length) {
149                     sb.append(", ");
150                 }
151             }
152 
153             sb.append(")");
154 
155             Type[] thrownExceptions = javaMethod.getExceptions();
156 
157             Set<String> newExceptions = new LinkedHashSet<String>();
158 
159             for (int j = 0; j < thrownExceptions.length; j++) {
160                 Type thrownException = thrownExceptions[j];
161 
162                 newExceptions.add(thrownException.getValue());
163             }
164 
165             if (newExceptions.size() > 0) {
166                 sb.append(" throws ");
167 
168                 Iterator<String> itr = newExceptions.iterator();
169 
170                 while (itr.hasNext()) {
171                     sb.append(itr.next());
172 
173                     if (itr.hasNext()) {
174                         sb.append(", ");
175                     }
176                 }
177             }
178 
179             sb.append("{\n");
180 
181             if (!javaMethod.getReturns().getValue().equals("void")) {
182                 sb.append("return ");
183             }
184 
185             sb.append(javaClass.getName() + "." + javaMethod.getName() + "(");
186 
187             for (int j = 0; j < parameters.length; j++) {
188                 JavaParameter javaParameter = parameters[j];
189 
190                 sb.append(javaParameter.getName());
191 
192                 if ((j + 1) != parameters.length) {
193                     sb.append(", ");
194                 }
195             }
196 
197             sb.append(");");
198             sb.append("}\n");
199         }
200 
201         // Private constructor
202 
203         sb.append("private " + javaClass.getName() + "_IW() {");
204         sb.append("}");
205 
206         // Fields
207 
208         sb.append("private static " + javaClass.getName() + "_IW _instance = new " + javaClass.getName() + "_IW();");
209 
210         // Class close brace
211 
212         sb.append("}");
213 
214         // Write file
215 
216         File file = new File(parentDir + "/" + StringUtil.replace(javaClass.getPackage().getName(), ".", "/") + "/" + javaClass.getName() + "_IW.java");
217 
218         ServiceBuilder.writeFile(file, sb.toString());
219     }
220 
221     private String _getDimensions(Type type) {
222         String dimensions = "";
223 
224         for (int i = 0; i < type.getDimensions(); i++) {
225             dimensions += "[]";
226         }
227 
228         return dimensions;
229     }
230 
231     private JavaClass _getJavaClass(String parentDir, String srcFile)
232         throws IOException {
233 
234         String className = StringUtil.replace(
235             srcFile.substring(0, srcFile.length() - 5), "/", ".");
236 
237         JavaDocBuilder builder = new JavaDocBuilder();
238 
239         builder.addSource(new File(parentDir + "/" + srcFile));
240 
241         return builder.getClassByName(className);
242     }
243 
244     private String _getTypeGenericsName(Type type) {
245         StringBuilder sb = new StringBuilder();
246 
247         sb.append(type.getValue());
248 
249         Type[] actualTypeArguments = type.getActualTypeArguments();
250 
251         if (actualTypeArguments != null) {
252             sb.append("<");
253 
254             for (int i = 0; i < actualTypeArguments.length; i++) {
255                 if (i > 0) {
256                     sb.append(", ");
257                 }
258 
259                 sb.append(_getTypeGenericsName(actualTypeArguments[i]));
260             }
261 
262             sb.append(">");
263         }
264 
265         sb.append(_getDimensions(type));
266 
267         return sb.toString();
268     }
269 
270 }