1   /**
2    * Copyright (c) 2000-2009 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.CharPool;
26  import com.liferay.portal.kernel.util.ClassUtil;
27  import com.liferay.portal.kernel.util.ListUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.util.ContentUtil;
31  import com.liferay.portal.util.FileImpl;
32  
33  import java.io.BufferedReader;
34  import java.io.File;
35  import java.io.InputStream;
36  import java.io.IOException;
37  import java.io.StringReader;
38  import java.net.URL;
39  import java.util.ArrayList;
40  import java.util.HashSet;
41  import java.util.List;
42  import java.util.Properties;
43  import java.util.Set;
44  import java.util.regex.Matcher;
45  import java.util.regex.Pattern;
46  
47  import org.apache.tools.ant.DirectoryScanner;
48  
49  /**
50   * <a href="SourceFormatter.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   *
54   */
55  public class SourceFormatter {
56  
57      public static void main(String[] args) {
58          try {
59              _readExclusions();
60  
61              _checkPersistenceTestSuite();
62              _checkWebXML();
63              _formatJava();
64              _formatJSP();
65          }
66          catch (Exception e) {
67              e.printStackTrace();
68          }
69      }
70  
71      public static String stripImports(
72              String content, String packageDir, String className)
73          throws IOException {
74  
75          int x = content.indexOf("import ");
76  
77          if (x == -1) {
78              return content;
79          }
80  
81          int y = content.indexOf("{", x);
82  
83          y = content.substring(0, y).lastIndexOf(";") + 1;
84  
85          String imports = _formatImports(content.substring(x, y));
86  
87          content =
88              content.substring(0, x) + imports +
89                  content.substring(y + 1, content.length());
90  
91          Set<String> classes = ClassUtil.getClasses(
92              new StringReader(content), className);
93  
94          classes.add("_getMarkup");
95          classes.add("_performBlockingInteraction");
96  
97          x = content.indexOf("import ");
98  
99          y = content.indexOf("{", x);
100 
101         y = content.substring(0, y).lastIndexOf(";") + 1;
102 
103         imports = content.substring(x, y);
104 
105         StringBuilder sb = new StringBuilder();
106 
107         BufferedReader br = new BufferedReader(new StringReader(imports));
108 
109         String line = null;
110 
111         while ((line = br.readLine()) != null) {
112             if (line.indexOf("import ") != -1) {
113                 int importX = line.indexOf(" ");
114                 int importY = line.lastIndexOf(".");
115 
116                 String importPackage = line.substring(importX + 1, importY);
117                 String importClass = line.substring(
118                     importY + 1, line.length() - 1);
119 
120                 if (!packageDir.equals(importPackage)) {
121                     if (!importClass.equals("*")) {
122                         if (classes.contains(importClass)) {
123                             sb.append(line);
124                             sb.append("\n");
125                         }
126                     }
127                     else {
128                         sb.append(line);
129                         sb.append("\n");
130                     }
131                 }
132             }
133         }
134 
135         imports = _formatImports(sb.toString());
136 
137         content =
138             content.substring(0, x) + imports +
139                 content.substring(y + 1, content.length());
140 
141         return content;
142     }
143 
144     public static void _checkPersistenceTestSuite() throws IOException {
145         String basedir = "./portal-impl/test";
146 
147         if (!_fileUtil.exists(basedir)) {
148             return;
149         }
150 
151         DirectoryScanner ds = new DirectoryScanner();
152 
153         ds.setBasedir(basedir);
154         ds.setIncludes(new String[] {"**\\*PersistenceTest.java"});
155 
156         ds.scan();
157 
158         String[] files = ds.getIncludedFiles();
159 
160         Set<String> persistenceTests = new HashSet<String>();
161 
162         for (String file : files) {
163             String persistenceTest = file.substring(0, file.length() - 5);
164 
165             persistenceTest = persistenceTest.substring(
166                 persistenceTest.lastIndexOf(File.separator) + 1,
167                 persistenceTest.length());
168 
169             persistenceTests.add(persistenceTest);
170         }
171 
172         String persistenceTestSuite = _fileUtil.read(
173             basedir + "/com/liferay/portal/service/persistence/" +
174                 "PersistenceTestSuite.java");
175 
176         for (String persistenceTest : persistenceTests) {
177             if (persistenceTestSuite.indexOf(persistenceTest) == -1) {
178                 System.out.println("PersistenceTestSuite: " + persistenceTest);
179             }
180         }
181     }
182 
183     private static void _checkWebXML() throws IOException {
184         String basedir = "./";
185 
186         if (_fileUtil.exists(basedir + "portal-impl")) {
187             return;
188         }
189 
190         String webXML = ContentUtil.get(
191             "com/liferay/portal/deploy/dependencies/web.xml");
192 
193         DirectoryScanner ds = new DirectoryScanner();
194 
195         ds.setBasedir(basedir);
196         ds.setIncludes(new String[] {"**\\web.xml"});
197 
198         ds.scan();
199 
200         String[] files = ds.getIncludedFiles();
201 
202         for (String file : files) {
203             String content = _fileUtil.read(basedir + file);
204 
205             if (content.equals(webXML)) {
206                 System.out.println(file);
207             }
208         }
209     }
210 
211     private static void _checkXSS(String fileName, String jspContent) {
212         Matcher matcher = _xssPattern.matcher(jspContent);
213 
214         while (matcher.find()) {
215             boolean xssVulnerable = false;
216 
217             String jspVariable = matcher.group(1);
218 
219             String inputVulnerability =
220                 "<input[^>]* value=\"<%= " + jspVariable + " %>";
221 
222             Pattern inputVulnerabilityPattern =
223                 Pattern.compile(inputVulnerability, Pattern.CASE_INSENSITIVE);
224 
225             Matcher inputVulnerabilityMatcher =
226                 inputVulnerabilityPattern.matcher(jspContent);
227 
228             if (inputVulnerabilityMatcher.find()) {
229                 xssVulnerable = true;
230             }
231 
232             String anchorVulnerability = " href=\"<%= " + jspVariable + " %>";
233 
234             if (jspContent.indexOf(anchorVulnerability) != -1) {
235                 xssVulnerable = true;
236             }
237 
238             String inlineStringVulnerability1 = "'<%= " + jspVariable + " %>";
239 
240             if (jspContent.indexOf(inlineStringVulnerability1) != -1) {
241                 xssVulnerable = true;
242             }
243 
244             String inlineStringVulnerability2 = "(\"<%= " + jspVariable + " %>";
245 
246             if (jspContent.indexOf(inlineStringVulnerability2) != -1) {
247                 xssVulnerable = true;
248             }
249 
250             String inlineStringVulnerability3 = " \"<%= " + jspVariable + " %>";
251 
252             if (jspContent.indexOf(inlineStringVulnerability3) != -1) {
253                 xssVulnerable = true;
254             }
255 
256             String documentIdVulnerability = ".<%= " + jspVariable + " %>";
257 
258             if (jspContent.indexOf(documentIdVulnerability) != -1) {
259                 xssVulnerable = true;
260             }
261 
262             if (xssVulnerable) {
263                 System.out.println(
264                     "(xss): " + fileName + " (" + jspVariable + ")");
265             }
266         }
267     }
268 
269     public static String _formatImports(String imports) throws IOException {
270         if ((imports.indexOf("/*") != -1) ||
271             (imports.indexOf("*/") != -1) ||
272             (imports.indexOf("//") != -1)) {
273 
274             return imports + "\n";
275         }
276 
277         List<String> importsList = new ArrayList<String>();
278 
279         BufferedReader br = new BufferedReader(new StringReader(imports));
280 
281         String line = null;
282 
283         while ((line = br.readLine()) != null) {
284             if (line.indexOf("import ") != -1) {
285                 if (!importsList.contains(line)) {
286                     importsList.add(line);
287                 }
288             }
289         }
290 
291         importsList = ListUtil.sort(importsList);
292 
293         StringBuilder sb = new StringBuilder();
294 
295         String temp = null;
296 
297         for (int i = 0; i < importsList.size(); i++) {
298             String s = importsList.get(i);
299 
300             int pos = s.indexOf(".");
301 
302             pos = s.indexOf(".", pos + 1);
303 
304             if (pos == -1) {
305                 pos = s.indexOf(".");
306             }
307 
308             String packageLevel = s.substring(7, pos);
309 
310             if ((i != 0) && (!packageLevel.equals(temp))) {
311                 sb.append("\n");
312             }
313 
314             temp = packageLevel;
315 
316             sb.append(s);
317             sb.append("\n");
318         }
319 
320         return sb.toString();
321     }
322 
323     private static void _formatJava() throws IOException {
324         String basedir = "./";
325 
326         String copyright = _getCopyright();
327 
328         boolean portalJavaFiles = true;
329 
330         String[] files = null;
331 
332         if (_fileUtil.exists(basedir + "portal-impl")) {
333             files = _getPortalJavaFiles();
334         }
335         else {
336             portalJavaFiles = false;
337 
338             files = _getPluginJavaFiles();
339         }
340 
341         for (int i = 0; i < files.length; i++) {
342             File file = new File(basedir + files[i]);
343 
344             String content = _fileUtil.read(file);
345 
346             String className = file.getName();
347 
348             className = className.substring(0, className.length() - 5);
349 
350             String packagePath = files[i];
351 
352             int packagePathX = packagePath.indexOf(
353                 File.separator + "src" + File.separator);
354             int packagePathY = packagePath.lastIndexOf(File.separator);
355 
356             if ((packagePathX + 5) >= packagePathY) {
357                 packagePath = StringPool.BLANK;
358             }
359             else {
360                 packagePath = packagePath.substring(
361                     packagePathX + 5, packagePathY);
362             }
363 
364             packagePath = StringUtil.replace(
365                 packagePath, File.separator, StringPool.PERIOD);
366 
367             if (packagePath.endsWith(".model")) {
368                 if (content.indexOf(
369                         "extends " + className + "Model {") != -1) {
370 
371                     continue;
372                 }
373             }
374 
375             String newContent = _formatJavaContent(files[i], content);
376 
377             if (newContent.indexOf("$\n */") != -1) {
378                 System.out.println("*: " + files[i]);
379 
380                 newContent = StringUtil.replace(
381                     newContent, "$\n */", "$\n *\n */");
382             }
383 
384             if (newContent.indexOf(copyright) == -1) {
385                 System.out.println("(c): " + files[i]);
386             }
387 
388             if (newContent.indexOf(className + ".java.html") == -1) {
389                 System.out.println("Java2HTML: " + files[i]);
390             }
391 
392             newContent = stripImports(newContent, packagePath, className);
393 
394             newContent = StringUtil.replace(
395                 newContent, "@author Raymond Aug?", "@author Raymond Augé");
396 
397             if (newContent.indexOf(";\n/**") != -1) {
398                 newContent = StringUtil.replace(
399                     newContent,
400                     ";\n/**",
401                     ";\n\n/**");
402             }
403 
404             if (newContent.indexOf("\t/*\n\t *") != -1) {
405                 newContent = StringUtil.replace(
406                     newContent,
407                     "\t/*\n\t *",
408                     "\t/**\n\t *");
409             }
410 
411             if (newContent.indexOf("if(") != -1) {
412                 newContent = StringUtil.replace(
413                     newContent,
414                     "if(",
415                     "if (");
416             }
417 
418             if (newContent.indexOf("while(") != -1) {
419                 newContent = StringUtil.replace(
420                     newContent,
421                     "while(",
422                     "while (");
423             }
424 
425             if (newContent.indexOf("\n\n\n") != -1) {
426                 newContent = StringUtil.replace(
427                     newContent,
428                     "\n\n\n",
429                     "\n\n");
430             }
431 
432             if (newContent.indexOf("*/\npackage ") != -1) {
433                 System.out.println("package: " + files[i]);
434             }
435 
436             if (newContent.indexOf("    ") != -1) {
437                 if (!files[i].endsWith("StringPool.java")) {
438                     System.out.println("tab: " + files[i]);
439                 }
440             }
441 
442             if (newContent.indexOf("  {") != -1) {
443                 System.out.println("{:" + files[i]);
444             }
445 
446             if (!newContent.endsWith("\n\n}") &&
447                 !newContent.endsWith("{\n}")) {
448 
449                 System.out.println("}: " + files[i]);
450             }
451 
452             if (portalJavaFiles && className.endsWith("ServiceImpl") &&
453                 (newContent.indexOf("ServiceUtil.") != -1)) {
454 
455                 System.out.println("ServiceUtil: " + files[i]);
456             }
457 
458             if ((newContent != null) && !content.equals(newContent)) {
459                 _fileUtil.write(file, newContent);
460 
461                 System.out.println(file);
462             }
463         }
464     }
465 
466     private static String _formatJavaContent(String fileName, String content)
467         throws IOException {
468 
469         boolean longLogFactoryUtil = false;
470 
471         StringBuilder sb = new StringBuilder();
472 
473         BufferedReader br = new BufferedReader(new StringReader(content));
474 
475         int lineCount = 0;
476 
477         String line = null;
478 
479         while ((line = br.readLine()) != null) {
480             lineCount++;
481 
482             if (line.trim().length() == 0) {
483                 line = StringPool.BLANK;
484             }
485 
486             line = StringUtil.trimTrailing(line);
487 
488             line = StringUtil.replace(
489                 line,
490                 new String[] {
491                     "* Copyright (c) 2000-2008 Liferay, Inc.",
492                     "* Copyright 2008 Sun Microsystems Inc."
493                 },
494                 new String[] {
495                     "* Copyright (c) 2000-2009 Liferay, Inc.",
496                     "* Copyright 2009 Sun Microsystems Inc."
497                 });
498 
499             sb.append(line);
500             sb.append("\n");
501 
502             StringBuilder lineSB = new StringBuilder();
503 
504             int spacesPerTab = 4;
505 
506             for (char c : line.toCharArray()) {
507                 if (c == CharPool.TAB) {
508                     for (int i = 0; i < spacesPerTab; i++) {
509                         lineSB.append(CharPool.SPACE);
510                     }
511 
512                     spacesPerTab = 4;
513                 }
514                 else {
515                     lineSB.append(c);
516 
517                     spacesPerTab--;
518 
519                     if (spacesPerTab <= 0) {
520                         spacesPerTab = 4;
521                     }
522                 }
523             }
524 
525             line = lineSB.toString();
526 
527             String excluded = _exclusions.getProperty(
528                 StringUtil.replace(fileName, "\\", "/") + StringPool.AT +
529                     lineCount);
530 
531             if (excluded == null) {
532                 excluded = _exclusions.getProperty(
533                     StringUtil.replace(fileName, "\\", "/"));
534             }
535 
536             if ((excluded == null) && (line.length() > 80) &&
537                 (!line.startsWith("import "))) {
538 
539                 if (line.contains(
540                         "private static Log _log = LogFactoryUtil.getLog(")) {
541 
542                     longLogFactoryUtil = true;
543                 }
544 
545                 if (fileName.endsWith("Table.java") &&
546                     line.contains("String TABLE_SQL_CREATE = ")) {
547                 }
548                 else {
549                     System.out.println("> 80: " + fileName + " " + lineCount);
550                 }
551             }
552         }
553 
554         br.close();
555 
556         String newContent = sb.toString();
557 
558         if (newContent.endsWith("\n")) {
559             newContent = newContent.substring(0, newContent.length() -1);
560         }
561 
562         if (longLogFactoryUtil) {
563             newContent = StringUtil.replace(
564                 newContent, "private static Log _log = ",
565                 "private static Log _log =\n\t\t");
566         }
567 
568         return newContent;
569     }
570 
571     private static void _formatJSP() throws IOException {
572         String basedir = "./";
573 
574         List<File> list = new ArrayList<File>();
575 
576         DirectoryScanner ds = new DirectoryScanner();
577 
578         ds.setBasedir(basedir);
579         ds.setExcludes(
580             new String[] {"**\\null.jsp", "**\\tmp\\**", "**\\tools\\tck\\**"});
581         ds.setIncludes(new String[] {"**\\*.jsp", "**\\*.jspf", "**\\*.vm"});
582 
583         ds.scan();
584 
585         list.addAll(ListUtil.fromArray(ds.getIncludedFiles()));
586 
587         String copyright = _getCopyright();
588 
589         String[] files = list.toArray(new String[list.size()]);
590 
591         for (int i = 0; i < files.length; i++) {
592             File file = new File(basedir + files[i]);
593 
594             String content = _fileUtil.read(file);
595             String newContent = _formatJSPContent(files[i], content);
596 
597             newContent = StringUtil.replace(
598                 newContent,
599                 new String[] {
600                     "<br/>", "\"/>", "\" >", "@page import", "\"%>", ")%>",
601                     "javascript: "
602                 },
603                 new String[] {
604                     "<br />", "\" />", "\">", "@ page import", "\" %>", ") %>",
605                     "javascript:"
606                 });
607 
608             newContent = StringUtil.replace(
609                 newContent,
610                 new String[] {
611                     "* Copyright (c) 2000-2008 Liferay, Inc.",
612                     "* Copyright 2008 Sun Microsystems Inc."
613                 },
614                 new String[] {
615                     "* Copyright (c) 2000-2009 Liferay, Inc.",
616                     "* Copyright 2009 Sun Microsystems Inc."
617                 });
618 
619             if (files[i].endsWith(".jsp")) {
620                 if (newContent.indexOf(copyright) == -1) {
621                     System.out.println("(c): " + files[i]);
622                 }
623             }
624 
625             if (newContent.indexOf("alert('<%= LanguageUtil.") != -1) {
626                 newContent = StringUtil.replace(newContent,
627                     "alert('<%= LanguageUtil.",
628                     "alert('<%= UnicodeLanguageUtil.");
629             }
630 
631             if (newContent.indexOf("alert(\"<%= LanguageUtil.") != -1) {
632                 newContent = StringUtil.replace(newContent,
633                     "alert(\"<%= LanguageUtil.",
634                     "alert(\"<%= UnicodeLanguageUtil.");
635             }
636 
637             if (newContent.indexOf("confirm('<%= LanguageUtil.") != -1) {
638                 newContent = StringUtil.replace(newContent,
639                     "confirm('<%= LanguageUtil.",
640                     "confirm('<%= UnicodeLanguageUtil.");
641             }
642 
643             if (newContent.indexOf("confirm(\"<%= LanguageUtil.") != -1) {
644                 newContent = StringUtil.replace(newContent,
645                     "confirm(\"<%= LanguageUtil.",
646                     "confirm(\"<%= UnicodeLanguageUtil.");
647             }
648 
649             if (newContent.indexOf("    ") != -1) {
650                 if (!files[i].endsWith("template.vm")) {
651                     System.out.println("tab: " + files[i]);
652                 }
653             }
654 
655             _checkXSS(files[i], content);
656 
657             if ((newContent != null) && !content.equals(newContent)) {
658                 _fileUtil.write(file, newContent);
659 
660                 System.out.println(file);
661             }
662         }
663     }
664 
665     private static String _formatJSPContent(String fileName, String content)
666         throws IOException {
667 
668         StringBuilder sb = new StringBuilder();
669 
670         BufferedReader br = new BufferedReader(new StringReader(content));
671 
672         String line = null;
673 
674         while ((line = br.readLine()) != null) {
675             if (line.trim().length() == 0) {
676                 line = StringPool.BLANK;
677             }
678 
679             line = StringUtil.trimTrailing(line);
680 
681             sb.append(line);
682             sb.append("\n");
683         }
684 
685         br.close();
686 
687         content = sb.toString();
688 
689         if (content.endsWith("\n")) {
690             content = content.substring(0, content.length() -1);
691         }
692 
693         content = _formatTaglibQuotes(fileName, content, StringPool.QUOTE);
694         content = _formatTaglibQuotes(fileName, content, StringPool.APOSTROPHE);
695 
696         return content;
697     }
698 
699     private static String _formatTaglibQuotes(
700         String fileName, String content, String quoteType) {
701 
702         String quoteFix = StringPool.APOSTROPHE;
703 
704         if (quoteFix.equals(quoteType)) {
705             quoteFix = StringPool.QUOTE;
706         }
707 
708         Pattern pattern = Pattern.compile(_getTaglibRegex(quoteType));
709 
710         Matcher matcher = pattern.matcher(content);
711 
712         while (matcher.find()) {
713             int x = content.indexOf(quoteType + "<%=", matcher.start());
714             int y = content.indexOf("%>" + quoteType, x);
715 
716             while ((x != -1) && (y != -1)) {
717                 String result = content.substring(x + 1, y + 2);
718 
719                 if (result.indexOf(quoteType) != -1) {
720                     int lineCount = 1;
721 
722                     char contentCharArray[] = content.toCharArray();
723 
724                     for (int i = 0; i < x; i++) {
725                         if (contentCharArray[i] == CharPool.NEW_LINE) {
726                             lineCount++;
727                         }
728                     }
729 
730                     if (result.indexOf(quoteFix) == -1) {
731                         StringBuilder sb = new StringBuilder();
732 
733                         sb.append(content.substring(0, x));
734                         sb.append(quoteFix);
735                         sb.append(result);
736                         sb.append(quoteFix);
737                         sb.append(content.substring(y + 3, content.length()));
738 
739                         content = sb.toString();
740                     }
741                     else {
742                         System.out.println(
743                             "taglib: " + fileName + " " + lineCount);
744                     }
745                 }
746 
747                 x = content.indexOf(quoteType + "<%=", y);
748 
749                 if (x > matcher.end()) {
750                     break;
751                 }
752 
753                 y = content.indexOf("%>" + quoteType, x);
754             }
755         }
756 
757         return content;
758     }
759 
760     private static String _getCopyright() throws IOException {
761         try {
762             return _fileUtil.read("copyright.txt");
763         }
764         catch (Exception e1) {
765             try {
766                 return _fileUtil.read("../copyright.txt");
767             }
768             catch (Exception e2) {
769                 return _fileUtil.read("../../copyright.txt");
770             }
771         }
772     }
773 
774     private static String[] _getPluginJavaFiles() {
775         String basedir = "./";
776 
777         List<File> list = new ArrayList<File>();
778 
779         DirectoryScanner ds = new DirectoryScanner();
780 
781         ds.setBasedir(basedir);
782         ds.setExcludes(
783             new String[] {
784                 "**\\model\\*Clp.java", "**\\model\\*Model.java",
785                 "**\\model\\*Soap.java", "**\\model\\impl\\*ModelImpl.java",
786                 "**\\service\\*Service.java", "**\\service\\*ServiceClp.java",
787                 "**\\service\\*ServiceFactory.java",
788                 "**\\service\\*ServiceUtil.java",
789                 "**\\service\\ClpSerializer.java",
790                 "**\\service\\base\\*ServiceBaseImpl.java",
791                 "**\\service\\http\\*JSONSerializer.java",
792                 "**\\service\\http\\*ServiceHttp.java",
793                 "**\\service\\http\\*ServiceJSON.java",
794                 "**\\service\\http\\*ServiceSoap.java",
795                 "**\\service\\messaging\\*ClpMessageListener.java",
796                 "**\\service\\persistence\\*Finder.java",
797                 "**\\service\\persistence\\*Persistence.java",
798                 "**\\service\\persistence\\*PersistenceImpl.java",
799                 "**\\service\\persistence\\*Util.java", "**\\tmp\\**"
800             });
801         ds.setIncludes(new String[] {"**\\*.java"});
802 
803         ds.scan();
804 
805         list.addAll(ListUtil.fromArray(ds.getIncludedFiles()));
806 
807         return list.toArray(new String[list.size()]);
808     }
809 
810     private static String[] _getPortalJavaFiles() {
811         String basedir = "./";
812 
813         List<File> list = new ArrayList<File>();
814 
815         DirectoryScanner ds = new DirectoryScanner();
816 
817         ds.setBasedir(basedir);
818         ds.setExcludes(
819             new String[] {
820                 "**\\classes\\*", "**\\jsp\\*", "**\\tmp\\**",
821                 "**\\EARXMLBuilder.java", "**\\EJBXMLBuilder.java",
822                 "**\\PropsKeys.java", "**\\InstanceWrapperBuilder.java",
823                 "**\\ServiceBuilder.java", "**\\SourceFormatter.java",
824                 "**\\UserAttributes.java", "**\\WebKeys.java",
825                 "**\\*_IW.java", "**\\XHTMLComplianceFormatter.java",
826                 "**\\portal-service\\**\\model\\*Model.java",
827                 "**\\portal-service\\**\\model\\*Soap.java",
828                 "**\\model\\impl\\*ModelImpl.java",
829                 "**\\portal\\service\\**", "**\\portal-client\\**",
830                 "**\\portal-web\\classes\\**\\*.java",
831                 "**\\portal-web\\test\\**\\*Test.java",
832                 "**\\portlet\\**\\service\\**", "**\\tools\\ext_tmpl\\**",
833                 "**\\tools\\tck\\**"
834             });
835         ds.setIncludes(new String[] {"**\\*.java"});
836 
837         ds.scan();
838 
839         list.addAll(ListUtil.fromArray(ds.getIncludedFiles()));
840 
841         ds = new DirectoryScanner();
842 
843         ds.setBasedir(basedir);
844         ds.setExcludes(
845             new String[] {
846                  "**\\portal-client\\**", "**\\tools\\ext_tmpl\\**",
847                 "**\\*_IW.java", "**\\test\\**\\*PersistenceTest.java"
848             });
849         ds.setIncludes(
850             new String[] {
851                 "**\\model\\BaseModel.java",
852                 "**\\model\\impl\\BaseModelImpl.java",
853                 "**\\service\\base\\PrincipalBean.java",
854                 "**\\service\\http\\*HttpTest.java",
855                 "**\\service\\http\\*SoapTest.java",
856                 "**\\service\\http\\TunnelUtil.java",
857                 "**\\service\\impl\\*.java", "**\\service\\jms\\*.java",
858                 "**\\service\\permission\\*.java",
859                 "**\\service\\persistence\\BasePersistence.java",
860                 "**\\service\\persistence\\BatchSession*.java",
861                 "**\\service\\persistence\\*FinderImpl.java",
862                 "**\\service\\persistence\\*Query.java",
863                 "**\\service\\persistence\\impl\\BasePersistenceImpl.java",
864                 "**\\portal-impl\\test\\**\\*.java",
865                 "**\\portal-service\\**\\liferay\\counter\\**.java",
866                 "**\\portal-service\\**\\liferay\\documentlibrary\\**.java",
867                 "**\\portal-service\\**\\liferay\\lock\\**.java",
868                 "**\\portal-service\\**\\liferay\\mail\\**.java",
869                 "**\\util-bridges\\**\\*.java"
870             });
871 
872         ds.scan();
873 
874         list.addAll(ListUtil.fromArray(ds.getIncludedFiles()));
875 
876         return list.toArray(new String[list.size()]);
877     }
878 
879     private static String _getTaglibRegex(String quoteType) {
880         StringBuilder sb = new StringBuilder();
881 
882         sb.append("<(");
883 
884         for (int i = 0; i < _TAG_LIBRARIES.length; i++) {
885             sb.append(_TAG_LIBRARIES[i]);
886             sb.append(StringPool.PIPE);
887         }
888 
889         sb.deleteCharAt(sb.length() - 1);
890         sb.append("):([^>]|%>)*");
891         sb.append(quoteType);
892         sb.append("<%=[^>]*");
893         sb.append(quoteType);
894         sb.append("[^>]*%>");
895         sb.append(quoteType);
896         sb.append("([^>]|%>)*>");
897 
898         return sb.toString();
899     }
900 
901     private static void _readExclusions() throws IOException {
902         _exclusions = new Properties();
903 
904         ClassLoader classLoader = SourceFormatter.class.getClassLoader();
905 
906         String sourceFormatterExclusions = System.getProperty(
907             "source-formatter-exclusions",
908             "com/liferay/portal/tools/dependencies/" +
909                 "source_formatter_exclusions.properties");
910 
911         URL url = classLoader.getResource(sourceFormatterExclusions);
912 
913         if (url == null) {
914             return;
915         }
916 
917         InputStream is = url.openStream();
918 
919         _exclusions.load(is);
920 
921         is.close();
922     }
923 
924     private static final String[] _TAG_LIBRARIES = new String[] {
925         "c", "html", "jsp", "liferay-portlet", "liferay-security",
926         "liferay-theme", "liferay-ui", "liferay-util", "portlet", "struts",
927         "tiles"
928     };
929 
930     private static FileImpl _fileUtil = FileImpl.getInstance();
931     private static Properties _exclusions;
932     private static Pattern _xssPattern = Pattern.compile(
933         "String\\s+([^\\s]+)\\s*=\\s*ParamUtil\\.getString\\(");
934 
935 }