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