Example usage for com.liferay.portal.kernel.util StringPool TAB

List of usage examples for com.liferay.portal.kernel.util StringPool TAB

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util StringPool TAB.

Prototype

String TAB

To view the source code for com.liferay.portal.kernel.util StringPool TAB.

Click Source Link

Usage

From source file:com.liferay.tools.sourceformatter.JavaSourceProcessor.java

License:Open Source License

protected String checkIfClauseTabsAndSpaces(String ifClause) throws IOException {

    if (ifClause.contains("!(") || ifClause.contains(StringPool.TAB + "//")) {

        return ifClause;
    }/*from w  w w  .j  a v a 2 s  . c  om*/

    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(new UnsyncStringReader(ifClause));

    String line = null;

    String previousLine = null;
    int previousLineLeadingWhiteSpace = 0;

    int lastCriteriumLineLeadingWhiteSpace = 0;

    int closeParenthesesCount = 0;
    int openParenthesesCount = 0;

    while ((line = unsyncBufferedReader.readLine()) != null) {
        String originalLine = line;

        line = StringUtil.replace(line, StringPool.TAB, StringPool.FOUR_SPACES);

        int leadingWhiteSpace = line.length() - StringUtil.trimLeading(line).length();

        if (Validator.isNull(previousLine)) {
            lastCriteriumLineLeadingWhiteSpace = line.indexOf(StringPool.OPEN_PARENTHESIS);
        } else if (previousLine.endsWith("|") || previousLine.endsWith("&") || previousLine.endsWith("^")) {

            int expectedLeadingWhiteSpace = lastCriteriumLineLeadingWhiteSpace + openParenthesesCount
                    - closeParenthesesCount;

            if (leadingWhiteSpace != expectedLeadingWhiteSpace) {
                return fixIfClause(ifClause, originalLine, leadingWhiteSpace - expectedLeadingWhiteSpace);
            }

            lastCriteriumLineLeadingWhiteSpace = leadingWhiteSpace;

            closeParenthesesCount = 0;
            openParenthesesCount = 0;
        } else {
            int expectedLeadingWhiteSpace = 0;

            if (previousLine.contains(StringPool.TAB + "if (")) {
                expectedLeadingWhiteSpace = previousLineLeadingWhiteSpace + 8;
            } else if (previousLine.contains(StringPool.TAB + "else if (")
                    || previousLine.contains(StringPool.TAB + "while (")) {

                expectedLeadingWhiteSpace = previousLineLeadingWhiteSpace + 12;
            }

            if ((expectedLeadingWhiteSpace != 0) && (leadingWhiteSpace != expectedLeadingWhiteSpace)) {

                return fixIfClause(ifClause, originalLine, leadingWhiteSpace - expectedLeadingWhiteSpace);
            }
        }

        if (line.endsWith(") {")) {
            return ifClause;
        }

        line = stripQuotes(line, CharPool.QUOTE);
        line = stripQuotes(line, CharPool.APOSTROPHE);

        closeParenthesesCount += StringUtil.count(line, StringPool.CLOSE_PARENTHESIS);
        openParenthesesCount += StringUtil.count(line, StringPool.OPEN_PARENTHESIS);

        previousLine = originalLine;
        previousLineLeadingWhiteSpace = leadingWhiteSpace;
    }

    return ifClause;
}

From source file:com.liferay.tools.sourceformatter.JavaSourceProcessor.java

License:Open Source License

protected void checkLogLevel(String content, String fileName, String logLevel) {

    if (fileName.contains("Log")) {
        return;//ww  w.  j a  v a 2  s.c  om
    }

    Pattern pattern = Pattern.compile("\n(\t+)_log." + logLevel + "\\(");

    Matcher matcher = pattern.matcher(content);

    while (matcher.find()) {
        int pos = matcher.start();

        while (true) {
            pos = content.lastIndexOf(StringPool.NEW_LINE + StringPool.TAB, pos - 1);

            char c = content.charAt(pos + 2);

            if (c != CharPool.TAB) {
                break;
            }
        }

        String codeBlock = content.substring(pos, matcher.start());
        String s = "_log.is" + StringUtil.upperCaseFirstLetter(logLevel) + "Enabled()";

        if (!codeBlock.contains(s)) {
            int lineCount = StringUtil.count(content.substring(0, matcher.start(1)), StringPool.NEW_LINE);

            lineCount += 1;

            processErrorMessage(fileName, "Use " + s + ": " + fileName + " " + lineCount);
        }
    }

    return;
}

From source file:com.liferay.tools.sourceformatter.JavaSourceProcessor.java

License:Open Source License

