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