1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools;
24  
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.kernel.xml.Document;
27  import com.liferay.portal.kernel.xml.DocumentException;
28  import com.liferay.portal.kernel.xml.Element;
29  import com.liferay.portal.kernel.xml.SAXReaderUtil;
30  import com.liferay.portal.tools.servicebuilder.ServiceBuilder;
31  import com.liferay.portal.util.InitUtil;
32  
33  import com.thoughtworks.qdox.JavaDocBuilder;
34  import com.thoughtworks.qdox.model.JavaClass;
35  import com.thoughtworks.qdox.model.JavaMethod;
36  import com.thoughtworks.qdox.model.JavaParameter;
37  import com.thoughtworks.qdox.model.Type;
38  
39  import java.io.File;
40  import java.io.IOException;
41  
42  import java.util.Iterator;
43  import java.util.LinkedHashSet;
44  import java.util.Set;
45  
46  /**
47   * <a href="InstanceWrapperBuilder.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class InstanceWrapperBuilder {
53  
54      public static void main(String[] args) {
55          InitUtil.initWithSpring();
56  
57          if (args.length == 1) {
58              new InstanceWrapperBuilder(args[0]);
59          }
60          else {
61              throw new IllegalArgumentException();
62          }
63      }
64  
65      public InstanceWrapperBuilder(String xml) {
66          try {
67              File file = new File(xml);
68  
69              Document doc = null;
70  
71              try {
72                  doc = SAXReaderUtil.read(file);
73              }
74              catch (DocumentException de) {
75                  de.printStackTrace();
76              }
77  
78              Element root = doc.getRootElement();
79  
80              Iterator<Element> itr = root.elements(
81                  "instance-wrapper").iterator();
82  
83              while (itr.hasNext()) {
84                  Element instanceWrapper = itr.next();
85  
86                  String parentDir = instanceWrapper.attributeValue("parent-dir");
87                  String srcFile = instanceWrapper.attributeValue("src-file");
88  
89                  _createIW(parentDir, srcFile);
90              }
91          }
92          catch (Exception e) {
93              e.printStackTrace();
94          }
95      }
96  
97      private void _createIW(String parentDir, String srcFile)
98          throws IOException {
99  
100         JavaClass javaClass = _getJavaClass(parentDir, srcFile);
101 
102         JavaMethod[] methods = javaClass.getMethods();
103 
104         StringBuilder sb = new StringBuilder();
105 
106         // Package
107 
108         sb.append("package " + javaClass.getPackage() + ";");
109 
110         // Class declaration
111 
112         sb.append("public class " + javaClass.getName() + "_IW {");
113 
114         // Methods
115 
116         sb.append("public static " + javaClass.getName() + "_IW getInstance() {");
117         sb.append("return _instance;");
118         sb.append("}");
119 
120         for (int i = 0; i < methods.length; i++) {
121             JavaMethod javaMethod = methods[i];
122 
123             String methodName = javaMethod.getName();
124 
125             if (javaMethod.isPublic() && javaMethod.isStatic()) {
126                 if (methodName.equals("getInstance")) {
127                     methodName = "getWrappedInstance";
128                 }
129 
130                 sb.append("public " + javaMethod.getReturns().getValue() + _getDimensions(javaMethod.getReturns()) + " " + methodName + "(");
131 
132                 JavaParameter[] parameters = javaMethod.getParameters();
133 
134                 for (int j = 0; j < parameters.length; j++) {
135                     JavaParameter javaParameter = parameters[j];
136 
137                     sb.append(javaParameter.getType().getValue() + javaParameter.getGenericsName() + _getDimensions(javaParameter.getType()) + " " + javaParameter.getName());
138 
139                     if ((j + 1) != parameters.length) {
140                         sb.append(", ");
141                     }
142                 }
143 
144                 sb.append(")");
145 
146                 Type[] thrownExceptions = javaMethod.getExceptions();
147 
148                 Set<String> newExceptions = new LinkedHashSet<String>();
149 
150                 for (int j = 0; j < thrownExceptions.length; j++) {
151                     Type thrownException = thrownExceptions[j];
152 
153                     newExceptions.add(thrownException.getValue());
154                 }
155 
156                 if (newExceptions.size() > 0) {
157                     sb.append(" throws ");
158 
159                     Iterator<String> itr = newExceptions.iterator();
160 
161                     while (itr.hasNext()) {
162                         sb.append(itr.next());
163 
164                         if (itr.hasNext()) {
165                             sb.append(", ");
166                         }
167                     }
168                 }
169 
170                 sb.append("{");
171 
172                 if (!javaMethod.getReturns().getValue().equals("void")) {
173                     sb.append("return ");
174                 }
175 
176                 sb.append(javaClass.getName() + "." + javaMethod.getName() + "(");
177 
178                 for (int j = 0; j < parameters.length; j++) {
179                     JavaParameter javaParameter = parameters[j];
180 
181                     sb.append(javaParameter.getName());
182 
183                     if ((j + 1) != parameters.length) {
184                         sb.append(", ");
185                     }
186                 }
187 
188                 sb.append(");");
189                 sb.append("}");
190             }
191         }
192 
193         // Private constructor
194 
195         sb.append("private " + javaClass.getName() + "_IW() {");
196         sb.append("}");
197 
198         // Fields
199 
200         sb.append("private static " + javaClass.getName() + "_IW _instance = new " + javaClass.getName() + "_IW();");
201 
202         // Class close brace
203 
204         sb.append("}");
205 
206         // Write file
207 
208         File file = new File(parentDir + "/" + StringUtil.replace(javaClass.getPackage(), ".", "/") + "/" + javaClass.getName() + "_IW.java");
209 
210         ServiceBuilder.writeFile(file, sb.toString());
211     }
212 
213     private String _getDimensions(Type type) {
214         String dimensions = "";
215 
216         for (int i = 0; i < type.getDimensions(); i++) {
217             dimensions += "[]";
218         }
219 
220         return dimensions;
221     }
222 
223     private JavaClass _getJavaClass(String parentDir, String srcFile)
224         throws IOException {
225 
226         String className = StringUtil.replace(
227             srcFile.substring(0, srcFile.length() - 5), "/", ".");
228 
229         JavaDocBuilder builder = new JavaDocBuilder();
230 
231         builder.addSource(new File(parentDir + "/" + srcFile));
232 
233         return builder.getClassByName(className);
234     }
235 
236 }