@Override
protected String doFormat(File file, String fileName, String absolutePath, String content) throws Exception {

    if (isGenerated(content)) {
        return content;
    }//  w w w .j  a  v  a  2  s. c om

    String className = file.getName();

    int pos = className.lastIndexOf(StringPool.PERIOD);

    className = className.substring(0, pos);

    String packagePath = fileName;

    int packagePathX = packagePath.indexOf("/src/");

    if (packagePathX == -1) {
        packagePathX = packagePath.indexOf("/integration/") + 8;
    }

    int packagePathY = packagePath.lastIndexOf(StringPool.SLASH);

    if ((packagePathX + 5) >= packagePathY) {
        packagePath = StringPool.BLANK;
    } else {
        packagePath = packagePath.substring(packagePathX + 5, packagePathY);
    }

    packagePath = StringUtil.replace(packagePath, StringPool.SLASH, StringPool.PERIOD);

    if (packagePath.endsWith(".model")) {
        if (content.contains("extends " + className + "Model")) {
            return content;
        }
    }

    String newContent = content;

    if (newContent.contains("$\n */")) {
        processErrorMessage(fileName, "*: " + fileName);

        newContent = StringUtil.replace(newContent, "$\n */", "$\n *\n */");
    }

    newContent = fixCopyright(newContent, absolutePath, fileName);

    if (newContent.contains(className + ".java.html")) {
        processErrorMessage(fileName, "Java2HTML: " + fileName);
    }

    if (newContent.contains(" * @author Raymond Aug") && !newContent.contains(" * @author Raymond Aug\u00e9")) {

        newContent = newContent.replaceFirst("Raymond Aug.++", "Raymond Aug\u00e9");

        processErrorMessage(fileName, "UTF-8: " + fileName);
    }

    newContent = fixDataAccessConnection(className, newContent);
    newContent = fixSessionKey(fileName, newContent, sessionKeyPattern);

    newContent = StringUtil.replace(newContent,
            new String[] { "com.liferay.portal.PortalException", "com.liferay.portal.SystemException",
                    "com.liferay.util.LocalizationUtil" },
            new String[] { "com.liferay.portal.kernel.exception.PortalException",
                    "com.liferay.portal.kernel.exception.SystemException",
                    "com.liferay.portal.kernel.util.LocalizationUtil" });

    newContent = StringUtil.replace(newContent, " final static ", " static final ");

    newContent = fixCompatClassImports(absolutePath, newContent);

    newContent = stripJavaImports(newContent, packagePath, className);

    newContent = StringUtil.replace(newContent,
            new String[] { ";\n/**", "\t/*\n\t *", "catch(", "else{", "if(", "for(", "while(", "List <", "){\n",
                    "]{\n", ";;\n" },
            new String[] { ";\n\n/**", "\t/**\n\t *", "catch (", "else {", "if (", "for (", "while (", "List<",
                    ") {\n", "] {\n", ";\n" });

    while (true) {
        Matcher matcher = _incorrectLineBreakPattern.matcher(newContent);

        if (!matcher.find()) {
            break;
        }

        newContent = StringUtil.replaceFirst(newContent, StringPool.NEW_LINE, StringPool.BLANK,
                matcher.start());
    }

    newContent = sortAnnotations(newContent, StringPool.BLANK);

    Matcher matcher = _logPattern.matcher(newContent);

    if (matcher.find()) {
        String logClassName = matcher.group(1);

        if (!logClassName.equals(className)) {
            newContent = StringUtil.replaceLast(newContent, logClassName + ".class)", className + ".class)");
        }
    }

    if (!isExcluded(_staticLogVariableExclusions, absolutePath)) {
        newContent = StringUtil.replace(newContent, "private Log _log", "private static final Log _log");
    }

    if (newContent.contains("*/\npackage ")) {
        processErrorMessage(fileName, "package: " + fileName);
    }

    if (!newContent.endsWith("\n\n}") && !newContent.endsWith("{\n}")) {
        processErrorMessage(fileName, "}: " + fileName);
    }

    if (portalSource && !_allowUseServiceUtilInServiceImpl && !className.equals("BaseServiceImpl")
            && className.endsWith("ServiceImpl") && newContent.contains("ServiceUtil.")) {

        processErrorMessage(fileName, "ServiceUtil: " + fileName);
    }

    // LPS-34911

    if (portalSource && !isExcluded(_upgradeServiceUtilExclusions, absolutePath)
            && fileName.contains("/portal/upgrade/") && !fileName.contains("/test/")
            && newContent.contains("ServiceUtil.")) {

        processErrorMessage(fileName, "ServiceUtil: " + fileName);
    }

    if (!isRunsOutsidePortal(absolutePath) && !isExcluded(_proxyExclusions, absolutePath)
            && newContent.contains("import java.lang.reflect.Proxy;")) {

        processErrorMessage(fileName, "Proxy: " + fileName);
    }

    if (newContent.contains("import edu.emory.mathcs.backport.java")) {
        processErrorMessage(fileName, "edu.emory.mathcs.backport.java: " + fileName);
    }

    if (newContent.contains("import jodd.util.StringPool")) {
        processErrorMessage(fileName, "jodd.util.StringPool: " + fileName);
    }

    // LPS-45027

    if (newContent.contains("com.liferay.portal.kernel.util.UnmodifiableList")) {

        processErrorMessage(fileName, "Use java.util.Collections.unmodifiableList instead of "
                + "com.liferay.portal.kernel.util.UnmodifiableList: " + fileName);
    }

    // LPS-28266

    for (int pos1 = -1;;) {
        pos1 = newContent.indexOf(StringPool.TAB + "try {", pos1 + 1);

        if (pos1 == -1) {
            break;
        }

        int pos2 = newContent.indexOf(StringPool.TAB + "try {", pos1 + 1);
        int pos3 = newContent.indexOf("\"select count(", pos1);

        if ((pos2 != -1) && (pos3 != -1) && (pos2 < pos3)) {
            continue;
        }

        int pos4 = newContent.indexOf("rs.getLong(1)", pos1);
        int pos5 = newContent.indexOf(StringPool.TAB + "finally {", pos1);

        if ((pos3 == -1) || (pos4 == -1) || (pos5 == -1)) {
            break;
        }

        if ((pos3 < pos4) && (pos4 < pos5)) {
            processErrorMessage(fileName, "Use getInt(1) for count: " + fileName);
        }
    }

    // LPS-33070

    matcher = _processCallablePattern.matcher(content);

    if (matcher.find() && !content.contains("private static final long serialVersionUID")) {

        processErrorMessage(fileName, "Assign ProcessCallable implementation a serialVersionUID: " + fileName);
    }

    checkLanguageKeys(fileName, newContent, languageKeyPattern);

    newContent = StringUtil.replace(newContent, StringPool.TAB + "for (;;) {",
            StringPool.TAB + "while (true) {");

    // LPS-36174

    if (_checkUnprocessedExceptions && !fileName.contains("/test/")) {
        checkUnprocessedExceptions(newContent, file, packagePath, fileName);
    }

    // LPS-39508

    if (!isExcluded(_secureRandomExclusions, absolutePath) && !isRunsOutsidePortal(absolutePath)
            && content.contains("java.security.SecureRandom")
            && !content.contains("javax.crypto.KeyGenerator")) {

        processErrorMessage(fileName, "Use SecureRandomUtil or com.liferay.portal.kernel.security."
                + "SecureRandom instead of java.security.SecureRandom: " + fileName);
    }

    // LPS-41315

    checkLogLevel(newContent, fileName, "debug");
    checkLogLevel(newContent, fileName, "info");
    checkLogLevel(newContent, fileName, "trace");
    checkLogLevel(newContent, fileName, "warn");

    // LPS-46632

    checkSystemEventAnnotations(newContent, fileName);

    // LPS-41205

    if (fileName.contains("/upgrade/") && newContent.contains("LocaleUtil.getDefault()")) {

        processErrorMessage(fileName, "Use UpgradeProcessUtil.getDefaultLanguageId(companyId) "
                + "instead of LocaleUtil.getDefault(): " + fileName);
    }

    // LPS-46017

    newContent = StringUtil.replace(newContent, " static interface ", " interface ");

    // LPS-47055

    newContent = fixSystemExceptions(newContent);

    // LPS-47648

    if (portalSource && fileName.contains("/test/integration/")) {
        newContent = StringUtil.replace(newContent, "FinderCacheUtil.clearCache();", StringPool.BLANK);
    }

    // LPS-47682

    newContent = fixIncorrectParameterTypeForLanguageUtil(newContent, false, fileName);

    if (portalSource && fileName.contains("/portal-service/")
            && content.contains("import javax.servlet.jsp.")) {

        processErrorMessage(fileName, "Never import javax.servlet.jsp.* from portal-service " + fileName);
    }

    // LPS-48153

    //newContent = applyDiamondOperator(newContent);

    // LPS-49552

    checkFinderCacheInterfaceMethod(fileName, newContent);

    newContent = fixIncorrectEmptyLineBeforeCloseCurlyBrace(newContent, fileName);

    pos = newContent.indexOf("\npublic ");

    if (pos != -1) {
        String javaClassContent = newContent.substring(pos + 1);

        String beforeJavaClass = newContent.substring(0, pos + 1);

        int javaClassLineCount = StringUtil.count(beforeJavaClass, "\n") + 1;

        newContent = formatJavaTerms(className, fileName, absolutePath, newContent, javaClassContent,
                javaClassLineCount, _checkJavaFieldTypesExclusions, _javaTermAccessLevelModifierExclusions,
                _javaTermSortExclusions, _testAnnotationsExclusions);
    }

    newContent = formatJava(fileName, absolutePath, newContent);

    return StringUtil.replace(newContent, "\n\n\n", "\n\n");
}

