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.servicebuilder.ServiceBuilder;
026: import com.liferay.portal.util.InitUtil;
027:
028: import com.thoughtworks.qdox.JavaDocBuilder;
029: import com.thoughtworks.qdox.model.JavaClass;
030: import com.thoughtworks.qdox.model.JavaMethod;
031: import com.thoughtworks.qdox.model.JavaParameter;
032: import com.thoughtworks.qdox.model.Type;
033:
034: import java.io.File;
035: import java.io.IOException;
036:
037: import java.util.Iterator;
038: import java.util.LinkedHashSet;
039: import java.util.Set;
040:
041: import org.dom4j.Document;
042: import org.dom4j.DocumentException;
043: import org.dom4j.Element;
044: import org.dom4j.io.SAXReader;
045:
046: /**
047: * <a href="InstanceWrapperBuilder.java.html"><b><i>View Source</i></b></a>
048: *
049: * @author Brian Wing Shun Chan
050: *
051: */
052: public class InstanceWrapperBuilder {
053:
054: static {
055: InitUtil.init();
056: }
057:
058: public static void main(String[] args) {
059: if (args.length == 1) {
060: new InstanceWrapperBuilder(args[0]);
061: } else {
062: throw new IllegalArgumentException();
063: }
064: }
065:
066: public InstanceWrapperBuilder(String xml) {
067: try {
068: File file = new File(xml);
069:
070: SAXReader reader = new SAXReader();
071:
072: Document doc = null;
073:
074: try {
075: doc = reader.read(file);
076: } catch (DocumentException de) {
077: de.printStackTrace();
078: }
079:
080: Element root = doc.getRootElement();
081:
082: Iterator itr = root.elements("instance-wrapper").iterator();
083:
084: while (itr.hasNext()) {
085: Element instanceWrapper = (Element) itr.next();
086:
087: String parentDir = instanceWrapper
088: .attributeValue("parent-dir");
089: String srcFile = instanceWrapper
090: .attributeValue("src-file");
091:
092: _createIW(parentDir, srcFile);
093: }
094: } catch (Exception e) {
095: e.printStackTrace();
096: }
097: }
098:
099: private void _createIW(String parentDir, String srcFile)
100: throws IOException {
101:
102: JavaClass javaClass = _getJavaClass(parentDir, srcFile);
103:
104: JavaMethod[] methods = javaClass.getMethods();
105:
106: StringMaker sm = new StringMaker();
107:
108: // Package
109:
110: sm.append("package " + javaClass.getPackage() + ";");
111:
112: // Class declaration
113:
114: sm.append("public class " + javaClass.getName() + "_IW {");
115:
116: // Methods
117:
118: sm.append("public static " + javaClass.getName()
119: + "_IW getInstance() {");
120: sm.append("return _instance;");
121: sm.append("}");
122:
123: for (int i = 0; i < methods.length; i++) {
124: JavaMethod javaMethod = methods[i];
125:
126: String methodName = javaMethod.getName();
127:
128: if (javaMethod.isPublic() && javaMethod.isStatic()) {
129: if (methodName.equals("getInstance")) {
130: methodName = "getWrappedInstance";
131: }
132:
133: sm.append("public "
134: + javaMethod.getReturns().getValue()
135: + _getDimensions(javaMethod.getReturns()) + " "
136: + 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()
144: + _getDimensions(javaParameter.getType())
145: + " " + javaParameter.getName());
146:
147: if ((j + 1) != parameters.length) {
148: sm.append(", ");
149: }
150: }
151:
152: sm.append(")");
153:
154: Type[] thrownExceptions = javaMethod.getExceptions();
155:
156: Set newExceptions = new LinkedHashSet();
157:
158: for (int j = 0; j < thrownExceptions.length; j++) {
159: Type thrownException = thrownExceptions[j];
160:
161: newExceptions.add(thrownException.getValue());
162: }
163:
164: if (newExceptions.size() > 0) {
165: sm.append(" throws ");
166:
167: Iterator itr = newExceptions.iterator();
168:
169: while (itr.hasNext()) {
170: sm.append(itr.next());
171:
172: if (itr.hasNext()) {
173: sm.append(", ");
174: }
175: }
176: }
177:
178: sm.append("{");
179:
180: if (!javaMethod.getReturns().getValue().equals("void")) {
181: sm.append("return ");
182: }
183:
184: sm.append(javaClass.getName() + "."
185: + javaMethod.getName() + "(");
186:
187: for (int j = 0; j < parameters.length; j++) {
188: JavaParameter javaParameter = parameters[j];
189:
190: sm.append(javaParameter.getName());
191:
192: if ((j + 1) != parameters.length) {
193: sm.append(", ");
194: }
195: }
196:
197: sm.append(");");
198: sm.append("}");
199: }
200: }
201:
202: // Private constructor
203:
204: sm.append("private " + javaClass.getName() + "_IW() {");
205: sm.append("}");
206:
207: // Fields
208:
209: sm.append("private static " + javaClass.getName()
210: + "_IW _instance = new " + javaClass.getName()
211: + "_IW();");
212:
213: // Class close brace
214:
215: sm.append("}");
216:
217: // Write file
218:
219: File file = new File(parentDir + "/"
220: + StringUtil.replace(javaClass.getPackage(), ".", "/")
221: + "/" + javaClass.getName() + "_IW.java");
222:
223: ServiceBuilder.writeFile(file, sm.toString());
224: }
225:
226: private String _getDimensions(Type type) {
227: String dimensions = "";
228:
229: for (int i = 0; i < type.getDimensions(); i++) {
230: dimensions += "[]";
231: }
232:
233: return dimensions;
234: }
235:
236: private JavaClass _getJavaClass(String parentDir, String srcFile)
237: throws IOException {
238:
239: String className = StringUtil.replace(srcFile.substring(0,
240: srcFile.length() - 5), "/", ".");
241:
242: JavaDocBuilder builder = new JavaDocBuilder();
243:
244: builder.addSource(new File(parentDir + "/" + srcFile));
245:
246: return builder.getClassByName(className);
247: }
248:
249: }
|