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