From source file:com.liferay.tools.sourceformatter.JavaSourceProcessor.java

License:Open Source License

protected String fixIfClause(String ifClause, String line, int delta) {
    String newLine = line;/*w ww . j  av a 2s  . c  o m*/

    String whiteSpace = StringPool.BLANK;
    int whiteSpaceLength = Math.abs(delta);

    while (whiteSpaceLength > 0) {
        if (whiteSpaceLength >= 4) {
            whiteSpace += StringPool.TAB;

            whiteSpaceLength -= 4;
        } else {
            whiteSpace += StringPool.SPACE;

            whiteSpaceLength -= 1;
        }
    }

    if (delta > 0) {
        if (!line.contains(StringPool.TAB + whiteSpace)) {
            newLine = StringUtil.replaceLast(newLine, StringPool.TAB, StringPool.FOUR_SPACES);
        }

        newLine = StringUtil.replaceLast(newLine, StringPool.TAB + whiteSpace, StringPool.TAB);
    } else {
        newLine = StringUtil.replaceLast(newLine, StringPool.TAB, StringPool.TAB + whiteSpace);
    }

    newLine = StringUtil.replaceLast(newLine, StringPool.FOUR_SPACES, StringPool.TAB);

    return StringUtil.replace(ifClause, line, newLine);
}

From source file:com.liferay.tools.sourceformatter.JavaSourceProcessor.java

License:Open Source License

