1   /**
2    * Copyright (c) 2000-2008 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.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  /**
49   * <a href="InstanceWrapperBuilder.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   *
53   */
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<Element> itr = root.elements(
87                  "instance-wrapper").iterator();
88  
89              while (itr.hasNext()) {
90                  Element instanceWrapper = itr.next();
91  
92                  String parentDir = instanceWrapper.attributeValue("parent-dir");
93                  String srcFile = instanceWrapper.attributeValue("src-file");
94  
95                  _createIW(parentDir, srcFile);
96              }
97          }
98          catch (Exception e) {
99              e.printStackTrace();
100         }
101     }
102 
103     private void _createIW(String parentDir, String srcFile)
104         throws IOException {
105 
106         JavaClass javaClass = _getJavaClass(parentDir, srcFile);
107 
108         JavaMethod[] methods = javaClass.getMethods();
109 
110         StringMaker sm = new StringMaker();
111 
112         // Package
113 
114         sm.append("package " + javaClass.getPackage() + ";");
115 
116         // Class declaration
117 
118         sm.append("public class " + javaClass.getName() + "_IW {");
119 
120         // Methods
121 
122         sm.append("public static " + javaClass.getName() + "_IW getInstance() {");
123         sm.append("return _instance;");
124         sm.append("}");
125 
126         for (int i = 0; i < methods.length; i++) {
127             JavaMethod javaMethod = methods[i];
128 
129             String methodName = javaMethod.getName();
130 
131             if (javaMethod.isPublic() && javaMethod.isStatic()) {
132                 if (methodName.equals("getInstance")) {
133                     methodName = "getWrappedInstance";
134                 }
135 
136                 sm.append("public " + javaMethod.getReturns().getValue() + _getDimensions(javaMethod.getReturns()) + " " + methodName + "(");
137 
138                 JavaParameter[] parameters = javaMethod.getParameters();
139 
140                 for (int j = 0; j < parameters.length; j++) {
141                     JavaParameter javaParameter = parameters[j];
142 
143                     sm.append(javaParameter.getType().getValue() + javaParameter.getGenericsName() + _getDimensions(javaParameter.getType()) + " " + javaParameter.getName());
144 
145                     if ((j + 1) != parameters.length) {
146                         sm.append(", ");
147                     }
148                 }
149 
150                 sm.append(")");
151 
152                 Type[] thrownExceptions = javaMethod.getExceptions();
153 
154                 Set<String> newExceptions = new LinkedHashSet<String>();
155 
156                 for (int j = 0; j < thrownExceptions.length; j++) {
157                     Type thrownException = thrownExceptions[j];
158 
159                     newExceptions.add(thrownException.getValue());
160                 }
161 
162                 if (newExceptions.size() > 0) {
163                     sm.append(" throws ");
164 
165                     Iterator<String> itr = newExceptions.iterator();
166 
167                     while (itr.hasNext()) {
168                         sm.append(itr.next());
169 
170                         if (itr.hasNext()) {
171                             sm.append(", ");
172                         }
173                     }
174                 }
175 
176                 sm.append("{");
177 
178                 if (!javaMethod.getReturns().getValue().equals("void")) {
179                     sm.append("return ");
180                 }
181 
182                 sm.append(javaClass.getName() + "." + javaMethod.getName() + "(");
183 
184                 for (int j = 0; j < parameters.length; j++) {
185                     JavaParameter javaParameter = parameters[j];
186 
187                     sm.append(javaParameter.getName());
188 
189                     if ((j + 1) != parameters.length) {
190                         sm.append(", ");
191                     }
192                 }
193 
194                 sm.append(");");
195                 sm.append("}");
196             }
197         }
198 
199         // Private constructor
200 
201         sm.append("private " + javaClass.getName() + "_IW() {");
202         sm.append("}");
203 
204         // Fields
205 
206         sm.append("private static " + javaClass.getName() + "_IW _instance = new " + javaClass.getName() + "_IW();");
207 
208         // Class close brace
209 
210         sm.append("}");
211 
212         // Write file
213 
214         File file = new File(parentDir + "/" + StringUtil.replace(javaClass.getPackage(), ".", "/") + "/" + javaClass.getName() + "_IW.java");
215 
216         ServiceBuilder.writeFile(file, sm.toString());
217     }
218 
219     private String _getDimensions(Type type) {
220         String dimensions = "";
221 
222         for (int i = 0; i < type.getDimensions(); i++) {
223             dimensions += "[]";
224         }
225 
226         return dimensions;
227     }
228 
229     private JavaClass _getJavaClass(String parentDir, String srcFile)
230         throws IOException {
231 
232         String className = StringUtil.replace(
233             srcFile.substring(0, srcFile.length() - 5), "/", ".");
234 
235         JavaDocBuilder builder = new JavaDocBuilder();
236 
237         builder.addSource(new File(parentDir + "/" + srcFile));
238 
239         return builder.getClassByName(className);
240     }
241 
242 }