1
22
23 package com.liferay.portal.tools;
24
25 import com.liferay.portal.kernel.util.ClassUtil;
26 import com.liferay.portal.kernel.util.StringMaker;
27 import com.liferay.portal.kernel.util.StringPool;
28 import com.liferay.portal.kernel.util.StringUtil;
29 import com.liferay.util.FileUtil;
30 import com.liferay.util.ListUtil;
31
32 import java.io.BufferedReader;
33 import java.io.File;
34 import java.io.IOException;
35 import java.io.StringReader;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.List;
39 import java.util.Set;
40
41 import org.apache.tools.ant.DirectoryScanner;
42
43
49 public class SourceFormatter {
50
51 public static void main(String[] args) {
52 try {
53 _formatJava();
54 _formatJSP();
55 }
56 catch (Exception e) {
57 e.printStackTrace();
58 }
59 }
60
61 public static String stripImports(
62 String content, String packageDir, String className)
63 throws IOException {
64
65 int x = content.indexOf("import ");
66
67 if (x == -1) {
68 return content;
69 }
70
71 int y = content.indexOf("{", x);
72
73 y = content.substring(0, y).lastIndexOf(";") + 1;
74
75 String imports = _formatImports(content.substring(x, y));
76
77 content =
78 content.substring(0, x) + imports +
79 content.substring(y + 1, content.length());
80
81 Set classes = ClassUtil.getClasses(
82 new StringReader(content), className);
83
84 classes.add("_getMarkup");
85 classes.add("_performBlockingInteraction");
86
87 x = content.indexOf("import ");
88
89 y = content.indexOf("{", x);
90
91 y = content.substring(0, y).lastIndexOf(";") + 1;
92
93 imports = content.substring(x, y);
94
95 StringMaker sm = new StringMaker();
96
97 BufferedReader br = new BufferedReader(new StringReader(imports));
98
99 String line = null;
100
101 while ((line = br.readLine()) != null) {
102 if (line.indexOf("import ") != -1) {
103 int importX = line.indexOf(" ");
104 int importY = line.lastIndexOf(".");
105
106 String importPackage = line.substring(importX + 1, importY);
107 String importClass = line.substring(
108 importY + 1, line.length() - 1);
109
110 if (!packageDir.equals(importPackage)) {
111 if (!importClass.equals("*")) {
112 if (classes.contains(importClass)) {
113 sm.append(line);
114 sm.append("\n");
115 }
116 }
117 else {
118 sm.append(line);
119 sm.append("\n");
120 }
121 }
122 }
123 }
124
125 imports = _formatImports(sm.toString());
126
127 content =
128 content.substring(0, x) + imports +
129 content.substring(y + 1, content.length());
130
131 return content;
132 }
133
134 public static String _formatImports(String imports) throws IOException {
135 if ((imports.indexOf("/*") != -1) ||
136 (imports.indexOf("*/") != -1) ||
137 (imports.indexOf("//") != -1)) {
138
139 return imports + "\n";
140 }
141
142 List importsList = new ArrayList();
143
144 BufferedReader br = new BufferedReader(new StringReader(imports));
145
146 String line = null;
147
148 while ((line = br.readLine()) != null) {
149 if (line.indexOf("import ") != -1) {
150 if (!importsList.contains(line)) {
151 importsList.add(line);
152 }
153 }
154 }
155
156 Collections.sort(importsList);
157
158 StringMaker sm = new StringMaker();
159
160 String temp = null;
161
162 for (int i = 0; i < importsList.size(); i++) {
163 String s = (String)importsList.get(i);
164
165 int pos = s.indexOf(".");
166
167 pos = s.indexOf(".", pos + 1);
168
169 if (pos == -1) {
170 pos = s.indexOf(".");
171 }
172
173 String packageLevel = s.substring(7, pos);
174
175 if ((i != 0) && (!packageLevel.equals(temp))) {
176 sm.append("\n");
177 }
178
179 temp = packageLevel;
180
181 sm.append(s);
182 sm.append("\n");
183 }
184
185 return sm.toString();
186 }
187
188 private static void _formatJava() throws IOException {
189 String basedir = "../";
190
191 List list = new ArrayList();
192
193 DirectoryScanner ds = new DirectoryScanner();
194
195 ds.setBasedir(basedir);
196 ds.setExcludes(
197 new String[] {
198 "**\\classes\\*", "**\\jsp\\*", "**\\tmp\\**",
199 "**\\EARXMLBuilder.java", "**\\EJBXMLBuilder.java",
200 "**\\JSMin.java", "**\\PropsUtil.java",
201 "**\\InstanceWrapperBuilder.java",
202 "**\\ServiceBuilder.java", "**\\SourceFormatter.java",
203 "**\\UserAttributes.java", "**\\WebKeys.java",
204 "**\\*_IW.java", "**\\XHTMLComplianceFormatter.java",
205 "**\\portal-service\\**\\model\\*Model.java",
206 "**\\portal-service\\**\\model\\*Soap.java",
207 "**\\model\\impl\\*ModelImpl.java",
208 "**\\portal\\service\\**", "**\\portal-client\\**",
209 "**\\portal-web\\test\\**\\*Test.java",
210 "**\\portlet\\**\\service\\**", "**\\tools\\ext_tmpl\\**",
211 "**\\util-wsrp\\**"
212 });
213 ds.setIncludes(new String[] {"**\\*.java"});
214
215 ds.scan();
216
217 list.addAll(ListUtil.fromArray(ds.getIncludedFiles()));
218
219 ds = new DirectoryScanner();
220
221 ds.setBasedir(basedir);
222 ds.setExcludes(
223 new String[] {
224 "**\\tools\\ext_tmpl\\**", "**\\*_IW.java",
225 "**\\test\\**\\*PersistenceTest.java"
226 });
227 ds.setIncludes(
228 new String[] {
229 "**\\service\\http\\*HttpTest.java",
230 "**\\service\\http\\*SoapTest.java",
231 "**\\service\\impl\\*.java", "**\\service\\jms\\*.java",
232 "**\\service\\permission\\*.java",
233 "**\\service\\persistence\\BasePersistence.java",
234 "**\\service\\persistence\\*FinderImpl.java",
235 "**\\portal-impl\\test\\**\\*.java",
236 "**\\portal-service\\**\\liferay\\counter\\**.java",
237 "**\\portal-service\\**\\liferay\\documentlibrary\\**.java",
238 "**\\portal-service\\**\\liferay\\lock\\**.java",
239 "**\\portal-service\\**\\liferay\\mail\\**.java",
240 "**\\util-bridges\\**\\*.java"
241 });
242
243 ds.scan();
244
245 list.addAll(ListUtil.fromArray(ds.getIncludedFiles()));
246
247 String copyright = FileUtil.read("../copyright.txt");
248
249 String[] files = (String[])list.toArray(new String[list.size()]);
250
251 for (int i = 0; i < files.length; i++) {
252 File file = new File(basedir + files[i]);
253
254 String content = FileUtil.read(file);
255
256 String className = file.getName();
257
258 className = className.substring(0, className.length() - 5);
259
260 String packagePath = files[i];
261
262 int packagePathX = packagePath.indexOf(
263 File.separator + "src" + File.separator);
264 int packagePathY = packagePath.lastIndexOf(File.separator);
265
266 packagePath = packagePath.substring(packagePathX + 5, packagePathY);
267 packagePath = StringUtil.replace(
268 packagePath, File.separator, StringPool.PERIOD);
269
270 if (packagePath.endsWith(".model")) {
271 if (content.indexOf(
272 "extends " + className + "Model {") != -1) {
273
274 continue;
275 }
276 }
277
278 String newContent = _formatJavaContent(files[i], content);
279
280 if (newContent.indexOf("$\n */") != -1) {
281 System.out.println("*: " + files[i]);
282
283 newContent = StringUtil.replace(
284 newContent, "$\n */", "$\n *\n */");
285 }
286
287 if (newContent.indexOf(copyright) == -1) {
288 System.out.println("(c): " + files[i]);
289 }
290
291 if (newContent.indexOf(className + ".java.html") == -1) {
292 System.out.println("Java2HTML: " + files[i]);
293 }
294
295 newContent = stripImports(newContent, packagePath, className);
296
297 if (newContent.indexOf(";\n/**") != -1) {
298 newContent = StringUtil.replace(
299 newContent,
300 ";\n/**",
301 ";\n\n/**");
302 }
303
304 if (newContent.indexOf("\t/*\n\t *") != -1) {
305 newContent = StringUtil.replace(
306 newContent,
307 "\t/*\n\t *",
308 "\t/**\n\t *");
309 }
310
311 if (newContent.indexOf("if(") != -1) {
312 newContent = StringUtil.replace(
313 newContent,
314 "if(",
315 "if (");
316 }
317
318 if (newContent.indexOf("while(") != -1) {
319 newContent = StringUtil.replace(
320 newContent,
321 "while(",
322 "while (");
323 }
324
325 if (newContent.indexOf("\n\n\n") != -1) {
326 newContent = StringUtil.replace(
327 newContent,
328 "\n\n\n",
329 "\n\n");
330 }
331
332 if (newContent.indexOf("*/\npackage ") != -1) {
333 System.out.println("package: " + files[i]);
334 }
335
336 if (!newContent.endsWith("\n\n}") &&
337 !newContent.endsWith("{\n}")) {
338
339 System.out.println("}: " + files[i]);
340 }
341
342 if ((newContent != null) && !content.equals(newContent)) {
343 FileUtil.write(file, newContent);
344
345 System.out.println(file.toString());
346 }
347 }
348 }
349
350 private static String _formatJavaContent(String fileName, String content)
351 throws IOException {
352
353 StringMaker sm = new StringMaker();
354
355 BufferedReader br = new BufferedReader(new StringReader(content));
356
357 int lineCount = 0;
358
359 String line = null;
360
361 while ((line = br.readLine()) != null) {
362 lineCount++;
363
364 if (line.trim().length() == 0) {
365 line = StringPool.BLANK;
366 }
367
368 line = StringUtil.trimTrailing(line);
369
370 sm.append(line);
371 sm.append("\n");
372
373 line = StringUtil.replace(line, "\t", " ");
374
375 if (((line.length() - 1) > 79) && !line.startsWith("import ")) {
376 System.out.println("> 80: " + fileName + " " + lineCount);
377 }
378 }
379
380 br.close();
381
382 String newContent = sm.toString();
383
384 if (newContent.endsWith("\n")) {
385 newContent = newContent.substring(0, newContent.length() -1);
386 }
387
388 return newContent;
389 }
390
391 private static void _formatJSP() throws IOException {
392 String basedir = "../";
393
394 List list = new ArrayList();
395
396 DirectoryScanner ds = new DirectoryScanner();
397
398 ds.setBasedir(basedir);
399 ds.setExcludes(new String[] {"**\\null.jsp", "**\\tmp\\**"});
400 ds.setIncludes(new String[] {"**\\*.jsp", "**\\*.jspf", "**\\*.vm"});
401
402 ds.scan();
403
404 list.addAll(ListUtil.fromArray(ds.getIncludedFiles()));
405
406 String copyright = FileUtil.read("../copyright.txt");
407
408 String[] files = (String[])list.toArray(new String[list.size()]);
409
410 for (int i = 0; i < files.length; i++) {
411 File file = new File(basedir + files[i]);
412
413 String content = FileUtil.read(file, true);
414 String newContent = _formatJSPContent(files[i], content);
415
416 if (files[i].endsWith(".jsp")) {
417 if (newContent.indexOf(copyright) == -1) {
418 System.out.println("(c): " + files[i]);
419 }
420 }
421
422 if (newContent.indexOf("alert('<%= LanguageUtil.") != -1) {
423 newContent = StringUtil.replace(newContent,
424 "alert('<%= LanguageUtil.",
425 "alert('<%= UnicodeLanguageUtil.");
426 }
427
428 if (newContent.indexOf("alert(\"<%= LanguageUtil.") != -1) {
429 newContent = StringUtil.replace(newContent,
430 "alert(\"<%= LanguageUtil.",
431 "alert(\"<%= UnicodeLanguageUtil.");
432 }
433
434 if (newContent.indexOf("confirm('<%= LanguageUtil.") != -1) {
435 newContent = StringUtil.replace(newContent,
436 "confirm('<%= LanguageUtil.",
437 "confirm('<%= UnicodeLanguageUtil.");
438 }
439
440 if (newContent.indexOf("confirm(\"<%= LanguageUtil.") != -1) {
441 newContent = StringUtil.replace(newContent,
442 "confirm(\"<%= LanguageUtil.",
443 "confirm(\"<%= UnicodeLanguageUtil.");
444 }
445
446 if ((newContent != null) && !content.equals(newContent)) {
447 FileUtil.write(file, newContent);
448
449 System.out.println(file.toString());
450 }
451 }
452 }
453
454 private static String _formatJSPContent(String fileName, String content)
455 throws IOException {
456
457 StringMaker sm = new StringMaker();
458
459 BufferedReader br =
460 new BufferedReader(new StringReader(content));
461
462 int lineCount = 0;
463
464 String line = null;
465
466 while ((line = br.readLine()) != null) {
467 lineCount++;
468
469 int x = line.indexOf("\"<%=");
470 int y = line.indexOf("%>\"", x);
471
472 boolean hasTagLibrary = false;
473
474 for (int i = 0; i < _TAG_LIBRARIES.length; i++) {
475 if (line.indexOf("<" + _TAG_LIBRARIES[i] + ":") != -1) {
476 hasTagLibrary = true;
477
478 break;
479 }
480 }
481
482 if ((x != -1) && (y != -1) && hasTagLibrary) {
483 String regexp = line.substring(x, y + 3);
484
485 if (regexp.indexOf("\\\"") == -1) {
486 regexp = regexp.substring(1, regexp.length() - 1);
487
488 if (regexp.indexOf("\"") != -1) {
489 line =
490 line.substring(0, x) + "'" + regexp + "'" +
491 line.substring(y + 3, line.length());
492 }
493 }
494 }
495
496 if (line.trim().length() == 0) {
497 line = StringPool.BLANK;
498 }
499
500 line = StringUtil.trimTrailing(line);
501
502 sm.append(line);
503 sm.append("\n");
504 }
505
506 br.close();
507
508 String newContent = sm.toString();
509
510 if (newContent.endsWith("\n")) {
511 newContent = newContent.substring(0, newContent.length() -1);
512 }
513
514 return newContent;
515 }
516
517 private static final String[] _TAG_LIBRARIES = new String[] {
518 "c", "html", "jsp", "liferay-portlet", "liferay-security",
519 "liferay-theme", "liferay-ui", "liferay-util", "portlet", "struts",
520 "tiles"
521 };
522
523 }