protected String formatJava(String fileName, String absolutePath, String content) throws Exception {

    StringBundler sb = new StringBundler();

    try (UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
            new UnsyncStringReader(content))) {

        String line = null;/*from ww w  .  j a  v  a  2s  . c  o  m*/
        String previousLine = StringPool.BLANK;

        int lineCount = 0;

        String ifClause = StringPool.BLANK;
        String packageName = StringPool.BLANK;
        String regexPattern = StringPool.BLANK;

        while ((line = unsyncBufferedReader.readLine()) != null) {
            lineCount++;

            line = trimLine(line, false);

            if (line.startsWith("package ")) {
                packageName = line.substring(8, line.length() - 1);
            }

            if (line.startsWith("import ")) {
                if (line.endsWith(".*;")) {
                    processErrorMessage(fileName, "import: " + fileName + " " + lineCount);
                }

                int pos = line.lastIndexOf(StringPool.PERIOD);

                if (pos != -1) {
                    String importPackageName = line.substring(7, pos);

                    if (importPackageName.equals(packageName)) {
                        continue;
                    }
                }
            }

            if (line.contains(StringPool.TAB + "for (") && line.contains(":") && !line.contains(" :")) {

                line = StringUtil.replace(line, ":", " :");
            }

            // LPS-42924

            if (line.contains("PortalUtil.getClassNameId(") && fileName.endsWith("ServiceImpl.java")) {

                processErrorMessage(fileName,
                        "Use classNameLocalService.getClassNameId: " + fileName + " " + lineCount);
            }

            // LPS-42599

            if (!isExcluded(_hibernateSQLQueryExclusions, absolutePath)
                    && line.contains("= session.createSQLQuery(")
                    && content.contains("com.liferay.portal.kernel.dao.orm.Session")) {

                line = StringUtil.replace(line, "createSQLQuery", "createSynchronizedSQLQuery");
            }

            line = replacePrimitiveWrapperInstantiation(fileName, line, lineCount);

            String trimmedLine = StringUtil.trimLeading(line);

            // LPS-45649

            if (trimmedLine.startsWith("throw new IOException(") && line.contains("e.getMessage()")) {

                line = StringUtil.replace(line, ".getMessage()", StringPool.BLANK);
            }

            // LPS-45492

            if (trimmedLine.contains("StopWatch stopWatch = null;")) {
                processErrorMessage(fileName, "Do not set stopwatch to null: " + fileName + " " + lineCount);
            }

            checkStringBundler(trimmedLine, fileName, lineCount);

            checkEmptyCollection(trimmedLine, fileName, lineCount);

            if (trimmedLine.startsWith("* @deprecated") && _addMissingDeprecationReleaseVersion) {

                if (!trimmedLine.startsWith("* @deprecated As of ")) {
                    line = StringUtil.replace(line, "* @deprecated",
                            "* @deprecated As of " + getMainReleaseVersion());
                } else {
                    String version = trimmedLine.substring(20);

                    version = StringUtil.split(version, StringPool.SPACE)[0];

                    version = StringUtil.replace(version, StringPool.COMMA, StringPool.BLANK);

                    if (StringUtil.count(version, StringPool.PERIOD) == 1) {
                        line = StringUtil.replaceFirst(line, version, version + ".0");
                    }
                }
            }

            if (trimmedLine.startsWith("* @see ") && (StringUtil.count(trimmedLine, StringPool.AT) > 1)) {

                processErrorMessage(fileName,
                        "Do not use @see with another annotation: " + fileName + " " + lineCount);
            }

            checkInefficientStringMethods(line, fileName, absolutePath, lineCount);

            if (trimmedLine.startsWith(StringPool.EQUAL)) {
                processErrorMessage(fileName, "line break: " + fileName + " " + lineCount);
            }

            if (line.contains("ActionForm form")) {
                processErrorMessage(fileName, "Rename form to actionForm: " + fileName + " " + lineCount);
            }

            if (line.contains("ActionMapping mapping")) {
                processErrorMessage(fileName, "Rename mapping to ActionMapping: " + fileName + " " + lineCount);
            }

            if (fileName.contains("/upgrade/") && line.contains("rs.getDate(")) {

                processErrorMessage(fileName, "Use rs.getTimeStamp: " + fileName + " " + lineCount);
            }

            if (!trimmedLine.equals("{") && line.endsWith("{") && !line.endsWith(" {")) {

                line = StringUtil.replaceLast(line, "{", " {");
            }

            line = sortExceptions(line);

            if (trimmedLine.startsWith("if (") || trimmedLine.startsWith("else if (")
                    || trimmedLine.startsWith("while (") || Validator.isNotNull(ifClause)) {

                ifClause = ifClause + line + StringPool.NEW_LINE;

                if (line.endsWith(") {")) {
                    String newIfClause = checkIfClause(ifClause, fileName, lineCount);

                    if (!ifClause.equals(newIfClause) && content.contains(ifClause)) {

                        return StringUtil.replace(content, ifClause, newIfClause);
                    }

                    ifClause = StringPool.BLANK;
                } else if (line.endsWith(StringPool.SEMICOLON)) {
                    ifClause = StringPool.BLANK;
                }
            }

            if (trimmedLine.startsWith("Pattern ") || Validator.isNotNull(regexPattern)) {

                regexPattern = regexPattern + trimmedLine;

                if (trimmedLine.endsWith(");")) {

                    // LPS-41084

                    checkRegexPattern(regexPattern, fileName, lineCount);

                    regexPattern = StringPool.BLANK;
                }
            }

            if (!trimmedLine.contains(StringPool.DOUBLE_SLASH) && !trimmedLine.startsWith(StringPool.STAR)) {

                String strippedQuotesLine = stripQuotes(trimmedLine, CharPool.QUOTE);

                for (int x = 0;;) {
                    x = strippedQuotesLine.indexOf(StringPool.EQUAL, x + 1);

                    if (x == -1) {
                        break;
                    }

                    char c = strippedQuotesLine.charAt(x - 1);

                    if (Character.isLetterOrDigit(c)) {
                        line = StringUtil.replace(line, c + "=", c + " =");

                        break;
                    }

                    if (x == (strippedQuotesLine.length() - 1)) {
                        break;
                    }

                    c = strippedQuotesLine.charAt(x + 1);

                    if (Character.isLetterOrDigit(c)) {
                        line = StringUtil.replace(line, "=" + c, "= " + c);

                        break;
                    }
                }

                while (trimmedLine.contains(StringPool.TAB)) {
                    line = StringUtil.replaceLast(line, StringPool.TAB, StringPool.SPACE);

                    trimmedLine = StringUtil.replaceLast(trimmedLine, StringPool.TAB, StringPool.SPACE);
                }

                if (line.contains(StringPool.TAB + StringPool.SPACE) && !previousLine.endsWith("&&")
                        && !previousLine.endsWith("||") && !previousLine.contains(StringPool.TAB + "((")
                        && !previousLine.contains(StringPool.TAB + StringPool.LESS_THAN)
                        && !previousLine.contains(StringPool.TAB + StringPool.SPACE)
                        && !previousLine.contains(StringPool.TAB + "for (")
                        && !previousLine.contains(StringPool.TAB + "implements ")
                        && !previousLine.contains(StringPool.TAB + "throws ")) {

                    line = StringUtil.replace(line, StringPool.TAB + StringPool.SPACE, StringPool.TAB);
                }

                while (trimmedLine.contains(StringPool.DOUBLE_SPACE)
                        && !trimmedLine.contains(StringPool.QUOTE + StringPool.DOUBLE_SPACE)
                        && !fileName.contains("Test")) {

                    line = StringUtil.replaceLast(line, StringPool.DOUBLE_SPACE, StringPool.SPACE);

                    trimmedLine = StringUtil.replaceLast(trimmedLine, StringPool.DOUBLE_SPACE,
                            StringPool.SPACE);
                }

                if (!line.contains(StringPool.QUOTE)) {
                    int pos = line.indexOf(") ");

                    if (pos != -1) {
                        String linePart = line.substring(pos + 2);

                        if (Character.isLetter(linePart.charAt(0)) && !linePart.startsWith("default")
                                && !linePart.startsWith("instanceof") && !linePart.startsWith("throws")) {

                            line = StringUtil.replaceLast(line, StringPool.SPACE + linePart, linePart);
                        }
                    }

                    if ((trimmedLine.startsWith("private ") || trimmedLine.startsWith("protected ")
                            || trimmedLine.startsWith("public ")) && !line.contains(StringPool.EQUAL)
                            && line.contains(" (")) {

                        line = StringUtil.replace(line, " (", "(");
                    }

                    if (line.contains(" [")) {
                        line = StringUtil.replace(line, " [", "[");
                    }

                    for (int x = -1;;) {
                        int posComma = line.indexOf(StringPool.COMMA, x + 1);
                        int posSemicolon = line.indexOf(StringPool.SEMICOLON, x + 1);

                        if ((posComma == -1) && (posSemicolon == -1)) {
                            break;
                        }

                        x = Math.min(posComma, posSemicolon);

                        if (x == -1) {
                            x = Math.max(posComma, posSemicolon);
                        }

                        if (line.length() > (x + 1)) {
                            char nextChar = line.charAt(x + 1);

                            if ((nextChar != CharPool.APOSTROPHE) && (nextChar != CharPool.CLOSE_PARENTHESIS)
                                    && (nextChar != CharPool.SPACE) && (nextChar != CharPool.STAR)) {

                                line = StringUtil.insert(line, StringPool.SPACE, x + 1);
                            }
                        }

                        if (x > 0) {
                            char previousChar = line.charAt(x - 1);

                            if (previousChar == CharPool.SPACE) {
                                line = line.substring(0, x - 1).concat(line.substring(x));
                            }
                        }
                    }
                }

                if ((line.contains(" && ") || line.contains(" || "))
                        && line.endsWith(StringPool.OPEN_PARENTHESIS)) {

                    processErrorMessage(fileName, "line break: " + fileName + " " + lineCount);
                }

                if (trimmedLine.endsWith(StringPool.PLUS)
                        && !trimmedLine.startsWith(StringPool.OPEN_PARENTHESIS)) {

                    int closeParenthesisCount = StringUtil.count(strippedQuotesLine,
                            StringPool.CLOSE_PARENTHESIS);
                    int openParenthesisCount = StringUtil.count(strippedQuotesLine,
                            StringPool.OPEN_PARENTHESIS);

                    if (openParenthesisCount > closeParenthesisCount) {
                        processErrorMessage(fileName, "line break: " + fileName + " " + lineCount);
                    }
                }

                int x = strippedQuotesLine.indexOf(", ");

                if (x != -1) {
                    String linePart = strippedQuotesLine.substring(0, x);

                    int closeParenthesisCount = StringUtil.count(linePart, StringPool.CLOSE_PARENTHESIS);
                    int openParenthesisCount = StringUtil.count(linePart, StringPool.OPEN_PARENTHESIS);

                    if (closeParenthesisCount > openParenthesisCount) {
                        processErrorMessage(fileName, "line break: " + fileName + " " + lineCount);
                    }
                } else if (trimmedLine.endsWith(StringPool.COMMA) && !trimmedLine.startsWith("for (")) {

                    int closeParenthesisCount = StringUtil.count(strippedQuotesLine,
                            StringPool.CLOSE_PARENTHESIS);
                    int openParenthesisCount = StringUtil.count(strippedQuotesLine,
                            StringPool.OPEN_PARENTHESIS);

                    if (closeParenthesisCount < openParenthesisCount) {
                        processErrorMessage(fileName, "line break: " + fileName + " " + lineCount);
                    }
                }

                if (line.contains(StringPool.COMMA) && !line.contains(StringPool.CLOSE_PARENTHESIS)
                        && !line.contains(StringPool.GREATER_THAN) && !line.contains(StringPool.QUOTE)
                        && line.endsWith(StringPool.OPEN_PARENTHESIS)) {

                    processErrorMessage(fileName, "line break: " + fileName + " " + lineCount);
                }

                if (line.endsWith(" +") || line.endsWith(" -") || line.endsWith(" *") || line.endsWith(" /")) {

                    x = line.indexOf(" = ");

                    if (x != -1) {
                        int y = line.indexOf(StringPool.QUOTE);

                        if ((y == -1) || (x < y)) {
                            processErrorMessage(fileName, "line break: " + fileName + " " + lineCount);
                        }
                    }
                }

                if (line.endsWith(" throws") || (previousLine.endsWith(StringPool.OPEN_PARENTHESIS)
                        && line.contains(" throws ") && line.endsWith(StringPool.OPEN_CURLY_BRACE))) {

                    processErrorMessage(fileName, "line break: " + fileName + " " + lineCount);
                }

                if (trimmedLine.startsWith(StringPool.PERIOD)
                        || (line.endsWith(StringPool.PERIOD) && line.contains(StringPool.EQUAL))) {

                    processErrorMessage(fileName, "line break: " + fileName + " " + lineCount);
                }

                if (trimmedLine.startsWith(StringPool.CLOSE_CURLY_BRACE)
                        && line.endsWith(StringPool.OPEN_CURLY_BRACE)) {

                    Matcher matcher = _lineBreakPattern.matcher(trimmedLine);

                    if (!matcher.find()) {
                        processErrorMessage(fileName, "line break: " + fileName + " " + lineCount);
                    }
                }
            }

            if (line.contains("    ") && !line.matches("\\s*\\*.*")) {
                if (!fileName.endsWith("StringPool.java")) {
                    processErrorMessage(fileName, "tab: " + fileName + " " + lineCount);
                }
            }

            if (line.contains("  {") && !line.matches("\\s*\\*.*")) {
                processErrorMessage(fileName, "{:" + fileName + " " + lineCount);
            }

            int lineLength = getLineLength(line);

            if (!line.startsWith("import ") && !line.startsWith("package ") && !line.matches("\\s*\\*.*")) {

                if (fileName.endsWith("Table.java") && line.contains("String TABLE_SQL_CREATE = ")) {
                } else if (fileName.endsWith("Table.java") && line.contains("String TABLE_SQL_DROP = ")) {
                } else if (fileName.endsWith("Table.java") && line.contains(" index IX_")) {
                } else if (lineLength > _MAX_LINE_LENGTH) {
                    if (!isExcluded(_lineLengthExclusions, absolutePath, lineCount)
                            && !isAnnotationParameter(content, trimmedLine)) {

                        String truncateLongLinesContent = getTruncateLongLinesContent(content, line,
                                trimmedLine, lineCount);

                        if (truncateLongLinesContent != null) {
                            return truncateLongLinesContent;
                        }

                        processErrorMessage(fileName, "> 80: " + fileName + " " + lineCount);
                    }
                } else {
                    int lineLeadingTabCount = getLeadingTabCount(line);
                    int previousLineLeadingTabCount = getLeadingTabCount(previousLine);

                    if (!trimmedLine.startsWith("//")) {
                        if (previousLine.endsWith(StringPool.COMMA)
                                && previousLine.contains(StringPool.OPEN_PARENTHESIS)
                                && !previousLine.contains("for (")
                                && (lineLeadingTabCount > previousLineLeadingTabCount)) {

                            processErrorMessage(fileName, "line break: " + fileName + " " + lineCount);
                        }

                        if ((lineLeadingTabCount == previousLineLeadingTabCount)
                                && (previousLine.endsWith(StringPool.EQUAL)
                                        || previousLine.endsWith(StringPool.OPEN_PARENTHESIS))) {

                            processErrorMessage(fileName, "tab: " + fileName + " " + lineCount);
                        }

                        if (Validator.isNotNull(trimmedLine)) {
                            if (((previousLine.endsWith(StringPool.COLON)
                                    && previousLine.contains(StringPool.TAB + "for "))
                                    || (previousLine.endsWith(StringPool.OPEN_PARENTHESIS)
                                            && previousLine.contains(StringPool.TAB + "if ")))
                                    && ((previousLineLeadingTabCount + 2) != lineLeadingTabCount)) {

                                processErrorMessage(fileName, "line break: " + fileName + " " + lineCount);
                            }

                            if (previousLine.endsWith(StringPool.OPEN_CURLY_BRACE)
                                    && !trimmedLine.startsWith(StringPool.CLOSE_CURLY_BRACE)
                                    && ((previousLineLeadingTabCount + 1) != lineLeadingTabCount)) {

                                processErrorMessage(fileName, "tab: " + fileName + " " + lineCount);
                            }
                        }

                        if (previousLine.endsWith(StringPool.PERIOD)) {
                            int x = trimmedLine.indexOf(StringPool.OPEN_PARENTHESIS);

                            if ((x != -1) && ((getLineLength(previousLine) + x) < _MAX_LINE_LENGTH)
                                    && (trimmedLine.endsWith(StringPool.OPEN_PARENTHESIS)
                                            || (trimmedLine.charAt(x + 1) != CharPool.CLOSE_PARENTHESIS))) {

                                processErrorMessage(fileName, "line break: " + fileName + " " + lineCount);
                            }
                        }

                        int diff = lineLeadingTabCount - previousLineLeadingTabCount;

                        if (trimmedLine.startsWith("throws ") && ((diff == 0) || (diff > 1))) {

                            processErrorMessage(fileName, "tab: " + fileName + " " + lineCount);
                        }

                        if ((diff == 2) && (previousLineLeadingTabCount > 0)
                                && line.endsWith(StringPool.SEMICOLON)
                                && !previousLine.contains(StringPool.TAB + "try (")) {

                            line = StringUtil.replaceFirst(line, StringPool.TAB, StringPool.BLANK);
                        }

                        if ((previousLine.contains(" class ") || previousLine.contains(" enum "))
                                && previousLine.endsWith(StringPool.OPEN_CURLY_BRACE)
                                && Validator.isNotNull(line)
                                && !trimmedLine.startsWith(StringPool.CLOSE_CURLY_BRACE)) {

                            processErrorMessage(fileName, "line break: " + fileName + " " + lineCount);
                        }
                    }

                    String combinedLinesContent = getCombinedLinesContent(content, fileName, absolutePath, line,
                            trimmedLine, lineLength, lineCount, previousLine, lineLeadingTabCount,
                            previousLineLeadingTabCount);

                    if (combinedLinesContent != null) {
                        return combinedLinesContent;
                    }
                }
            }

            if (lineCount > 1) {
                sb.append(previousLine);

                if (Validator.isNotNull(previousLine) && Validator.isNotNull(trimmedLine)
                        && !previousLine.contains("/*") && !previousLine.endsWith("*/")) {

                    String trimmedPreviousLine = StringUtil.trimLeading(previousLine);

                    if ((trimmedPreviousLine.startsWith("// ") && !trimmedLine.startsWith("// "))
                            || (!trimmedPreviousLine.startsWith("// ") && trimmedLine.startsWith("// "))) {

                        sb.append("\n");
                    } else if (!trimmedPreviousLine.endsWith(StringPool.OPEN_CURLY_BRACE)
                            && !trimmedPreviousLine.endsWith(StringPool.COLON)
                            && (trimmedLine.startsWith("for (") || trimmedLine.startsWith("if ("))) {

                        sb.append("\n");
                    } else if (previousLine.endsWith(StringPool.TAB + StringPool.CLOSE_CURLY_BRACE)
                            && !trimmedLine.startsWith(StringPool.CLOSE_CURLY_BRACE)
                            && !trimmedLine.startsWith(StringPool.CLOSE_PARENTHESIS)
                            && !trimmedLine.startsWith(StringPool.DOUBLE_SLASH) && !trimmedLine.equals("*/")
                            && !trimmedLine.startsWith("catch ") && !trimmedLine.startsWith("else ")
                            && !trimmedLine.startsWith("finally ") && !trimmedLine.startsWith("while ")) {

                        sb.append("\n");
                    }
                }

                sb.append("\n");
            }

            previousLine = line;
        }

        sb.append(previousLine);
    }

    String newContent = sb.toString();

    if (newContent.endsWith("\n")) {
        newContent = newContent.substring(0, newContent.length() - 1);
    }

    return newContent;
}

