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 " + javaMethod.getReturns().getValue() + _getDimensions(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(javaParameter.getType().getValue() + javaParameter.getGenericsName() + _getDimensions(javaParameter.getType()) + " " + javaParameter.getName());
141 
142                 if ((j + 1) != parameters.length) {
143                     sb.append(", ");
144                 }
145             }
146 
147             sb.append(")");
148 
149             Type[] thrownExceptions = javaMethod.getExceptions();
150 
151             Set<String> newExceptions = new LinkedHashSet<String>();
152 
153             for (int j = 0; j < thrownExceptions.length; j++) {
154                 Type thrownException = thrownExceptions[j];
155 
156                 newExceptions.add(thrownException.getValue());
157             }
158 
159             if (newExceptions.size() > 0) {
160                 sb.append(" throws ");
161 
162                 Iterator<String> itr = newExceptions.iterator();
163 
164                 while (itr.hasNext()) {
165                     sb.append(itr.next());
166 
167                     if (itr.hasNext()) {
168                         sb.append(", ");
169                     }
170                 }
171             }
172 
173             sb.append("{\n");
174 
175             if (!javaMethod.getReturns().getValue().equals("void")) {
176                 sb.append("return ");
177             }
178 
179             sb.append(javaClass.getName() + "." + javaMethod.getName() + "(");
180 
181             for (int j = 0; j < parameters.length; j++) {
182                 JavaParameter javaParameter = parameters[j];
183 
184                 sb.append(javaParameter.getName());
185 
186                 if ((j + 1) != parameters.length) {
187                     sb.append(", ");
188                 }
189             }
190 
191             sb.append(");");
192             sb.append("}\n");
193         }
194 
195         // Private constructor
196 
197         sb.append("private " + javaClass.getName() + "_IW() {");
198         sb.append("}");
199 
200         // Fields
201 
202         sb.append("private static " + javaClass.getName() + "_IW _instance = new " + javaClass.getName() + "_IW();");
203 
204         // Class close brace
205 
206         sb.append("}");
207 
208         // Write file
209 
210         File file = new File(parentDir + "/" + StringUtil.replace(javaClass.getPackage().getName(), ".", "/") + "/" + javaClass.getName() + "_IW.java");
211 
212         ServiceBuilder.writeFile(file, sb.toString());
213     }
214 
215     private String _getDimensions(Type type) {
216         String dimensions = "";
217 
218         for (int i = 0; i < type.getDimensions(); i++) {
219             dimensions += "[]";
220         }
221 
222         return dimensions;
223     }
224 
225     private JavaClass _getJavaClass(String parentDir, String srcFile)
226         throws IOException {
227 
228         String className = StringUtil.replace(
229             srcFile.substring(0, srcFile.length() - 5), "/", ".");
230 
231         JavaDocBuilder builder = new JavaDocBuilder();
232 
233         builder.addSource(new File(parentDir + "/" + srcFile));
234 
235         return builder.getClassByName(className);
236     }
237 
238 }