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 itr = root.elements("instance-wrapper").iterator();
87  
88              while (itr.hasNext()) {
89                  Element instanceWrapper = (Element)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         StringMaker sm = new StringMaker();
110 
111         // Package
112 
113         sm.append("package " + javaClass.getPackage() + ";");
114 
115         // Class declaration
116 
117         sm.append("public class " + javaClass.getName() + "_IW {");
118 
119         // Methods
120 
121         sm.append("public static " + javaClass.getName() + "_IW getInstance() {");
122         sm.append("return _instance;");
123         sm.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                 sm.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                     sm.append(javaParameter.getType().getValue() + _getDimensions(javaParameter.getType()) + " " + javaParameter.getName());
143 
144                     if ((j + 1) != parameters.length) {
145                         sm.append(", ");
146                     }
147                 }
148 
149                 sm.append(")");
150 
151                 Type[] thrownExceptions = javaMethod.getExceptions();
152 
153                 Set newExceptions = new LinkedHashSet();
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                     sm.append(" throws ");
163 
164                     Iterator itr = newExceptions.iterator();
165 
166                     while (itr.hasNext()) {
167                         sm.append(itr.next());
168 
169                         if (itr.hasNext()) {
170                             sm.append(", ");
171                         }
172                     }
173                 }
174 
175                 sm.append("{");
176 
177                 if (!javaMethod.getReturns().getValue().equals("void")) {
178                     sm.append("return ");
179                 }
180 
181                 sm.append(javaClass.getName() + "." + javaMethod.getName() + "(");
182 
183                 for (int j = 0; j < parameters.length; j++) {
184                     JavaParameter javaParameter = parameters[j];
185 
186                     sm.append(javaParameter.getName());
187 
188                     if ((j + 1) != parameters.length) {
189                         sm.append(", ");
190                     }
191                 }
192 
193                 sm.append(");");
194                 sm.append("}");
195             }
196         }
197 
198         // Private constructor
199 
200         sm.append("private " + javaClass.getName() + "_IW() {");
201         sm.append("}");
202 
203         // Fields
204 
205         sm.append("private static " + javaClass.getName() + "_IW _instance = new " + javaClass.getName() + "_IW();");
206 
207         // Class close brace
208 
209         sm.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, sm.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 }