From source file:com.liferay.tools.sourceformatter.JavaSourceProcessor.java

License:Open Source License

protected String getCombinedLinesContent(String content, String fileName, String line, String trimmedLine,
        int lineLength, int lineCount, String previousLine, String linePart, int tabDiff,
        boolean addToPreviousLine, boolean extraSpace, boolean removeTabOnNextLine) {

    if (linePart == null) {
        String combinedLine = previousLine;

        if (extraSpace) {
            combinedLine += StringPool.SPACE;
        }/*from w  w  w. j av  a  2  s .c om*/

        combinedLine += trimmedLine;

        String nextLine = getNextLine(content, lineCount);

        if (nextLine == null) {
            return null;
        }

        if (removeTabOnNextLine) {
            return StringUtil.replace(content, "\n" + previousLine + "\n" + line + "\n" + nextLine + "\n",
                    "\n" + combinedLine + "\n" + nextLine.substring(1) + "\n");
        }

        if (line.endsWith(StringPool.OPEN_CURLY_BRACE) && (tabDiff != 0) && !previousLine.contains(" class ")
                && Validator.isNull(nextLine)) {

            return StringUtil.replace(content, "\n" + previousLine + "\n" + line + "\n", "\n" + combinedLine);
        }

        return StringUtil.replace(content, "\n" + previousLine + "\n" + line + "\n",
                "\n" + combinedLine + "\n");
    }

    String firstLine = previousLine;
    String secondLine = line;

    if (addToPreviousLine) {
        if (extraSpace) {
            firstLine += StringPool.SPACE;
        }

        firstLine += linePart;

        secondLine = StringUtil.replaceFirst(line, linePart, StringPool.BLANK);
    } else {
        if (((linePart.length() + lineLength) <= _MAX_LINE_LENGTH)
                && (line.endsWith(StringPool.OPEN_CURLY_BRACE) || line.endsWith(StringPool.SEMICOLON))) {

            firstLine = StringUtil.replaceLast(firstLine, StringUtil.trim(linePart), StringPool.BLANK);

            secondLine = StringUtil.replaceLast(line, StringPool.TAB, StringPool.TAB + linePart);
        } else {
            processErrorMessage(fileName, "line break: " + fileName + " " + lineCount);

            return null;
        }
    }

    firstLine = StringUtil.trimTrailing(firstLine);

    return StringUtil.replace(content, "\n" + previousLine + "\n" + line + "\n",
            "\n" + firstLine + "\n" + secondLine + "\n");
}

