1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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             if (newContent.contains("com.liferay.portal.PortalException")) {
478                 newContent = StringUtil.replace(
479                     newContent, "com.liferay.portal.PortalException",
480                     "com.liferay.portal.kernel.exception.PortalException");
481             }
482 
483             if (newContent.contains("com.liferay.portal.SystemException")) {
484                 newContent = StringUtil.replace(
485                     newContent, "com.liferay.portal.SystemException",
486                     "com.liferay.portal.kernel.exception.SystemException");
487             }
488 
489             if (newContent.contains("com.liferay.util.LocalizationUtil")) {
490                 newContent = StringUtil.replace(
491                     newContent, "com.liferay.util.LocalizationUtil",
492                     "com.liferay.portal.kernel.util.LocalizationUtil");
493             }
494 
495             newContent = stripImports(newContent, packagePath, className);
496 
497             if (newContent.indexOf(";\n/**") != -1) {
498                 newContent = StringUtil.replace(
499                     newContent,
500                     ";\n/**",
501                     ";\n\n/**");
502             }
503 
504             if (newContent.indexOf("\t/*\n\t *") != -1) {
505                 newContent = StringUtil.replace(
506                     newContent,
507                     "\t/*\n\t *",
508                     "\t/**\n\t *");
509             }
510 
511             if (newContent.indexOf("if(") != -1) {
512                 newContent = StringUtil.replace(
513                     newContent,
514                     "if(",
515                     "if (");
516             }
517 
518             if (newContent.indexOf("while(") != -1) {
519                 newContent = StringUtil.replace(
520                     newContent,
521                     "while(",
522                     "while (");
523             }
524 
525             if (newContent.indexOf("\n\n\n") != -1) {
526                 newContent = StringUtil.replace(
527                     newContent,
528                     "\n\n\n",
529                     "\n\n");
530             }
531 
532             if (newContent.indexOf("*/\npackage ") != -1) {
533                 System.out.println("package: " + files[i]);
534             }
535 
536             if (newContent.indexOf("    ") != -1) {
537                 if (!files[i].endsWith("StringPool.java")) {
538                     System.out.println("tab: " + files[i]);
539                 }
540             }
541 
542             if (newContent.indexOf("  {") != -1) {
543                 System.out.println("{:" + files[i]);
544             }
545 
546             if (!newContent.endsWith("\n\n}") &&
547                 !newContent.endsWith("{\n}")) {
548 
549                 System.out.println("}: " + files[i]);
550             }
551 
552             if (portalJavaFiles && className.endsWith("ServiceImpl") &&
553                 (newContent.indexOf("ServiceUtil.") != -1)) {
554 
555                 System.out.println("ServiceUtil: " + files[i]);
556             }
557 
558             if ((newContent != null) && !content.equals(newContent)) {
559                 _fileUtil.write(file, newContent);
560 
561                 System.out.println(file);
562             }
563         }
564     }
565 
566     private static String _formatJavaContent(
567             String fileName, String className, String content)
568         throws IOException {
569 
570         boolean longLogFactoryUtil = false;
571 
572         StringBuilder sb = new StringBuilder();
573 
574         UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
575             new UnsyncStringReader(content));
576 
577         int lineCount = 0;
578 
579         String line = null;
580 
581         while ((line = unsyncBufferedReader.readLine()) != null) {
582             lineCount++;
583 
584             if (line.trim().length() == 0) {
585                 line = StringPool.BLANK;
586             }
587 
588             line = StringUtil.trimTrailing(line);
589 
590             line = StringUtil.replace(
591                 line,
592                 new String[] {
593                     "* Copyright (c) 2000-2009 Liferay, Inc."
594                 },
595                 new String[] {
596                     "* Copyright (c) 2000-2010 Liferay, Inc."
597                 });
598 
599             sb.append(line);
600             sb.append("\n");
601 
602             StringBuilder lineSB = new StringBuilder();
603 
604             int spacesPerTab = 4;
605 
606             for (char c : line.toCharArray()) {
607                 if (c == CharPool.TAB) {
608                     for (int i = 0; i < spacesPerTab; i++) {
609                         lineSB.append(CharPool.SPACE);
610                     }
611 
612                     spacesPerTab = 4;
613                 }
614                 else {
615                     lineSB.append(c);
616 
617                     spacesPerTab--;
618 
619                     if (spacesPerTab <= 0) {
620                         spacesPerTab = 4;
621                     }
622                 }
623             }
624 
625             line = lineSB.toString();
626 
627             if (line.endsWith("private static Log _log =")) {
628                 longLogFactoryUtil = true;
629             }
630 
631             String excluded = _exclusions.getProperty(
632                 StringUtil.replace(fileName, "\\", "/") + StringPool.AT +
633                     lineCount);
634 
635             if (excluded == null) {
636                 excluded = _exclusions.getProperty(
637                     StringUtil.replace(fileName, "\\", "/"));
638             }
639 
640             if ((excluded == null) && (line.length() > 80) &&
641                 !line.startsWith("import ") && !line.startsWith("package ")) {
642 
643                 if (fileName.endsWith("Table.java") &&
644                     line.contains("String TABLE_SQL_CREATE = ")) {
645                 }
646                 else {
647                     System.out.println("> 80: " + fileName + " " + lineCount);
648                 }
649             }
650         }
651 
652         unsyncBufferedReader.close();
653 
654         String newContent = sb.toString();
655 
656         if (newContent.endsWith("\n")) {
657             newContent = newContent.substring(0, newContent.length() -1);
658         }
659 
660         if (longLogFactoryUtil) {
661             newContent = StringUtil.replace(
662                 newContent,
663                 "private static Log _log =\n\t\tLogFactoryUtil.getLog(",
664                 "private static Log _log = LogFactoryUtil.getLog(\n\t\t");
665         }
666 
667         return newContent;
668     }
669 
670     private static void _formatJSP() throws IOException {
671         String basedir = "./";
672 
673         List<String> list = new ArrayList<String>();
674 
675         DirectoryScanner ds = new DirectoryScanner();
676 
677         ds.setBasedir(basedir);
678         ds.setExcludes(
679             new String[] {
680                 "**\\portal\\aui\\**", "**\\bin\\**", "**\\null.jsp",
681                 "**\\tmp\\**", "**\\tools\\tck\\**"
682             });
683         ds.setIncludes(new String[] {"**\\*.jsp", "**\\*.jspf", "**\\*.vm"});
684 
685         ds.scan();
686 
687         list.addAll(ListUtil.fromArray(ds.getIncludedFiles()));
688 
689         String copyright = _getCopyright();
690         String oldCopyright = _getOldCopyright();
691 
692         String[] files = list.toArray(new String[list.size()]);
693 
694         for (int i = 0; i < files.length; i++) {
695             File file = new File(basedir + files[i]);
696 
697             String content = _fileUtil.read(file);
698             String newContent = _formatJSPContent(files[i], content);
699 
700             newContent = StringUtil.replace(
701                 newContent,
702                 new String[] {
703                     "<br/>", "\"/>", "\" >", "@page import", "\"%>", ")%>",
704                     "javascript: "
705                 },
706                 new String[] {
707                     "<br />", "\" />", "\">", "@ page import", "\" %>", ") %>",
708                     "javascript:"
709                 });
710 
711             newContent = StringUtil.replace(
712                 newContent,
713                 new String[] {
714                     "* Copyright (c) 2000-2009 Liferay, Inc."
715                 },
716                 new String[] {
717                     "* Copyright (c) 2000-2010 Liferay, Inc."
718                 });
719 
720             if (files[i].endsWith(".jsp") || files[i].endsWith(".jspf")) {
721                 if ((oldCopyright != null) &&
722                     newContent.contains(oldCopyright)) {
723 
724                     newContent = StringUtil.replace(
725                         newContent, oldCopyright, copyright);
726 
727                     System.out.println("old (c): " + files[i]);
728                 }
729 
730                 if (newContent.indexOf(copyright) == -1) {
731                     System.out.println("(c): " + files[i]);
732                 }
733             }
734 
735             if (newContent.indexOf("alert('<%= LanguageUtil.") != -1) {
736                 newContent = StringUtil.replace(newContent,
737                     "alert('<%= LanguageUtil.",
738                     "alert('<%= UnicodeLanguageUtil.");
739             }
740 
741             if (newContent.indexOf("alert(\"<%= LanguageUtil.") != -1) {
742                 newContent = StringUtil.replace(newContent,
743                     "alert(\"<%= LanguageUtil.",
744                     "alert(\"<%= UnicodeLanguageUtil.");
745             }
746 
747             if (newContent.indexOf("confirm('<%= LanguageUtil.") != -1) {
748                 newContent = StringUtil.replace(newContent,
749                     "confirm('<%= LanguageUtil.",
750                     "confirm('<%= UnicodeLanguageUtil.");
751             }
752 
753             if (newContent.indexOf("confirm(\"<%= LanguageUtil.") != -1) {
754                 newContent = StringUtil.replace(newContent,
755                     "confirm(\"<%= LanguageUtil.",
756                     "confirm(\"<%= UnicodeLanguageUtil.");
757             }
758 
759             if (newContent.indexOf("    ") != -1) {
760                 if (!files[i].endsWith("template.vm")) {
761                     System.out.println("tab: " + files[i]);
762                 }
763             }
764 
765             _checkXSS(files[i], content);
766 
767             if ((newContent != null) && !content.equals(newContent)) {
768                 _fileUtil.write(file, newContent);
769 
770                 System.out.println(file);
771             }
772         }
773     }
774 
775     private static String _formatJSPContent(String fileName, String content)
776         throws IOException {
777 
778         StringBuilder sb = new StringBuilder();
779 
780         UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
781             new UnsyncStringReader(content));
782 
783         String line = null;
784 
785         while ((line = unsyncBufferedReader.readLine()) != null) {
786             if (line.trim().length() == 0) {
787                 line = StringPool.BLANK;
788             }
789 
790             line = StringUtil.trimTrailing(line);
791 
792             sb.append(line);
793             sb.append("\n");
794         }
795 
796         unsyncBufferedReader.close();
797 
798         content = sb.toString();
799 
800         if (content.endsWith("\n")) {
801             content = content.substring(0, content.length() -1);
802         }
803 
804         content = _formatTaglibQuotes(fileName, content, StringPool.QUOTE);
805         content = _formatTaglibQuotes(fileName, content, StringPool.APOSTROPHE);
806 
807         return content;
808     }
809 
810     private static String _formatTaglibQuotes(
811         String fileName, String content, String quoteType) {
812 
813         String quoteFix = StringPool.APOSTROPHE;
814 
815         if (quoteFix.equals(quoteType)) {
816             quoteFix = StringPool.QUOTE;
817         }
818 
819         Pattern pattern = Pattern.compile(_getTaglibRegex(quoteType));
820 
821         Matcher matcher = pattern.matcher(content);
822 
823         while (matcher.find()) {
824             int x = content.indexOf(quoteType + "<%=", matcher.start());
825             int y = content.indexOf("%>" + quoteType, x);
826 
827             while ((x != -1) && (y != -1)) {
828                 String result = content.substring(x + 1, y + 2);
829 
830                 if (result.indexOf(quoteType) != -1) {
831                     int lineCount = 1;
832 
833                     char contentCharArray[] = content.toCharArray();
834 
835                     for (int i = 0; i < x; i++) {
836                         if (contentCharArray[i] == CharPool.NEW_LINE) {
837                             lineCount++;
838                         }
839                     }
840 
841                     if (result.indexOf(quoteFix) == -1) {
842                         StringBuilder sb = new StringBuilder();
843 
844                         sb.append(content.substring(0, x));
845                         sb.append(quoteFix);
846                         sb.append(result);
847                         sb.append(quoteFix);
848                         sb.append(content.substring(y + 3, content.length()));
849 
850                         content = sb.toString();
851                     }
852                     else {
853                         System.out.println(
854                             "taglib: " + fileName + " " + lineCount);
855                     }
856                 }
857 
858                 x = content.indexOf(quoteType + "<%=", y);
859 
860                 if (x > matcher.end()) {
861                     break;
862                 }
863 
864                 y = content.indexOf("%>" + quoteType, x);
865             }
866         }
867 
868         return content;
869     }
870 
871     private static String _getCopyright() throws IOException {
872         try {
873             return _fileUtil.read("copyright.txt");
874         }
875         catch (Exception e1) {
876             try {
877                 return _fileUtil.read("../copyright.txt");
878             }
879             catch (Exception e2) {
880                 return _fileUtil.read("../../copyright.txt");
881             }
882         }
883     }
884 
885     private static String _getOldCopyright() {
886         try {
887             return _fileUtil.read("old-copyright.txt");
888         }
889         catch (Exception e1) {
890             try {
891                 return _fileUtil.read("../old-copyright.txt");
892             }
893             catch (Exception e2) {
894                 try {
895                     return _fileUtil.read("../../old-copyright.txt");
896                 }
897                 catch (Exception e3) {
898                     return null;
899                 }
900             }
901         }
902     }
903 
904     private static String[] _getPluginJavaFiles() {
905         String basedir = "./";
906 
907         List<String> list = new ArrayList<String>();
908 
909         DirectoryScanner ds = new DirectoryScanner();
910 
911         ds.setBasedir(basedir);
912         ds.setExcludes(
913             new String[] {
914                 "**\\bin\\**", "**\\model\\*Clp.java",
915                 "**\\model\\impl\\*ModelImpl.java",
916                 "**\\service\\**\\model\\*Model.java",
917                 "**\\service\\**\\model\\*Soap.java",
918                 "**\\service\\**\\model\\*Wrapper.java",
919                 "**\\service\\**\\service\\*Service.java",
920                 "**\\service\\**\\service\\*ServiceClp.java",
921                 "**\\service\\**\\service\\*ServiceFactory.java",
922                 "**\\service\\**\\service\\*ServiceUtil.java",
923                 "**\\service\\**\\service\\*ServiceWrapper.java",
924                 "**\\service\\**\\service\\ClpSerializer.java",
925                 "**\\service\\**\\service\\messaging\\*ClpMessageListener.java",
926                 "**\\service\\**\\service\\persistence\\*Finder.java",
927                 "**\\service\\**\\service\\persistence\\*Persistence.java",
928                 "**\\service\\**\\service\\persistence\\*Util.java",
929                 "**\\service\\base\\*ServiceBaseImpl.java",
930                 "**\\service\\http\\*JSONSerializer.java",
931                 "**\\service\\http\\*ServiceHttp.java",
932                 "**\\service\\http\\*ServiceJSON.java",
933                 "**\\service\\http\\*ServiceSoap.java",
934                 "**\\service\\persistence\\*PersistenceImpl.java",
935                 "**\\tmp\\**"
936             });
937         ds.setIncludes(new String[] {"**\\*.java"});
938 
939         ds.scan();
940 
941         list.addAll(ListUtil.fromArray(ds.getIncludedFiles()));
942 
943         return list.toArray(new String[list.size()]);
944     }
945 
946     private static String[] _getPortalJavaFiles() {
947         String basedir = "./";
948 
949         List<String> list = new ArrayList<String>();
950 
951         DirectoryScanner ds = new DirectoryScanner();
952 
953         ds.setBasedir(basedir);
954         ds.setExcludes(
955             new String[] {
956                 "**\\bin\\**", "**\\classes\\*", "**\\jsp\\*", "**\\tmp\\**",
957                 "**\\PropsKeys.java", "**\\InstanceWrapperBuilder.java",
958                 "**\\ServiceBuilder.java", "**\\SourceFormatter.java",
959                 "**\\UserAttributes.java", "**\\WebKeys.java",
960                 "**\\*_IW.java", "**\\portal-service\\**\\model\\*Model.java",
961                 "**\\portal-service\\**\\model\\*Soap.java",
962                 "**\\portal-service\\**\\model\\*Wrapper.java",
963                 "**\\model\\impl\\*ModelImpl.java",
964                 "**\\portal\\service\\**", "**\\portal-client\\**",
965                 "**\\portal-web\\classes\\**\\*.java",
966                 "**\\portal-web\\test\\**\\*Test.java",
967                 "**\\portlet\\**\\service\\**", "**\\tools\\ext_tmpl\\**",
968                 "**\\tools\\tck\\**"
969             });
970         ds.setIncludes(new String[] {"**\\*.java"});
971 
972         ds.scan();
973 
974         list.addAll(ListUtil.fromArray(ds.getIncludedFiles()));
975 
976         ds = new DirectoryScanner();
977 
978         ds.setBasedir(basedir);
979         ds.setExcludes(
980             new String[] {
981                 "**\\bin\\**", "**\\portal-client\\**",
982                 "**\\tools\\ext_tmpl\\**", "**\\*_IW.java",
983                 "**\\test\\**\\*PersistenceTest.java"
984             });
985         ds.setIncludes(
986             new String[] {
987                 "**\\com\\liferay\\portal\\service\\ServiceContext*.java",
988                 "**\\model\\BaseModel.java",
989                 "**\\model\\impl\\BaseModelImpl.java",
990                 "**\\service\\base\\PrincipalBean.java",
991                 "**\\service\\http\\*HttpTest.java",
992                 "**\\service\\http\\*SoapTest.java",
993                 "**\\service\\http\\TunnelUtil.java",
994                 "**\\service\\impl\\*.java", "**\\service\\jms\\*.java",
995                 "**\\service\\permission\\*.java",
996                 "**\\service\\persistence\\BasePersistence.java",
997                 "**\\service\\persistence\\BatchSession*.java",
998                 "**\\service\\persistence\\*FinderImpl.java",
999                 "**\\service\\persistence\\*Query.java",
1000                "**\\service\\persistence\\impl\\BasePersistenceImpl.java",
1001                "**\\portal-impl\\test\\**\\*.java",
1002                "**\\portal-service\\**\\liferay\\counter\\**.java",
1003                "**\\portal-service\\**\\liferay\\documentlibrary\\**.java",
1004                "**\\portal-service\\**\\liferay\\lock\\**.java",
1005                "**\\portal-service\\**\\liferay\\mail\\**.java",
1006                "**\\util-bridges\\**\\*.java"
1007            });
1008
1009        ds.scan();
1010
1011        list.addAll(ListUtil.fromArray(ds.getIncludedFiles()));
1012
1013        return list.toArray(new String[list.size()]);
1014    }
1015
1016    private static String _getTaglibRegex(String quoteType) {
1017        StringBuilder sb = new StringBuilder();
1018
1019        sb.append("<(");
1020
1021        for (int i = 0; i < _TAG_LIBRARIES.length; i++) {
1022            sb.append(_TAG_LIBRARIES[i]);
1023            sb.append(StringPool.PIPE);
1024        }
1025
1026        sb.deleteCharAt(sb.length() - 1);
1027        sb.append("):([^>]|%>)*");
1028        sb.append(quoteType);
1029        sb.append("<%=.*");
1030        sb.append(quoteType);
1031        sb.append(".*%>");
1032        sb.append(quoteType);
1033        sb.append("([^>]|%>)*>");
1034
1035        return sb.toString();
1036    }
1037
1038    private static void _readExclusions() throws IOException {
1039        _exclusions = new Properties();
1040
1041        ClassLoader classLoader = SourceFormatter.class.getClassLoader();
1042
1043        String sourceFormatterExclusions = System.getProperty(
1044            "source-formatter-exclusions",
1045            "com/liferay/portal/tools/dependencies/" +
1046                "source_formatter_exclusions.properties");
1047
1048        URL url = classLoader.getResource(sourceFormatterExclusions);
1049
1050        if (url == null) {
1051            return;
1052        }
1053
1054        InputStream is = url.openStream();
1055
1056        _exclusions.load(is);
1057
1058        is.close();
1059    }
1060
1061    private static final String[] _TAG_LIBRARIES = new String[] {
1062        "aui", "c", "html", "jsp", "liferay-portlet", "liferay-security",
1063        "liferay-theme", "liferay-ui", "liferay-util", "portlet", "struts",
1064        "tiles"
1065    };
1066
1067    private static FileImpl _fileUtil = FileImpl.getInstance();
1068    private static Properties _exclusions;
1069    private static Pattern _xssPattern = Pattern.compile(
1070        "String\\s+([^\\s]+)\\s*=\\s*ParamUtil\\.getString\\(");
1071
1072}