001
014
015 package com.liferay.portal.tools.sourceformatter;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
018 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
019 import com.liferay.portal.kernel.util.ArrayUtil;
020 import com.liferay.portal.kernel.util.CharPool;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.ListUtil;
023 import com.liferay.portal.kernel.util.ReleaseInfo;
024 import com.liferay.portal.kernel.util.StringBundler;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.StringUtil;
027 import com.liferay.portal.kernel.util.TextFormatter;
028 import com.liferay.portal.kernel.util.Validator;
029 import com.liferay.portal.util.FileImpl;
030 import com.liferay.portal.xml.SAXReaderImpl;
031
032 import java.io.File;
033 import java.io.FileInputStream;
034 import java.io.FileNotFoundException;
035 import java.io.IOException;
036 import java.io.InputStream;
037
038 import java.net.URL;
039
040 import java.util.ArrayList;
041 import java.util.HashMap;
042 import java.util.List;
043 import java.util.Map;
044 import java.util.Properties;
045 import java.util.regex.Matcher;
046 import java.util.regex.Pattern;
047
048 import org.apache.tools.ant.DirectoryScanner;
049
050
056 public abstract class BaseSourceProcessor implements SourceProcessor {
057
058 @Override
059 public void format(
060 boolean useProperties, boolean printErrors, boolean autoFix)
061 throws Exception {
062
063 _init(useProperties, printErrors, autoFix);
064
065 format();
066
067 sourceFormatterHelper.close();
068 }
069
070 @Override
071 public String format(
072 String fileName, boolean useProperties, boolean printErrors,
073 boolean autoFix)
074 throws Exception {
075
076 try {
077 _init(useProperties, printErrors, autoFix);
078
079 return format(fileName);
080 }
081 finally {
082 sourceFormatterHelper.close();
083 }
084 }
085
086 @Override
087 public List<String> getErrorMessages() {
088 return _errorMessages;
089 }
090
091 protected static String formatImports(String imports, int classStartPos)
092 throws IOException {
093
094 if (imports.contains("") ||
095 imports.contains("
096
097 return imports + "\n";
098 }
099
100 List<ImportPackage> importPackages = new ArrayList<ImportPackage>();
101
102 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
103 new UnsyncStringReader(imports));
104
105 String line = null;
106
107 while ((line = unsyncBufferedReader.readLine()) != null) {
108 ImportPackage importPackage = ImportPackageFactoryUtil.create(line);
109
110 if ((importPackage != null) &&
111 !importPackages.contains(importPackage)) {
112
113 importPackages.add(importPackage);
114 }
115 }
116
117 importPackages = ListUtil.sort(importPackages);
118
119 StringBundler sb = new StringBundler();
120
121 String temp = null;
122
123 for (int i = 0; i < importPackages.size(); i++) {
124 ImportPackage importPackage = importPackages.get(i);
125
126 String s = importPackage.getLine();
127
128 int pos = s.indexOf(".");
129
130 pos = s.indexOf(".", pos + 1);
131
132 if (pos == -1) {
133 pos = s.indexOf(".");
134 }
135
136 String packageLevel = s.substring(classStartPos, pos);
137
138 if ((i != 0) && !packageLevel.equals(temp)) {
139 sb.append("\n");
140 }
141
142 temp = packageLevel;
143
144 sb.append(s);
145 sb.append("\n");
146 }
147
148 return sb.toString();
149 }
150
151 protected void checkIfClauseParentheses(
152 String ifClause, String fileName, int lineCount) {
153
154 int quoteCount = StringUtil.count(ifClause, StringPool.QUOTE);
155
156 if ((quoteCount % 2) == 1) {
157 return;
158 }
159
160 ifClause = stripQuotes(ifClause, StringPool.QUOTE);
161
162 ifClause = stripQuotes(ifClause, StringPool.APOSTROPHE);
163
164 if (ifClause.contains(StringPool.DOUBLE_SLASH) ||
165 ifClause.contains("")) {
166
167 return;
168 }
169
170 ifClause = stripRedundantParentheses(ifClause);
171
172 int level = 0;
173 int max = StringUtil.count(ifClause, StringPool.OPEN_PARENTHESIS);
174 int previousParenthesisPos = -1;
175
176 int[] levels = new int[max];
177
178 for (int i = 0; i < ifClause.length(); i++) {
179 char c = ifClause.charAt(i);
180
181 if ((c == CharPool.OPEN_PARENTHESIS) ||
182 (c == CharPool.CLOSE_PARENTHESIS)) {
183
184 if (previousParenthesisPos != -1) {
185 String s = ifClause.substring(
186 previousParenthesisPos + 1, i);
187
188 if (hasMissingParentheses(s)) {
189 processErrorMessage(
190 fileName,
191 "missing parentheses: " + fileName + " " +
192 lineCount);
193 }
194 }
195
196 previousParenthesisPos = i;
197
198 if (c == CharPool.OPEN_PARENTHESIS) {
199 levels[level] = i;
200
201 level += 1;
202 }
203 else {
204 int posOpenParenthesis = levels[level - 1];
205
206 if (level > 1) {
207 char nextChar = ifClause.charAt(i + 1);
208 char previousChar = ifClause.charAt(
209 posOpenParenthesis - 1);
210
211 if (!Character.isLetterOrDigit(nextChar) &&
212 (nextChar != CharPool.PERIOD) &&
213 !Character.isLetterOrDigit(previousChar)) {
214
215 String s = ifClause.substring(
216 posOpenParenthesis + 1, i);
217
218 if (hasRedundantParentheses(s)) {
219 processErrorMessage(
220 fileName,
221 "redundant parentheses: " + fileName + " " +
222 lineCount);
223 }
224 }
225
226 if ((previousChar == CharPool.OPEN_PARENTHESIS) &&
227 (nextChar == CharPool.CLOSE_PARENTHESIS)) {
228
229 processErrorMessage(
230 fileName,
231 "redundant parentheses: " + fileName + " " +
232 lineCount);
233 }
234 }
235
236 level -= 1;
237 }
238 }
239 }
240 }
241
242 protected void checkLanguageKeys(
243 String fileName, String content, Pattern pattern)
244 throws IOException {
245
246 String fileExtension = fileUtil.getExtension(fileName);
247
248 if (!portalSource || fileExtension.equals("vm")) {
249 return;
250 }
251
252 if (_portalLanguageKeysProperties == null) {
253 _portalLanguageKeysProperties = new Properties();
254
255 ClassLoader classLoader =
256 BaseSourceProcessor.class.getClassLoader();
257
258 InputStream inputStream = classLoader.getResourceAsStream(
259 "content/Language.properties");
260
261 _portalLanguageKeysProperties.load(inputStream);
262 }
263
264 Matcher matcher = pattern.matcher(content);
265
266 while (matcher.find()) {
267 String[] languageKeys = getLanguageKeys(matcher);
268
269 for (String languageKey : languageKeys) {
270 if (Validator.isNumber(languageKey) ||
271 languageKey.endsWith(StringPool.DASH) ||
272 languageKey.endsWith(StringPool.PERIOD) ||
273 languageKey.endsWith(StringPool.UNDERLINE) ||
274 languageKey.startsWith(StringPool.DASH) ||
275 languageKey.startsWith(StringPool.OPEN_BRACKET) ||
276 languageKey.startsWith(StringPool.OPEN_CURLY_BRACE) ||
277 languageKey.startsWith(StringPool.PERIOD) ||
278 languageKey.startsWith(StringPool.UNDERLINE)) {
279
280 continue;
281 }
282
283 if (!_portalLanguageKeysProperties.containsKey(languageKey)) {
284 processErrorMessage(
285 fileName,
286 "missing language key: " + languageKey +
287 StringPool.SPACE + fileName);
288 }
289 }
290 }
291 }
292
293 protected String fixCompatClassImports(File file, String content)
294 throws IOException {
295
296 String absolutePath = fileUtil.getAbsolutePath(file);
297
298 if (portalSource ||
299 !mainReleaseVersion.equals(MAIN_RELEASE_VERSION_6_1_0) ||
300 absolutePath.contains("/ext-") ||
301 absolutePath.contains("/portal-compat-shared/")) {
302
303 return content;
304 }
305
306 Map<String, String> compatClassNamesMap = getCompatClassNamesMap();
307
308 String newContent = content;
309
310 for (Map.Entry<String, String> entry : compatClassNamesMap.entrySet()) {
311 String compatClassName = entry.getKey();
312 String extendedClassName = entry.getValue();
313
314 Pattern pattern = Pattern.compile(extendedClassName + "\\W");
315
316 for (;;) {
317 Matcher matcher = pattern.matcher(newContent);
318
319 if (!matcher.find()) {
320 break;
321 }
322
323 newContent =
324 newContent.substring(0, matcher.start()) + compatClassName +
325 newContent.substring(matcher.end() - 1);
326 }
327 }
328
329 return newContent;
330 }
331
332 protected String fixCopyright(
333 String content, String copyright, String oldCopyright, File file,
334 String fileName)
335 throws IOException {
336
337 if (fileName.endsWith(".vm")) {
338 return content;
339 }
340
341 if ((oldCopyright != null) && content.contains(oldCopyright)) {
342 content = StringUtil.replace(content, oldCopyright, copyright);
343
344 processErrorMessage(fileName, "old (c): " + fileName);
345 }
346
347 if (!content.contains(copyright)) {
348 String customCopyright = getCustomCopyright(file);
349
350 if (Validator.isNotNull(customCopyright)) {
351 copyright = customCopyright;
352 }
353
354 if (!content.contains(copyright)) {
355 processErrorMessage(fileName, "(c): " + fileName);
356 }
357 }
358
359 if (fileName.endsWith(".jsp") || fileName.endsWith(".jspf")) {
360 content = StringUtil.replace(
361 content, "<%\n" + copyright + "\n%>",
362 "<%--\n" + copyright + "\n--%>");
363 }
364
365 int x = content.indexOf("* Copyright (c) 2000-20");
366
367 if (x == -1) {
368 return content;
369 }
370
371 int y = copyright.indexOf("* Copyright (c) 2000-20");
372
373 if (y == -1) {
374 return content;
375 }
376
377 String contentCopyrightYear = content.substring(x, x + 25);
378 String copyrightYear = copyright.substring(y, y + 25);
379
380 return StringUtil.replace(content, contentCopyrightYear, copyrightYear);
381 }
382
383 protected String fixSessionKey(
384 String fileName, String content, Pattern pattern) {
385
386 if (mainReleaseVersion.equals(MAIN_RELEASE_VERSION_6_1_0)) {
387 return content;
388 }
389
390 Matcher matcher = pattern.matcher(content);
391
392 if (!matcher.find()) {
393 return content;
394 }
395
396 String newContent = content;
397
398 do {
399 String match = matcher.group();
400
401 String s = null;
402
403 if (pattern.equals(sessionKeyPattern)) {
404 s = StringPool.COMMA;
405 }
406 else if (pattern.equals(taglibSessionKeyPattern)) {
407 s = "key=";
408 }
409
410 int x = match.indexOf(s);
411
412 if (x == -1) {
413 continue;
414 }
415
416 x = x + s.length();
417
418 String substring = match.substring(x).trim();
419
420 String quote = StringPool.BLANK;
421
422 if (substring.startsWith(StringPool.APOSTROPHE)) {
423 quote = StringPool.APOSTROPHE;
424 }
425 else if (substring.startsWith(StringPool.QUOTE)) {
426 quote = StringPool.QUOTE;
427 }
428 else {
429 continue;
430 }
431
432 int y = match.indexOf(quote, x);
433 int z = match.indexOf(quote, y + 1);
434
435 if ((y == -1) || (z == -1)) {
436 continue;
437 }
438
439 String prefix = match.substring(0, y + 1);
440 String suffix = match.substring(z);
441 String oldKey = match.substring(y + 1, z);
442
443 boolean alphaNumericKey = true;
444
445 for (char c : oldKey.toCharArray()) {
446 if (!Validator.isChar(c) && !Validator.isDigit(c) &&
447 (c != CharPool.DASH) && (c != CharPool.UNDERLINE)) {
448
449 alphaNumericKey = false;
450 }
451 }
452
453 if (!alphaNumericKey) {
454 continue;
455 }
456
457 String newKey = TextFormatter.format(oldKey, TextFormatter.O);
458
459 newKey = TextFormatter.format(newKey, TextFormatter.M);
460
461 if (newKey.equals(oldKey)) {
462 continue;
463 }
464
465 String oldSub = prefix.concat(oldKey).concat(suffix);
466 String newSub = prefix.concat(newKey).concat(suffix);
467
468 newContent = StringUtil.replaceFirst(newContent, oldSub, newSub);
469 }
470 while (matcher.find());
471
472 return newContent;
473 }
474
475 protected abstract void format() throws Exception;
476
477 protected String format(String fileName)
478 throws Exception {
479
480 return null;
481 }
482
483 protected Map<String, String> getCompatClassNamesMap() throws IOException {
484 if (_compatClassNamesMap != null) {
485 return _compatClassNamesMap;
486 }
487
488 _compatClassNamesMap = new HashMap<String, String>();
489
490 String[] includes = new String[] {
491 "**\\portal-compat-shared\\src\\com\\liferay\\compat\\**\\*.java"
492 };
493
494 String basedir = BASEDIR;
495
496 List<String> fileNames = new ArrayList<String>();
497
498 for (int i = 0; i < 3; i++) {
499 fileNames = getFileNames(basedir, new String[0], includes);
500
501 if (!fileNames.isEmpty()) {
502 break;
503 }
504
505 basedir = "../" + basedir;
506 }
507
508 for (String fileName : fileNames) {
509 File file = new File(basedir + fileName);
510
511 String content = fileUtil.read(file);
512
513 fileName = StringUtil.replace(
514 fileName, StringPool.BACK_SLASH, StringPool.SLASH);
515
516 fileName = StringUtil.replace(
517 fileName, StringPool.SLASH, StringPool.PERIOD);
518
519 int pos = fileName.indexOf("com.");
520
521 String compatClassName = fileName.substring(pos);
522
523 compatClassName = compatClassName.substring(
524 0, compatClassName.length() - 5);
525
526 String extendedClassName = StringUtil.replace(
527 compatClassName, "compat.", StringPool.BLANK);
528
529 if (content.contains("extends " + extendedClassName)) {
530 _compatClassNamesMap.put(compatClassName, extendedClassName);
531 }
532 }
533
534 return _compatClassNamesMap;
535 }
536
537 protected String getCopyright() throws IOException {
538 if (Validator.isNotNull(_copyright)) {
539 return _copyright;
540 }
541
542 _copyright = fileUtil.read("copyright.txt");
543
544 if (Validator.isNull(_copyright)) {
545 _copyright = fileUtil.read("../copyright.txt");
546 }
547
548 if (Validator.isNull(_copyright)) {
549 _copyright = fileUtil.read("../../copyright.txt");
550 }
551
552 return _copyright;
553 }
554
555 protected String getCustomCopyright(File file) throws IOException {
556 String absolutePath = fileUtil.getAbsolutePath(file);
557
558 for (int x = absolutePath.length();;) {
559 x = absolutePath.lastIndexOf(StringPool.SLASH, x);
560
561 if (x == -1) {
562 break;
563 }
564
565 String copyright = fileUtil.read(
566 absolutePath.substring(0, x + 1) + "copyright.txt");
567
568 if (Validator.isNotNull(copyright)) {
569 return copyright;
570 }
571
572 x = x - 1;
573 }
574
575 return null;
576 }
577
578 protected List<String> getFileNames(
579 String basedir, String[] excludes, String[] includes) {
580
581 DirectoryScanner directoryScanner = new DirectoryScanner();
582
583 directoryScanner.setBasedir(basedir);
584
585 excludes = ArrayUtil.append(
586 excludes, _excludes, new String[] {"**\\.git\\**"});
587
588 directoryScanner.setExcludes(excludes);
589
590 directoryScanner.setIncludes(includes);
591
592 return sourceFormatterHelper.scanForFiles(directoryScanner);
593 }
594
595 protected List<String> getFileNames(String[] excludes, String[] includes) {
596 return getFileNames(BASEDIR, excludes, includes);
597 }
598
599 protected String[] getLanguageKeys(Matcher matcher) {
600 if (matcher.groupCount() > 0) {
601 String languageKey = matcher.group(1);
602
603 if (Validator.isNotNull(languageKey)) {
604 return new String[] {languageKey};
605 }
606 }
607
608 StringBundler sb = new StringBundler();
609
610 String match = matcher.group();
611
612 int count = 0;
613
614 for (int i = 0; i < match.length(); i++) {
615 char c = match.charAt(i);
616
617 switch (c) {
618 case CharPool.CLOSE_PARENTHESIS:
619 if (count <= 1) {
620 return new String[0];
621 }
622
623 count--;
624
625 break;
626
627 case CharPool.OPEN_PARENTHESIS:
628 count++;
629
630 break;
631
632 case CharPool.QUOTE:
633 if (count > 1) {
634 break;
635 }
636
637 while (i < match.length()) {
638 i++;
639
640 if (match.charAt(i) == CharPool.QUOTE) {
641 String languageKey = sb.toString();
642
643 if (match.startsWith("names")) {
644 return StringUtil.split(languageKey);
645 }
646 else {
647 return new String[] {languageKey};
648 }
649
650 }
651
652 sb.append(match.charAt(i));
653 }
654 }
655 }
656
657 return new String[0];
658 }
659
660 protected String getOldCopyright() throws IOException {
661 if (Validator.isNotNull(_oldCopyright)) {
662 return _oldCopyright;
663 }
664
665 _oldCopyright = fileUtil.read("old-copyright.txt");
666
667 if (Validator.isNull(_oldCopyright)) {
668 _oldCopyright = fileUtil.read("../old-copyright.txt");
669 }
670
671 if (Validator.isNull(_oldCopyright)) {
672 _oldCopyright = fileUtil.read("../../old-copyright.txt");
673 }
674
675 return _oldCopyright;
676 }
677
678 protected Properties getExclusionsProperties(String fileName)
679 throws IOException {
680
681 InputStream inputStream = null;
682
683 int level = 0;
684
685 if (portalSource) {
686 ClassLoader classLoader =
687 BaseSourceProcessor.class.getClassLoader();
688
689 String sourceFormatterExclusions = System.getProperty(
690 "source-formatter-exclusions",
691 "com/liferay/portal/tools/dependencies/" + fileName);
692
693 URL url = classLoader.getResource(sourceFormatterExclusions);
694
695 if (url == null) {
696 return null;
697 }
698
699 inputStream = url.openStream();
700 }
701 else {
702 try {
703 inputStream = new FileInputStream(fileName);
704 }
705 catch (FileNotFoundException fnfe) {
706 }
707
708 if (inputStream == null) {
709 try {
710 inputStream = new FileInputStream("../" + fileName);
711
712 level = 1;
713 }
714 catch (FileNotFoundException fnfe) {
715 }
716 }
717
718 if (inputStream == null) {
719 try {
720 inputStream = new FileInputStream("../../" + fileName);
721
722 level = 2;
723 }
724 catch (FileNotFoundException fnfe) {
725 return null;
726 }
727 }
728 }
729
730 Properties properties = new Properties();
731
732 properties.load(inputStream);
733
734 inputStream.close();
735
736 if (level > 0) {
737 properties = stripTopLevelDirectories(properties, level);
738 }
739
740 return properties;
741 }
742
743 protected boolean hasMissingParentheses(String s) {
744 if (Validator.isNull(s)) {
745 return false;
746 }
747
748 boolean containsAndOrOperator = (s.contains("&&") || s.contains("||"));
749
750 boolean containsCompareOperator =
751 (s.contains(" == ") || s.contains(" != ") || s.contains(" < ") ||
752 s.contains(" > ") || s.contains(" =< ") || s.contains(" => ") ||
753 s.contains(" <= ") || s.contains(" >= "));
754
755 boolean containsMathOperator =
756 (s.contains(" = ") || s.contains(" - ") || s.contains(" + ") ||
757 s.contains(" & ") || s.contains(" % ") || s.contains(" * ") ||
758 s.contains(" / "));
759
760 if (containsCompareOperator &&
761 (containsAndOrOperator ||
762 (containsMathOperator && !s.contains(StringPool.OPEN_BRACKET)))) {
763
764 return true;
765 }
766 else {
767 return false;
768 }
769 }
770
771 protected boolean hasRedundantParentheses(String s) {
772 if (!s.contains("&&") && !s.contains("||")) {
773 for (int x = 0;;) {
774 x = s.indexOf(StringPool.CLOSE_PARENTHESIS);
775
776 if (x == -1) {
777 break;
778 }
779
780 int y = s.substring(0, x).lastIndexOf(
781 StringPool.OPEN_PARENTHESIS);
782
783 if (y == -1) {
784 break;
785 }
786
787 s = s.substring(0, y) + s.substring(x + 1);
788 }
789 }
790
791 if (Validator.isNotNull(s) && !s.contains(StringPool.SPACE)) {
792 return true;
793 }
794 else {
795 return false;
796 }
797 }
798
799 protected boolean isAutoFix() {
800 return _autoFix;
801 }
802
803 protected void processErrorMessage(String fileName, String message) {
804 _errorMessages.add(message);
805
806 if (_printErrors) {
807 sourceFormatterHelper.printError(fileName, message);
808 }
809 }
810
811 protected String replacePrimitiveWrapperInstantiation(
812 String fileName, String line, int lineCount) {
813
814 if (true) {
815 return line;
816 }
817
818 String newLine = StringUtil.replace(
819 line,
820 new String[] {
821 "new Boolean(", "new Byte(", "new Character(", "new Integer(",
822 "new Long(", "new Short("
823 },
824 new String[] {
825 "Boolean.valueOf(", "Byte.valueOf(", "Character.valueOf(",
826 "Integer.valueOf(", "Long.valueOf(", "Short.valueOf("
827 });
828
829 if (!line.equals(newLine)) {
830 processErrorMessage(
831 fileName, "> new Primitive(: " + fileName + " " + lineCount);
832 }
833
834 return newLine;
835 }
836
837 protected String stripQuotes(String s, String delimeter) {
838 String[] parts = StringUtil.split(s, delimeter);
839
840 int i = 1;
841
842 while (i < parts.length) {
843 s = StringUtil.replaceFirst(
844 s, delimeter + parts[i] + delimeter, StringPool.BLANK);
845
846 i = i + 2;
847 }
848
849 return s;
850 }
851
852 protected String stripRedundantParentheses(String s) {
853 for (int x = 0;;) {
854 x = s.indexOf(StringPool.OPEN_PARENTHESIS, x + 1);
855 int y = s.indexOf(StringPool.CLOSE_PARENTHESIS, x);
856
857 if ((x == -1) || (y == -1)) {
858 return s;
859 }
860
861 String linePart = s.substring(x + 1, y);
862
863 linePart = StringUtil.replace(
864 linePart, StringPool.COMMA, StringPool.BLANK);
865
866 if (Validator.isAlphanumericName(linePart) ||
867 Validator.isNull(linePart)) {
868
869 s = s.substring(0, x) + s.substring(y + 1);
870 }
871 }
872 }
873
874 protected Properties stripTopLevelDirectories(
875 Properties properties, int level)
876 throws IOException {
877
878 File dir = new File(".");
879
880 String dirName = dir.getCanonicalPath();
881
882 dirName = StringUtil.replace(
883 dirName, StringPool.BACK_SLASH, StringPool.SLASH);
884
885 int pos = dirName.length();
886
887 for (int i = 0; i < level; i++) {
888 pos = dirName.lastIndexOf(StringPool.SLASH, pos - 1);
889 }
890
891 String topLevelDirNames = dirName.substring(pos + 1) + StringPool.SLASH;
892
893 Properties newProperties = new Properties();
894
895 for (Map.Entry<Object, Object> entry : properties.entrySet()) {
896 String key = (String)entry.getKey();
897
898 if (!key.startsWith(topLevelDirNames)) {
899 continue;
900 }
901
902 key = StringUtil.replaceFirst(
903 key, topLevelDirNames, StringPool.BLANK);
904
905 String value = (String)entry.getValue();
906
907 newProperties.setProperty(key, value);
908 }
909
910 return newProperties;
911 }
912
913 protected String trimContent(String content, boolean allowLeadingSpaces)
914 throws IOException {
915
916 StringBundler sb = new StringBundler();
917
918 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
919 new UnsyncStringReader(content));
920
921 String line = null;
922
923 while ((line = unsyncBufferedReader.readLine()) != null) {
924 sb.append(trimLine(line, allowLeadingSpaces));
925 sb.append("\n");
926 }
927
928 unsyncBufferedReader.close();
929
930 content = sb.toString();
931
932 if (content.endsWith("\n")) {
933 content = content.substring(0, content.length() - 1);
934 }
935
936 return content;
937 }
938
939 protected String trimLine(String line, boolean allowLeadingSpaces) {
940 if (line.trim().length() == 0) {
941 return StringPool.BLANK;
942 }
943
944 line = StringUtil.trimTrailing(line);
945
946 if (allowLeadingSpaces || !line.startsWith(StringPool.SPACE) ||
947 line.startsWith(" *")) {
948
949 return line;
950 }
951
952 if (!line.startsWith(StringPool.FOUR_SPACES)) {
953 while (line.startsWith(StringPool.SPACE)) {
954 line = StringUtil.replaceFirst(
955 line, StringPool.SPACE, StringPool.BLANK);
956 }
957 }
958 else {
959 int pos = 0;
960
961 String temp = line;
962
963 while (temp.startsWith(StringPool.FOUR_SPACES)) {
964 line = StringUtil.replaceFirst(
965 line, StringPool.FOUR_SPACES, StringPool.TAB);
966
967 pos++;
968
969 temp = line.substring(pos);
970 }
971 }
972
973 return line;
974 }
975
976 protected static final String BASEDIR = "./";
977
978 protected static final String MAIN_RELEASE_VERSION_6_1_0 = "6.1.0";
979
980 protected static final String MAIN_RELEASE_VERSION_6_2_0 = "6.2.0";
981
982 protected static FileImpl fileUtil = FileImpl.getInstance();
983 protected static Pattern languageKeyPattern = Pattern.compile(
984 "LanguageUtil.(?:get|format)\\([^;%]+|Liferay.Language.get\\('([^']+)");
985 protected static String mainReleaseVersion;
986 protected static boolean portalSource;
987 protected static SAXReaderImpl saxReaderUtil = SAXReaderImpl.getInstance();
988 protected static Pattern sessionKeyPattern = Pattern.compile(
989 "SessionErrors.(?:add|contains|get)\\([^;%&|!]+|".concat(
990 "SessionMessages.(?:add|contains|get)\\([^;%&|!]+"),
991 Pattern.MULTILINE);
992 protected static SourceFormatterHelper sourceFormatterHelper;
993 protected static Pattern taglibSessionKeyPattern = Pattern.compile(
994 "<liferay-ui:error [^>]+>|<liferay-ui:success [^>]+>",
995 Pattern.MULTILINE);
996
997 private void _init(
998 boolean useProperties, boolean printErrors, boolean autoFix)
999 throws Exception {
1000
1001 _errorMessages = new ArrayList<String>();
1002
1003 sourceFormatterHelper = new SourceFormatterHelper(useProperties);
1004
1005 sourceFormatterHelper.init();
1006
1007 if (_initialized) {
1008 return;
1009 }
1010
1011 _autoFix = autoFix;
1012
1013 _setVersion();
1014
1015 _excludes = StringUtil.split(
1016 GetterUtil.getString(
1017 System.getProperty("source.formatter.excludes")));
1018
1019 portalSource = _isPortalSource();
1020
1021 _printErrors = printErrors;
1022
1023 _initialized = true;
1024 }
1025
1026 private boolean _isPortalSource() {
1027 if (fileUtil.exists(BASEDIR + "portal-impl")) {
1028 return true;
1029 }
1030 else {
1031 return false;
1032 }
1033 }
1034
1035 private void _setVersion() throws Exception {
1036 String releaseInfoVersion = ReleaseInfo.getVersion();
1037
1038 if (releaseInfoVersion.startsWith("6.1")) {
1039 mainReleaseVersion = MAIN_RELEASE_VERSION_6_1_0;
1040 }
1041 else if (releaseInfoVersion.startsWith("6.2")) {
1042 mainReleaseVersion = MAIN_RELEASE_VERSION_6_2_0;
1043 }
1044 else {
1045 throw new Exception(
1046 "Invalid release information: " + ReleaseInfo.getVersion());
1047 }
1048 }
1049
1050 private static boolean _autoFix;
1051 private static Map<String, String> _compatClassNamesMap;
1052 private static String _copyright;
1053 private static List<String> _errorMessages = new ArrayList<String>();
1054 private static String[] _excludes;
1055 private static boolean _initialized;
1056 private static String _oldCopyright;
1057 private static Properties _portalLanguageKeysProperties;
1058 private static boolean _printErrors;
1059
1060 }