From source file:com.liferay.tools.sourceformatter.JavaSourceProcessor.java

License:Open Source License

protected String getTruncateLongLinesContent(String content, String line, String trimmedLine, int lineCount) {

    String indent = StringPool.BLANK;

    for (int i = 0; i < getLeadingTabCount(line); i++) {
        indent += StringPool.TAB;
    }//from w  w  w  . j a va  2 s.c o m

    if (line.endsWith(StringPool.OPEN_PARENTHESIS) || line.endsWith(StringPool.SEMICOLON)) {

        int x = line.indexOf(" = ");

        if (x != -1) {
            String firstLine = line.substring(0, x + 2);

            if (firstLine.contains(StringPool.QUOTE)) {
                return null;
            }

            String secondLine = indent + StringPool.TAB + line.substring(x + 3);

            if (line.endsWith(StringPool.SEMICOLON)) {
                return StringUtil.replace(content, "\n" + line + "\n",
                        "\n" + firstLine + "\n" + secondLine + "\n");
            } else if (Validator.isNotNull(getNextLine(content, lineCount))) {
                return StringUtil.replace(content, "\n" + line + "\n",
                        "\n" + firstLine + "\n" + secondLine + "\n" + StringPool.TAB);
            }
        }
    }

    if (line.endsWith(StringPool.CLOSE_PARENTHESIS) || line.endsWith(StringPool.COMMA)
            || line.endsWith(StringPool.OPEN_CURLY_BRACE) || line.endsWith(StringPool.SEMICOLON)) {

        int x = 0;

        while (true) {
            x = line.indexOf(", ", x + 1);

            if (x == -1) {
                break;
            }

            if (isValidJavaParameter(line.substring(0, x))) {
                String firstLine = line.substring(0, x + 1);
                String secondLine = indent + line.substring(x + 2);

                return StringUtil.replace(content, "\n" + line + "\n",
                        "\n" + firstLine + "\n" + secondLine + "\n");
            }
        }
    }

    if ((line.endsWith(StringPool.OPEN_CURLY_BRACE) || line.endsWith(StringPool.SEMICOLON))
            && (trimmedLine.startsWith("private ") || trimmedLine.startsWith("protected ")
                    || trimmedLine.startsWith("public "))) {

        String firstLine = null;

        int x = 0;

        while (true) {
            int y = line.indexOf(" extends ", x);

            if (y != -1) {
                firstLine = line.substring(0, y);

                if (StringUtil.count(firstLine, StringPool.GREATER_THAN) != StringUtil.count(firstLine,
                        StringPool.LESS_THAN)) {

                    x = y + 1;

                    continue;
                }
            } else {
                y = line.indexOf(" implements ");

                if (y == -1) {
                    y = line.indexOf(" throws ");
                }

                if (y == -1) {
                    break;
                }

                firstLine = line.substring(0, y);
            }

            String secondLine = indent + StringPool.TAB + line.substring(y + 1);

            return StringUtil.replace(content, "\n" + line + "\n", "\n" + firstLine + "\n" + secondLine + "\n");
        }
    }

    if ((line.endsWith(StringPool.CLOSE_PARENTHESIS) || line.endsWith(StringPool.OPEN_CURLY_BRACE))
            && (trimmedLine.startsWith("private ") || trimmedLine.startsWith("protected ")
                    || trimmedLine.startsWith("public "))) {

        int x = line.indexOf(StringPool.OPEN_PARENTHESIS);

        if ((x != -1) && (line.charAt(x + 1) != CharPool.CLOSE_PARENTHESIS)) {

            String secondLineIndent = indent + StringPool.TAB;

            if (line.endsWith(StringPool.CLOSE_PARENTHESIS)) {
                secondLineIndent += StringPool.TAB;
            }

            String firstLine = line.substring(0, x + 1);
            String secondLine = secondLineIndent + line.substring(x + 1);

            return StringUtil.replace(content, "\n" + line + "\n", "\n" + firstLine + "\n" + secondLine + "\n");
        }
    }

    if (line.endsWith(StringPool.SEMICOLON)) {
        int x = line.indexOf(StringPool.OPEN_PARENTHESIS);

        if (x != -1) {
            char c = line.charAt(x - 1);

            if ((c != CharPool.SPACE) && (c != CharPool.TAB)
                    && (line.charAt(x + 1) != CharPool.CLOSE_PARENTHESIS)) {

                String firstLine = line.substring(0, x + 1);

                if (firstLine.contains(StringPool.QUOTE)) {
                    return null;
                }

                String secondLine = indent + StringPool.TAB + line.substring(x + 1);

                return StringUtil.replace(content, "\n" + line + "\n",
                        "\n" + firstLine + "\n" + secondLine + "\n");
            }
        }
    }

    if (line.contains(StringPool.TAB + "for (") && line.endsWith(" {")) {
        int x = line.indexOf(" : ");

        if (x != -1) {
            String firstLine = line.substring(0, x + 2);
            String secondLine = indent + StringPool.TAB + StringPool.TAB + line.substring(x + 3);

            return StringUtil.replace(content, "\n" + line + "\n",
                    "\n" + firstLine + "\n" + secondLine + "\n" + "\n");
        }
    }

    return null;
}

