001 /** 002 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved. 003 * 004 * This library is free software; you can redistribute it and/or modify it under 005 * the terms of the GNU Lesser General Public License as published by the Free 006 * Software Foundation; either version 2.1 of the License, or (at your option) 007 * any later version. 008 * 009 * This library is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 011 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 012 * details. 013 */ 014 015 package com.liferay.portal.tools; 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.ClassUtil; 022 import com.liferay.portal.kernel.util.GetterUtil; 023 import com.liferay.portal.kernel.util.ListUtil; 024 import com.liferay.portal.kernel.util.PropertiesUtil; 025 import com.liferay.portal.kernel.util.PropsKeys; 026 import com.liferay.portal.kernel.util.StringBundler; 027 import com.liferay.portal.kernel.util.StringPool; 028 import com.liferay.portal.kernel.util.StringUtil; 029 import com.liferay.portal.kernel.util.TextFormatter; 030 import com.liferay.portal.kernel.util.Tuple; 031 import com.liferay.portal.kernel.util.Validator; 032 import com.liferay.portal.kernel.xml.Document; 033 import com.liferay.portal.kernel.xml.DocumentException; 034 import com.liferay.portal.kernel.xml.Element; 035 import com.liferay.portal.util.FileImpl; 036 import com.liferay.portal.xml.SAXReaderImpl; 037 import com.liferay.util.ContentUtil; 038 039 import java.io.File; 040 import java.io.FileInputStream; 041 import java.io.FileNotFoundException; 042 import java.io.IOException; 043 import java.io.InputStream; 044 045 import java.net.URL; 046 047 import java.util.ArrayList; 048 import java.util.Arrays; 049 import java.util.Collection; 050 import java.util.Collections; 051 import java.util.HashMap; 052 import java.util.HashSet; 053 import java.util.List; 054 import java.util.Map; 055 import java.util.Properties; 056 import java.util.Set; 057 import java.util.TreeSet; 058 import java.util.regex.Matcher; 059 import java.util.regex.Pattern; 060 061 import org.apache.tools.ant.DirectoryScanner; 062 063 /** 064 * @author Brian Wing Shun Chan 065 * @author Igor Spasic 066 * @author Wesley Gong 067 * @author Hugo Huijser 068 */ 069 public class SourceFormatter { 070 071 public static void main(String[] args) { 072 try { 073 _excludes = StringUtil.split( 074 GetterUtil.getString( 075 System.getProperty("source.formatter.excludes"))); 076 077 _portalSource = _isPortalSource(); 078 079 _sourceFormatterHelper = new SourceFormatterHelper(false); 080 081 _sourceFormatterHelper.init(); 082 083 Thread thread1 = new Thread () { 084 085 @Override 086 public void run() { 087 try { 088 _formatJSP(); 089 _formatAntXML(); 090 _formatDDLStructuresXML(); 091 _formatFriendlyURLRoutesXML(); 092 _formatFTL(); 093 _formatJS(); 094 _formatPortalProperties(); 095 _formatPortletXML(); 096 _formatServiceXML(); 097 _formatSH(); 098 _formatSQL(); 099 _formatStrutsConfigXML(); 100 _formatTilesDefsXML(); 101 _formatTLD(); 102 _formatWebXML(); 103 } 104 catch (Exception e) { 105 e.printStackTrace(); 106 } 107 } 108 109 }; 110 111 Thread thread2 = new Thread () { 112 113 @Override 114 public void run() { 115 try { 116 _formatJava(); 117 } 118 catch (Exception e) { 119 e.printStackTrace(); 120 } 121 } 122 123 }; 124 125 thread1.start(); 126 thread2.start(); 127 128 thread1.join(); 129 thread2.join(); 130 131 _sourceFormatterHelper.close(); 132 } 133 catch (Exception e) { 134 e.printStackTrace(); 135 } 136 } 137 138 public static String stripJavaImports( 139 String content, String packageDir, String className) 140 throws IOException { 141 142 Matcher matcher = _javaImportPattern.matcher(content); 143 144 if (!matcher.find()) { 145 return content; 146 } 147 148 String imports = matcher.group(); 149 150 Set<String> classes = ClassUtil.getClasses( 151 new UnsyncStringReader(content), className); 152 153 StringBundler sb = new StringBundler(); 154 155 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader( 156 new UnsyncStringReader(imports)); 157 158 String line = null; 159 160 while ((line = unsyncBufferedReader.readLine()) != null) { 161 if (line.contains("import ")) { 162 int importX = line.indexOf(" "); 163 int importY = line.lastIndexOf("."); 164 165 String importPackage = line.substring(importX + 1, importY); 166 String importClass = line.substring( 167 importY + 1, line.length() - 1); 168 169 if (!packageDir.equals(importPackage)) { 170 if (!importClass.equals("*")) { 171 if (classes.contains(importClass)) { 172 sb.append(line); 173 sb.append("\n"); 174 } 175 } 176 else { 177 sb.append(line); 178 sb.append("\n"); 179 } 180 } 181 } 182 } 183 184 imports = _formatImports(sb.toString(), 7); 185 186 content = 187 content.substring(0, matcher.start()) + imports + 188 content.substring(matcher.end()); 189 190 // Ensure a blank line exists between the package and the first import 191 192 content = content.replaceFirst( 193 "(?m)^[ \t]*(package .*;)\\s*^[ \t]*import", "$1\n\nimport"); 194 195 // Ensure a blank line exists between the last import (or package if 196 // there are no imports) and the class comment 197 198 content = content.replaceFirst( 199 "(?m)^[ \t]*((?:package|import) .*;)\\s*^[ \t]*/\\*\\*", 200 "$1\n\n/**"); 201 202 return content; 203 } 204 205 private static void _addJSPIncludeFileNames( 206 String fileName, Set<String> includeFileNames) { 207 208 String content = _jspContents.get(fileName); 209 210 if (Validator.isNull(content)) { 211 return; 212 } 213 214 for (int x = 0;;) { 215 x = content.indexOf("<%@ include file=", x); 216 217 if (x == -1) { 218 break; 219 } 220 221 x = content.indexOf(StringPool.QUOTE, x); 222 223 if (x == -1) { 224 break; 225 } 226 227 int y = content.indexOf(StringPool.QUOTE, x + 1); 228 229 if (y == -1) { 230 break; 231 } 232 233 String includeFileName = content.substring(x + 1, y); 234 235 Matcher matcher = _jspIncludeFilePattern.matcher(includeFileName); 236 237 if (!matcher.find()) { 238 throw new RuntimeException( 239 "Invalid include " + includeFileName); 240 } 241 242 String docrootPath = fileName.substring( 243 0, fileName.indexOf("docroot") + 7); 244 245 includeFileName = docrootPath + includeFileName; 246 247 if ((includeFileName.endsWith("jsp") || 248 includeFileName.endsWith("jspf")) && 249 !includeFileName.endsWith("html/portlet/init.jsp") && 250 !includeFileName.endsWith("html/taglib/init.jsp") && 251 !includeFileNames.contains(includeFileName)) { 252 253 includeFileNames.add(includeFileName); 254 } 255 256 x = y; 257 } 258 } 259 260 private static void _addJSPReferenceFileNames( 261 String fileName, Set<String> includeFileNames) { 262 263 for (Map.Entry<String, String> entry : _jspContents.entrySet()) { 264 String referenceFileName = entry.getKey(); 265 String content = entry.getValue(); 266 267 if (content.contains("<%@ include file=\"" + fileName) && 268 !includeFileNames.contains(referenceFileName)) { 269 270 includeFileNames.add(referenceFileName); 271 } 272 } 273 } 274 275 private static void _addJSPUnusedImports( 276 String fileName, List<String> importLines, 277 List<String> unneededImports) { 278 279 for (String importLine : importLines) { 280 Set<String> includeFileNames = new HashSet<String>(); 281 282 includeFileNames.add(fileName); 283 284 Set<String> checkedFileNames = new HashSet<String>(); 285 286 int x = importLine.indexOf(StringPool.QUOTE); 287 int y = importLine.indexOf(StringPool.QUOTE, x + 1); 288 289 if ((x == -1) || (y == -1)) { 290 continue; 291 } 292 293 String className = importLine.substring(x + 1, y); 294 295 className = className.substring( 296 className.lastIndexOf(StringPool.PERIOD) + 1); 297 298 if (!_isJSPImportRequired( 299 fileName, className, includeFileNames, checkedFileNames)) { 300 301 unneededImports.add(importLine); 302 } 303 } 304 } 305 306 private static List<String> _addParameterTypes( 307 String line, List<String> parameterTypes) { 308 309 int x = line.indexOf(StringPool.OPEN_PARENTHESIS); 310 311 if (x != -1) { 312 line = line.substring(x + 1); 313 314 if (Validator.isNull(line) || 315 line.startsWith(StringPool.CLOSE_PARENTHESIS)) { 316 317 return parameterTypes; 318 } 319 } 320 321 for (x = 0;;) { 322 x = line.indexOf(StringPool.SPACE); 323 324 if (x == -1) { 325 return parameterTypes; 326 } 327 328 String parameterType = line.substring(0, x); 329 330 if (parameterType.equals("throws")) { 331 return parameterTypes; 332 } 333 334 if (parameterType.endsWith("...")) { 335 parameterType = StringUtil.replaceLast( 336 parameterType, "...", StringPool.BLANK); 337 } 338 339 parameterTypes.add(parameterType); 340 341 int y = line.indexOf(StringPool.COMMA); 342 int z = line.indexOf(StringPool.CLOSE_PARENTHESIS); 343 344 if ((y == -1) || ((z != -1) && (z < y))) { 345 return parameterTypes; 346 } 347 348 line = line.substring(y + 1); 349 line = line.trim(); 350 } 351 } 352 353 private static void _checkIfClause( 354 String ifClause, String fileName, int lineCount) { 355 356 int quoteCount = StringUtil.count(ifClause, StringPool.QUOTE); 357 358 if ((quoteCount % 2) == 1) { 359 return; 360 } 361 362 ifClause = _stripQuotes(ifClause); 363 364 if (ifClause.contains(StringPool.DOUBLE_SLASH) || 365 ifClause.contains("/*") || ifClause.contains("*/")) { 366 367 return; 368 } 369 370 ifClause = _stripRedundantParentheses(ifClause); 371 372 ifClause = StringUtil.replace( 373 ifClause, new String[] {"'('", "')'"}, 374 new String[] {StringPool.BLANK, StringPool.BLANK}); 375 376 int level = 0; 377 int max = StringUtil.count(ifClause, StringPool.OPEN_PARENTHESIS); 378 int previousParenthesisPos = -1; 379 380 int[] levels = new int[max]; 381 382 for (int i = 0; i < ifClause.length(); i++) { 383 char c = ifClause.charAt(i); 384 385 if ((c == CharPool.OPEN_PARENTHESIS) || 386 (c == CharPool.CLOSE_PARENTHESIS)) { 387 388 if (previousParenthesisPos != -1) { 389 String s = ifClause.substring( 390 previousParenthesisPos + 1, i); 391 392 _checkMissingParentheses(s, fileName, lineCount); 393 } 394 395 previousParenthesisPos = i; 396 397 if (c == CharPool.OPEN_PARENTHESIS) { 398 levels[level] = i; 399 400 level += 1; 401 } 402 else { 403 int posOpenParenthesis = levels[level - 1]; 404 405 if (level > 1) { 406 char nextChar = ifClause.charAt(i + 1); 407 char previousChar = ifClause.charAt( 408 posOpenParenthesis - 1); 409 410 if (!Character.isLetterOrDigit(nextChar) && 411 (nextChar != CharPool.PERIOD) && 412 !Character.isLetterOrDigit(previousChar)) { 413 414 String s = ifClause.substring( 415 posOpenParenthesis + 1, i); 416 417 if (Validator.isNotNull(s) && 418 !s.contains(StringPool.SPACE)) { 419 420 _sourceFormatterHelper.printError( 421 fileName, 422 "redundant parentheses: " + fileName + " " + 423 lineCount); 424 } 425 } 426 427 if ((previousChar == CharPool.OPEN_PARENTHESIS) && 428 (nextChar == CharPool.CLOSE_PARENTHESIS)) { 429 430 _sourceFormatterHelper.printError( 431 fileName, 432 "redundant parentheses: " + fileName + " " + 433 lineCount); 434 } 435 } 436 437 level -= 1; 438 } 439 } 440 } 441 } 442 443 private static void _checkLanguageKeys( 444 String fileName, String content, Pattern pattern) 445 throws IOException { 446 447 String fileExtension = _fileUtil.getExtension(fileName); 448 449 if (!_portalSource || fileExtension.equals("vm")) { 450 return; 451 } 452 453 if (_portalLanguageKeysProperties == null) { 454 _portalLanguageKeysProperties = new Properties(); 455 456 ClassLoader classLoader = SourceFormatter.class.getClassLoader(); 457 458 InputStream inputStream = classLoader.getResourceAsStream( 459 "content/Language.properties"); 460 461 _portalLanguageKeysProperties.load(inputStream); 462 } 463 464 Matcher matcher = pattern.matcher(content); 465 466 while (matcher.find()) { 467 String[] languageKeys = _getLanguageKeys(matcher); 468 469 for (String languageKey : languageKeys) { 470 if (Validator.isNumber(languageKey) || 471 languageKey.endsWith(StringPool.DASH) || 472 languageKey.endsWith(StringPool.PERIOD) || 473 languageKey.endsWith(StringPool.UNDERLINE) || 474 languageKey.startsWith(StringPool.DASH) || 475 languageKey.startsWith(StringPool.OPEN_BRACKET) || 476 languageKey.startsWith(StringPool.OPEN_CURLY_BRACE) || 477 languageKey.startsWith(StringPool.PERIOD) || 478 languageKey.startsWith(StringPool.UNDERLINE)) { 479 480 continue; 481 } 482 483 if (!_portalLanguageKeysProperties.containsKey(languageKey)) { 484 _sourceFormatterHelper.printError( 485 fileName, 486 "missing language key: " + languageKey + 487 StringPool.SPACE + fileName); 488 } 489 } 490 } 491 } 492 493 private static void _checkMissingParentheses( 494 String s, String fileName, int lineCount) { 495 496 if (Validator.isNull(s)) { 497 return; 498 } 499 500 boolean containsAndOrOperator = (s.contains("&&") || s.contains("||")); 501 502 boolean containsCompareOperator = 503 (s.contains(" == ") || s.contains(" != ") || s.contains(" < ") || 504 s.contains(" > ") || s.contains(" =< ") || s.contains(" => ") || 505 s.contains(" <= ") || s.contains(" >= ")); 506 507 boolean containsMathOperator = 508 (s.contains(" = ") || s.contains(" - ") || s.contains(" + ") || 509 s.contains(" & ") || s.contains(" % ") || s.contains(" * ") || 510 s.contains(" / ")); 511 512 if (containsCompareOperator && 513 (containsAndOrOperator || 514 (containsMathOperator && !s.contains(StringPool.OPEN_BRACKET)))) { 515 516 _sourceFormatterHelper.printError( 517 fileName, "missing parentheses: " + fileName + " " + lineCount); 518 } 519 } 520 521 private static boolean _checkTaglibVulnerability( 522 String jspContent, String vulnerability) { 523 524 int pos1 = -1; 525 526 do { 527 pos1 = jspContent.indexOf(vulnerability, pos1 + 1); 528 529 if (pos1 != -1) { 530 int pos2 = jspContent.lastIndexOf(CharPool.LESS_THAN, pos1); 531 532 while ((pos2 > 0) && 533 (jspContent.charAt(pos2 + 1) == CharPool.PERCENT)) { 534 535 pos2 = jspContent.lastIndexOf(CharPool.LESS_THAN, pos2 - 1); 536 } 537 538 String tagContent = jspContent.substring(pos2, pos1); 539 540 if (!tagContent.startsWith("<aui:") && 541 !tagContent.startsWith("<liferay-portlet:") && 542 !tagContent.startsWith("<liferay-util:") && 543 !tagContent.startsWith("<portlet:")) { 544 545 return true; 546 } 547 } 548 } 549 while (pos1 != -1); 550 551 return false; 552 } 553 554 private static void _checkXSS(String fileName, String jspContent) { 555 Matcher matcher = _xssPattern.matcher(jspContent); 556 557 while (matcher.find()) { 558 boolean xssVulnerable = false; 559 560 String jspVariable = matcher.group(1); 561 562 String anchorVulnerability = " href=\"<%= " + jspVariable + " %>"; 563 564 if (_checkTaglibVulnerability(jspContent, anchorVulnerability)) { 565 xssVulnerable = true; 566 } 567 568 String inputVulnerability = " value=\"<%= " + jspVariable + " %>"; 569 570 if (_checkTaglibVulnerability(jspContent, inputVulnerability)) { 571 xssVulnerable = true; 572 } 573 574 String inlineStringVulnerability1 = "'<%= " + jspVariable + " %>"; 575 576 if (jspContent.contains(inlineStringVulnerability1)) { 577 xssVulnerable = true; 578 } 579 580 String inlineStringVulnerability2 = "(\"<%= " + jspVariable + " %>"; 581 582 if (jspContent.contains(inlineStringVulnerability2)) { 583 xssVulnerable = true; 584 } 585 586 String inlineStringVulnerability3 = " \"<%= " + jspVariable + " %>"; 587 588 if (jspContent.contains(inlineStringVulnerability3)) { 589 xssVulnerable = true; 590 } 591 592 String documentIdVulnerability = ".<%= " + jspVariable + " %>"; 593 594 if (jspContent.contains(documentIdVulnerability)) { 595 xssVulnerable = true; 596 } 597 598 if (xssVulnerable) { 599 _sourceFormatterHelper.printError( 600 fileName, "(xss): " + fileName + " (" + jspVariable + ")"); 601 } 602 } 603 } 604 605 private static String _fixAntXMLProjectName( 606 String basedir, String fileName, String content) 607 throws IOException { 608 609 int x = 0; 610 611 if (fileName.endsWith("-ext/build.xml")) { 612 x = fileName.indexOf("ext/"); 613 614 if (x == -1) { 615 x = 0; 616 } 617 else { 618 x = x + 4; 619 } 620 } 621 else if (fileName.endsWith("-hook/build.xml")) { 622 x = fileName.indexOf("hooks/"); 623 624 if (x == -1) { 625 x = 0; 626 } 627 else { 628 x = x + 6; 629 } 630 } 631 else if (fileName.endsWith("-layouttpl/build.xml")) { 632 x = fileName.indexOf("layouttpl/"); 633 634 if (x == -1) { 635 x = 0; 636 } 637 else { 638 x = x + 10; 639 } 640 } 641 else if (fileName.endsWith("-portlet/build.xml")) { 642 x = fileName.indexOf("portlets/"); 643 644 if (x == -1) { 645 x = 0; 646 } 647 else { 648 x = x + 9; 649 } 650 } 651 else if (fileName.endsWith("-theme/build.xml")) { 652 x = fileName.indexOf("themes/"); 653 654 if (x == -1) { 655 x = 0; 656 } 657 else { 658 x = x + 7; 659 } 660 } 661 else if (fileName.endsWith("-web/build.xml") && 662 !fileName.endsWith("/ext-web/build.xml")) { 663 664 x = fileName.indexOf("webs/"); 665 666 if (x == -1) { 667 x = 0; 668 } 669 else { 670 x = x + 5; 671 } 672 } 673 else { 674 return content; 675 } 676 677 int y = fileName.indexOf("/", x); 678 679 String correctProjectElementText = 680 "<project name=\"" + fileName.substring(x, y) + "\""; 681 682 if (!content.contains(correctProjectElementText)) { 683 x = content.indexOf("<project name=\""); 684 685 y = content.indexOf("\"", x) + 1; 686 y = content.indexOf("\"", y) + 1; 687 688 content = 689 content.substring(0, x) + correctProjectElementText + 690 content.substring(y); 691 692 _sourceFormatterHelper.printError( 693 fileName, fileName + " has an incorrect project name"); 694 695 _fileUtil.write(basedir + fileName, content); 696 } 697 698 return content; 699 } 700 701 private static String _fixDataAccessConnection( 702 String className, String content) { 703 704 int x = content.indexOf("package "); 705 706 int y = content.indexOf(CharPool.SEMICOLON, x); 707 708 if ((x == -1) || (y == -1)) { 709 return content; 710 } 711 712 String packageName = content.substring(x + 8, y); 713 714 if (!packageName.startsWith("com.liferay.portal.kernel.upgrade") && 715 !packageName.startsWith("com.liferay.portal.kernel.verify") && 716 !packageName.startsWith("com.liferay.portal.upgrade") && 717 !packageName.startsWith("com.liferay.portal.verify")) { 718 719 return content; 720 } 721 722 content = StringUtil.replace( 723 content, "DataAccess.getConnection", 724 "DataAccess.getUpgradeOptimizedConnection"); 725 726 return content; 727 } 728 729 private static String _fixSessionKey( 730 String fileName, String content, Pattern pattern) { 731 732 Matcher matcher = pattern.matcher(content); 733 734 if (!matcher.find()) { 735 return content; 736 } 737 738 String newContent = content; 739 740 do { 741 String match = matcher.group(); 742 743 String s = null; 744 745 if (pattern.equals(_sessionKeyPattern)) { 746 s = StringPool.COMMA; 747 } 748 else if (pattern.equals(_taglibSessionKeyPattern)) { 749 s = "key="; 750 } 751 752 int x = match.indexOf(s); 753 754 if (x == -1) { 755 continue; 756 } 757 758 x = x + s.length(); 759 760 String substring = match.substring(x).trim(); 761 762 String quote = StringPool.BLANK; 763 764 if (substring.startsWith(StringPool.APOSTROPHE)) { 765 quote = StringPool.APOSTROPHE; 766 } 767 else if (substring.startsWith(StringPool.QUOTE)) { 768 quote = StringPool.QUOTE; 769 } 770 else { 771 continue; 772 } 773 774 int y = match.indexOf(quote, x); 775 int z = match.indexOf(quote, y + 1); 776 777 if ((y == -1) || (z == -1)) { 778 continue; 779 } 780 781 String prefix = match.substring(0, y + 1); 782 String suffix = match.substring(z); 783 String oldKey = match.substring(y + 1, z); 784 785 boolean alphaNumericKey = true; 786 787 for (char c : oldKey.toCharArray()) { 788 if (!Validator.isChar(c) && !Validator.isDigit(c) && 789 (c != CharPool.DASH) && (c != CharPool.UNDERLINE)) { 790 791 alphaNumericKey = false; 792 } 793 } 794 795 if (!alphaNumericKey) { 796 continue; 797 } 798 799 String newKey = TextFormatter.format(oldKey, TextFormatter.O); 800 801 newKey = TextFormatter.format(newKey, TextFormatter.M); 802 803 if (newKey.equals(oldKey)) { 804 continue; 805 } 806 807 String oldSub = prefix.concat(oldKey).concat(suffix); 808 String newSub = prefix.concat(newKey).concat(suffix); 809 810 newContent = StringUtil.replaceFirst(newContent, oldSub, newSub); 811 } 812 while (matcher.find()); 813 814 return newContent; 815 } 816 817 private static void _formatAntXML() throws DocumentException, IOException { 818 String basedir = "./"; 819 820 DirectoryScanner directoryScanner = new DirectoryScanner(); 821 822 directoryScanner.setBasedir(basedir); 823 directoryScanner.setIncludes(new String[] {"**\\b*.xml"}); 824 directoryScanner.setExcludes(new String[] {"**\\tools\\**"}); 825 826 List<String> fileNames = _sourceFormatterHelper.scanForFiles( 827 directoryScanner); 828 829 for (String fileName : fileNames) { 830 File file = new File(basedir + fileName); 831 832 String content = _fileUtil.read(file); 833 834 String newContent = _trimContent(content); 835 836 newContent = _fixAntXMLProjectName(basedir, fileName, newContent); 837 838 Document document = _saxReaderUtil.read(newContent); 839 840 Element rootElement = document.getRootElement(); 841 842 String previousName = StringPool.BLANK; 843 844 List<Element> targetElements = rootElement.elements("target"); 845 846 for (Element targetElement : targetElements) { 847 String name = targetElement.attributeValue("name"); 848 849 if (name.equals("Test")) { 850 name = name.toLowerCase(); 851 } 852 853 if (name.compareTo(previousName) < -1) { 854 _sourceFormatterHelper.printError( 855 fileName, 856 fileName + " has an unordered target " + name); 857 858 break; 859 } 860 861 previousName = name; 862 } 863 864 if ((newContent != null) && !content.equals(newContent)) { 865 _fileUtil.write(file, newContent); 866 867 _sourceFormatterHelper.printError(fileName, file); 868 } 869 } 870 } 871 872 private static void _formatDDLStructuresXML() 873 throws DocumentException, IOException { 874 875 String basedir = 876 "./portal-impl/src/com/liferay/portal/events/dependencies/"; 877 878 if (!_fileUtil.exists(basedir)) { 879 return; 880 } 881 882 DirectoryScanner directoryScanner = new DirectoryScanner(); 883 884 directoryScanner.setBasedir(basedir); 885 directoryScanner.setIncludes(new String[] {"**\\*structures.xml"}); 886 887 List<String> fileNames = _sourceFormatterHelper.scanForFiles( 888 directoryScanner); 889 890 for (String fileName : fileNames) { 891 File file = new File(basedir + fileName); 892 893 String content = _fileUtil.read(file); 894 895 String newContent = _trimContent(content); 896 897 newContent = _formatDDLStructuresXML(content); 898 899 if ((newContent != null) && !content.equals(newContent)) { 900 _fileUtil.write(file, newContent); 901 902 _sourceFormatterHelper.printError(fileName, file); 903 } 904 } 905 } 906 907 private static String _formatDDLStructuresXML(String content) 908 throws DocumentException, IOException { 909 910 Document document = _saxReaderUtil.read(content); 911 912 Element rootElement = document.getRootElement(); 913 914 rootElement.sortAttributes(true); 915 916 rootElement.sortElementsByChildElement("structure", "name"); 917 918 List<Element> structureElements = rootElement.elements("structure"); 919 920 for (Element structureElement : structureElements) { 921 Element structureRootElement = structureElement.element("root"); 922 923 structureRootElement.sortElementsByAttribute( 924 "dynamic-element", "name"); 925 926 List<Element> dynamicElementElements = 927 structureRootElement.elements("dynamic-element"); 928 929 for (Element dynamicElementElement : dynamicElementElements) { 930 Element metaDataElement = dynamicElementElement.element( 931 "meta-data"); 932 933 metaDataElement.sortElementsByAttribute("entry", "name"); 934 } 935 } 936 937 return document.formattedString(); 938 } 939 940 private static void _formatFriendlyURLRoutesXML() 941 throws DocumentException, IOException { 942 943 String basedir = "./"; 944 945 DirectoryScanner directoryScanner = new DirectoryScanner(); 946 947 directoryScanner.setBasedir(basedir); 948 directoryScanner.setIncludes(new String[] {"**\\*routes.xml"}); 949 directoryScanner.setExcludes( 950 new String[] {"**\\classes\\**", "**\\bin\\**"}); 951 952 List<String> fileNames = _sourceFormatterHelper.scanForFiles( 953 directoryScanner); 954 955 for (String fileName : fileNames) { 956 File file = new File(basedir + fileName); 957 958 String content = _fileUtil.read(file); 959 960 if (content.contains("<!-- SourceFormatter.Ignore -->")) { 961 continue; 962 } 963 964 String newContent = _trimContent(content); 965 966 newContent = _formatFriendlyURLRoutesXML(content); 967 968 if ((newContent != null) && !content.equals(newContent)) { 969 _fileUtil.write(file, newContent); 970 971 _sourceFormatterHelper.printError(fileName, file); 972 } 973 } 974 } 975 976 private static String _formatFriendlyURLRoutesXML(String content) 977 throws DocumentException { 978 979 Document document = _saxReaderUtil.read(content); 980 981 Element rootElement = document.getRootElement(); 982 983 List<ComparableRoute> comparableRoutes = 984 new ArrayList<ComparableRoute>(); 985 986 for (Element routeElement : rootElement.elements("route")) { 987 String pattern = routeElement.elementText("pattern"); 988 989 ComparableRoute comparableRoute = new ComparableRoute(pattern); 990 991 for (Element generatedParameterElement : 992 routeElement.elements("generated-parameter")) { 993 994 String name = generatedParameterElement.attributeValue("name"); 995 String value = generatedParameterElement.getText(); 996 997 comparableRoute.addGeneratedParameter(name, value); 998 } 999 1000 for (Element ignoredParameterElement : 1001 routeElement.elements("ignored-parameter")) { 1002 1003 String name = ignoredParameterElement.attributeValue("name"); 1004 1005 comparableRoute.addIgnoredParameter(name); 1006 } 1007 1008 for (Element implicitParameterElement : 1009 routeElement.elements("implicit-parameter")) { 1010 1011 String name = implicitParameterElement.attributeValue("name"); 1012 String value = implicitParameterElement.getText(); 1013 1014 comparableRoute.addImplicitParameter(name, value); 1015 } 1016 1017 for (Element overriddenParameterElement : 1018 routeElement.elements("overridden-parameter")) { 1019 1020 String name = overriddenParameterElement.attributeValue("name"); 1021 String value = overriddenParameterElement.getText(); 1022 1023 comparableRoute.addOverriddenParameter(name, value); 1024 } 1025 1026 comparableRoutes.add(comparableRoute); 1027 } 1028 1029 Collections.sort(comparableRoutes); 1030 1031 StringBundler sb = new StringBundler(); 1032 1033 sb.append("<?xml version=\"1.0\"?>\n"); 1034 sb.append("<!DOCTYPE routes PUBLIC \"-//Liferay//DTD Friendly URL "); 1035 sb.append("Routes 6.2.0//EN\" \"http://www.liferay.com/dtd/"); 1036 sb.append("liferay-friendly-url-routes_6_2_0.dtd\">\n\n<routes>\n"); 1037 1038 for (ComparableRoute comparableRoute : comparableRoutes) { 1039 sb.append("\t<route>\n"); 1040 sb.append("\t\t<pattern>"); 1041 sb.append(comparableRoute.getPattern()); 1042 sb.append("</pattern>\n"); 1043 1044 Map<String, String> generatedParameters = 1045 comparableRoute.getGeneratedParameters(); 1046 1047 for (Map.Entry<String, String> entry : 1048 generatedParameters.entrySet()) { 1049 1050 sb.append("\t\t<generated-parameter name=\""); 1051 sb.append(entry.getKey()); 1052 sb.append("\">"); 1053 sb.append(entry.getValue()); 1054 sb.append("</generated-parameter>\n"); 1055 } 1056 1057 Set<String> ignoredParameters = 1058 comparableRoute.getIgnoredParameters(); 1059 1060 for (String entry : ignoredParameters) { 1061 sb.append("\t\t<ignored-parameter name=\""); 1062 sb.append(entry); 1063 sb.append("\" />\n"); 1064 } 1065 1066 Map<String, String> implicitParameters = 1067 comparableRoute.getImplicitParameters(); 1068 1069 for (Map.Entry<String, String> entry : 1070 implicitParameters.entrySet()) { 1071 1072 sb.append("\t\t<implicit-parameter name=\""); 1073 sb.append(entry.getKey()); 1074 sb.append("\">"); 1075 sb.append(entry.getValue()); 1076 sb.append("</implicit-parameter>\n"); 1077 } 1078 1079 Map<String, String> overriddenParameters = 1080 comparableRoute.getOverriddenParameters(); 1081 1082 for (Map.Entry<String, String> entry : 1083 overriddenParameters.entrySet()) { 1084 1085 sb.append("\t\t<overridden-parameter name=\""); 1086 sb.append(entry.getKey()); 1087 sb.append("\">"); 1088 sb.append(entry.getValue()); 1089 sb.append("</overridden-parameter>\n"); 1090 } 1091 1092 sb.append("\t</route>\n"); 1093 } 1094 1095 sb.append("</routes>"); 1096 1097 return sb.toString(); 1098 } 1099 1100 private static void _formatFTL() throws IOException { 1101 String basedir = "./"; 1102 1103 DirectoryScanner directoryScanner = new DirectoryScanner(); 1104 1105 directoryScanner.setBasedir(basedir); 1106 directoryScanner.setIncludes(new String[] {"**\\*.ftl"}); 1107 directoryScanner.setExcludes( 1108 new String[] { 1109 "**\\journal\\dependencies\\template.ftl", 1110 "**\\servicebuilder\\dependencies\\props.ftl" 1111 }); 1112 1113 List<String> fileNames = _sourceFormatterHelper.scanForFiles( 1114 directoryScanner); 1115 1116 for (String fileName : fileNames) { 1117 File file = new File(basedir + fileName); 1118 1119 String content = _fileUtil.read(file); 1120 1121 String newContent = _trimContent(content); 1122 1123 if ((newContent != null) && !content.equals(newContent)) { 1124 _fileUtil.write(file, newContent); 1125 1126 _sourceFormatterHelper.printError(fileName, file); 1127 } 1128 } 1129 } 1130 1131 private static String _formatImports(String imports, int classStartPos) 1132 throws IOException { 1133 1134 if (imports.contains("/*") || imports.contains("*/") || 1135 imports.contains("//")) { 1136 1137 return imports + "\n"; 1138 } 1139 1140 List<String> importsList = new ArrayList<String>(); 1141 1142 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader( 1143 new UnsyncStringReader(imports)); 1144 1145 String line = null; 1146 1147 while ((line = unsyncBufferedReader.readLine()) != null) { 1148 if ((line.contains("import=") || line.contains("import ")) && 1149 !importsList.contains(line)) { 1150 1151 importsList.add(line); 1152 } 1153 } 1154 1155 importsList = ListUtil.sort(importsList); 1156 1157 StringBundler sb = new StringBundler(); 1158 1159 String temp = null; 1160 1161 for (int i = 0; i < importsList.size(); i++) { 1162 String s = importsList.get(i); 1163 1164 int pos = s.indexOf("."); 1165 1166 pos = s.indexOf(".", pos + 1); 1167 1168 if (pos == -1) { 1169 pos = s.indexOf("."); 1170 } 1171 1172 String packageLevel = s.substring(classStartPos, pos); 1173 1174 if ((i != 0) && !packageLevel.equals(temp)) { 1175 sb.append("\n"); 1176 } 1177 1178 temp = packageLevel; 1179 1180 sb.append(s); 1181 sb.append("\n"); 1182 } 1183 1184 return sb.toString(); 1185 } 1186 1187 private static void _formatJava() throws IOException { 1188 String copyright = _getCopyright(); 1189 String oldCopyright = _getOldCopyright(); 1190 1191 boolean portalJavaFiles = true; 1192 1193 Collection<String> fileNames = null; 1194 1195 if (_portalSource) { 1196 fileNames = _getPortalJavaFiles(); 1197 1198 _javaTermSortExclusions = _getPortalExclusionsProperties( 1199 "source_formatter_javaterm_sort_exclusions.properties"); 1200 _lineLengthExclusions = _getPortalExclusionsProperties( 1201 "source_formatter_line_length_exclusions.properties"); 1202 } 1203 else { 1204 portalJavaFiles = false; 1205 1206 fileNames = _getPluginJavaFiles(); 1207 1208 _javaTermSortExclusions = _getPluginExclusionsProperties( 1209 "source_formatter_javaterm_sort_exclusions.properties"); 1210 _lineLengthExclusions = _getPluginExclusionsProperties( 1211 "source_formatter_line_length_exclusions.properties"); 1212 } 1213 1214 for (String fileName : fileNames) { 1215 File file = new File(fileName); 1216 1217 String content = _fileUtil.read(file); 1218 1219 if (_isGenerated(content)) { 1220 continue; 1221 } 1222 1223 String className = file.getName(); 1224 1225 className = className.substring(0, className.length() - 5); 1226 1227 String packagePath = fileName; 1228 1229 int packagePathX = packagePath.indexOf( 1230 File.separator + "src" + File.separator); 1231 int packagePathY = packagePath.lastIndexOf(File.separator); 1232 1233 if ((packagePathX + 5) >= packagePathY) { 1234 packagePath = StringPool.BLANK; 1235 } 1236 else { 1237 packagePath = packagePath.substring( 1238 packagePathX + 5, packagePathY); 1239 } 1240 1241 packagePath = StringUtil.replace( 1242 packagePath, File.separator, StringPool.PERIOD); 1243 1244 if (packagePath.endsWith(".model")) { 1245 if (content.contains("extends " + className + "Model")) { 1246 continue; 1247 } 1248 } 1249 1250 String newContent = content; 1251 1252 if (newContent.contains("$\n */")) { 1253 _sourceFormatterHelper.printError(fileName, "*: " + fileName); 1254 1255 newContent = StringUtil.replace( 1256 newContent, "$\n */", "$\n *\n */"); 1257 } 1258 1259 if ((oldCopyright != null) && newContent.contains(oldCopyright)) { 1260 newContent = StringUtil.replace( 1261 newContent, oldCopyright, copyright); 1262 1263 _sourceFormatterHelper.printError( 1264 fileName, "old (c): " + fileName); 1265 } 1266 1267 if (!newContent.contains(copyright)) { 1268 String customCopyright = _getCustomCopyright(file); 1269 1270 if (Validator.isNull(customCopyright) || 1271 !newContent.contains(customCopyright)) { 1272 1273 _sourceFormatterHelper.printError( 1274 fileName, "(c): " + fileName); 1275 } 1276 } 1277 1278 if (newContent.contains(className + ".java.html")) { 1279 _sourceFormatterHelper.printError( 1280 fileName, "Java2HTML: " + fileName); 1281 } 1282 1283 if (newContent.contains(" * @author Raymond Aug") && 1284 !newContent.contains(" * @author Raymond Aug\u00e9")) { 1285 1286 newContent = newContent.replaceFirst( 1287 "Raymond Aug.++", "Raymond Aug\u00e9"); 1288 1289 _sourceFormatterHelper.printError( 1290 fileName, "UTF-8: " + fileName); 1291 } 1292 1293 newContent = _fixDataAccessConnection(className, newContent); 1294 newContent = _fixSessionKey( 1295 fileName, newContent, _sessionKeyPattern); 1296 1297 newContent = StringUtil.replace( 1298 newContent, 1299 new String[] { 1300 "com.liferay.portal.PortalException", 1301 "com.liferay.portal.SystemException", 1302 "com.liferay.util.LocalizationUtil", 1303 "private static final Log _log" 1304 }, 1305 new String[] { 1306 "com.liferay.portal.kernel.exception.PortalException", 1307 "com.liferay.portal.kernel.exception.SystemException", 1308 "com.liferay.portal.kernel.util.LocalizationUtil", 1309 "private static Log _log" 1310 }); 1311 1312 newContent = stripJavaImports(newContent, packagePath, className); 1313 1314 newContent = StringUtil.replace( 1315 newContent, 1316 new String[] { 1317 ";\n/**", "\t/*\n\t *", "catch(", "else{", "if(", "for(", 1318 "while(", "List <", "){\n", "]{\n", "\n\n\n" 1319 }, 1320 new String[] { 1321 ";\n\n/**", "\t/**\n\t *", "catch (", "else {", "if (", 1322 "for (", "while (", "List<", ") {\n", "] {\n", "\n\n" 1323 }); 1324 1325 if (newContent.contains("*/\npackage ")) { 1326 _sourceFormatterHelper.printError( 1327 fileName, "package: " + fileName); 1328 } 1329 1330 if (!newContent.endsWith("\n\n}") && !newContent.endsWith("{\n}")) { 1331 _sourceFormatterHelper.printError(fileName, "}: " + fileName); 1332 } 1333 1334 if (portalJavaFiles && !className.equals("BaseServiceImpl") && 1335 className.endsWith("ServiceImpl") && 1336 newContent.contains("ServiceUtil.")) { 1337 1338 _sourceFormatterHelper.printError( 1339 fileName, "ServiceUtil: " + fileName); 1340 } 1341 1342 if (!className.equals("DeepNamedValueScanner") && 1343 !className.equals("ProxyUtil") && 1344 newContent.contains("import java.lang.reflect.Proxy;")) { 1345 1346 _sourceFormatterHelper.printError( 1347 fileName, "Proxy: " + fileName); 1348 } 1349 1350 // LPS-28266 1351 1352 for (int pos1 = -1;;) { 1353 pos1 = newContent.indexOf(StringPool.TAB + "try {", pos1 + 1); 1354 1355 if (pos1 == -1) { 1356 break; 1357 } 1358 1359 int pos2 = newContent.indexOf( 1360 StringPool.TAB + "try {", pos1 + 1); 1361 int pos3 = newContent.indexOf("\"select count(", pos1); 1362 1363 if ((pos2 != -1) && (pos3 != -1) && (pos2 < pos3)) { 1364 continue; 1365 } 1366 1367 int pos4 = newContent.indexOf("rs.getLong(1)", pos1); 1368 int pos5 = newContent.indexOf( 1369 StringPool.TAB + "finally {", pos1); 1370 1371 if ((pos3 == -1) || (pos4 == -1) || (pos5 == -1)) { 1372 break; 1373 } 1374 1375 if ((pos3 < pos4) && (pos4 < pos5)) { 1376 _sourceFormatterHelper.printError( 1377 fileName, "Use getInt(1) for count: " + fileName); 1378 } 1379 } 1380 1381 // LPS-33070 1382 1383 if (content.contains("implements ProcessCallable") && 1384 !content.contains( 1385 "private static final long serialVersionUID")) { 1386 1387 _sourceFormatterHelper.printError( 1388 fileName, 1389 "Assign ProcessCallable implementation a " + 1390 "serialVersionUID: " + fileName); 1391 } 1392 1393 _checkLanguageKeys(fileName, newContent, _languageKeyPattern); 1394 1395 String oldContent = newContent; 1396 1397 for (;;) { 1398 newContent = _formatJava(fileName, oldContent); 1399 1400 if (oldContent.equals(newContent)) { 1401 break; 1402 } 1403 1404 oldContent = newContent; 1405 } 1406 1407 if ((newContent != null) && !content.equals(newContent)) { 1408 _fileUtil.write(file, newContent); 1409 1410 _sourceFormatterHelper.printError(fileName, file); 1411 } 1412 } 1413 } 1414 1415 private static String _formatJava(String fileName, String content) 1416 throws IOException { 1417 1418 StringBundler sb = new StringBundler(); 1419 1420 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader( 1421 new UnsyncStringReader(content)); 1422 1423 int index = 0; 1424 int lineCount = 0; 1425 1426 String line = null; 1427 1428 String previousLine = StringPool.BLANK; 1429 1430 int lineToSkipIfEmpty = 0; 1431 1432 String javaTermName = null; 1433 int javaTermPos = -1; 1434 int javaTermType = 0; 1435 1436 int nextJavaTermPos = -1; 1437 int nextJavaTermType = 0; 1438 1439 String previousJavaTermName = null; 1440 int previousJavaTermPos = -1; 1441 int previousJavaTermType = 0; 1442 1443 List<String> parameterTypes = new ArrayList<String>(); 1444 List<String> previousParameterTypes = null; 1445 1446 int lastCommentOrAnnotationPos = -1; 1447 1448 boolean hasSameConstructorOrMethodName = false; 1449 boolean readParameterTypes = false; 1450 1451 boolean hasUnsortedJavaTerms = false; 1452 1453 String ifClause = StringPool.BLANK; 1454 1455 String packageName = StringPool.BLANK; 1456 1457 while ((line = unsyncBufferedReader.readLine()) != null) { 1458 lineCount++; 1459 1460 line = _trimLine(line); 1461 1462 if (Validator.isNull(line)) { 1463 lastCommentOrAnnotationPos = -1; 1464 } 1465 1466 line = StringUtil.replace( 1467 line, 1468 new String[] { 1469 "* Copyright (c) 2000-2011 Liferay, Inc." 1470 }, 1471 new String[] { 1472 "* Copyright (c) 2000-2012 Liferay, Inc." 1473 }); 1474 1475 if (line.startsWith("package ")) { 1476 packageName = line.substring(8, line.length() - 1); 1477 } 1478 1479 if (line.startsWith("import ")) { 1480 if (line.endsWith(".*;")) { 1481 _sourceFormatterHelper.printError( 1482 fileName, "import: " + fileName + " " + lineCount); 1483 } 1484 1485 int pos = line.lastIndexOf(StringPool.PERIOD); 1486 1487 if (pos != -1) { 1488 String importPackageName = line.substring(7, pos); 1489 1490 if (importPackageName.equals(packageName)) { 1491 continue; 1492 } 1493 } 1494 } 1495 1496 if (line.contains(StringPool.TAB + "for (") && line.contains(":") && 1497 !line.contains(" :")) { 1498 1499 line = StringUtil.replace(line, ":" , " :"); 1500 } 1501 1502 line = _replacePrimitiveWrapperInstantiation( 1503 fileName, line, lineCount); 1504 1505 String trimmedLine = StringUtil.trimLeading(line); 1506 1507 if (trimmedLine.startsWith(StringPool.EQUAL)) { 1508 _sourceFormatterHelper.printError( 1509 fileName, "equal: " + fileName + " " + lineCount); 1510 } 1511 1512 if (!trimmedLine.equals("{") && line.endsWith("{") && 1513 !line.endsWith(" {")) { 1514 1515 line = StringUtil.replaceLast(line, "{", " {"); 1516 } 1517 1518 line = _sortExceptions(line); 1519 1520 if (trimmedLine.startsWith("if (") || 1521 trimmedLine.startsWith("else if (") || 1522 trimmedLine.startsWith("while (") || 1523 Validator.isNotNull(ifClause)) { 1524 1525 if (Validator.isNull(ifClause) || 1526 ifClause.endsWith(StringPool.OPEN_PARENTHESIS)) { 1527 1528 ifClause = ifClause + trimmedLine; 1529 } 1530 else { 1531 ifClause = ifClause + StringPool.SPACE + trimmedLine; 1532 } 1533 1534 if (ifClause.endsWith(") {")) { 1535 _checkIfClause(ifClause, fileName, lineCount); 1536 1537 ifClause = StringPool.BLANK; 1538 } 1539 } 1540 1541 String excluded = null; 1542 1543 String fileNameWithForwardSlashes = StringUtil.replace( 1544 fileName, "\\", "/"); 1545 1546 if (line.startsWith(StringPool.TAB + "private ") || 1547 line.startsWith(StringPool.TAB + "protected ") || 1548 line.startsWith(StringPool.TAB + "public ")) { 1549 1550 Tuple tuple = _getJavaTermTuple(line); 1551 1552 if (hasUnsortedJavaTerms) { 1553 if (nextJavaTermPos == -1) { 1554 if (lastCommentOrAnnotationPos == -1) { 1555 nextJavaTermPos = index; 1556 } 1557 else { 1558 nextJavaTermPos = lastCommentOrAnnotationPos; 1559 } 1560 1561 if (tuple != null) { 1562 nextJavaTermType = (Integer)tuple.getObject(1); 1563 } 1564 } 1565 } 1566 else { 1567 hasSameConstructorOrMethodName = false; 1568 1569 if (tuple != null) { 1570 javaTermName = (String)tuple.getObject(0); 1571 1572 if (lastCommentOrAnnotationPos == -1) { 1573 javaTermPos = index; 1574 } 1575 else { 1576 javaTermPos = lastCommentOrAnnotationPos; 1577 } 1578 1579 if (Validator.isNotNull(javaTermName)) { 1580 javaTermType = (Integer)tuple.getObject(1); 1581 1582 boolean isConstructorOrMethod = 1583 _isInJavaTermTypeGroup( 1584 javaTermType, _TYPE_CONSTRUCTOR) || 1585 _isInJavaTermTypeGroup( 1586 javaTermType, _TYPE_METHOD); 1587 1588 if (isConstructorOrMethod) { 1589 readParameterTypes = true; 1590 } 1591 1592 if (_javaTermSortExclusions != null) { 1593 excluded = _javaTermSortExclusions.getProperty( 1594 fileNameWithForwardSlashes + StringPool.AT + 1595 lineCount); 1596 1597 if (excluded == null) { 1598 excluded = 1599 _javaTermSortExclusions.getProperty( 1600 fileNameWithForwardSlashes + 1601 StringPool.AT + javaTermName); 1602 } 1603 1604 if (excluded == null) { 1605 excluded = 1606 _javaTermSortExclusions.getProperty( 1607 fileNameWithForwardSlashes); 1608 } 1609 } 1610 1611 if (excluded == null) { 1612 if (_isInJavaTermTypeGroup( 1613 javaTermType, 1614 _TYPE_VARIABLE_NOT_FINAL)) { 1615 1616 char firstChar = javaTermName.charAt(0); 1617 1618 if (firstChar == CharPool.UNDERLINE) { 1619 firstChar = javaTermName.charAt(1); 1620 } 1621 1622 if (Character.isUpperCase(firstChar)) { 1623 _sourceFormatterHelper.printError( 1624 fileName, 1625 "final: " + fileName + " " + 1626 lineCount); 1627 } 1628 } 1629 1630 if (Validator.isNotNull(previousJavaTermName)) { 1631 if (previousJavaTermType > javaTermType) { 1632 hasUnsortedJavaTerms = true; 1633 } 1634 else if (previousJavaTermType == 1635 javaTermType) { 1636 1637 if (isConstructorOrMethod && 1638 previousJavaTermName.equals( 1639 javaTermName)) { 1640 1641 hasSameConstructorOrMethodName = 1642 true; 1643 } 1644 else if (_hasUnsortedJavaTerms( 1645 fileName, 1646 previousJavaTermName, 1647 javaTermName, 1648 javaTermType)) { 1649 1650 hasUnsortedJavaTerms = true; 1651 } 1652 } 1653 } 1654 } 1655 1656 if (!hasUnsortedJavaTerms && !readParameterTypes) { 1657 previousJavaTermName = javaTermName; 1658 previousJavaTermPos = javaTermPos; 1659 previousJavaTermType = javaTermType; 1660 } 1661 } 1662 } 1663 } 1664 } 1665 else if (line.startsWith(StringPool.TAB + "/**") || 1666 line.startsWith(StringPool.TAB + "@")) { 1667 1668 if (lastCommentOrAnnotationPos == -1) { 1669 lastCommentOrAnnotationPos = index; 1670 } 1671 } 1672 1673 if (readParameterTypes) { 1674 parameterTypes = _addParameterTypes( 1675 trimmedLine, parameterTypes); 1676 1677 if (trimmedLine.contains(StringPool.CLOSE_PARENTHESIS)) { 1678 if (hasSameConstructorOrMethodName) { 1679 if (_hasUnsortedConstructorsOrMethods( 1680 previousParameterTypes, parameterTypes)) { 1681 1682 hasUnsortedJavaTerms = true; 1683 } 1684 } 1685 1686 readParameterTypes = false; 1687 1688 previousParameterTypes = ListUtil.copy(parameterTypes); 1689 1690 parameterTypes.clear(); 1691 1692 if (!hasUnsortedJavaTerms) { 1693 previousJavaTermName = javaTermName; 1694 previousJavaTermPos = javaTermPos; 1695 previousJavaTermType = javaTermType; 1696 } 1697 } 1698 } 1699 1700 if (!trimmedLine.contains(StringPool.DOUBLE_SLASH) && 1701 !trimmedLine.startsWith(StringPool.STAR)) { 1702 1703 while (trimmedLine.contains(StringPool.TAB)) { 1704 line = StringUtil.replaceLast( 1705 line, StringPool.TAB, StringPool.SPACE); 1706 1707 trimmedLine = StringUtil.replaceLast( 1708 trimmedLine, StringPool.TAB, StringPool.SPACE); 1709 } 1710 1711 if (line.contains(StringPool.TAB + StringPool.SPACE) && 1712 !previousLine.endsWith("&&") && 1713 !previousLine.endsWith("||") && 1714 !previousLine.contains(StringPool.TAB + "((") && 1715 !previousLine.contains(StringPool.TAB + StringPool.SPACE) && 1716 !previousLine.contains(StringPool.TAB + "implements ") && 1717 !previousLine.contains(StringPool.TAB + "throws ")) { 1718 1719 line = StringUtil.replace( 1720 line, StringPool.TAB + StringPool.SPACE, 1721 StringPool.TAB); 1722 } 1723 1724 while (trimmedLine.contains(StringPool.DOUBLE_SPACE) && 1725 !trimmedLine.contains( 1726 StringPool.QUOTE + StringPool.DOUBLE_SPACE) && 1727 !fileName.contains("Test")) { 1728 1729 line = StringUtil.replaceLast( 1730 line, StringPool.DOUBLE_SPACE, StringPool.SPACE); 1731 1732 trimmedLine = StringUtil.replaceLast( 1733 trimmedLine, StringPool.DOUBLE_SPACE, StringPool.SPACE); 1734 } 1735 1736 if (!line.contains(StringPool.QUOTE)) { 1737 int pos = line.indexOf(") "); 1738 1739 if (pos != -1) { 1740 String linePart = line.substring(pos + 2); 1741 1742 if (Character.isLetter(linePart.charAt(0)) && 1743 !linePart.startsWith("default") && 1744 !linePart.startsWith("instanceof") && 1745 !linePart.startsWith("throws")) { 1746 1747 line = StringUtil.replaceLast( 1748 line, StringPool.SPACE + linePart, linePart); 1749 } 1750 } 1751 1752 if ((trimmedLine.startsWith("private ") || 1753 trimmedLine.startsWith("protected ") || 1754 trimmedLine.startsWith("public ")) && 1755 line.contains(" (")) { 1756 1757 line = StringUtil.replace(line, " (", "("); 1758 } 1759 1760 if (line.contains(" [")) { 1761 line = StringUtil.replace(line, " [", "["); 1762 } 1763 1764 for (int x = -1;;) { 1765 int posComma = line.indexOf(StringPool.COMMA, x + 1); 1766 int posSemicolon = line.indexOf( 1767 StringPool.SEMICOLON, x + 1); 1768 1769 if ((posComma == -1) && (posSemicolon == -1)) { 1770 break; 1771 } 1772 1773 x = Math.min(posComma, posSemicolon); 1774 1775 if (x == -1) { 1776 x = Math.max(posComma, posSemicolon); 1777 } 1778 1779 if (line.length() > (x + 1)) { 1780 char nextChar = line.charAt(x + 1); 1781 1782 if ((nextChar != CharPool.APOSTROPHE) && 1783 (nextChar != CharPool.CLOSE_PARENTHESIS) && 1784 (nextChar != CharPool.SPACE) && 1785 (nextChar != CharPool.STAR)) { 1786 1787 line = StringUtil.insert( 1788 line, StringPool.SPACE, x + 1); 1789 } 1790 } 1791 1792 if (x > 0) { 1793 char previousChar = line.charAt(x - 1); 1794 1795 if (previousChar == CharPool.SPACE) { 1796 line = line.substring(0, x - 1).concat( 1797 line.substring(x)); 1798 } 1799 } 1800 } 1801 } 1802 } 1803 1804 if ((line.contains(" && ") || line.contains(" || ")) && 1805 line.endsWith(StringPool.OPEN_PARENTHESIS)) { 1806 1807 _sourceFormatterHelper.printError( 1808 fileName, "line break: " + fileName + " " + lineCount); 1809 } 1810 1811 if (trimmedLine.endsWith(StringPool.PLUS) && 1812 !trimmedLine.startsWith(StringPool.OPEN_PARENTHESIS)) { 1813 1814 String strippedQuotesLine = _stripQuotes(trimmedLine); 1815 1816 int closeParenthesisCount = StringUtil.count( 1817 strippedQuotesLine, StringPool.CLOSE_PARENTHESIS); 1818 int openParenthesisCount = StringUtil.count( 1819 strippedQuotesLine, StringPool.OPEN_PARENTHESIS); 1820 1821 if (openParenthesisCount > closeParenthesisCount) { 1822 _sourceFormatterHelper.printError( 1823 fileName, "line break: " + fileName + " " + lineCount); 1824 } 1825 } 1826 1827 if (line.contains(StringPool.COMMA) && 1828 !line.contains(StringPool.CLOSE_PARENTHESIS) && 1829 !line.contains(StringPool.GREATER_THAN) && 1830 !line.contains(StringPool.QUOTE) && 1831 line.endsWith(StringPool.OPEN_PARENTHESIS)) { 1832 1833 _sourceFormatterHelper.printError( 1834 fileName, "line break: " + fileName + " " + lineCount); 1835 } 1836 1837 if (line.endsWith(" +") || line.endsWith(" -") || 1838 line.endsWith(" *") || line.endsWith(" /")) { 1839 1840 int x = line.indexOf(" = "); 1841 1842 if (x != -1) { 1843 int y = line.indexOf(StringPool.QUOTE); 1844 1845 if ((y == -1) || (x < y)) { 1846 _sourceFormatterHelper.printError( 1847 fileName, 1848 "line break: " + fileName + " " + lineCount); 1849 } 1850 } 1851 } 1852 1853 if (line.contains(" ") && !line.matches("\\s*\\*.*")) { 1854 if (!fileName.endsWith("StringPool.java")) { 1855 _sourceFormatterHelper.printError( 1856 fileName, "tab: " + fileName + " " + lineCount); 1857 } 1858 } 1859 1860 if (line.contains(" {") && !line.matches("\\s*\\*.*")) { 1861 _sourceFormatterHelper.printError( 1862 fileName, "{:" + fileName + " " + lineCount); 1863 } 1864 1865 excluded = null; 1866 1867 if (_lineLengthExclusions != null) { 1868 excluded = _lineLengthExclusions.getProperty( 1869 fileNameWithForwardSlashes + StringPool.AT + lineCount); 1870 1871 if (excluded == null) { 1872 excluded = _lineLengthExclusions.getProperty( 1873 fileNameWithForwardSlashes); 1874 } 1875 } 1876 1877 Tuple combinedLines = null; 1878 int lineLength = _getLineLength(line); 1879 1880 if ((excluded == null) && 1881 !line.startsWith("import ") && !line.startsWith("package ") && 1882 !line.matches("\\s*\\*.*")) { 1883 1884 if (fileName.endsWith("Table.java") && 1885 line.contains("String TABLE_SQL_CREATE = ")) { 1886 } 1887 else if (fileName.endsWith("Table.java") && 1888 line.contains("String TABLE_SQL_DROP = ")) { 1889 } 1890 else if (fileName.endsWith("Table.java") && 1891 line.contains(" index IX_")) { 1892 } 1893 else { 1894 if (lineLength > 80) { 1895 _sourceFormatterHelper.printError( 1896 fileName, "> 80: " + fileName + " " + lineCount); 1897 } 1898 else if (!trimmedLine.startsWith("//")) { 1899 int lineLeadingTabCount = _getLeadingTabCount(line); 1900 int previousLineLeadingTabCount = _getLeadingTabCount( 1901 previousLine); 1902 1903 if (previousLine.endsWith(StringPool.COMMA) && 1904 previousLine.contains( 1905 StringPool.OPEN_PARENTHESIS) && 1906 !previousLine.contains("for (") && 1907 (lineLeadingTabCount > 1908 previousLineLeadingTabCount)) { 1909 1910 _sourceFormatterHelper.printError( 1911 fileName, 1912 "line break: " + fileName + " " + lineCount); 1913 } 1914 1915 if (Validator.isNotNull(trimmedLine)) { 1916 if (((previousLine.endsWith(StringPool.COLON) && 1917 previousLine.contains( 1918 StringPool.TAB + "for ")) || 1919 (previousLine.endsWith( 1920 StringPool.OPEN_PARENTHESIS) && 1921 previousLine.contains( 1922 StringPool.TAB + "if "))) && 1923 ((previousLineLeadingTabCount + 2) != 1924 lineLeadingTabCount)) { 1925 1926 _sourceFormatterHelper.printError( 1927 fileName, 1928 "line break: " + fileName + " " + lineCount); 1929 } 1930 1931 if (previousLine.endsWith( 1932 StringPool.OPEN_CURLY_BRACE) && 1933 !trimmedLine.startsWith( 1934 StringPool.CLOSE_CURLY_BRACE) && 1935 ((previousLineLeadingTabCount + 1) != 1936 lineLeadingTabCount)) { 1937 1938 _sourceFormatterHelper.printError( 1939 fileName, 1940 "tab: " + fileName + " " + lineCount); 1941 } 1942 } 1943 1944 if (previousLine.endsWith(StringPool.PERIOD)) { 1945 int x = trimmedLine.indexOf( 1946 StringPool.OPEN_PARENTHESIS); 1947 1948 if ((x != -1) && 1949 ((_getLineLength(previousLine) + x) < 80) && 1950 (trimmedLine.endsWith( 1951 StringPool.OPEN_PARENTHESIS) || 1952 (trimmedLine.charAt(x + 1) != 1953 CharPool.CLOSE_PARENTHESIS))) { 1954 1955 _sourceFormatterHelper.printError( 1956 fileName, 1957 "line break: " + fileName + " " + 1958 lineCount); 1959 } 1960 } 1961 1962 if (trimmedLine.startsWith("throws ") && 1963 (lineLeadingTabCount == 1964 previousLineLeadingTabCount)) { 1965 1966 _sourceFormatterHelper.printError( 1967 fileName, "tab: " + fileName + " " + lineCount); 1968 } 1969 1970 if ((previousLine.contains(" class " ) || 1971 previousLine.contains(" enum ")) && 1972 previousLine.endsWith( 1973 StringPool.OPEN_CURLY_BRACE) && 1974 Validator.isNotNull(line) && 1975 !trimmedLine.startsWith( 1976 StringPool.CLOSE_CURLY_BRACE)) { 1977 1978 _sourceFormatterHelper.printError( 1979 fileName, 1980 "new line: " + fileName + " " + lineCount); 1981 } 1982 1983 combinedLines = _getCombinedLines( 1984 trimmedLine, previousLine, lineLeadingTabCount, 1985 previousLineLeadingTabCount); 1986 } 1987 } 1988 } 1989 1990 if (combinedLines != null) { 1991 previousLine = (String)combinedLines.getObject(0); 1992 1993 if (combinedLines.getSize() > 1) { 1994 String linePart = (String)combinedLines.getObject(1); 1995 boolean addToPreviousLine = 1996 (Boolean)combinedLines.getObject(2); 1997 1998 if (addToPreviousLine) { 1999 previousLine = previousLine + linePart; 2000 line = StringUtil.replaceFirst( 2001 line, linePart, StringPool.BLANK); 2002 } 2003 else { 2004 if (((linePart.length() + lineLength) <= 80) && 2005 (line.endsWith(StringPool.OPEN_CURLY_BRACE) || 2006 line.endsWith(StringPool.SEMICOLON))) { 2007 2008 previousLine = StringUtil.replaceLast( 2009 previousLine, StringUtil.trim(linePart), 2010 StringPool.BLANK); 2011 2012 line = StringUtil.replaceLast( 2013 line, StringPool.TAB, 2014 StringPool.TAB + linePart); 2015 } 2016 else { 2017 _sourceFormatterHelper.printError( 2018 fileName, 2019 "line break: " + fileName + " " + lineCount); 2020 } 2021 } 2022 2023 sb.append(previousLine); 2024 sb.append("\n"); 2025 2026 previousLine = line; 2027 } 2028 else if (line.endsWith(StringPool.OPEN_CURLY_BRACE) && 2029 !previousLine.contains(" class ")) { 2030 2031 lineToSkipIfEmpty = lineCount + 1; 2032 } 2033 } 2034 else { 2035 if ((lineCount > 1) && 2036 (Validator.isNotNull(previousLine) || 2037 (lineToSkipIfEmpty != (lineCount - 1)))) { 2038 2039 sb.append(previousLine); 2040 2041 if (Validator.isNotNull(previousLine) && 2042 Validator.isNotNull(trimmedLine) && 2043 !previousLine.contains("/*")) { 2044 2045 String trimmedPreviousLine = StringUtil.trimLeading( 2046 previousLine); 2047 2048 if ((trimmedPreviousLine.startsWith("// ") && 2049 !trimmedLine.startsWith("// ")) || 2050 (!trimmedPreviousLine.startsWith("// ") && 2051 trimmedLine.startsWith("// "))) { 2052 2053 sb.append("\n"); 2054 } 2055 else if (previousLine.endsWith( 2056 StringPool.TAB + 2057 StringPool.CLOSE_CURLY_BRACE) && 2058 !trimmedLine.startsWith( 2059 StringPool.CLOSE_CURLY_BRACE) && 2060 !trimmedLine.startsWith( 2061 StringPool.CLOSE_PARENTHESIS) && 2062 !trimmedLine.startsWith( 2063 StringPool.DOUBLE_SLASH) && 2064 !trimmedLine.startsWith("catch ") && 2065 !trimmedLine.startsWith("else ") && 2066 !trimmedLine.startsWith("finally ") && 2067 !trimmedLine.startsWith("while ")) { 2068 2069 sb.append("\n"); 2070 } 2071 } 2072 2073 sb.append("\n"); 2074 } 2075 2076 previousLine = line; 2077 } 2078 2079 index = index + line.length() + 1; 2080 } 2081 2082 sb.append(previousLine); 2083 2084 unsyncBufferedReader.close(); 2085 2086 String newContent = sb.toString(); 2087 2088 if (newContent.endsWith("\n")) { 2089 newContent = newContent.substring(0, newContent.length() - 1); 2090 } 2091 2092 if (hasUnsortedJavaTerms && content.equals(newContent)) { 2093 if (nextJavaTermPos == -1) { 2094 nextJavaTermPos = newContent.length() - 2; 2095 } 2096 2097 String javaTerm1 = newContent.substring( 2098 previousJavaTermPos, javaTermPos); 2099 2100 if (!newContent.contains("\n\n" + javaTerm1)) { 2101 newContent = StringUtil.replace( 2102 newContent, javaTerm1, "\n" + javaTerm1); 2103 } 2104 2105 javaTerm1 = javaTerm1.trim(); 2106 2107 String javaTerm2 = newContent.substring( 2108 javaTermPos, nextJavaTermPos); 2109 2110 javaTerm2 = javaTerm2.trim(); 2111 2112 newContent = StringUtil.replaceFirst( 2113 newContent, javaTerm1, javaTerm2); 2114 newContent = StringUtil.replaceLast( 2115 newContent, javaTerm2, javaTerm1); 2116 2117 if ((previousJavaTermType == nextJavaTermType) && 2118 _isInJavaTermTypeGroup( 2119 previousJavaTermType, _TYPE_VARIABLE_NOT_STATIC)) { 2120 2121 newContent = StringUtil.replace( 2122 newContent, javaTerm1 + "\n\n", javaTerm1 + "\n"); 2123 } 2124 } 2125 2126 return newContent; 2127 } 2128 2129 private static void _formatJS() throws IOException { 2130 String basedir = "./"; 2131 2132 DirectoryScanner directoryScanner = new DirectoryScanner(); 2133 2134 directoryScanner.setBasedir(basedir); 2135 2136 String[] excludes = { 2137 "**\\js\\aui\\**", "**\\js\\editor\\**", "**\\js\\misc\\**", 2138 "**\\tools\\**", "**\\VAADIN\\**" 2139 }; 2140 2141 excludes = ArrayUtil.append(excludes, _excludes); 2142 2143 directoryScanner.setExcludes(excludes); 2144 2145 directoryScanner.setIncludes(new String[] {"**\\*.js"}); 2146 2147 List<String> fileNames = _sourceFormatterHelper.scanForFiles( 2148 directoryScanner); 2149 2150 for (String fileName : fileNames) { 2151 File file = new File(basedir + fileName); 2152 2153 String content = _fileUtil.read(file); 2154 2155 String newContent = _trimContent(content); 2156 2157 newContent = StringUtil.replace( 2158 newContent, 2159 new String[] { 2160 "else{", "for(", "function (", "if(", "while(", "){\n", 2161 "= new Array();", "= new Object();" 2162 }, 2163 new String[] { 2164 "else {", "for (", "function(", "if (", "while (", ") {\n", 2165 "= [];", "= {};" 2166 }); 2167 2168 Pattern pattern = Pattern.compile("\t+var \\w+\\, "); 2169 2170 for (;;) { 2171 Matcher matcher = pattern.matcher(newContent); 2172 2173 if (!matcher.find()) { 2174 break; 2175 } 2176 2177 String match = newContent.substring( 2178 matcher.start(), matcher.end()); 2179 2180 int pos = match.indexOf("var "); 2181 2182 StringBundler sb = new StringBundler(4); 2183 2184 sb.append(match.substring(0, match.length() - 2)); 2185 sb.append(StringPool.SEMICOLON); 2186 sb.append("\n"); 2187 sb.append(match.substring(0, pos + 4)); 2188 2189 newContent = StringUtil.replace( 2190 newContent, match, sb.toString()); 2191 } 2192 2193 if (newContent.endsWith("\n")) { 2194 newContent = newContent.substring(0, newContent.length() - 1); 2195 } 2196 2197 _checkLanguageKeys(fileName, newContent, _languageKeyPattern); 2198 2199 if ((newContent != null) && !content.equals(newContent)) { 2200 _fileUtil.write(file, newContent); 2201 2202 _sourceFormatterHelper.printError(fileName, file); 2203 } 2204 } 2205 } 2206 2207 private static void _formatJSP() throws IOException { 2208 String basedir = "./"; 2209 2210 String copyright = _getCopyright(); 2211 String oldCopyright = _getOldCopyright(); 2212 2213 DirectoryScanner directoryScanner = new DirectoryScanner(); 2214 2215 directoryScanner.setBasedir(basedir); 2216 2217 String[] excludes = { 2218 "**\\portal\\aui\\**", "**\\bin\\**", "**\\null.jsp", "**\\tmp\\**", 2219 "**\\tools\\**" 2220 }; 2221 2222 excludes = ArrayUtil.append(excludes, _excludes); 2223 2224 directoryScanner.setExcludes(excludes); 2225 2226 directoryScanner.setIncludes( 2227 new String[] {"**\\*.jsp", "**\\*.jspf", "**\\*.vm"}); 2228 2229 List<String> fileNames = _sourceFormatterHelper.scanForFiles( 2230 directoryScanner); 2231 2232 for (String fileName : fileNames) { 2233 File file = new File(basedir + fileName); 2234 2235 String content = _fileUtil.read(file); 2236 2237 fileName = fileName.replace( 2238 CharPool.BACK_SLASH, CharPool.FORWARD_SLASH); 2239 2240 _jspContents.put(fileName, content); 2241 } 2242 2243 boolean stripJSPImports = true; 2244 2245 for (String fileName : fileNames) { 2246 File file = new File(basedir + fileName); 2247 2248 String content = _fileUtil.read(file); 2249 2250 String oldContent = content; 2251 String newContent = StringPool.BLANK; 2252 2253 for (;;) { 2254 newContent = _formatJSP(fileName, oldContent); 2255 2256 if (oldContent.equals(newContent)) { 2257 break; 2258 } 2259 2260 oldContent = newContent; 2261 } 2262 2263 newContent = StringUtil.replace( 2264 newContent, 2265 new String[] { 2266 "<br/>", "\"/>", "\" >", "@page import", "\"%>", ")%>", 2267 "javascript: " 2268 }, 2269 new String[] { 2270 "<br />", "\" />", "\">", "@ page import", "\" %>", ") %>", 2271 "javascript:" 2272 }); 2273 2274 if (stripJSPImports) { 2275 try { 2276 newContent = _stripJSPImports(fileName, newContent); 2277 } 2278 catch (RuntimeException re) { 2279 stripJSPImports = false; 2280 } 2281 } 2282 2283 newContent = StringUtil.replace( 2284 newContent, 2285 new String[] { 2286 "* Copyright (c) 2000-2011 Liferay, Inc." 2287 }, 2288 new String[] { 2289 "* Copyright (c) 2000-2012 Liferay, Inc." 2290 }); 2291 2292 if (fileName.endsWith(".jsp") || fileName.endsWith(".jspf")) { 2293 if ((oldCopyright != null) && 2294 newContent.contains(oldCopyright)) { 2295 2296 newContent = StringUtil.replace( 2297 newContent, oldCopyright, copyright); 2298 2299 _sourceFormatterHelper.printError( 2300 fileName, "old (c): " + fileName); 2301 } 2302 2303 if (!newContent.contains(copyright)) { 2304 String customCopyright = _getCustomCopyright(file); 2305 2306 if (Validator.isNull(customCopyright) || 2307 !newContent.contains(customCopyright)) { 2308 2309 _sourceFormatterHelper.printError( 2310 fileName, "(c): " + fileName); 2311 } 2312 else { 2313 newContent = StringUtil.replace( 2314 newContent, "<%\n" + customCopyright + "\n%>", 2315 "<%--\n" + customCopyright + "\n--%>"); 2316 } 2317 } 2318 else { 2319 newContent = StringUtil.replace( 2320 newContent, "<%\n" + copyright + "\n%>", 2321 "<%--\n" + copyright + "\n--%>"); 2322 } 2323 } 2324 2325 newContent = StringUtil.replace( 2326 newContent, 2327 new String[] { 2328 "alert('<%= LanguageUtil.", 2329 "alert(\"<%= LanguageUtil.", "confirm('<%= LanguageUtil.", 2330 "confirm(\"<%= LanguageUtil." 2331 }, 2332 new String[] { 2333 "alert('<%= UnicodeLanguageUtil.", 2334 "alert(\"<%= UnicodeLanguageUtil.", 2335 "confirm('<%= UnicodeLanguageUtil.", 2336 "confirm(\"<%= UnicodeLanguageUtil." 2337 }); 2338 2339 if (newContent.contains(" ")) { 2340 if (!fileName.matches(".*template.*\\.vm$")) { 2341 _sourceFormatterHelper.printError( 2342 fileName, "tab: " + fileName); 2343 } 2344 } 2345 2346 if (fileName.endsWith("init.jsp")) { 2347 int x = newContent.indexOf("<%@ page import="); 2348 2349 int y = newContent.lastIndexOf("<%@ page import="); 2350 2351 y = newContent.indexOf("%>", y); 2352 2353 if ((x != -1) && (y != -1) && (y > x)) { 2354 2355 // Set compressImports to false to decompress imports 2356 2357 boolean compressImports = true; 2358 2359 if (compressImports) { 2360 String imports = newContent.substring(x, y); 2361 2362 imports = StringUtil.replace( 2363 imports, new String[] {"%>\r\n<%@ ", "%>\n<%@ "}, 2364 new String[] {"%><%@\r\n", "%><%@\n"}); 2365 2366 newContent = 2367 newContent.substring(0, x) + imports + 2368 newContent.substring(y); 2369 } 2370 } 2371 } 2372 2373 newContent = _fixSessionKey( 2374 fileName, newContent, _sessionKeyPattern); 2375 newContent = _fixSessionKey( 2376 fileName, newContent, _taglibSessionKeyPattern); 2377 2378 _checkLanguageKeys(fileName, newContent, _languageKeyPattern); 2379 _checkLanguageKeys(fileName, newContent, _taglibLanguageKeyPattern); 2380 _checkXSS(fileName, newContent); 2381 2382 if ((newContent != null) && !content.equals(newContent)) { 2383 _fileUtil.write(file, newContent); 2384 2385 _sourceFormatterHelper.printError(fileName, file); 2386 } 2387 } 2388 } 2389 2390 private static String _formatJSP(String fileName, String content) 2391 throws IOException { 2392 2393 StringBundler sb = new StringBundler(); 2394 2395 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader( 2396 new UnsyncStringReader(content)); 2397 2398 int lineCount = 0; 2399 2400 String line = null; 2401 2402 String previousLine = StringPool.BLANK; 2403 2404 String currentAttributeAndValue = null; 2405 String previousAttribute = null; 2406 String previousAttributeAndValue = null; 2407 2408 boolean readAttributes = false; 2409 2410 String currentException = null; 2411 String previousException = null; 2412 2413 boolean hasUnsortedExceptions = false; 2414 2415 while ((line = unsyncBufferedReader.readLine()) != null) { 2416 lineCount++; 2417 2418 if (!fileName.contains("jsonw") || 2419 !fileName.endsWith("action.jsp")) { 2420 2421 line = _trimLine(line); 2422 } 2423 2424 if (line.contains("<aui:button ") && 2425 line.contains("type=\"button\"")) { 2426 2427 _sourceFormatterHelper.printError( 2428 fileName, "aui:button " + fileName + " " + lineCount); 2429 } 2430 2431 String trimmedLine = StringUtil.trimLeading(line); 2432 String trimmedPreviousLine = StringUtil.trimLeading(previousLine); 2433 2434 if (!trimmedLine.equals("%>") && line.contains("%>") && 2435 !line.contains("--%>") && !line.contains(" %>")) { 2436 2437 line = StringUtil.replace(line, "%>", " %>"); 2438 } 2439 2440 if (line.contains("<%=") && !line.contains("<%= ")) { 2441 line = StringUtil.replace(line, "<%=", "<%= "); 2442 } 2443 2444 if (trimmedPreviousLine.equals("%>") && Validator.isNotNull(line) && 2445 !trimmedLine.equals("-->")) { 2446 2447 sb.append("\n"); 2448 } 2449 else if (Validator.isNotNull(previousLine) && 2450 !trimmedPreviousLine.equals("<!--") && 2451 trimmedLine.equals("<%")) { 2452 2453 sb.append("\n"); 2454 } 2455 else if (trimmedPreviousLine.equals("<%") && 2456 Validator.isNull(line)) { 2457 2458 continue; 2459 } 2460 else if (trimmedPreviousLine.equals("<%") && 2461 trimmedLine.startsWith("//")) { 2462 2463 sb.append("\n"); 2464 } 2465 else if (Validator.isNull(previousLine) && 2466 trimmedLine.equals("%>") && (sb.index() > 2)) { 2467 2468 String lineBeforePreviousLine = sb.stringAt(sb.index() - 3); 2469 2470 if (!lineBeforePreviousLine.startsWith("//")) { 2471 sb.setIndex(sb.index() - 1); 2472 } 2473 } 2474 2475 if ((trimmedLine.startsWith("if (") || 2476 trimmedLine.startsWith("else if (") || 2477 trimmedLine.startsWith("while (")) && 2478 trimmedLine.endsWith(") {")) { 2479 2480 _checkIfClause(trimmedLine, fileName, lineCount); 2481 } 2482 2483 if (readAttributes) { 2484 if (!trimmedLine.startsWith(StringPool.FORWARD_SLASH) && 2485 !trimmedLine.startsWith(StringPool.GREATER_THAN)) { 2486 2487 int pos = trimmedLine.indexOf(StringPool.EQUAL); 2488 2489 if (pos != -1) { 2490 String attribute = trimmedLine.substring(0, pos); 2491 2492 if (!trimmedLine.endsWith(StringPool.QUOTE) && 2493 !trimmedLine.endsWith(StringPool.APOSTROPHE)) { 2494 2495 _sourceFormatterHelper.printError( 2496 fileName, 2497 "attribute: " + fileName + " " + lineCount); 2498 2499 readAttributes = false; 2500 } 2501 else if (Validator.isNotNull(previousAttribute)) { 2502 if (!_isJSPAttributName(attribute)) { 2503 _sourceFormatterHelper.printError( 2504 fileName, 2505 "attribute: " + fileName + " " + lineCount); 2506 2507 readAttributes = false; 2508 } 2509 else if (Validator.isNull( 2510 previousAttributeAndValue) && 2511 (previousAttribute.compareTo( 2512 attribute) > 0)) { 2513 2514 previousAttributeAndValue = previousLine; 2515 currentAttributeAndValue = line; 2516 } 2517 } 2518 2519 if (!readAttributes) { 2520 previousAttribute = null; 2521 previousAttributeAndValue = null; 2522 } 2523 else { 2524 previousAttribute = attribute; 2525 } 2526 } 2527 } 2528 else { 2529 previousAttribute = null; 2530 2531 readAttributes = false; 2532 } 2533 } 2534 2535 if (!hasUnsortedExceptions) { 2536 int i = line.indexOf("<liferay-ui:error exception=\"<%="); 2537 2538 if (i != -1) { 2539 currentException = line.substring(i + 33); 2540 2541 if (Validator.isNotNull(previousException) && 2542 (previousException.compareTo(currentException) > 0)) { 2543 2544 hasUnsortedExceptions = true; 2545 } 2546 } 2547 2548 if (!hasUnsortedExceptions) { 2549 previousException = currentException; 2550 currentException = null; 2551 } 2552 } 2553 2554 if (trimmedLine.startsWith(StringPool.LESS_THAN) && 2555 !trimmedLine.startsWith("<%") && 2556 !trimmedLine.startsWith("<!")) { 2557 2558 if (!trimmedLine.contains(StringPool.GREATER_THAN) && 2559 !trimmedLine.contains(StringPool.SPACE)) { 2560 2561 readAttributes = true; 2562 } 2563 else { 2564 line = _sortJSPAttributes(fileName, line, lineCount); 2565 } 2566 } 2567 2568 if (!trimmedLine.contains(StringPool.DOUBLE_SLASH) && 2569 !trimmedLine.startsWith(StringPool.STAR)) { 2570 2571 while (trimmedLine.contains(StringPool.TAB)) { 2572 line = StringUtil.replaceLast( 2573 line, StringPool.TAB, StringPool.SPACE); 2574 2575 trimmedLine = StringUtil.replaceLast( 2576 trimmedLine, StringPool.TAB, StringPool.SPACE); 2577 } 2578 2579 while (trimmedLine.contains(StringPool.DOUBLE_SPACE) && 2580 !trimmedLine.contains( 2581 StringPool.QUOTE + StringPool.DOUBLE_SPACE) && 2582 !fileName.endsWith(".vm")) { 2583 2584 line = StringUtil.replaceLast( 2585 line, StringPool.DOUBLE_SPACE, StringPool.SPACE); 2586 2587 trimmedLine = StringUtil.replaceLast( 2588 trimmedLine, StringPool.DOUBLE_SPACE, StringPool.SPACE); 2589 } 2590 } 2591 2592 int x = line.indexOf("<%@ include file"); 2593 2594 if (x != -1) { 2595 x = line.indexOf(StringPool.QUOTE, x); 2596 2597 int y = line.indexOf(StringPool.QUOTE, x + 1); 2598 2599 if (y != -1) { 2600 String includeFileName = line.substring(x + 1, y); 2601 2602 Matcher matcher = _jspIncludeFilePattern.matcher( 2603 includeFileName); 2604 2605 if (!matcher.find()) { 2606 _sourceFormatterHelper.printError( 2607 fileName, "include: " + fileName + " " + lineCount); 2608 } 2609 } 2610 } 2611 2612 line = _replacePrimitiveWrapperInstantiation( 2613 fileName, line, lineCount); 2614 2615 previousLine = line; 2616 2617 sb.append(line); 2618 sb.append("\n"); 2619 } 2620 2621 unsyncBufferedReader.close(); 2622 2623 content = sb.toString(); 2624 2625 if (content.endsWith("\n")) { 2626 content = content.substring(0, content.length() - 1); 2627 } 2628 2629 content = _formatTaglibQuotes(fileName, content, StringPool.QUOTE); 2630 content = _formatTaglibQuotes(fileName, content, StringPool.APOSTROPHE); 2631 2632 if (Validator.isNotNull(previousAttributeAndValue)) { 2633 content = StringUtil.replaceFirst( 2634 content, 2635 previousAttributeAndValue + "\n" + currentAttributeAndValue, 2636 currentAttributeAndValue + "\n" + previousAttributeAndValue); 2637 } 2638 2639 if (hasUnsortedExceptions) { 2640 if ((StringUtil.count(content, currentException) > 1) || 2641 (StringUtil.count(content, previousException) > 1)) { 2642 2643 _sourceFormatterHelper.printError( 2644 fileName, "unsorted exceptions: " + fileName); 2645 } 2646 else { 2647 content = StringUtil.replaceFirst( 2648 content, previousException, currentException); 2649 2650 content = StringUtil.replaceLast( 2651 content, currentException, previousException); 2652 } 2653 } 2654 2655 return content; 2656 } 2657 2658 private static void _formatPortalProperties() throws IOException { 2659 String basedir = "./"; 2660 2661 String portalPortalProperties = null; 2662 2663 if (_portalSource) { 2664 File portalPortalPropertiesFile = new File( 2665 basedir + "portal-impl/src/portal.properties"); 2666 2667 portalPortalProperties = _fileUtil.read(portalPortalPropertiesFile); 2668 } 2669 else { 2670 portalPortalProperties = ContentUtil.get("portal.properties"); 2671 } 2672 2673 DirectoryScanner directoryScanner = new DirectoryScanner(); 2674 2675 directoryScanner.setBasedir(basedir); 2676 2677 if (_portalSource) { 2678 directoryScanner.setExcludes( 2679 new String[] {"**\\classes\\**", "**\\bin\\**"}); 2680 directoryScanner.setIncludes( 2681 new String[] { 2682 "**\\portal-ext.properties", 2683 "**\\portal-legacy-*.properties", 2684 }); 2685 } 2686 else { 2687 directoryScanner.setIncludes( 2688 new String[] { 2689 "**\\portal.properties", "**\\portal-ext.properties" 2690 }); 2691 } 2692 2693 List<String> fileNames = _sourceFormatterHelper.scanForFiles( 2694 directoryScanner); 2695 2696 for (String fileName : fileNames) { 2697 File file = new File(basedir + fileName); 2698 2699 String content = _fileUtil.read(file); 2700 2701 _formatPortalProperties(fileName, content, portalPortalProperties); 2702 } 2703 } 2704 2705 private static void _formatPortalProperties( 2706 String fileName, String content, String portalPortalProperties) 2707 throws IOException { 2708 2709 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader( 2710 new UnsyncStringReader(content)); 2711 2712 int lineCount = 0; 2713 2714 String line = null; 2715 2716 int previousPos = -1; 2717 2718 while ((line = unsyncBufferedReader.readLine()) != null) { 2719 lineCount++; 2720 2721 int pos = line.indexOf(StringPool.EQUAL); 2722 2723 if (pos == -1) { 2724 continue; 2725 } 2726 2727 String property = line.substring(0, pos + 1); 2728 2729 property = property.trim(); 2730 2731 pos = portalPortalProperties.indexOf( 2732 StringPool.FOUR_SPACES + property); 2733 2734 if (pos == -1) { 2735 continue; 2736 } 2737 2738 if (pos < previousPos) { 2739 _sourceFormatterHelper.printError( 2740 fileName, "sort " + fileName + " " + lineCount); 2741 } 2742 2743 previousPos = pos; 2744 } 2745 } 2746 2747 private static void _formatPortletXML() 2748 throws DocumentException, IOException { 2749 2750 String basedir = "./"; 2751 2752 if (_portalSource) { 2753 File file = new File( 2754 basedir + "portal-web/docroot/WEB-INF/portlet-custom.xml"); 2755 2756 String content = _fileUtil.read(file); 2757 2758 String newContent = _formatPortletXML(content); 2759 2760 if ((newContent != null) && !content.equals(newContent)) { 2761 _fileUtil.write(file, newContent); 2762 2763 _sourceFormatterHelper.printError(file.toString(), file); 2764 } 2765 } 2766 else { 2767 DirectoryScanner directoryScanner = new DirectoryScanner(); 2768 2769 directoryScanner.setBasedir(basedir); 2770 directoryScanner.setIncludes(new String[] {"**\\portlet.xml"}); 2771 2772 List<String> fileNames = _sourceFormatterHelper.scanForFiles( 2773 directoryScanner); 2774 2775 for (String fileName : fileNames) { 2776 File file = new File(basedir + fileName); 2777 2778 String content = _fileUtil.read(file); 2779 2780 String newContent = _trimContent(content); 2781 2782 newContent = _formatPortletXML(content); 2783 2784 if ((newContent != null) && !content.equals(newContent)) { 2785 _fileUtil.write(file, newContent); 2786 2787 _sourceFormatterHelper.printError(fileName, file); 2788 } 2789 } 2790 } 2791 } 2792 2793 private static String _formatPortletXML(String content) 2794 throws DocumentException, IOException { 2795 2796 Document document = _saxReaderUtil.read(content); 2797 2798 Element rootElement = document.getRootElement(); 2799 2800 rootElement.sortAttributes(true); 2801 2802 List<Element> portletElements = rootElement.elements("portlet"); 2803 2804 for (Element portletElement : portletElements) { 2805 portletElement.sortElementsByChildElement("init-param", "name"); 2806 2807 Element portletPreferencesElement = portletElement.element( 2808 "portlet-preferences"); 2809 2810 if (portletPreferencesElement != null) { 2811 portletPreferencesElement.sortElementsByChildElement( 2812 "preference", "name"); 2813 } 2814 } 2815 2816 return document.formattedString(); 2817 } 2818 2819 private static void _formatServiceXML() 2820 throws DocumentException, IOException { 2821 2822 String basedir = "./"; 2823 2824 DirectoryScanner directoryScanner = new DirectoryScanner(); 2825 2826 directoryScanner.setBasedir(basedir); 2827 directoryScanner.setIncludes(new String[] {"**\\service.xml"}); 2828 2829 List<String> fileNames = _sourceFormatterHelper.scanForFiles( 2830 directoryScanner); 2831 2832 for (String fileName : fileNames) { 2833 File file = new File(basedir + fileName); 2834 2835 String content = _fileUtil.read(file); 2836 2837 String newContent = _trimContent(content); 2838 2839 _formatServiceXML(fileName, content); 2840 2841 if ((newContent != null) && !content.equals(newContent)) { 2842 _fileUtil.write(file, newContent); 2843 2844 _sourceFormatterHelper.printError(fileName, file); 2845 } 2846 } 2847 } 2848 2849 private static void _formatServiceXML(String fileName, String content) 2850 throws DocumentException { 2851 2852 Document document = _saxReaderUtil.read(content); 2853 2854 Element rootElement = document.getRootElement(); 2855 2856 List<Element> entityElements = rootElement.elements("entity"); 2857 2858 String previousEntityName = StringPool.BLANK; 2859 2860 for (Element entityElement : entityElements) { 2861 String entityName = entityElement.attributeValue("name"); 2862 2863 if (Validator.isNotNull(previousEntityName) && 2864 (previousEntityName.compareToIgnoreCase(entityName) > 0)) { 2865 2866 _sourceFormatterHelper.printError( 2867 fileName, "sort: " + fileName + " " + entityName); 2868 } 2869 2870 String previousReferenceEntity = StringPool.BLANK; 2871 String previousReferencePackagePath = StringPool.BLANK; 2872 2873 List<Element> referenceElements = entityElement.elements( 2874 "reference"); 2875 2876 for (Element referenceElement : referenceElements) { 2877 String referenceEntity = referenceElement.attributeValue( 2878 "entity"); 2879 String referencePackagePath = referenceElement.attributeValue( 2880 "package-path"); 2881 2882 2883 if (Validator.isNotNull(previousReferencePackagePath)) { 2884 if ((previousReferencePackagePath.compareToIgnoreCase( 2885 referencePackagePath) > 0) || 2886 (previousReferencePackagePath.equals( 2887 referencePackagePath) && 2888 (previousReferenceEntity.compareToIgnoreCase( 2889 referenceEntity) > 0))) { 2890 2891 _sourceFormatterHelper.printError( 2892 fileName, 2893 "sort: " + fileName + " " + referencePackagePath); 2894 } 2895 } 2896 2897 previousReferenceEntity = referenceEntity; 2898 previousReferencePackagePath = referencePackagePath; 2899 } 2900 2901 previousEntityName = entityName; 2902 } 2903 2904 Element exceptionsElement = rootElement.element("exceptions"); 2905 2906 if (exceptionsElement == null) { 2907 return; 2908 } 2909 2910 List<Element> exceptionElements = exceptionsElement.elements( 2911 "exception"); 2912 2913 String previousException = StringPool.BLANK; 2914 2915 for (Element exceptionElement : exceptionElements) { 2916 String exception = exceptionElement.getStringValue(); 2917 2918 if (Validator.isNotNull(previousException) && 2919 (previousException.compareToIgnoreCase(exception) > 0)) { 2920 2921 _sourceFormatterHelper.printError( 2922 fileName, "sort: " + fileName + " " + exception); 2923 } 2924 2925 previousException = exception; 2926 } 2927 } 2928 2929 private static void _formatSH() throws IOException { 2930 _formatSH("ext/create.sh"); 2931 _formatSH("hooks/create.sh"); 2932 _formatSH("layouttpl/create.sh"); 2933 _formatSH("portlets/create.sh"); 2934 _formatSH("themes/create.sh"); 2935 } 2936 2937 private static void _formatSH(String fileName) throws IOException { 2938 File file = new File(fileName); 2939 2940 if (!file.exists()) { 2941 return; 2942 } 2943 2944 String content = _fileUtil.read(new File(fileName), true); 2945 2946 if (content.contains("\r")) { 2947 _sourceFormatterHelper.printError( 2948 fileName, "Invalid new line character"); 2949 2950 content = StringUtil.replace(content, "\r", ""); 2951 2952 _fileUtil.write(fileName, content); 2953 } 2954 } 2955 2956 private static void _formatSQL() throws IOException { 2957 String basedir = "./"; 2958 2959 DirectoryScanner directoryScanner = new DirectoryScanner(); 2960 2961 directoryScanner.setBasedir(basedir); 2962 directoryScanner.setIncludes(new String[] {"**\\sql\\*.sql"}); 2963 2964 List<String> fileNames = _sourceFormatterHelper.scanForFiles( 2965 directoryScanner); 2966 2967 for (String fileName : fileNames) { 2968 File file = new File(basedir + fileName); 2969 2970 String content = _fileUtil.read(file); 2971 2972 String newContent = _formatSQL(content); 2973 2974 if ((newContent != null) && !content.equals(newContent)) { 2975 _fileUtil.write(file, newContent); 2976 2977 _sourceFormatterHelper.printError(fileName, file); 2978 } 2979 } 2980 } 2981 2982 private static String _formatSQL(String content) throws IOException { 2983 StringBundler sb = new StringBundler(); 2984 2985 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader( 2986 new UnsyncStringReader(content)); 2987 2988 String line = null; 2989 2990 String previousLineSqlCommand = StringPool.BLANK; 2991 2992 while ((line = unsyncBufferedReader.readLine()) != null) { 2993 line = _trimLine(line); 2994 2995 if (Validator.isNotNull(line) && !line.startsWith(StringPool.TAB)) { 2996 String sqlCommand = StringUtil.split(line, CharPool.SPACE)[0]; 2997 2998 if (Validator.isNotNull(previousLineSqlCommand) && 2999 !previousLineSqlCommand.equals(sqlCommand)) { 3000 3001 sb.append("\n"); 3002 } 3003 3004 previousLineSqlCommand = sqlCommand; 3005 } 3006 else { 3007 previousLineSqlCommand = StringPool.BLANK; 3008 } 3009 3010 sb.append(line); 3011 sb.append("\n"); 3012 } 3013 3014 unsyncBufferedReader.close(); 3015 3016 content = sb.toString(); 3017 3018 if (content.endsWith("\n")) { 3019 content = content.substring(0, content.length() - 1); 3020 } 3021 3022 return content; 3023 } 3024 3025 private static void _formatStrutsConfigXML() 3026 throws DocumentException, IOException { 3027 3028 String basedir = "./"; 3029 3030 if (!_portalSource) { 3031 return; 3032 } 3033 3034 String fileName = "portal-web/docroot/WEB-INF/struts-config.xml"; 3035 3036 File file = new File(basedir + fileName); 3037 3038 String content = _fileUtil.read(file); 3039 3040 String newContent = _trimContent(content); 3041 3042 Document document = _saxReaderUtil.read(newContent); 3043 3044 Element rootElement = document.getRootElement(); 3045 3046 Element actionMappingsElement = rootElement.element("action-mappings"); 3047 3048 List<Element> actionElements = actionMappingsElement.elements("action"); 3049 3050 String previousPath = StringPool.BLANK; 3051 3052 for (Element actionElement : actionElements) { 3053 String path = actionElement.attributeValue("path"); 3054 3055 if (Validator.isNotNull(previousPath) && 3056 (previousPath.compareTo(path) > 0) && 3057 (!previousPath.startsWith("/portal/") || 3058 path.startsWith("/portal/"))) { 3059 3060 _sourceFormatterHelper.printError( 3061 fileName, "sort: " + fileName + " " + path); 3062 } 3063 3064 previousPath = path; 3065 } 3066 3067 if ((newContent != null) && !content.equals(newContent)) { 3068 _fileUtil.write(file, newContent); 3069 3070 _sourceFormatterHelper.printError(fileName, file); 3071 } 3072 } 3073 3074 private static String _formatTaglibQuotes( 3075 String fileName, String content, String quoteType) { 3076 3077 String quoteFix = StringPool.APOSTROPHE; 3078 3079 if (quoteFix.equals(quoteType)) { 3080 quoteFix = StringPool.QUOTE; 3081 } 3082 3083 Pattern pattern = Pattern.compile(_getTaglibRegex(quoteType)); 3084 3085 Matcher matcher = pattern.matcher(content); 3086 3087 while (matcher.find()) { 3088 int x = content.indexOf(quoteType + "<%=", matcher.start()); 3089 int y = content.indexOf("%>" + quoteType, x); 3090 3091 while ((x != -1) && (y != -1)) { 3092 String result = content.substring(x + 1, y + 2); 3093 3094 if (result.contains(quoteType)) { 3095 int lineCount = 1; 3096 3097 char[] contentCharArray = content.toCharArray(); 3098 3099 for (int i = 0; i < x; i++) { 3100 if (contentCharArray[i] == CharPool.NEW_LINE) { 3101 lineCount++; 3102 } 3103 } 3104 3105 if (!result.contains(quoteFix)) { 3106 StringBundler sb = new StringBundler(5); 3107 3108 sb.append(content.substring(0, x)); 3109 sb.append(quoteFix); 3110 sb.append(result); 3111 sb.append(quoteFix); 3112 sb.append(content.substring(y + 3, content.length())); 3113 3114 content = sb.toString(); 3115 } 3116 else { 3117 _sourceFormatterHelper.printError( 3118 fileName, "taglib: " + fileName + " " + lineCount); 3119 } 3120 } 3121 3122 x = content.indexOf(quoteType + "<%=", y); 3123 3124 if (x > matcher.end()) { 3125 break; 3126 } 3127 3128 y = content.indexOf("%>" + quoteType, x); 3129 } 3130 } 3131 3132 return content; 3133 } 3134 3135 private static void _formatTilesDefsXML() 3136 throws DocumentException, IOException { 3137 3138 String basedir = "./"; 3139 3140 if (!_portalSource) { 3141 return; 3142 } 3143 3144 String fileName = "portal-web/docroot/WEB-INF/tiles-defs.xml"; 3145 3146 File file = new File(basedir + fileName); 3147 3148 String content = _fileUtil.read(file); 3149 3150 String newContent = _trimContent(content); 3151 3152 Document document = _saxReaderUtil.read(newContent); 3153 3154 Element rootElement = document.getRootElement(); 3155 3156 List<Element> definitionElements = rootElement.elements("definition"); 3157 3158 String previousName = StringPool.BLANK; 3159 3160 for (Element definitionElement : definitionElements) { 3161 String name = definitionElement.attributeValue("name"); 3162 3163 if (Validator.isNotNull(previousName) && 3164 (previousName.compareTo(name) > 0) && 3165 !previousName.equals("portlet")) { 3166 3167 _sourceFormatterHelper.printError( 3168 fileName, "sort: " + fileName + " " + name); 3169 3170 } 3171 3172 previousName = name; 3173 } 3174 3175 if ((newContent != null) && !content.equals(newContent)) { 3176 _fileUtil.write(file, newContent); 3177 3178 _sourceFormatterHelper.printError(fileName, file); 3179 } 3180 } 3181 3182 private static void _formatTLD() throws IOException { 3183 String basedir = "./"; 3184 3185 DirectoryScanner directoryScanner = new DirectoryScanner(); 3186 3187 directoryScanner.setBasedir(basedir); 3188 directoryScanner.setIncludes(new String[] {"**\\*.tld"}); 3189 directoryScanner.setExcludes( 3190 new String[] { 3191 "**\\classes\\**", "**\\bin\\**", "**\\WEB-INF\\tld\\**" 3192 }); 3193 3194 List<String> fileNames = _sourceFormatterHelper.scanForFiles( 3195 directoryScanner); 3196 3197 for (String fileName : fileNames) { 3198 File file = new File(basedir + fileName); 3199 3200 String content = _fileUtil.read(file); 3201 3202 String newContent = _trimContent(content); 3203 3204 if ((newContent != null) && !content.equals(newContent)) { 3205 _fileUtil.write(file, newContent); 3206 3207 _sourceFormatterHelper.printError(fileName, file); 3208 } 3209 } 3210 } 3211 3212 private static void _formatWebXML() throws IOException { 3213 String basedir = "./"; 3214 3215 if (_portalSource) { 3216 Properties properties = new Properties(); 3217 3218 String propertiesContent = _fileUtil.read( 3219 basedir + "portal-impl/src/portal.properties"); 3220 3221 PropertiesUtil.load(properties, propertiesContent); 3222 3223 String[] locales = StringUtil.split( 3224 properties.getProperty(PropsKeys.LOCALES)); 3225 3226 Arrays.sort(locales); 3227 3228 Set<String> urlPatterns = new TreeSet<String>(); 3229 3230 for (String locale : locales) { 3231 int pos = locale.indexOf(StringPool.UNDERLINE); 3232 3233 String languageCode = locale.substring(0, pos); 3234 3235 urlPatterns.add(languageCode); 3236 urlPatterns.add(locale); 3237 } 3238 3239 StringBundler sb = new StringBundler(); 3240 3241 for (String urlPattern : urlPatterns) { 3242 sb.append("\t<servlet-mapping>\n"); 3243 sb.append("\t\t<servlet-name>I18n Servlet</servlet-name>\n"); 3244 sb.append( 3245 "\t\t<url-pattern>/" + urlPattern +"/*</url-pattern>\n"); 3246 sb.append("\t</servlet-mapping>\n"); 3247 } 3248 3249 File file = new File( 3250 basedir + "portal-web/docroot/WEB-INF/web.xml"); 3251 3252 String content = _fileUtil.read(file); 3253 3254 String newContent = _trimContent(content); 3255 3256 int x = newContent.indexOf("<servlet-mapping>"); 3257 3258 x = newContent.indexOf( 3259 "<servlet-name>I18n Servlet</servlet-name>", x); 3260 3261 x = newContent.lastIndexOf("<servlet-mapping>", x) - 1; 3262 3263 int y = newContent.lastIndexOf( 3264 "<servlet-name>I18n Servlet</servlet-name>"); 3265 3266 y = newContent.indexOf("</servlet-mapping>", y) + 19; 3267 3268 newContent = 3269 newContent.substring(0, x) + sb.toString() + 3270 newContent.substring(y); 3271 3272 x = newContent.indexOf("<security-constraint>"); 3273 3274 x = newContent.indexOf( 3275 "<web-resource-name>/c/portal/protected</web-resource-name>", 3276 x); 3277 3278 x = newContent.indexOf("<url-pattern>", x) - 3; 3279 3280 y = newContent.indexOf("<http-method>", x); 3281 3282 y = newContent.lastIndexOf("</url-pattern>", y) + 15; 3283 3284 sb = new StringBundler(); 3285 3286 sb.append("\t\t\t<url-pattern>/c/portal/protected</url-pattern>\n"); 3287 3288 for (String urlPattern : urlPatterns) { 3289 sb.append( 3290 "\t\t\t<url-pattern>/" + urlPattern + 3291 "/c/portal/protected</url-pattern>\n"); 3292 } 3293 3294 newContent = 3295 newContent.substring(0, x) + sb.toString() + 3296 newContent.substring(y); 3297 3298 if ((newContent != null) && !content.equals(newContent)) { 3299 _fileUtil.write(file, newContent); 3300 3301 System.out.println(file); 3302 } 3303 } 3304 else { 3305 String webXML = ContentUtil.get( 3306 "com/liferay/portal/deploy/dependencies/web.xml"); 3307 3308 DirectoryScanner directoryScanner = new DirectoryScanner(); 3309 3310 directoryScanner.setBasedir(basedir); 3311 directoryScanner.setIncludes(new String[] {"**\\web.xml"}); 3312 3313 List<String> fileNames = _sourceFormatterHelper.scanForFiles( 3314 directoryScanner); 3315 3316 for (String fileName : fileNames) { 3317 String content = _fileUtil.read(basedir + fileName); 3318 3319 if (content.equals(webXML)) { 3320 _sourceFormatterHelper.printError(fileName, fileName); 3321 } 3322 } 3323 } 3324 } 3325 3326 private static String _getClassName(String line) { 3327 int pos = line.indexOf(" implements "); 3328 3329 if (pos == -1) { 3330 pos = line.indexOf(" extends "); 3331 } 3332 3333 if (pos == -1) { 3334 pos = line.indexOf(StringPool.OPEN_CURLY_BRACE); 3335 } 3336 3337 if (pos != -1) { 3338 line = line.substring(0, pos); 3339 } 3340 3341 line = line.trim(); 3342 3343 pos = line.lastIndexOf(StringPool.SPACE); 3344 3345 return line.substring(pos + 1); 3346 } 3347 3348 private static Tuple _getCombinedLines( 3349 String line, String previousLine, int lineTabCount, 3350 int previousLineTabCount) { 3351 3352 if (Validator.isNull(line) || Validator.isNull(previousLine)) { 3353 return null; 3354 } 3355 3356 String trimmedPreviousLine = StringUtil.trimLeading(previousLine); 3357 3358 if (line.startsWith("// ") || trimmedPreviousLine.startsWith("// ")) { 3359 return null; 3360 } 3361 3362 if (previousLine.endsWith(" implements")) { 3363 return new Tuple(previousLine, "implements ", false); 3364 } 3365 3366 if (line.startsWith("+ ") || line.startsWith("- ") || 3367 line.startsWith("|| ") || line.startsWith("&& ")) { 3368 3369 int pos = line.indexOf(StringPool.SPACE); 3370 3371 String linePart = line.substring(0, pos); 3372 3373 return new Tuple(previousLine + StringPool.SPACE, linePart, true); 3374 } 3375 3376 int previousLineLength = _getLineLength(previousLine); 3377 3378 if ((line.length() + previousLineLength) < 80) { 3379 if (trimmedPreviousLine.startsWith("for ") && 3380 previousLine.endsWith(StringPool.COLON) && 3381 line.endsWith(StringPool.OPEN_CURLY_BRACE)) { 3382 3383 return new Tuple(previousLine + StringPool.SPACE + line); 3384 } 3385 3386 if ((previousLine.endsWith(StringPool.EQUAL) || 3387 previousLine.endsWith(StringPool.PERIOD) || 3388 trimmedPreviousLine.equals("return")) && 3389 line.endsWith(StringPool.SEMICOLON)) { 3390 3391 return new Tuple(previousLine + StringPool.SPACE + line); 3392 } 3393 3394 if ((trimmedPreviousLine.startsWith("if ") || 3395 trimmedPreviousLine.startsWith("else ")) && 3396 (previousLine.endsWith("||") || previousLine.endsWith("&&")) && 3397 line.endsWith(StringPool.OPEN_CURLY_BRACE)) { 3398 3399 return new Tuple(previousLine + StringPool.SPACE + line); 3400 } 3401 3402 if ((line.startsWith("extends ") || 3403 line.startsWith("implements ") || 3404 line.startsWith("throws")) && 3405 line.endsWith(StringPool.OPEN_CURLY_BRACE) && 3406 (lineTabCount == (previousLineTabCount + 1))) { 3407 3408 return new Tuple(previousLine + StringPool.SPACE + line); 3409 } 3410 } 3411 3412 if (previousLine.endsWith(StringPool.EQUAL) && 3413 line.endsWith(StringPool.SEMICOLON)) { 3414 3415 String tempLine = line; 3416 3417 for (int pos = 0;;) { 3418 pos = tempLine.indexOf(StringPool.DASH); 3419 3420 if (pos == -1) { 3421 pos = tempLine.indexOf(StringPool.PLUS); 3422 } 3423 3424 if (pos == -1) { 3425 pos = tempLine.indexOf(StringPool.SLASH); 3426 } 3427 3428 if (pos == -1) { 3429 pos = tempLine.indexOf(StringPool.STAR); 3430 } 3431 3432 if (pos == -1) { 3433 pos = tempLine.indexOf("||"); 3434 } 3435 3436 if (pos == -1) { 3437 pos = tempLine.indexOf("&&"); 3438 } 3439 3440 if (pos == -1) { 3441 break; 3442 } 3443 3444 String linePart = tempLine.substring(0, pos); 3445 3446 int openParenthesisCount = StringUtil.count( 3447 linePart, StringPool.OPEN_PARENTHESIS); 3448 int closeParenthesisCount = StringUtil.count( 3449 linePart, StringPool.CLOSE_PARENTHESIS); 3450 3451 if (openParenthesisCount == closeParenthesisCount) { 3452 return null; 3453 } 3454 3455 tempLine = 3456 tempLine.substring(0, pos) + tempLine.substring(pos + 1); 3457 } 3458 3459 int x = line.indexOf(StringPool.OPEN_PARENTHESIS); 3460 3461 if (x == 0) { 3462 x = line.indexOf(StringPool.OPEN_PARENTHESIS, 1); 3463 } 3464 3465 if (x != -1) { 3466 int y = line.indexOf(StringPool.CLOSE_PARENTHESIS, x); 3467 int z = line.indexOf(StringPool.QUOTE); 3468 3469 if (((x + 1) != y) && ((z == -1) || (z > x))) { 3470 char previousChar = line.charAt(x - 1); 3471 3472 if ((previousChar != CharPool.CLOSE_PARENTHESIS) && 3473 (previousChar != CharPool.OPEN_PARENTHESIS) && 3474 (previousChar != CharPool.SPACE) && 3475 (previousLineLength + 1 + x) < 80) { 3476 3477 String linePart = line.substring(0, x + 1); 3478 3479 if (linePart.startsWith(StringPool.OPEN_PARENTHESIS) && 3480 !linePart.contains( 3481 StringPool.CLOSE_PARENTHESIS)) { 3482 3483 return null; 3484 } 3485 3486 return new Tuple( 3487 previousLine + StringPool.SPACE, linePart, true); 3488 } 3489 } 3490 } 3491 } 3492 3493 if (previousLine.endsWith(StringPool.COMMA) && 3494 (previousLineTabCount == lineTabCount) && 3495 !previousLine.contains(StringPool.CLOSE_CURLY_BRACE)) { 3496 3497 int x = line.indexOf(StringPool.COMMA); 3498 3499 if (x != -1) { 3500 while ((previousLineLength + 1 + x) < 80) { 3501 String linePart = line.substring(0, x + 1); 3502 3503 if (_isValidJavaParameter(linePart)) { 3504 if (line.equals(linePart)) { 3505 return new Tuple( 3506 previousLine + StringPool.SPACE + linePart); 3507 } 3508 else { 3509 return new Tuple( 3510 previousLine + StringPool.SPACE, 3511 linePart + StringPool.SPACE, true); 3512 } 3513 } 3514 3515 String partAfterComma = line.substring(x + 1); 3516 3517 int pos = partAfterComma.indexOf(StringPool.COMMA); 3518 3519 if (pos == -1) { 3520 break; 3521 } 3522 3523 x = x + pos + 1; 3524 } 3525 } 3526 else if (!line.endsWith(StringPool.OPEN_PARENTHESIS) && 3527 !line.endsWith(StringPool.PLUS) && 3528 !line.endsWith(StringPool.PERIOD) && 3529 (!line.startsWith("new ") || 3530 !line.endsWith(StringPool.OPEN_CURLY_BRACE)) && 3531 ((line.length() + previousLineLength) < 80)) { 3532 3533 return new Tuple(previousLine + StringPool.SPACE + line); 3534 } 3535 } 3536 3537 if (!previousLine.endsWith(StringPool.OPEN_PARENTHESIS)) { 3538 return null; 3539 } 3540 3541 if (StringUtil.count(previousLine, StringPool.OPEN_PARENTHESIS) > 1) { 3542 int pos = trimmedPreviousLine.lastIndexOf( 3543 StringPool.OPEN_PARENTHESIS, trimmedPreviousLine.length() - 2); 3544 3545 if ((pos > 0) && 3546 Character.isLetterOrDigit( 3547 trimmedPreviousLine.charAt(pos -1 ))) { 3548 3549 String filePart = trimmedPreviousLine.substring(pos + 1); 3550 3551 if (!filePart.contains(StringPool.CLOSE_PARENTHESIS) && 3552 !filePart.contains(StringPool.QUOTE)) { 3553 3554 return new Tuple(previousLine, filePart, false); 3555 } 3556 } 3557 } 3558 3559 if ((line.length() + previousLineLength) > 80) { 3560 return null; 3561 } 3562 3563 if (line.endsWith(StringPool.SEMICOLON)) { 3564 return new Tuple(previousLine + line); 3565 } 3566 3567 if (((line.endsWith(StringPool.OPEN_CURLY_BRACE) && 3568 !line.startsWith("new ")) || 3569 line.endsWith(StringPool.CLOSE_PARENTHESIS)) && 3570 (trimmedPreviousLine.startsWith("else ") || 3571 trimmedPreviousLine.startsWith("if ") || 3572 trimmedPreviousLine.startsWith("private ") || 3573 trimmedPreviousLine.startsWith("protected ") || 3574 trimmedPreviousLine.startsWith("public "))) { 3575 3576 return new Tuple(previousLine + line); 3577 } 3578 3579 return null; 3580 } 3581 3582 private static String _getConstructorOrMethodName(String line, int pos) { 3583 line = line.substring(0, pos); 3584 3585 int x = line.lastIndexOf(StringPool.SPACE); 3586 3587 return line.substring(x + 1); 3588 } 3589 3590 private static String _getCopyright() throws IOException { 3591 String copyright = _fileUtil.read("copyright.txt"); 3592 3593 if (Validator.isNull(copyright)) { 3594 copyright = _fileUtil.read("../copyright.txt"); 3595 } 3596 3597 if (Validator.isNull(copyright)) { 3598 copyright = _fileUtil.read("../../copyright.txt"); 3599 } 3600 3601 return copyright; 3602 } 3603 3604 private static String _getCustomCopyright(File file) throws IOException { 3605 String absolutePath = _fileUtil.getAbsolutePath(file); 3606 3607 for (int x = absolutePath.length();;) { 3608 x = absolutePath.lastIndexOf(StringPool.SLASH, x); 3609 3610 if (x == -1) { 3611 break; 3612 } 3613 3614 String copyright = _fileUtil.read( 3615 absolutePath.substring(0, x + 1) + "copyright.txt"); 3616 3617 if (Validator.isNotNull(copyright)) { 3618 return copyright; 3619 } 3620 3621 x = x - 1; 3622 } 3623 3624 return null; 3625 } 3626 3627 private static Tuple _getJavaTermTuple(String line) { 3628 int pos = line.indexOf(StringPool.OPEN_PARENTHESIS); 3629 3630 if (line.startsWith(StringPool.TAB + "public static final ") && 3631 (line.contains(StringPool.EQUAL) || 3632 (line.endsWith(StringPool.SEMICOLON) && (pos == -1)))) { 3633 3634 return new Tuple( 3635 _getVariableName(line), _TYPE_VARIABLE_PUBLIC_STATIC_FINAL); 3636 } 3637 else if (line.startsWith(StringPool.TAB + "public static ")) { 3638 if (line.contains(StringPool.EQUAL) || 3639 (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) { 3640 3641 return new Tuple( 3642 _getVariableName(line), _TYPE_VARIABLE_PUBLIC_STATIC); 3643 } 3644 3645 if (pos != -1) { 3646 return new Tuple( 3647 _getConstructorOrMethodName(line, pos), 3648 _TYPE_METHOD_PUBLIC_STATIC); 3649 } 3650 3651 if (line.startsWith(StringPool.TAB + "public static class ") || 3652 line.startsWith(StringPool.TAB + "public static enum")) { 3653 3654 return new Tuple( 3655 _getClassName(line), _TYPE_CLASS_PUBLIC_STATIC); 3656 } 3657 } 3658 else if (line.startsWith(StringPool.TAB + "public ")) { 3659 if (line.contains(StringPool.EQUAL) || 3660 (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) { 3661 3662 return new Tuple(_getVariableName(line), _TYPE_VARIABLE_PUBLIC); 3663 } 3664 3665 if (pos != -1) { 3666 int spaceCount = StringUtil.count( 3667 line.substring(0, pos), StringPool.SPACE); 3668 3669 if (spaceCount == 1) { 3670 return new Tuple( 3671 _getConstructorOrMethodName(line, pos), 3672 _TYPE_CONSTRUCTOR_PUBLIC); 3673 } 3674 3675 if (spaceCount > 1) { 3676 return new Tuple( 3677 _getConstructorOrMethodName(line, pos), 3678 _TYPE_METHOD_PUBLIC); 3679 } 3680 } 3681 else if (line.startsWith(StringPool.TAB + "public class ") || 3682 line.startsWith(StringPool.TAB + "public enum")) { 3683 3684 return new Tuple(_getClassName(line), _TYPE_CLASS_PUBLIC); 3685 } 3686 } 3687 else if (line.startsWith(StringPool.TAB + "protected static final ")) { 3688 if (line.contains(StringPool.EQUAL) || 3689 (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) { 3690 3691 return new Tuple( 3692 _getVariableName(line), 3693 _TYPE_VARIABLE_PROTECTED_STATIC_FINAL); 3694 } 3695 } 3696 else if (line.startsWith(StringPool.TAB + "protected static ")) { 3697 if (line.contains(StringPool.EQUAL) || 3698 (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) { 3699 3700 return new Tuple( 3701 _getVariableName(line), _TYPE_VARIABLE_PROTECTED_STATIC); 3702 } 3703 3704 if (pos != -1) { 3705 return new Tuple( 3706 _getConstructorOrMethodName(line, pos), 3707 _TYPE_METHOD_PROTECTED_STATIC); 3708 } 3709 3710 if (line.startsWith(StringPool.TAB + "protected static class ") || 3711 line.startsWith(StringPool.TAB + "protected static enum ")) { 3712 3713 return new Tuple( 3714 _getClassName(line), _TYPE_CLASS_PROTECTED_STATIC); 3715 } 3716 } 3717 else if (line.startsWith(StringPool.TAB + "protected ")) { 3718 if (pos != -1) { 3719 if (!line.contains(StringPool.EQUAL)) { 3720 int spaceCount = StringUtil.count( 3721 line.substring(0, pos), StringPool.SPACE); 3722 3723 if (spaceCount == 1) { 3724 return new Tuple( 3725 _getConstructorOrMethodName(line, pos), 3726 _TYPE_CONSTRUCTOR_PROTECTED); 3727 } 3728 3729 if (spaceCount > 1) { 3730 return new Tuple( 3731 _getConstructorOrMethodName(line, pos), 3732 _TYPE_METHOD_PROTECTED); 3733 } 3734 } 3735 } 3736 else if (line.startsWith(StringPool.TAB + "protected class ") || 3737 line.startsWith(StringPool.TAB + "protected enum ")) { 3738 3739 return new Tuple(_getClassName(line), _TYPE_CLASS_PROTECTED); 3740 } 3741 3742 return new Tuple(_getVariableName(line), _TYPE_VARIABLE_PROTECTED); 3743 } 3744 else if (line.startsWith(StringPool.TAB + "private static final ")) { 3745 if (line.contains(StringPool.EQUAL) || 3746 (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) { 3747 3748 return new Tuple( 3749 _getVariableName(line), 3750 _TYPE_VARIABLE_PRIVATE_STATIC_FINAL); 3751 } 3752 } 3753 else if (line.startsWith(StringPool.TAB + "private static ")) { 3754 if (line.contains(StringPool.EQUAL) || 3755 (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) { 3756 3757 return new Tuple( 3758 _getVariableName(line), _TYPE_VARIABLE_PRIVATE_STATIC); 3759 } 3760 3761 if (pos != -1) { 3762 return new Tuple( 3763 _getConstructorOrMethodName(line, pos), 3764 _TYPE_METHOD_PRIVATE_STATIC); 3765 } 3766 3767 if (line.startsWith(StringPool.TAB + "private static class ") || 3768 line.startsWith(StringPool.TAB + "private static enum ")) { 3769 3770 return new Tuple( 3771 _getClassName(line), _TYPE_CLASS_PRIVATE_STATIC); 3772 } 3773 } 3774 else if (line.startsWith(StringPool.TAB + "private ")) { 3775 if (line.contains(StringPool.EQUAL) || 3776 (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) { 3777 3778 return new Tuple( 3779 _getVariableName(line), _TYPE_VARIABLE_PRIVATE); 3780 } 3781 3782 if (pos != -1) { 3783 int spaceCount = StringUtil.count( 3784 line.substring(0, pos), StringPool.SPACE); 3785 3786 if (spaceCount == 1) { 3787 return new Tuple( 3788 _getConstructorOrMethodName(line, pos), 3789 _TYPE_CONSTRUCTOR_PRIVATE); 3790 } 3791 3792 if (spaceCount > 1) { 3793 return new Tuple( 3794 _getConstructorOrMethodName(line, pos), 3795 _TYPE_METHOD_PRIVATE); 3796 } 3797 } 3798 else if (line.startsWith(StringPool.TAB + "private class ") || 3799 line.startsWith(StringPool.TAB + "private enum ")) { 3800 3801 return new Tuple(_getClassName(line), _TYPE_CLASS_PRIVATE); 3802 } 3803 } 3804 3805 return null; 3806 } 3807 3808 private static List<String> _getJSPDuplicateImports( 3809 String fileName, String content, List<String> importLines) { 3810 3811 List<String> duplicateImports = new ArrayList<String>(); 3812 3813 for (String importLine : importLines) { 3814 int x = content.indexOf("<%@ include file="); 3815 3816 if (x == -1) { 3817 continue; 3818 } 3819 3820 int y = content.indexOf("<%@ page import="); 3821 3822 if (y == -1) { 3823 continue; 3824 } 3825 3826 if ((x < y) && _isJSPDuplicateImport(fileName, importLine, false)) { 3827 duplicateImports.add(importLine); 3828 } 3829 } 3830 3831 return duplicateImports; 3832 } 3833 3834 private static String[] _getLanguageKeys(Matcher matcher) { 3835 if (matcher.groupCount() > 0) { 3836 String languageKey = matcher.group(1); 3837 3838 if (Validator.isNotNull(languageKey)) { 3839 return new String[] {languageKey}; 3840 } 3841 } 3842 3843 StringBundler sb = new StringBundler(); 3844 3845 String match = matcher.group(); 3846 3847 int count = 0; 3848 3849 for (int i = 0; i < match.length(); i++) { 3850 char c = match.charAt(i); 3851 3852 switch (c) { 3853 case CharPool.CLOSE_PARENTHESIS: 3854 if (count <= 1) { 3855 return new String[0]; 3856 } 3857 3858 count--; 3859 3860 break; 3861 3862 case CharPool.OPEN_PARENTHESIS: 3863 count++; 3864 3865 break; 3866 3867 case CharPool.QUOTE: 3868 if (count > 1) { 3869 break; 3870 } 3871 3872 while (i < match.length()) { 3873 i++; 3874 3875 if (match.charAt(i) == CharPool.QUOTE) { 3876 String languageKey = sb.toString(); 3877 3878 if (match.startsWith("names")) { 3879 return StringUtil.split(languageKey); 3880 } 3881 else { 3882 return new String[] {languageKey}; 3883 } 3884 3885 } 3886 3887 sb.append(match.charAt(i)); 3888 } 3889 } 3890 } 3891 3892 return new String[0]; 3893 } 3894 3895 private static int _getLeadingTabCount(String line) { 3896 int leadingTabCount = 0; 3897 3898 while (line.startsWith(StringPool.TAB)) { 3899 line = line.substring(1); 3900 3901 leadingTabCount++; 3902 } 3903 3904 return leadingTabCount; 3905 } 3906 3907 private static int _getLineLength(String line) { 3908 int lineLength = 0; 3909 3910 int tabLength = 4; 3911 3912 for (char c : line.toCharArray()) { 3913 if (c == CharPool.TAB) { 3914 for (int i = 0; i < tabLength; i++) { 3915 lineLength++; 3916 } 3917 3918 tabLength = 4; 3919 } 3920 else { 3921 lineLength++; 3922 3923 tabLength--; 3924 3925 if (tabLength <= 0) { 3926 tabLength = 4; 3927 } 3928 } 3929 } 3930 3931 return lineLength; 3932 } 3933 3934 private static String _getOldCopyright() throws IOException { 3935 String copyright = _fileUtil.read("old-copyright.txt"); 3936 3937 if (Validator.isNull(copyright)) { 3938 copyright = _fileUtil.read("../old-copyright.txt"); 3939 } 3940 3941 if (Validator.isNull(copyright)) { 3942 copyright = _fileUtil.read("../../old-copyright.txt"); 3943 } 3944 3945 return copyright; 3946 } 3947 3948 private static Properties _getPluginExclusionsProperties(String fileName) 3949 throws IOException { 3950 3951 FileInputStream fileInputStream = null; 3952 3953 int level = 0; 3954 3955 try { 3956 fileInputStream = new FileInputStream(fileName); 3957 } 3958 catch (FileNotFoundException fnfe) { 3959 } 3960 3961 if (fileInputStream == null) { 3962 try { 3963 fileInputStream = new FileInputStream("../" + fileName); 3964 3965 level = 1; 3966 } 3967 catch (FileNotFoundException fnfe) { 3968 } 3969 } 3970 3971 if (fileInputStream == null) { 3972 try { 3973 fileInputStream = new FileInputStream("../../" + fileName); 3974 3975 level = 2; 3976 } 3977 catch (FileNotFoundException fnfe) { 3978 return null; 3979 } 3980 } 3981 3982 Properties properties = new Properties(); 3983 3984 properties.load(fileInputStream); 3985 3986 if (level > 0) { 3987 properties = _stripTopLevelDirectories(properties, level); 3988 } 3989 3990 return properties; 3991 } 3992 3993 private static Collection<String> _getPluginJavaFiles() { 3994 String basedir = "./"; 3995 3996 Collection<String> fileNames = new TreeSet<String>(); 3997 3998 DirectoryScanner directoryScanner = new DirectoryScanner(); 3999 4000 directoryScanner.setBasedir(basedir); 4001 4002 String[] excludes = { 4003 "**\\bin\\**", "**\\model\\*Clp.java", 4004 "**\\model\\impl\\*BaseImpl.java", "**\\model\\impl\\*Model.java", 4005 "**\\model\\impl\\*ModelImpl.java", 4006 "**\\service\\**\\model\\*Model.java", 4007 "**\\service\\**\\model\\*Soap.java", 4008 "**\\service\\**\\model\\*Wrapper.java", 4009 "**\\service\\**\\service\\*Service.java", 4010 "**\\service\\**\\service\\*ServiceClp.java", 4011 "**\\service\\**\\service\\*ServiceFactory.java", 4012 "**\\service\\**\\service\\*ServiceUtil.java", 4013 "**\\service\\**\\service\\*ServiceWrapper.java", 4014 "**\\service\\**\\service\\ClpSerializer.java", 4015 "**\\service\\**\\service\\messaging\\*ClpMessageListener.java", 4016 "**\\service\\**\\service\\persistence\\*Finder.java", 4017 "**\\service\\**\\service\\persistence\\*Persistence.java", 4018 "**\\service\\**\\service\\persistence\\*Util.java", 4019 "**\\service\\base\\*ServiceBaseImpl.java", 4020 "**\\service\\base\\*ServiceClpInvoker.java", 4021 "**\\service\\http\\*JSONSerializer.java", 4022 "**\\service\\http\\*ServiceHttp.java", 4023 "**\\service\\http\\*ServiceJSON.java", 4024 "**\\service\\http\\*ServiceSoap.java", 4025 "**\\service\\persistence\\*PersistenceImpl.java", "**\\tmp\\**" 4026 }; 4027 4028 excludes = ArrayUtil.append(excludes, _excludes); 4029 4030 directoryScanner.setExcludes(excludes); 4031 4032 directoryScanner.setIncludes(new String[] {"**\\*.java"}); 4033 4034 fileNames.addAll(_sourceFormatterHelper.scanForFiles(directoryScanner)); 4035 4036 return fileNames; 4037 } 4038 4039 private static Properties _getPortalExclusionsProperties(String fileName) 4040 throws IOException { 4041 4042 Properties properties = new Properties(); 4043 4044 ClassLoader classLoader = SourceFormatter.class.getClassLoader(); 4045 4046 String sourceFormatterExclusions = System.getProperty( 4047 "source-formatter-exclusions", 4048 "com/liferay/portal/tools/dependencies/" + fileName); 4049 4050 URL url = classLoader.getResource(sourceFormatterExclusions); 4051 4052 if (url == null) { 4053 return null; 4054 } 4055 4056 InputStream inputStream = url.openStream(); 4057 4058 properties.load(inputStream); 4059 4060 inputStream.close(); 4061 4062 return properties; 4063 } 4064 4065 private static Collection<String> _getPortalJavaFiles() { 4066 String basedir = "./"; 4067 4068 Collection<String> fileNames = new TreeSet<String>(); 4069 4070 DirectoryScanner directoryScanner = new DirectoryScanner(); 4071 4072 directoryScanner.setBasedir(basedir); 4073 4074 String[] excludes = { 4075 "**\\*_IW.java", "**\\PropsValues.java", "**\\bin\\**", 4076 "**\\classes\\*", "**\\counter\\service\\**", "**\\jsp\\*", 4077 "**\\model\\impl\\*BaseImpl.java", "**\\model\\impl\\*Model.java", 4078 "**\\model\\impl\\*ModelImpl.java", "**\\portal\\service\\**", 4079 "**\\portal-client\\**", 4080 "**\\portal-service\\**\\model\\*Model.java", 4081 "**\\portal-service\\**\\model\\*Soap.java", 4082 "**\\portal-service\\**\\model\\*Wrapper.java", 4083 "**\\portal-web\\classes\\**\\*.java", 4084 "**\\portal-web\\test\\**\\*Test.java", 4085 "**\\portal-web\\test\\**\\*Tests.java", 4086 "**\\portlet\\**\\service\\**", "**\\test\\*-generated\\**", 4087 "**\\tmp\\**", "**\\tools\\tck\\**" 4088 }; 4089 4090 excludes = ArrayUtil.append(excludes, _excludes); 4091 4092 directoryScanner.setExcludes(excludes); 4093 4094 directoryScanner.setIncludes(new String[] {"**\\*.java"}); 4095 4096 fileNames.addAll(_sourceFormatterHelper.scanForFiles(directoryScanner)); 4097 4098 directoryScanner = new DirectoryScanner(); 4099 4100 directoryScanner.setBasedir(basedir); 4101 4102 excludes = new String[] { 4103 "**\\bin\\**", "**\\portal-client\\**", "**\\tools\\ext_tmpl\\**", 4104 "**\\*_IW.java", "**\\test\\**\\*PersistenceTest.java" 4105 }; 4106 4107 excludes = ArrayUtil.append(excludes, _excludes); 4108 4109 directoryScanner.setExcludes(excludes); 4110 4111 directoryScanner.setIncludes( 4112 new String[] { 4113 "**\\com\\liferay\\portal\\service\\ServiceContext*.java", 4114 "**\\model\\BaseModel.java", 4115 "**\\model\\impl\\BaseModelImpl.java", 4116 "**\\service\\Base*.java", 4117 "**\\service\\PersistedModelLocalService*.java", 4118 "**\\service\\base\\PrincipalBean.java", 4119 "**\\service\\http\\*HttpTest.java", 4120 "**\\service\\http\\*SoapTest.java", 4121 "**\\service\\http\\TunnelUtil.java", 4122 "**\\service\\impl\\*.java", "**\\service\\jms\\*.java", 4123 "**\\service\\permission\\*.java", 4124 "**\\service\\persistence\\BasePersistence.java", 4125 "**\\service\\persistence\\BatchSession*.java", 4126 "**\\service\\persistence\\*FinderImpl.java", 4127 "**\\service\\persistence\\*Query.java", 4128 "**\\service\\persistence\\impl\\BasePersistenceImpl.java", 4129 "**\\portal-impl\\test\\**\\*.java", 4130 "**\\portal-service\\**\\liferay\\documentlibrary\\**.java", 4131 "**\\portal-service\\**\\liferay\\lock\\**.java", 4132 "**\\portal-service\\**\\liferay\\mail\\**.java", 4133 "**\\util-bridges\\**\\*.java" 4134 }); 4135 4136 fileNames.addAll(_sourceFormatterHelper.scanForFiles(directoryScanner)); 4137 4138 return fileNames; 4139 } 4140 4141 private static String _getTaglibRegex(String quoteType) { 4142 StringBuilder sb = new StringBuilder(); 4143 4144 sb.append("<("); 4145 4146 for (int i = 0; i < _TAG_LIBRARIES.length; i++) { 4147 sb.append(_TAG_LIBRARIES[i]); 4148 sb.append(StringPool.PIPE); 4149 } 4150 4151 sb.deleteCharAt(sb.length() - 1); 4152 sb.append("):([^>]|%>)*"); 4153 sb.append(quoteType); 4154 sb.append("<%=.*"); 4155 sb.append(quoteType); 4156 sb.append(".*%>"); 4157 sb.append(quoteType); 4158 sb.append("([^>]|%>)*>"); 4159 4160 return sb.toString(); 4161 } 4162 4163 private static String _getVariableName(String line) { 4164 int x = line.indexOf(StringPool.EQUAL); 4165 int y = line.lastIndexOf(StringPool.SPACE); 4166 4167 if (x != -1) { 4168 line = line.substring(0, x); 4169 line = StringUtil.trim(line); 4170 4171 y = line.lastIndexOf(StringPool.SPACE); 4172 4173 return line.substring(y + 1); 4174 } 4175 4176 if (line.endsWith(StringPool.SEMICOLON)) { 4177 return line.substring(y + 1, line.length() - 1); 4178 } 4179 4180 return StringPool.BLANK; 4181 } 4182 4183 private static boolean _hasUnsortedConstructorsOrMethods( 4184 List<String> previousMethodParameterTypes, 4185 List<String> methodParameterTypes) { 4186 4187 if (methodParameterTypes.isEmpty()) { 4188 return true; 4189 } 4190 4191 for (int i = 0; i < previousMethodParameterTypes.size(); i++) { 4192 if (methodParameterTypes.size() < (i + 1)) { 4193 return true; 4194 } 4195 4196 String previousParameterType = previousMethodParameterTypes.get(i); 4197 4198 if (previousParameterType.endsWith("...")) { 4199 previousParameterType = StringUtil.replaceLast( 4200 previousParameterType, "...", StringPool.BLANK); 4201 } 4202 4203 String parameterType = methodParameterTypes.get(i); 4204 4205 if (previousParameterType.compareToIgnoreCase(parameterType) < 0) { 4206 return false; 4207 } 4208 4209 if (previousParameterType.compareToIgnoreCase(parameterType) > 0) { 4210 return true; 4211 } 4212 4213 if (previousParameterType.compareTo(parameterType) > 0) { 4214 return false; 4215 } 4216 4217 if (previousParameterType.compareTo(parameterType) < 0) { 4218 return true; 4219 } 4220 } 4221 4222 return false; 4223 } 4224 4225 private static boolean _hasUnsortedJavaTerms( 4226 String fileName, String previousJavaTermName, String javaTermName, 4227 int javaTermType) { 4228 4229 if (Validator.isNull(previousJavaTermName) || 4230 Validator.isNull(javaTermName)) { 4231 4232 return false; 4233 } 4234 4235 if (javaTermType == _TYPE_VARIABLE_PRIVATE_STATIC) { 4236 if (javaTermName.equals("_log")) { 4237 return true; 4238 } 4239 4240 if (previousJavaTermName.equals("_instance") || 4241 previousJavaTermName.equals("_log")) { 4242 4243 return false; 4244 } 4245 4246 if (javaTermName.equals("_instance")) { 4247 return true; 4248 } 4249 } 4250 4251 String javaTermNameLowerCase = javaTermName.toLowerCase(); 4252 String previousJavaTermNameLowerCase = 4253 previousJavaTermName.toLowerCase(); 4254 4255 if (fileName.contains("persistence") && 4256 ((previousJavaTermName.startsWith("doCount") && 4257 javaTermName.startsWith("doCount")) || 4258 (previousJavaTermName.startsWith("doFind") && 4259 javaTermName.startsWith("doFind")) || 4260 (previousJavaTermNameLowerCase.startsWith("count") && 4261 javaTermNameLowerCase.startsWith("count")) || 4262 (previousJavaTermNameLowerCase.startsWith("filter") && 4263 javaTermNameLowerCase.startsWith("filter")) || 4264 (previousJavaTermNameLowerCase.startsWith("find") && 4265 javaTermNameLowerCase.startsWith("find")) || 4266 (previousJavaTermNameLowerCase.startsWith("join") && 4267 javaTermNameLowerCase.startsWith("join")))) { 4268 4269 return false; 4270 } 4271 4272 if (previousJavaTermName.compareToIgnoreCase(javaTermName) <= 0) { 4273 return false; 4274 } 4275 4276 return true; 4277 } 4278 4279 private static boolean _isGenerated(String content) { 4280 if (content.contains("* @generated") || content.contains("$ANTLR")) { 4281 return true; 4282 } 4283 else { 4284 return false; 4285 } 4286 } 4287 4288 private static boolean _isInJavaTermTypeGroup( 4289 int javaTermType, int[] javaTermTypeGroup) { 4290 4291 for (int type : javaTermTypeGroup) { 4292 if (javaTermType == type) { 4293 return true; 4294 } 4295 } 4296 4297 return false; 4298 } 4299 4300 private static boolean _isJSPAttributName(String attributeName) { 4301 if (Validator.isNull(attributeName)) { 4302 return false; 4303 } 4304 4305 Matcher matcher = _jspAttributeNamePattern.matcher(attributeName); 4306 4307 return matcher.matches(); 4308 } 4309 4310 private static boolean _isJSPDuplicateImport( 4311 String fileName, String importLine, boolean checkFile) { 4312 4313 String content = _jspContents.get(fileName); 4314 4315 if (Validator.isNull(content)) { 4316 return false; 4317 } 4318 4319 int x = importLine.indexOf("page"); 4320 4321 if (x == -1) { 4322 return false; 4323 } 4324 4325 if (checkFile && content.contains(importLine.substring(x))) { 4326 return true; 4327 } 4328 4329 int y = content.indexOf("<%@ include file="); 4330 4331 if (y == -1) { 4332 return false; 4333 } 4334 4335 y = content.indexOf(StringPool.QUOTE, y); 4336 4337 if (y == -1) { 4338 return false; 4339 } 4340 4341 int z = content.indexOf(StringPool.QUOTE, y + 1); 4342 4343 if (z == -1) { 4344 return false; 4345 } 4346 4347 String includeFileName = content.substring(y + 1, z); 4348 4349 String docrootPath = fileName.substring( 4350 0, fileName.indexOf("docroot") + 7); 4351 4352 includeFileName = docrootPath + includeFileName; 4353 4354 return _isJSPDuplicateImport(includeFileName, importLine, true); 4355 } 4356 4357 private static boolean _isJSPImportRequired( 4358 String fileName, String className, Set<String> includeFileNames, 4359 Set<String> checkedFileNames) { 4360 4361 if (checkedFileNames.contains(fileName)) { 4362 return false; 4363 } 4364 4365 checkedFileNames.add(fileName); 4366 4367 String content = _jspContents.get(fileName); 4368 4369 if (Validator.isNull(content)) { 4370 return false; 4371 } 4372 4373 Pattern pattern = Pattern.compile( 4374 "[^A-Za-z0-9_]" + className + "[^A-Za-z0-9_\"]"); 4375 4376 Matcher matcher = pattern.matcher(content); 4377 4378 if (matcher.find()) { 4379 return true; 4380 } 4381 4382 _addJSPIncludeFileNames(fileName, includeFileNames); 4383 4384 String docrootPath = fileName.substring( 4385 0, fileName.indexOf("docroot") + 7); 4386 4387 fileName = fileName.replaceFirst(docrootPath, StringPool.BLANK); 4388 4389 if (fileName.endsWith("init.jsp") || 4390 fileName.contains("init-ext.jsp")) { 4391 4392 _addJSPReferenceFileNames(fileName, includeFileNames); 4393 } 4394 4395 String[] includeFileNamesArray = includeFileNames.toArray( 4396 new String[includeFileNames.size()]); 4397 4398 for (String includeFileName : includeFileNamesArray) { 4399 if (!checkedFileNames.contains(includeFileName) && 4400 _isJSPImportRequired( 4401 includeFileName, className, includeFileNames, 4402 checkedFileNames)) { 4403 4404 return true; 4405 } 4406 } 4407 4408 return false; 4409 } 4410 4411 private static boolean _isPortalSource() { 4412 String basedir = "./"; 4413 4414 if (_fileUtil.exists(basedir + "portal-impl")) { 4415 return true; 4416 } 4417 else { 4418 return false; 4419 } 4420 } 4421 4422 private static boolean _isValidJavaParameter(String javaParameter) { 4423 int quoteCount = StringUtil.count(javaParameter, StringPool.QUOTE); 4424 4425 if ((quoteCount % 2) == 1) { 4426 return false; 4427 } 4428 4429 javaParameter = _stripQuotes(javaParameter); 4430 4431 int openParenthesisCount = StringUtil.count( 4432 javaParameter, StringPool.OPEN_PARENTHESIS); 4433 int closeParenthesisCount = StringUtil.count( 4434 javaParameter, StringPool.CLOSE_PARENTHESIS); 4435 int lessThanCount = StringUtil.count( 4436 javaParameter, StringPool.LESS_THAN); 4437 int greaterThanCount = StringUtil.count( 4438 javaParameter, StringPool.GREATER_THAN); 4439 int openCurlyBraceCount = StringUtil.count( 4440 javaParameter, StringPool.OPEN_CURLY_BRACE); 4441 int closeCurlyBraceCount = StringUtil.count( 4442 javaParameter, StringPool.CLOSE_CURLY_BRACE); 4443 4444 if ((openParenthesisCount == closeParenthesisCount) && 4445 (lessThanCount == greaterThanCount) && 4446 (openCurlyBraceCount == closeCurlyBraceCount)) { 4447 4448 return true; 4449 } 4450 4451 return false; 4452 } 4453 4454 private static String _replacePrimitiveWrapperInstantiation( 4455 String fileName, String line, int lineCount) { 4456 4457 if (true) { 4458 return line; 4459 } 4460 4461 String newLine = StringUtil.replace( 4462 line, 4463 new String[] { 4464 "new Boolean(", "new Byte(", "new Character(", "new Integer(", 4465 "new Long(", "new Short(" 4466 }, 4467 new String[] { 4468 "Boolean.valueOf(", "Byte.valueOf(", "Character.valueOf(", 4469 "Integer.valueOf(", "Long.valueOf(", "Short.valueOf(" 4470 }); 4471 4472 if (!line.equals(newLine)) { 4473 _sourceFormatterHelper.printError( 4474 fileName, "> new Primitive(: " + fileName + " " + lineCount); 4475 } 4476 4477 return newLine; 4478 } 4479 4480 private static String _sortExceptions(String line) { 4481 if (!line.endsWith(StringPool.OPEN_CURLY_BRACE) && 4482 !line.endsWith(StringPool.SEMICOLON)) { 4483 4484 return line; 4485 } 4486 4487 int x = line.indexOf("throws "); 4488 4489 if (x == -1) { 4490 return line; 4491 } 4492 4493 String previousException = StringPool.BLANK; 4494 4495 String[] exceptions = StringUtil.split( 4496 line.substring(x), CharPool.SPACE); 4497 4498 for (int i = 1; i < exceptions.length; i++) { 4499 String exception = exceptions[i]; 4500 4501 if (exception.equals(StringPool.OPEN_CURLY_BRACE)) { 4502 break; 4503 } 4504 4505 if (exception.endsWith(StringPool.COMMA) || 4506 exception.endsWith(StringPool.SEMICOLON)) { 4507 4508 exception = exception.substring(0, exception.length() - 1); 4509 } 4510 4511 if (Validator.isNotNull(previousException) && 4512 (previousException.compareToIgnoreCase(exception) > 0)) { 4513 4514 return StringUtil.replace( 4515 line, previousException + ", " + exception, 4516 exception + ", " + previousException); 4517 } 4518 4519 previousException = exception; 4520 } 4521 4522 return line; 4523 } 4524 4525 private static String _sortJSPAttributes( 4526 String fileName, String line, int lineCount) { 4527 4528 String s = line; 4529 4530 int x = s.indexOf(StringPool.SPACE); 4531 4532 if (x == -1) { 4533 return line; 4534 } 4535 4536 s = s.substring(x + 1); 4537 4538 String previousAttribute = null; 4539 String previousAttributeAndValue = null; 4540 4541 boolean wrongOrder = false; 4542 4543 for (x = 0;;) { 4544 x = s.indexOf(StringPool.EQUAL); 4545 4546 if ((x == -1) || (s.length() <= (x + 1))) { 4547 return line; 4548 } 4549 4550 String attribute = s.substring(0, x); 4551 4552 if (!_isJSPAttributName(attribute)) { 4553 return line; 4554 } 4555 4556 if (Validator.isNotNull(previousAttribute) && 4557 (previousAttribute.compareTo(attribute) > 0)) { 4558 4559 wrongOrder = true; 4560 } 4561 4562 s = s.substring(x + 1); 4563 4564 char delimeter = s.charAt(0); 4565 4566 if ((delimeter != CharPool.APOSTROPHE) && 4567 (delimeter != CharPool.QUOTE)) { 4568 4569 _sourceFormatterHelper.printError( 4570 fileName, "delimeter: " + fileName + " " + lineCount); 4571 4572 return line; 4573 } 4574 4575 s = s.substring(1); 4576 4577 int y = s.indexOf(delimeter); 4578 4579 if ((y == -1) || (s.length() <= (y + 1))) { 4580 return line; 4581 } 4582 4583 String value = s.substring(0, y); 4584 4585 if ((delimeter == CharPool.APOSTROPHE) && 4586 !value.contains(StringPool.QUOTE)) { 4587 4588 return StringUtil.replace( 4589 line, StringPool.APOSTROPHE + value + StringPool.APOSTROPHE, 4590 StringPool.QUOTE + value + StringPool.QUOTE); 4591 } 4592 4593 if (value.contains("<%") && !value.contains("%>")) { 4594 int z = s.indexOf("%>"); 4595 4596 if (z == -1) { 4597 return line; 4598 } 4599 4600 y = s.substring(z).indexOf(delimeter); 4601 4602 value = s.substring(0, y + z); 4603 } 4604 4605 StringBundler sb = new StringBundler(5); 4606 4607 sb.append(attribute); 4608 sb.append(StringPool.EQUAL); 4609 sb.append(delimeter); 4610 sb.append(value); 4611 sb.append(delimeter); 4612 4613 String currentAttributeAndValue = sb.toString(); 4614 4615 if (wrongOrder) { 4616 if (line.contains(currentAttributeAndValue) && 4617 line.contains(previousAttributeAndValue)) { 4618 4619 line = StringUtil.replaceFirst( 4620 line, previousAttributeAndValue, 4621 currentAttributeAndValue); 4622 4623 line = StringUtil.replaceLast( 4624 line, currentAttributeAndValue, 4625 previousAttributeAndValue); 4626 } 4627 4628 return line; 4629 } 4630 4631 s = s.substring(y + 1); 4632 4633 s = StringUtil.trimLeading(s); 4634 4635 previousAttribute = attribute; 4636 previousAttributeAndValue = currentAttributeAndValue; 4637 } 4638 } 4639 4640 private static String _stripJSPImports(String fileName, String content) 4641 throws IOException { 4642 4643 fileName = fileName.replace( 4644 CharPool.BACK_SLASH, CharPool.FORWARD_SLASH); 4645 4646 if (!fileName.contains("docroot") || 4647 fileName.endsWith("init-ext.jsp")) { 4648 4649 return content; 4650 } 4651 4652 Matcher matcher = _jspImportPattern.matcher(content); 4653 4654 if (!matcher.find()) { 4655 return content; 4656 } 4657 4658 String imports = matcher.group(); 4659 4660 imports = StringUtil.replace( 4661 imports, new String[] {"%><%@\r\n", "%><%@\n"}, 4662 new String[] {"%>\r\n<%@ ", "%>\n<%@ "}); 4663 4664 if (!fileName.endsWith("html/common/init.jsp") && 4665 !fileName.endsWith("html/portal/init.jsp")) { 4666 4667 List<String> importLines = new ArrayList<String>(); 4668 4669 UnsyncBufferedReader unsyncBufferedReader = 4670 new UnsyncBufferedReader(new UnsyncStringReader(imports)); 4671 4672 String line = null; 4673 4674 while ((line = unsyncBufferedReader.readLine()) != null) { 4675 if (line.contains("import=")) { 4676 importLines.add(line); 4677 } 4678 } 4679 4680 List<String> unneededImports = _getJSPDuplicateImports( 4681 fileName, content, importLines); 4682 4683 _addJSPUnusedImports(fileName, importLines, unneededImports); 4684 4685 for (String unneededImport : unneededImports) { 4686 imports = StringUtil.replace( 4687 imports, unneededImport, StringPool.BLANK); 4688 } 4689 } 4690 4691 imports = _formatImports(imports, 17); 4692 4693 String beforeImports = content.substring(0, matcher.start()); 4694 4695 if (Validator.isNull(imports)) { 4696 beforeImports = StringUtil.replaceLast( 4697 beforeImports, "\n", StringPool.BLANK); 4698 } 4699 4700 String afterImports = content.substring(matcher.end()); 4701 4702 if (Validator.isNull(afterImports)) { 4703 imports = StringUtil.replaceLast(imports, "\n", StringPool.BLANK); 4704 4705 content = beforeImports + imports; 4706 4707 return content; 4708 } 4709 4710 content = beforeImports + imports + "\n" + afterImports; 4711 4712 return content; 4713 } 4714 4715 private static String _stripQuotes(String s) { 4716 String[] parts = StringUtil.split(s, CharPool.QUOTE); 4717 4718 int i = 1; 4719 4720 while (i < parts.length) { 4721 s = StringUtil.replaceFirst( 4722 s, StringPool.QUOTE + parts[i] + StringPool.QUOTE, 4723 StringPool.BLANK); 4724 4725 i = i + 2; 4726 } 4727 4728 return s; 4729 } 4730 4731 private static String _stripRedundantParentheses(String s) { 4732 for (int x = 0;;) { 4733 x = s.indexOf(StringPool.OPEN_PARENTHESIS, x + 1); 4734 int y = s.indexOf(StringPool.CLOSE_PARENTHESIS, x); 4735 4736 if ((x == -1) || (y == -1)) { 4737 return s; 4738 } 4739 4740 String linePart = s.substring(x + 1, y); 4741 4742 linePart = StringUtil.replace( 4743 linePart, StringPool.COMMA, StringPool.BLANK); 4744 4745 if (Validator.isAlphanumericName(linePart) || 4746 Validator.isNull(linePart)) { 4747 4748 s = s.substring(0, x) + s.substring(y + 1); 4749 } 4750 } 4751 } 4752 4753 private static Properties _stripTopLevelDirectories( 4754 Properties properties, int level) 4755 throws IOException { 4756 4757 File dir = new File("."); 4758 4759 String dirName = dir.getCanonicalPath(); 4760 4761 dirName = StringUtil.replace( 4762 dirName, StringPool.BACK_SLASH, StringPool.SLASH); 4763 4764 int pos = dirName.length(); 4765 4766 for (int i = 0; i < level; i++) { 4767 pos = dirName.lastIndexOf(StringPool.SLASH, pos - 1); 4768 } 4769 4770 String topLevelDirNames = dirName.substring(pos + 1) + StringPool.SLASH; 4771 4772 Properties newProperties = new Properties(); 4773 4774 for (Map.Entry<Object, Object> entry : properties.entrySet()) { 4775 String key = (String)entry.getKey(); 4776 4777 if (!key.startsWith(topLevelDirNames)) { 4778 continue; 4779 } 4780 4781 key = StringUtil.replaceFirst( 4782 key, topLevelDirNames, StringPool.BLANK); 4783 4784 String value = (String)entry.getValue(); 4785 4786 newProperties.setProperty(key, value); 4787 } 4788 4789 return newProperties; 4790 } 4791 4792 private static String _trimContent(String content) throws IOException { 4793 StringBundler sb = new StringBundler(); 4794 4795 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader( 4796 new UnsyncStringReader(content)); 4797 4798 String line = null; 4799 4800 while ((line = unsyncBufferedReader.readLine()) != null) { 4801 sb.append(_trimLine(line)); 4802 sb.append("\n"); 4803 } 4804 4805 unsyncBufferedReader.close(); 4806 4807 content = sb.toString(); 4808 4809 if (content.endsWith("\n")) { 4810 content = content.substring(0, content.length() - 1); 4811 } 4812 4813 return content; 4814 } 4815 4816 private static String _trimLine(String line) { 4817 if (line.trim().length() == 0) { 4818 return StringPool.BLANK; 4819 } 4820 4821 line = StringUtil.trimTrailing(line); 4822 4823 if (!line.startsWith(StringPool.SPACE) || line.startsWith(" *")) { 4824 return line; 4825 } 4826 4827 if (!line.startsWith(StringPool.FOUR_SPACES)) { 4828 while (line.startsWith(StringPool.SPACE)) { 4829 line = StringUtil.replaceFirst( 4830 line, StringPool.SPACE, StringPool.BLANK); 4831 } 4832 } 4833 else { 4834 int pos = 0; 4835 4836 String temp = line; 4837 4838 while (temp.startsWith(StringPool.FOUR_SPACES)) { 4839 line = StringUtil.replaceFirst( 4840 line, StringPool.FOUR_SPACES, StringPool.TAB); 4841 4842 pos++; 4843 4844 temp = line.substring(pos); 4845 } 4846 } 4847 4848 return line; 4849 } 4850 4851 private static final String[] _TAG_LIBRARIES = new String[] { 4852 "aui", "c", "html", "jsp", "liferay-portlet", "liferay-security", 4853 "liferay-theme", "liferay-ui", "liferay-util", "portlet", "struts", 4854 "tiles" 4855 }; 4856 4857 private static final int _TYPE_CLASS_PRIVATE = 24; 4858 4859 private static final int _TYPE_CLASS_PRIVATE_STATIC = 23; 4860 4861 private static final int _TYPE_CLASS_PROTECTED = 16; 4862 4863 private static final int _TYPE_CLASS_PROTECTED_STATIC = 15; 4864 4865 private static final int _TYPE_CLASS_PUBLIC = 8; 4866 4867 private static final int _TYPE_CLASS_PUBLIC_STATIC = 7; 4868 4869 private static final int[] _TYPE_CONSTRUCTOR = { 4870 SourceFormatter._TYPE_CONSTRUCTOR_PRIVATE, 4871 SourceFormatter._TYPE_CONSTRUCTOR_PROTECTED, 4872 SourceFormatter._TYPE_CONSTRUCTOR_PUBLIC 4873 }; 4874 4875 private static final int _TYPE_CONSTRUCTOR_PRIVATE = 18; 4876 4877 private static final int _TYPE_CONSTRUCTOR_PROTECTED = 10; 4878 4879 private static final int _TYPE_CONSTRUCTOR_PUBLIC = 4; 4880 4881 private static final int[] _TYPE_METHOD = { 4882 SourceFormatter._TYPE_METHOD_PRIVATE, 4883 SourceFormatter._TYPE_METHOD_PRIVATE_STATIC, 4884 SourceFormatter._TYPE_METHOD_PROTECTED, 4885 SourceFormatter._TYPE_METHOD_PROTECTED_STATIC, 4886 SourceFormatter._TYPE_METHOD_PUBLIC, 4887 SourceFormatter._TYPE_METHOD_PUBLIC_STATIC 4888 }; 4889 4890 private static final int _TYPE_METHOD_PRIVATE = 19; 4891 4892 private static final int _TYPE_METHOD_PRIVATE_STATIC = 17; 4893 4894 private static final int _TYPE_METHOD_PROTECTED = 11; 4895 4896 private static final int _TYPE_METHOD_PROTECTED_STATIC = 9; 4897 4898 private static final int _TYPE_METHOD_PUBLIC = 5; 4899 4900 private static final int _TYPE_METHOD_PUBLIC_STATIC = 3; 4901 4902 private static final int[] _TYPE_VARIABLE_NOT_FINAL = { 4903 SourceFormatter._TYPE_VARIABLE_PRIVATE, 4904 SourceFormatter._TYPE_VARIABLE_PRIVATE_STATIC, 4905 SourceFormatter._TYPE_VARIABLE_PROTECTED, 4906 SourceFormatter._TYPE_VARIABLE_PROTECTED_STATIC, 4907 SourceFormatter._TYPE_VARIABLE_PUBLIC, 4908 SourceFormatter._TYPE_VARIABLE_PUBLIC_STATIC 4909 }; 4910 4911 private static final int[] _TYPE_VARIABLE_NOT_STATIC = { 4912 SourceFormatter._TYPE_VARIABLE_PRIVATE, 4913 SourceFormatter._TYPE_VARIABLE_PROTECTED, 4914 SourceFormatter._TYPE_VARIABLE_PUBLIC 4915 }; 4916 4917 private static final int _TYPE_VARIABLE_PRIVATE = 22; 4918 4919 private static final int _TYPE_VARIABLE_PRIVATE_STATIC = 21; 4920 4921 private static final int _TYPE_VARIABLE_PRIVATE_STATIC_FINAL = 20; 4922 4923 private static final int _TYPE_VARIABLE_PROTECTED = 14; 4924 4925 private static final int _TYPE_VARIABLE_PROTECTED_STATIC = 13; 4926 4927 private static final int _TYPE_VARIABLE_PROTECTED_STATIC_FINAL = 12; 4928 4929 private static final int _TYPE_VARIABLE_PUBLIC = 6; 4930 4931 private static final int _TYPE_VARIABLE_PUBLIC_STATIC = 2; 4932 4933 private static final int _TYPE_VARIABLE_PUBLIC_STATIC_FINAL = 1; 4934 4935 private static String[] _excludes; 4936 private static FileImpl _fileUtil = FileImpl.getInstance(); 4937 private static Pattern _javaImportPattern = Pattern.compile( 4938 "(^[ \t]*import\\s+.*;\n+)+", Pattern.MULTILINE); 4939 private static Properties _javaTermSortExclusions; 4940 private static Pattern _jspAttributeNamePattern = Pattern.compile( 4941 "[a-z]+[-_a-zA-Z0-9]*"); 4942 private static Map<String, String> _jspContents = 4943 new HashMap<String, String>(); 4944 private static Pattern _jspImportPattern = Pattern.compile( 4945 "(<.*\n*page.import=\".*>\n*)+", Pattern.MULTILINE); 4946 private static Pattern _jspIncludeFilePattern = Pattern.compile( 4947 "/.*[.]jsp[f]?"); 4948 private static Pattern _languageKeyPattern = Pattern.compile( 4949 "LanguageUtil.(?:get|format)\\([^;%]+|Liferay.Language.get\\('([^']+)"); 4950 private static Properties _lineLengthExclusions; 4951 private static Properties _portalLanguageKeysProperties; 4952 private static boolean _portalSource; 4953 private static SAXReaderImpl _saxReaderUtil = SAXReaderImpl.getInstance(); 4954 private static Pattern _sessionKeyPattern = Pattern.compile( 4955 "SessionErrors.(?:add|contains|get)\\([^;%&|!]+|".concat( 4956 "SessionMessages.(?:add|contains|get)\\([^;%&|!]+"), 4957 Pattern.MULTILINE); 4958 private static SourceFormatterHelper _sourceFormatterHelper; 4959 private static Pattern _taglibLanguageKeyPattern = Pattern.compile( 4960 "(?:confirmation|label|(?:M|m)essage|message key|names|title)=\"[^A-Z" + 4961 "<=%\\[\\s]+\""); 4962 private static Pattern _taglibSessionKeyPattern = Pattern.compile( 4963 "<liferay-ui:error [^>]+>|<liferay-ui:success [^>]+>", 4964 Pattern.MULTILINE); 4965 private static Pattern _xssPattern = Pattern.compile( 4966 "String\\s+([^\\s]+)\\s*=\\s*(Bean)?ParamUtil\\.getString\\("); 4967 4968 }