001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.tools;
022:
023: import com.liferay.portal.kernel.util.StringMaker;
024: import com.liferay.portal.kernel.util.StringUtil;
025: import com.liferay.portal.tools.comparator.JavaMethodComparator;
026: import com.liferay.portal.tools.servicebuilder.ServiceBuilder;
027: import com.liferay.util.TextFormatter;
028:
029: import com.thoughtworks.qdox.JavaDocBuilder;
030: import com.thoughtworks.qdox.model.JavaClass;
031: import com.thoughtworks.qdox.model.JavaMethod;
032: import com.thoughtworks.qdox.model.JavaParameter;
033: import com.thoughtworks.qdox.model.Type;
034:
035: import java.io.File;
036: import java.io.IOException;
037:
038: import java.util.Arrays;
039: import java.util.Iterator;
040: import java.util.LinkedHashSet;
041: import java.util.Set;
042: import java.util.TreeSet;
043:
044: /**
045: * <a href="CopyInterfaceBuilder.java.html"><b><i>View Source</i></b></a>
046: *
047: * @author Brian Wing Shun Chan
048: *
049: */
050: public class CopyInterfaceBuilder {
051:
052: public static void main(String[] args) {
053: if (args.length == 2) {
054: new CopyInterfaceBuilder(args[0], args[1]);
055: } else {
056: throw new IllegalArgumentException();
057: }
058: }
059:
060: public CopyInterfaceBuilder(String parentDir, String srcFile) {
061: try {
062: _copyInterface(parentDir, srcFile);
063: } catch (Exception e) {
064: e.printStackTrace();
065: }
066: }
067:
068: private void _copyInterface(String parentDir, String srcFile)
069: throws IOException {
070:
071: JavaClass javaClass = _getJavaClass(parentDir, srcFile);
072:
073: JavaMethod[] methods = javaClass.getMethods();
074:
075: Arrays.sort(methods, new JavaMethodComparator());
076:
077: StringMaker sm = new StringMaker();
078:
079: // Package
080:
081: sm.append("package " + javaClass.getPackage() + ";");
082:
083: // Imports
084:
085: sm.append("[$IMPORTS$]");
086:
087: // Class declaration
088:
089: sm.append("public class Copy" + javaClass.getName()
090: + " implements " + javaClass.getName() + " {");
091:
092: String varName = "_"
093: + TextFormatter.format(javaClass.getName(),
094: TextFormatter.I);
095:
096: // Methods
097:
098: Set imports = new TreeSet();
099:
100: for (int i = 0; i < methods.length; i++) {
101: JavaMethod javaMethod = methods[i];
102:
103: String methodName = javaMethod.getName();
104:
105: if (javaMethod.isPublic()) {
106: String returnValueName = javaMethod.getReturns()
107: .getValue();
108:
109: imports.add(returnValueName);
110:
111: sm.append("public "
112: + javaMethod.getReturns().getJavaClass()
113: .getName()
114: + _getDimensions(javaMethod.getReturns()) + " "
115: + methodName + "(");
116:
117: JavaParameter[] parameters = javaMethod.getParameters();
118:
119: for (int j = 0; j < parameters.length; j++) {
120: JavaParameter javaParameter = parameters[j];
121:
122: sm.append(javaParameter.getType().getJavaClass()
123: .getName()
124: + _getDimensions(javaParameter.getType())
125: + " " + javaParameter.getName());
126:
127: imports.add(javaParameter.getType().getValue());
128:
129: if ((j + 1) != parameters.length) {
130: sm.append(", ");
131: }
132: }
133:
134: sm.append(")");
135:
136: Type[] thrownExceptions = javaMethod.getExceptions();
137:
138: Set newExceptions = new LinkedHashSet();
139:
140: for (int j = 0; j < thrownExceptions.length; j++) {
141: Type thrownException = thrownExceptions[j];
142:
143: newExceptions.add(thrownException.getJavaClass()
144: .getName());
145:
146: imports.add(thrownException.getValue());
147: }
148:
149: if (newExceptions.size() > 0) {
150: sm.append(" throws ");
151:
152: Iterator itr = newExceptions.iterator();
153:
154: while (itr.hasNext()) {
155: sm.append(itr.next());
156:
157: if (itr.hasNext()) {
158: sm.append(", ");
159: }
160: }
161: }
162:
163: sm.append("{");
164:
165: if (!returnValueName.equals("void")) {
166: sm.append("return ");
167: }
168:
169: sm.append(varName + "." + methodName + "(");
170:
171: for (int j = 0; j < parameters.length; j++) {
172: JavaParameter javaParameter = parameters[j];
173:
174: sm.append(javaParameter.getName());
175:
176: if ((j + 1) != parameters.length) {
177: sm.append(", ");
178: }
179: }
180:
181: sm.append(");");
182: sm.append("}");
183: }
184: }
185:
186: // Fields
187:
188: sm.append("private " + javaClass.getName() + " " + varName
189: + ";");
190:
191: // Class close brace
192:
193: sm.append("}");
194:
195: // Imports
196:
197: String content = sm.toString();
198:
199: sm = new StringMaker();
200:
201: Iterator itr = imports.iterator();
202:
203: while (itr.hasNext()) {
204: String importClass = (String) itr.next();
205:
206: if (!importClass.equals("boolean")
207: && !importClass.equals("double")
208: && !importClass.equals("int")
209: && !importClass.equals("long")
210: && !importClass.equals("short")
211: && !importClass.equals("void")) {
212: sm.append("import " + importClass + ";");
213: }
214: }
215:
216: content = StringUtil.replace(content, "[$IMPORTS$]", sm
217: .toString());
218:
219: // Write file
220:
221: File file = new File(parentDir + "/"
222: + StringUtil.replace(javaClass.getPackage(), ".", "/")
223: + "/Copy" + javaClass.getName() + ".java");
224:
225: ServiceBuilder.writeFile(file, content);
226: }
227:
228: private String _getDimensions(Type type) {
229: String dimensions = "";
230:
231: for (int i = 0; i < type.getDimensions(); i++) {
232: dimensions += "[]";
233: }
234:
235: return dimensions;
236: }
237:
238: private JavaClass _getJavaClass(String parentDir, String srcFile)
239: throws IOException {
240:
241: String className = StringUtil.replace(srcFile.substring(0,
242: srcFile.length() - 5), "/", ".");
243:
244: JavaDocBuilder builder = new JavaDocBuilder();
245:
246: builder.addSource(new File(parentDir + "/" + srcFile));
247:
248: return builder.getClassByName(className);
249: }
250:
251: }
|