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.ClassUtil;
024: import com.liferay.portal.kernel.util.StringMaker;
025: import com.liferay.portal.kernel.util.StringPool;
026: import com.liferay.portal.kernel.util.StringUtil;
027: import com.liferay.util.FileUtil;
028: import com.liferay.util.ListUtil;
029:
030: import java.io.BufferedReader;
031: import java.io.File;
032: import java.io.IOException;
033: import java.io.StringReader;
034: import java.util.ArrayList;
035: import java.util.Collections;
036: import java.util.List;
037: import java.util.Set;
038:
039: import org.apache.tools.ant.DirectoryScanner;
040:
041: /**
042: * <a href="SourceFormatter.java.html"><b><i>View Source</i></b></a>
043: *
044: * @author Brian Wing Shun Chan
045: *
046: */
047: public class SourceFormatter {
048:
049: public static void main(String[] args) {
050: try {
051: _formatJava();
052: _formatJSP();
053: } catch (Exception e) {
054: e.printStackTrace();
055: }
056: }
057:
058: public static String stripImports(String content,
059: String packageDir, String className) throws IOException {
060:
061: int x = content.indexOf("import ");
062:
063: if (x == -1) {
064: return content;
065: }
066:
067: int y = content.indexOf("{", x);
068:
069: y = content.substring(0, y).lastIndexOf(";") + 1;
070:
071: String imports = _formatImports(content.substring(x, y));
072:
073: content = content.substring(0, x) + imports
074: + content.substring(y + 1, content.length());
075:
076: Set classes = ClassUtil.getClasses(new StringReader(content),
077: className);
078:
079: classes.add("_getMarkup");
080: classes.add("_performBlockingInteraction");
081:
082: x = content.indexOf("import ");
083:
084: y = content.indexOf("{", x);
085:
086: y = content.substring(0, y).lastIndexOf(";") + 1;
087:
088: imports = content.substring(x, y);
089:
090: StringMaker sm = new StringMaker();
091:
092: BufferedReader br = new BufferedReader(
093: new StringReader(imports));
094:
095: String line = null;
096:
097: while ((line = br.readLine()) != null) {
098: if (line.indexOf("import ") != -1) {
099: int importX = line.indexOf(" ");
100: int importY = line.lastIndexOf(".");
101:
102: String importPackage = line.substring(importX + 1,
103: importY);
104: String importClass = line.substring(importY + 1, line
105: .length() - 1);
106:
107: if (!packageDir.equals(importPackage)) {
108: if (!importClass.equals("*")) {
109: if (classes.contains(importClass)) {
110: sm.append(line);
111: sm.append("\n");
112: }
113: } else {
114: sm.append(line);
115: sm.append("\n");
116: }
117: }
118: }
119: }
120:
121: imports = _formatImports(sm.toString());
122:
123: content = content.substring(0, x) + imports
124: + content.substring(y + 1, content.length());
125:
126: return content;
127: }
128:
129: public static String _formatImports(String imports)
130: throws IOException {
131: if ((imports.indexOf("/*") != -1)
132: || (imports.indexOf("*/") != -1)
133: || (imports.indexOf("//") != -1)) {
134:
135: return imports + "\n";
136: }
137:
138: List importsList = new ArrayList();
139:
140: BufferedReader br = new BufferedReader(
141: new StringReader(imports));
142:
143: String line = null;
144:
145: while ((line = br.readLine()) != null) {
146: if (line.indexOf("import ") != -1) {
147: if (!importsList.contains(line)) {
148: importsList.add(line);
149: }
150: }
151: }
152:
153: Collections.sort(importsList);
154:
155: StringMaker sm = new StringMaker();
156:
157: String temp = null;
158:
159: for (int i = 0; i < importsList.size(); i++) {
160: String s = (String) importsList.get(i);
161:
162: int pos = s.indexOf(".");
163:
164: pos = s.indexOf(".", pos + 1);
165:
166: if (pos == -1) {
167: pos = s.indexOf(".");
168: }
169:
170: String packageLevel = s.substring(7, pos);
171:
172: if ((i != 0) && (!packageLevel.equals(temp))) {
173: sm.append("\n");
174: }
175:
176: temp = packageLevel;
177:
178: sm.append(s);
179: sm.append("\n");
180: }
181:
182: return sm.toString();
183: }
184:
185: private static void _formatJava() throws IOException {
186: String basedir = "../";
187:
188: List list = new ArrayList();
189:
190: DirectoryScanner ds = new DirectoryScanner();
191:
192: ds.setBasedir(basedir);
193: ds.setExcludes(new String[] { "**\\classes\\*", "**\\jsp\\*",
194: "**\\tmp\\**", "**\\EARXMLBuilder.java",
195: "**\\EJBXMLBuilder.java", "**\\JSMin.java",
196: "**\\PropsUtil.java",
197: "**\\InstanceWrapperBuilder.java",
198: "**\\ServiceBuilder.java", "**\\SourceFormatter.java",
199: "**\\UserAttributes.java", "**\\WebKeys.java",
200: "**\\*_IW.java", "**\\XHTMLComplianceFormatter.java",
201: "**\\portal-service\\**\\model\\*Model.java",
202: "**\\portal-service\\**\\model\\*Soap.java",
203: "**\\model\\impl\\*ModelImpl.java",
204: "**\\portal\\service\\**", "**\\portal-client\\**",
205: "**\\portal-web\\test\\**\\*Test.java",
206: "**\\portlet\\**\\service\\**",
207: "**\\tools\\ext_tmpl\\**", "**\\util-wsrp\\**" });
208: ds.setIncludes(new String[] { "**\\*.java" });
209:
210: ds.scan();
211:
212: list.addAll(ListUtil.fromArray(ds.getIncludedFiles()));
213:
214: ds = new DirectoryScanner();
215:
216: ds.setBasedir(basedir);
217: ds
218: .setExcludes(new String[] { "**\\tools\\ext_tmpl\\**",
219: "**\\*_IW.java",
220: "**\\test\\**\\*PersistenceTest.java" });
221: ds
222: .setIncludes(new String[] {
223: "**\\service\\http\\*HttpTest.java",
224: "**\\service\\http\\*SoapTest.java",
225: "**\\service\\impl\\*.java",
226: "**\\service\\jms\\*.java",
227: "**\\service\\permission\\*.java",
228: "**\\service\\persistence\\BasePersistence.java",
229: "**\\service\\persistence\\*FinderImpl.java",
230: "**\\portal-impl\\test\\**\\*.java",
231: "**\\portal-service\\**\\liferay\\counter\\**.java",
232: "**\\portal-service\\**\\liferay\\documentlibrary\\**.java",
233: "**\\portal-service\\**\\liferay\\lock\\**.java",
234: "**\\portal-service\\**\\liferay\\mail\\**.java",
235: "**\\util-bridges\\**\\*.java" });
236:
237: ds.scan();
238:
239: list.addAll(ListUtil.fromArray(ds.getIncludedFiles()));
240:
241: String copyright = FileUtil.read("../copyright.txt");
242:
243: String[] files = (String[]) list
244: .toArray(new String[list.size()]);
245:
246: for (int i = 0; i < files.length; i++) {
247: File file = new File(basedir + files[i]);
248:
249: String content = FileUtil.read(file);
250:
251: String className = file.getName();
252:
253: className = className.substring(0, className.length() - 5);
254:
255: String packagePath = files[i];
256:
257: int packagePathX = packagePath.indexOf(File.separator
258: + "src" + File.separator);
259: int packagePathY = packagePath.lastIndexOf(File.separator);
260:
261: packagePath = packagePath.substring(packagePathX + 5,
262: packagePathY);
263: packagePath = StringUtil.replace(packagePath,
264: File.separator, StringPool.PERIOD);
265:
266: if (packagePath.endsWith(".model")) {
267: if (content.indexOf("extends " + className + "Model {") != -1) {
268:
269: continue;
270: }
271: }
272:
273: String newContent = _formatJavaContent(files[i], content);
274:
275: if (newContent.indexOf("$\n */") != -1) {
276: System.out.println("*: " + files[i]);
277:
278: newContent = StringUtil.replace(newContent, "$\n */",
279: "$\n *\n */");
280: }
281:
282: if (newContent.indexOf(copyright) == -1) {
283: System.out.println("(c): " + files[i]);
284: }
285:
286: if (newContent.indexOf(className + ".java.html") == -1) {
287: System.out.println("Java2HTML: " + files[i]);
288: }
289:
290: newContent = stripImports(newContent, packagePath,
291: className);
292:
293: if (newContent.indexOf(";\n/**") != -1) {
294: newContent = StringUtil.replace(newContent, ";\n/**",
295: ";\n\n/**");
296: }
297:
298: if (newContent.indexOf("\t/*\n\t *") != -1) {
299: newContent = StringUtil.replace(newContent,
300: "\t/*\n\t *", "\t/**\n\t *");
301: }
302:
303: if (newContent.indexOf("if(") != -1) {
304: newContent = StringUtil.replace(newContent, "if(",
305: "if (");
306: }
307:
308: if (newContent.indexOf("while(") != -1) {
309: newContent = StringUtil.replace(newContent, "while(",
310: "while (");
311: }
312:
313: if (newContent.indexOf("\n\n\n") != -1) {
314: newContent = StringUtil.replace(newContent, "\n\n\n",
315: "\n\n");
316: }
317:
318: if (newContent.indexOf("*/\npackage ") != -1) {
319: System.out.println("package: " + files[i]);
320: }
321:
322: if (!newContent.endsWith("\n\n}")
323: && !newContent.endsWith("{\n}")) {
324:
325: System.out.println("}: " + files[i]);
326: }
327:
328: if ((newContent != null) && !content.equals(newContent)) {
329: FileUtil.write(file, newContent);
330:
331: System.out.println(file.toString());
332: }
333: }
334: }
335:
336: private static String _formatJavaContent(String fileName,
337: String content) throws IOException {
338:
339: StringMaker sm = new StringMaker();
340:
341: BufferedReader br = new BufferedReader(
342: new StringReader(content));
343:
344: int lineCount = 0;
345:
346: String line = null;
347:
348: while ((line = br.readLine()) != null) {
349: lineCount++;
350:
351: if (line.trim().length() == 0) {
352: line = StringPool.BLANK;
353: }
354:
355: line = StringUtil.trimTrailing(line);
356:
357: sm.append(line);
358: sm.append("\n");
359:
360: line = StringUtil.replace(line, "\t", " ");
361:
362: if (((line.length() - 1) > 79)
363: && !line.startsWith("import ")) {
364: System.out.println("> 80: " + fileName + " "
365: + lineCount);
366: }
367: }
368:
369: br.close();
370:
371: String newContent = sm.toString();
372:
373: if (newContent.endsWith("\n")) {
374: newContent = newContent.substring(0,
375: newContent.length() - 1);
376: }
377:
378: return newContent;
379: }
380:
381: private static void _formatJSP() throws IOException {
382: String basedir = "../";
383:
384: List list = new ArrayList();
385:
386: DirectoryScanner ds = new DirectoryScanner();
387:
388: ds.setBasedir(basedir);
389: ds.setExcludes(new String[] { "**\\null.jsp", "**\\tmp\\**" });
390: ds.setIncludes(new String[] { "**\\*.jsp", "**\\*.jspf",
391: "**\\*.vm" });
392:
393: ds.scan();
394:
395: list.addAll(ListUtil.fromArray(ds.getIncludedFiles()));
396:
397: String copyright = FileUtil.read("../copyright.txt");
398:
399: String[] files = (String[]) list
400: .toArray(new String[list.size()]);
401:
402: for (int i = 0; i < files.length; i++) {
403: File file = new File(basedir + files[i]);
404:
405: String content = FileUtil.read(file, true);
406: String newContent = _formatJSPContent(files[i], content);
407:
408: if (files[i].endsWith(".jsp")) {
409: if (newContent.indexOf(copyright) == -1) {
410: System.out.println("(c): " + files[i]);
411: }
412: }
413:
414: if (newContent.indexOf("alert('<%= LanguageUtil.") != -1) {
415: newContent = StringUtil.replace(newContent,
416: "alert('<%= LanguageUtil.",
417: "alert('<%= UnicodeLanguageUtil.");
418: }
419:
420: if (newContent.indexOf("alert(\"<%= LanguageUtil.") != -1) {
421: newContent = StringUtil.replace(newContent,
422: "alert(\"<%= LanguageUtil.",
423: "alert(\"<%= UnicodeLanguageUtil.");
424: }
425:
426: if (newContent.indexOf("confirm('<%= LanguageUtil.") != -1) {
427: newContent = StringUtil.replace(newContent,
428: "confirm('<%= LanguageUtil.",
429: "confirm('<%= UnicodeLanguageUtil.");
430: }
431:
432: if (newContent.indexOf("confirm(\"<%= LanguageUtil.") != -1) {
433: newContent = StringUtil.replace(newContent,
434: "confirm(\"<%= LanguageUtil.",
435: "confirm(\"<%= UnicodeLanguageUtil.");
436: }
437:
438: if ((newContent != null) && !content.equals(newContent)) {
439: FileUtil.write(file, newContent);
440:
441: System.out.println(file.toString());
442: }
443: }
444: }
445:
446: private static String _formatJSPContent(String fileName,
447: String content) throws IOException {
448:
449: StringMaker sm = new StringMaker();
450:
451: BufferedReader br = new BufferedReader(
452: new StringReader(content));
453:
454: int lineCount = 0;
455:
456: String line = null;
457:
458: while ((line = br.readLine()) != null) {
459: lineCount++;
460:
461: int x = line.indexOf("\"<%=");
462: int y = line.indexOf("%>\"", x);
463:
464: boolean hasTagLibrary = false;
465:
466: for (int i = 0; i < _TAG_LIBRARIES.length; i++) {
467: if (line.indexOf("<" + _TAG_LIBRARIES[i] + ":") != -1) {
468: hasTagLibrary = true;
469:
470: break;
471: }
472: }
473:
474: if ((x != -1) && (y != -1) && hasTagLibrary) {
475: String regexp = line.substring(x, y + 3);
476:
477: if (regexp.indexOf("\\\"") == -1) {
478: regexp = regexp.substring(1, regexp.length() - 1);
479:
480: if (regexp.indexOf("\"") != -1) {
481: line = line.substring(0, x) + "'" + regexp
482: + "'"
483: + line.substring(y + 3, line.length());
484: }
485: }
486: }
487:
488: if (line.trim().length() == 0) {
489: line = StringPool.BLANK;
490: }
491:
492: line = StringUtil.trimTrailing(line);
493:
494: sm.append(line);
495: sm.append("\n");
496: }
497:
498: br.close();
499:
500: String newContent = sm.toString();
501:
502: if (newContent.endsWith("\n")) {
503: newContent = newContent.substring(0,
504: newContent.length() - 1);
505: }
506:
507: return newContent;
508: }
509:
510: private static final String[] _TAG_LIBRARIES = new String[] { "c",
511: "html", "jsp", "liferay-portlet", "liferay-security",
512: "liferay-theme", "liferay-ui", "liferay-util", "portlet",
513: "struts", "tiles" };
514:
515: }
|