From source file:com.liferay.tools.sourceformatter.JavaTerm.java

License:Open Source License

protected void readParameterNamesAndTypes() {
    _parameterNames = new ArrayList<String>();
    _parameterTypes = new ArrayList<String>();

    if (!isConstructor() && !isMethod()) {
        return;/*from   w  w  w  . j a v  a 2  s  . c om*/
    }

    Matcher matcher = _parameterTypesPattern.matcher(_content);

    if (!matcher.find()) {
        return;
    }

    String parameters = matcher.group(3);

    if (Validator.isNull(parameters)) {
        return;
    }

    parameters = StringUtil.replace(parameters, new String[] { StringPool.TAB, StringPool.NEW_LINE },
            new String[] { StringPool.BLANK, StringPool.SPACE });

    for (int x = 0;;) {
        parameters = StringUtil.trim(parameters);

        if (parameters.startsWith("final ")) {
            parameters = parameters.substring(6);
        }

        x = parameters.indexOf(StringPool.SPACE, x + 1);

        if (x == -1) {
            return;
        }

        String parameterType = parameters.substring(0, x);

        int greaterThanCount = StringUtil.count(parameterType, StringPool.GREATER_THAN);
        int lessThanCount = StringUtil.count(parameterType, StringPool.LESS_THAN);

        if (greaterThanCount != lessThanCount) {
            continue;
        }

        _parameterTypes.add(parameterType);

        int y = parameters.indexOf(StringPool.COMMA, x);

        if (y == -1) {
            _parameterNames.add(parameters.substring(x + 1));

            return;
        }

        _parameterNames.add(parameters.substring(x + 1, y));

        parameters = parameters.substring(y + 1);

        x = 0;
    }
}

From source file:com.liferay.tools.sourceformatter.JSPSourceProcessor.java

License:Open Source License

