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.StringUtil;
26  import com.liferay.portal.tools.servicebuilder.ServiceBuilder;
27  import com.liferay.portal.util.InitUtil;
28  
29  import com.thoughtworks.qdox.JavaDocBuilder;
30  import com.thoughtworks.qdox.model.JavaClass;
31  import com.thoughtworks.qdox.model.JavaMethod;
32  import com.thoughtworks.qdox.model.JavaParameter;
33  import com.thoughtworks.qdox.model.Type;
34  
35  import java.io.File;
36  import java.io.IOException;
37  
38  import java.util.Iterator;
39  import java.util.LinkedHashSet;
40  import java.util.Set;
41  
42  import org.dom4j.Document;
43  import org.dom4j.DocumentException;
44  import org.dom4j.Element;
45  import org.dom4j.io.SAXReader;
46  
47  /**
48   * <a href="InstanceWrapperBuilder.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class InstanceWrapperBuilder {
54  
55      static {
56          InitUtil.init();
57      }
58  
59      public static void main(String[] args) {
60          if (args.length == 1) {
61              new InstanceWrapperBuilder(args[0]);
62          }
63          else {
64              throw new IllegalArgumentException();
65          }
66      }
67  
68      public InstanceWrapperBuilder(String xml) {
69          try {
70              File file = new File(xml);
71  
72              SAXReader reader = new SAXReader();
73  
74              Document doc = null;
75  
76              try {
77                  doc = reader.read(file);
78              }
79              catch (DocumentException de) {
80                  de.printStackTrace();
81              }
82  
83              Element root = doc.getRootElement();
84  
85              Iterator<Element> itr = root.elements(
86                  "instance-wrapper").iterator();
87  
88              while (itr.hasNext()) {
89                  Element instanceWrapper = 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         StringBuilder sb = new StringBuilder();
110 
111         // Package
112 
113         sb.append("package " + javaClass.getPackage() + ";");
114 
115         // Class declaration
116 
117         sb.append("public class " + javaClass.getName() + "_IW {");
118 
119         // Methods
120 
121         sb.append("public static " + javaClass.getName() + "_IW getInstance() {");
122         sb.append("return _instance;");
123         sb.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                 sb.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                     sb.append(javaParameter.getType().getValue() + javaParameter.getGenericsName() + _getDimensions(javaParameter.getType()) + " " + javaParameter.getName());
143 
144                     if ((j + 1) != parameters.length) {
145                         sb.append(", ");
146                     }
147                 }
148 
149                 sb.append(")");
150 
151                 Type[] thrownExceptions = javaMethod.getExceptions();
152 
153                 Set<String> newExceptions = new LinkedHashSet<String>();
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                     sb.append(" throws ");
163 
164                     Iterator<String> itr = newExceptions.iterator();
165 
166                     while (itr.hasNext()) {
167                         sb.append(itr.next());
168 
169                         if (itr.hasNext()) {
170                             sb.append(", ");
171                         }
172                     }
173                 }
174 
175                 sb.append("{");
176 
177                 if (!javaMethod.getReturns().getValue().equals("void")) {
178                     sb.append("return ");
179                 }
180 
181                 sb.append(javaClass.getName() + "." + javaMethod.getName() + "(");
182 
183                 for (int j = 0; j < parameters.length; j++) {
184                     JavaParameter javaParameter = parameters[j];
185 
186                     sb.append(javaParameter.getName());
187 
188                     if ((j + 1) != parameters.length) {
189                         sb.append(", ");
190                     }
191                 }
192 
193                 sb.append(");");
194                 sb.append("}");
195             }
196         }
197 
198         // Private constructor
199 
200         sb.append("private " + javaClass.getName() + "_IW() {");
201         sb.append("}");
202 
203         // Fields
204 
205         sb.append("private static " + javaClass.getName() + "_IW _instance = new " + javaClass.getName() + "_IW();");
206 
207         // Class close brace
208 
209         sb.append("}");
210 
211         // Write file
212 
213         File file = new File(parentDir + "/" + StringUtil.replace(javaClass.getPackage(), ".", "/") + "/" + javaClass.getName() + "_IW.java");
214 
215         ServiceBuilder.writeFile(file, sb.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 }