protected String formatJSP(String fileName, String absolutePath, String content) throws IOException {

    StringBundler sb = new StringBundler();

    String currentAttributeAndValue = null;
    String previousAttribute = null;
    String previousAttributeAndValue = null;

    String currentException = null;
    String previousException = null;

    boolean hasUnsortedExceptions = false;

    try (UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
            new UnsyncStringReader(content))) {

        _checkedForIncludesFileNames = new HashSet<String>();
        _includeFileNames = new HashSet<String>();

        int lineCount = 0;

        String line = null;//from  ww  w .  ja  v a2 s . c  o  m

        String previousLine = StringPool.BLANK;

        boolean readAttributes = false;

        boolean javaSource = false;

        while ((line = unsyncBufferedReader.readLine()) != null) {
            lineCount++;

            if (portalSource && hasUnusedTaglib(fileName, line)) {
                continue;
            }

            if (!fileName.contains("jsonw") || !fileName.endsWith("action.jsp")) {

                line = trimLine(line, false);
            }

            if (line.contains("<aui:button ") && line.contains("type=\"button\"")) {

                processErrorMessage(fileName, "aui:button " + fileName + " " + lineCount);
            }

            if (line.contains("debugger.")) {
                processErrorMessage(fileName, "debugger " + fileName + " " + lineCount);
            }

            String trimmedLine = StringUtil.trimLeading(line);
            String trimmedPreviousLine = StringUtil.trimLeading(previousLine);

            checkStringBundler(trimmedLine, fileName, lineCount);

            checkEmptyCollection(trimmedLine, fileName, lineCount);

            if (trimmedLine.equals("<%") || trimmedLine.equals("<%!")) {
                javaSource = true;
            } else if (trimmedLine.equals("%>")) {
                javaSource = false;
            }

            if (javaSource || trimmedLine.contains("<%= ")) {
                checkInefficientStringMethods(line, fileName, absolutePath, lineCount);
            }

            if (javaSource && portalSource && !isExcluded(_unusedVariablesExclusions, absolutePath, lineCount)
                    && !_jspContents.isEmpty() && hasUnusedVariable(fileName, trimmedLine)) {

                continue;
            }

            // LPS-47179

            if (line.contains(".sendRedirect(") && !fileName.endsWith("_jsp.jsp")) {

                processErrorMessage(fileName, "Do not use sendRedirect in jsp: " + fileName + " " + lineCount);
            }

            if (!trimmedLine.equals("%>") && line.contains("%>") && !line.contains("--%>")
                    && !line.contains(" %>")) {

                line = StringUtil.replace(line, "%>", " %>");
            }

            if (line.contains("<%=") && !line.contains("<%= ")) {
                line = StringUtil.replace(line, "<%=", "<%= ");
            }

            if (trimmedPreviousLine.equals("%>") && Validator.isNotNull(line) && !trimmedLine.equals("-->")) {

                sb.append("\n");
            } else if (Validator.isNotNull(previousLine) && !trimmedPreviousLine.equals("<!--")
                    && trimmedLine.equals("<%")) {

                sb.append("\n");
            } else if (trimmedPreviousLine.equals("<%") && Validator.isNull(line)) {

                continue;
            } else if (trimmedPreviousLine.equals("<%") && trimmedLine.startsWith("//")) {

                sb.append("\n");
            } else if (Validator.isNull(previousLine) && trimmedLine.equals("%>") && (sb.index() > 2)) {

                String lineBeforePreviousLine = sb.stringAt(sb.index() - 3);

                if (!lineBeforePreviousLine.startsWith("//")) {
                    sb.setIndex(sb.index() - 1);
                }
            }

            if ((trimmedLine.startsWith("if (") || trimmedLine.startsWith("else if (")
                    || trimmedLine.startsWith("while (")) && trimmedLine.endsWith(") {")) {

                checkIfClauseParentheses(trimmedLine, fileName, lineCount);
            }

            if (readAttributes) {
                if (!trimmedLine.startsWith(StringPool.FORWARD_SLASH)
                        && !trimmedLine.startsWith(StringPool.GREATER_THAN)) {

                    int pos = trimmedLine.indexOf(StringPool.EQUAL);

                    if (pos != -1) {
                        String attribute = trimmedLine.substring(0, pos);

                        if (!trimmedLine.endsWith(StringPool.APOSTROPHE)
                                && !trimmedLine.endsWith(StringPool.GREATER_THAN)
                                && !trimmedLine.endsWith(StringPool.QUOTE)) {

                            processErrorMessage(fileName, "attribute: " + fileName + " " + lineCount);

                            readAttributes = false;
                        } else if (trimmedLine.endsWith(StringPool.APOSTROPHE)
                                && !trimmedLine.contains(StringPool.QUOTE)) {

                            line = StringUtil.replace(line, StringPool.APOSTROPHE, StringPool.QUOTE);

                            readAttributes = false;
                        } else if (Validator.isNotNull(previousAttribute)) {
                            if (!isAttributName(attribute) && !attribute.startsWith(StringPool.LESS_THAN)) {

                                processErrorMessage(fileName, "attribute: " + fileName + " " + lineCount);

                                readAttributes = false;
                            } else if (Validator.isNull(previousAttributeAndValue)
                                    && (previousAttribute.compareTo(attribute) > 0)) {

                                previousAttributeAndValue = previousLine;
                                currentAttributeAndValue = line;
                            }
                        }

                        if (!readAttributes) {
                            previousAttribute = null;
                            previousAttributeAndValue = null;
                        } else {
                            previousAttribute = attribute;
                        }
                    }
                } else {
                    previousAttribute = null;

                    readAttributes = false;
                }
            }

            if (!hasUnsortedExceptions) {
                int x = line.indexOf("<liferay-ui:error exception=\"<%=");

                if (x != -1) {
                    int y = line.indexOf(".class %>", x);

                    if (y != -1) {
                        currentException = line.substring(x, y);

                        if (Validator.isNotNull(previousException)
                                && (previousException.compareTo(currentException) > 0)) {

                            currentException = line;
                            previousException = previousLine;

                            hasUnsortedExceptions = true;
                        }
                    }
                }

                if (!hasUnsortedExceptions) {
                    previousException = currentException;
                    currentException = null;
                }
            }

            if (trimmedLine.startsWith(StringPool.LESS_THAN) && !trimmedLine.startsWith("<%")
                    && !trimmedLine.startsWith("<!")) {

                if (!trimmedLine.contains(StringPool.GREATER_THAN) && !trimmedLine.contains(StringPool.SPACE)) {

                    readAttributes = true;
                } else {
                    line = sortAttributes(fileName, line, lineCount, true);
                }
            }

            if (!trimmedLine.contains(StringPool.DOUBLE_SLASH) && !trimmedLine.startsWith(StringPool.STAR)) {

                while (trimmedLine.contains(StringPool.TAB)) {
                    line = StringUtil.replaceLast(line, StringPool.TAB, StringPool.SPACE);

                    trimmedLine = StringUtil.replaceLast(trimmedLine, StringPool.TAB, StringPool.SPACE);
                }

                while (trimmedLine.contains(StringPool.DOUBLE_SPACE)
                        && !trimmedLine.contains(StringPool.QUOTE + StringPool.DOUBLE_SPACE)
                        && !fileName.endsWith(".vm")) {

                    line = StringUtil.replaceLast(line, StringPool.DOUBLE_SPACE, StringPool.SPACE);

                    trimmedLine = StringUtil.replaceLast(trimmedLine, StringPool.DOUBLE_SPACE,
                            StringPool.SPACE);
                }
            }

            if (!fileName.endsWith("/touch.jsp")) {
                int x = line.indexOf("<%@ include file");

                if (x != -1) {
                    x = line.indexOf(StringPool.QUOTE, x);

                    int y = line.indexOf(StringPool.QUOTE, x + 1);

                    if (y != -1) {
                        String includeFileName = line.substring(x + 1, y);

                        Matcher matcher = _jspIncludeFilePattern.matcher(includeFileName);

                        if (!matcher.find()) {
                            processErrorMessage(fileName, "include: " + fileName + " " + lineCount);
                        }
                    }
                }
            }

            line = replacePrimitiveWrapperInstantiation(fileName, line, lineCount);

            previousLine = line;

            sb.append(line);
            sb.append("\n");
        }
    }

    content = sb.toString();

    if (content.endsWith("\n")) {
        content = content.substring(0, content.length() - 1);
    }

    content = formatTaglibQuotes(fileName, content, StringPool.QUOTE);
    content = formatTaglibQuotes(fileName, content, StringPool.APOSTROPHE);

    if (Validator.isNotNull(previousAttributeAndValue)) {
        content = StringUtil.replaceFirst(content, previousAttributeAndValue + "\n" + currentAttributeAndValue,
                currentAttributeAndValue + "\n" + previousAttributeAndValue);
    }

    if (hasUnsortedExceptions) {
        if ((StringUtil.count(content, currentException) > 1)
                || (StringUtil.count(content, previousException) > 1)) {

            processErrorMessage(fileName, "unsorted exceptions: " + fileName);
        } else {
            content = StringUtil.replaceFirst(content, previousException, currentException);

            content = StringUtil.replaceLast(content, currentException, previousException);
        }
    }

    return content;
}

From source file:com.liferay.tools.sourceformatter.PropertiesSourceProcessor.java

License:Open Source License

protected String formatPortalPortalProperties() throws Exception {
    if (!portalSource) {
        return ContentUtil.get("portal.properties");
    }//from w w  w . ja  va2s . c  om

    String fileName = "portal-impl/src/portal.properties";

    File file = getFile(fileName, 4);

    String content = fileUtil.read(file);

    StringBundler sb = new StringBundler();

    try (UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
            new UnsyncStringReader(content))) {

        String line = null;

        while ((line = unsyncBufferedReader.readLine()) != null) {
            line = trimLine(line, true);

            if (line.startsWith(StringPool.TAB)) {
                line = line.replaceFirst(StringPool.TAB, StringPool.FOUR_SPACES);
            }

            sb.append(line);
            sb.append("\n");
        }
    }

    String newContent = sb.toString();

    if (newContent.endsWith("\n")) {
        newContent = newContent.substring(0, newContent.length() - 1);
    }

    processFormattedFile(file, fileName, content, newContent);

    return newContent;
}