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

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

Introduction

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

Prototype

String EQUAL

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

Click Source Link

Usage

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

License:Open Source License

protected String sortAttributes(String fileName, String line, int lineCount, boolean allowApostropheDelimeter) {

    String s = line;//from   ww w .  j ava  2  s. c  om

    int x = s.indexOf(StringPool.SPACE);

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

    s = s.substring(x + 1);

    String previousAttribute = null;
    String previousAttributeAndValue = null;

    boolean wrongOrder = false;

    for (x = 0;;) {
        x = s.indexOf(StringPool.EQUAL);

        if ((x == -1) || (s.length() <= (x + 1))) {
            return line;
        }

        String attribute = s.substring(0, x);

        if (!isAttributName(attribute)) {
            return line;
        }

        if (Validator.isNotNull(previousAttribute) && (previousAttribute.compareTo(attribute) > 0)) {

            wrongOrder = true;
        }

        s = s.substring(x + 1);

        char delimeter = s.charAt(0);

        if ((delimeter != CharPool.APOSTROPHE) && (delimeter != CharPool.QUOTE)) {

            if (delimeter != CharPool.AMPERSAND) {
                processErrorMessage(fileName, "delimeter: " + fileName + " " + lineCount);
            }

            return line;
        }

        s = s.substring(1);

        String value = null;

        int y = -1;

        while (true) {
            y = s.indexOf(delimeter, y + 1);

            if ((y == -1) || (s.length() <= (y + 1))) {
                return line;
            }

            value = s.substring(0, y);

            if (value.startsWith("<%")) {
                int endJavaCodeSignCount = StringUtil.count(value, "%>");
                int startJavaCodeSignCount = StringUtil.count(value, "<%");

                if (endJavaCodeSignCount == startJavaCodeSignCount) {
                    break;
                }
            } else {
                int greaterThanCount = StringUtil.count(value, StringPool.GREATER_THAN);
                int lessThanCount = StringUtil.count(value, StringPool.LESS_THAN);

                if (greaterThanCount == lessThanCount) {
                    break;
                }
            }
        }

        if (delimeter == CharPool.APOSTROPHE) {
            if (!value.contains(StringPool.QUOTE)) {
                line = StringUtil.replace(line, StringPool.APOSTROPHE + value + StringPool.APOSTROPHE,
                        StringPool.QUOTE + value + StringPool.QUOTE);

                return sortAttributes(fileName, line, lineCount, allowApostropheDelimeter);
            } else if (!allowApostropheDelimeter) {
                String newValue = StringUtil.replace(value, StringPool.QUOTE, "&quot;");

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

                return sortAttributes(fileName, line, lineCount, allowApostropheDelimeter);
            }
        }

        StringBundler sb = new StringBundler(5);

        sb.append(attribute);
        sb.append(StringPool.EQUAL);
        sb.append(delimeter);
        sb.append(value);
        sb.append(delimeter);

        String currentAttributeAndValue = sb.toString();

        if (wrongOrder) {
            if ((StringUtil.count(line, currentAttributeAndValue) == 1)
                    && (StringUtil.count(line, previousAttributeAndValue) == 1)) {

                line = StringUtil.replaceFirst(line, previousAttributeAndValue, currentAttributeAndValue);

                line = StringUtil.replaceLast(line, currentAttributeAndValue, previousAttributeAndValue);

                return sortAttributes(fileName, line, lineCount, allowApostropheDelimeter);
            }

            return line;
        }

        s = s.substring(y + 1);

        if (s.startsWith(StringPool.GREATER_THAN)) {
            x = s.indexOf(StringPool.SPACE);

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

            s = s.substring(x + 1);

            previousAttribute = null;
            previousAttributeAndValue = null;
        } else {
            s = StringUtil.trimLeading(s);

            previousAttribute = attribute;
            previousAttributeAndValue = currentAttributeAndValue;
        }
    }
}

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

License:Open Source License

protected void checkStaticableFieldType(JavaTerm javaTerm) {
    String javaTermContent = javaTerm.getContent();

    if (!javaTermContent.contains(StringPool.EQUAL)) {
        return;/*from   www  .j  a v  a2  s .  c om*/
    }

    String newJavaTermContent = StringUtil.replaceFirst(javaTermContent, "private final",
            "private static final");

    _content = StringUtil.replace(_content, javaTermContent, newJavaTermContent);
}

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

License:Open Source License

protected Tuple getJavaTermTuple(String line, String content, int index) {
    int posStartNextLine = index;

    while (!line.endsWith(StringPool.OPEN_CURLY_BRACE) && !line.endsWith(StringPool.SEMICOLON)) {

        posStartNextLine = content.indexOf(StringPool.NEW_LINE, posStartNextLine) + 1;

        int posEndNextline = content.indexOf(StringPool.NEW_LINE, posStartNextLine);

        String nextLine = content.substring(posStartNextLine, posEndNextline);

        nextLine = StringUtil.trimLeading(nextLine);

        if (line.endsWith(StringPool.OPEN_PARENTHESIS)) {
            line += nextLine;//from  www . j a  va  2s  .co  m
        } else {
            line += StringPool.SPACE + nextLine;
        }
    }

    line = StringUtil.replace(line, " synchronized ", StringPool.SPACE);

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

    if (line.startsWith(_indent + "public static ")) {
        if (line.contains(" class ") || line.contains(" enum ")) {
            return new Tuple(getClassName(line), JavaTerm.TYPE_CLASS_PUBLIC_STATIC);
        }

        if (line.contains(StringPool.EQUAL) || (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) {

            return new Tuple(getVariableName(line), JavaTerm.TYPE_VARIABLE_PUBLIC_STATIC);
        }

        if (pos != -1) {
            return new Tuple(getConstructorOrMethodName(line, pos), JavaTerm.TYPE_METHOD_PUBLIC_STATIC);
        }
    } else if (line.startsWith(_indent + "public ")) {
        if (line.contains(" @interface ") || line.contains(" class ") || line.contains(" enum ")
                || line.contains(" interface ")) {

            return new Tuple(getClassName(line), JavaTerm.TYPE_CLASS_PUBLIC);
        }

        if (line.contains(StringPool.EQUAL) || (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) {

            return new Tuple(getVariableName(line), JavaTerm.TYPE_VARIABLE_PUBLIC);
        }

        if (pos != -1) {
            int spaceCount = StringUtil.count(line.substring(0, pos), StringPool.SPACE);

            if (spaceCount == 1) {
                return new Tuple(getConstructorOrMethodName(line, pos), JavaTerm.TYPE_CONSTRUCTOR_PUBLIC);
            }

            if (spaceCount > 1) {
                return new Tuple(getConstructorOrMethodName(line, pos), JavaTerm.TYPE_METHOD_PUBLIC);
            }
        }
    } else if (line.startsWith(_indent + "protected static ")) {
        if (line.contains(" class ") || line.contains(" enum ")) {
            return new Tuple(getClassName(line), JavaTerm.TYPE_CLASS_PROTECTED_STATIC);
        }

        if (line.contains(StringPool.EQUAL) || (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) {

            return new Tuple(getVariableName(line), JavaTerm.TYPE_VARIABLE_PROTECTED_STATIC);
        }

        if (pos != -1) {
            return new Tuple(getConstructorOrMethodName(line, pos), JavaTerm.TYPE_METHOD_PROTECTED_STATIC);
        }
    } else if (line.startsWith(_indent + "protected ")) {
        if (line.contains(" @interface ") || line.contains(" class ") || line.contains(" enum ")
                || line.contains(" interface ")) {

            return new Tuple(getClassName(line), JavaTerm.TYPE_CLASS_PROTECTED);
        }

        if (pos != -1) {
            if (!line.contains(StringPool.EQUAL)) {
                int spaceCount = StringUtil.count(line.substring(0, pos), StringPool.SPACE);

                if (spaceCount == 1) {
                    return new Tuple(getConstructorOrMethodName(line, pos),
                            JavaTerm.TYPE_CONSTRUCTOR_PROTECTED);
                }

                if (spaceCount > 1) {
                    return new Tuple(getConstructorOrMethodName(line, pos), JavaTerm.TYPE_METHOD_PROTECTED);
                }
            }
        }

        return new Tuple(getVariableName(line), JavaTerm.TYPE_VARIABLE_PROTECTED);
    } else if (line.startsWith(_indent + "private static ")) {
        if (line.contains(" class ") || line.contains(" enum ")) {
            return new Tuple(getClassName(line), JavaTerm.TYPE_CLASS_PRIVATE_STATIC);
        }

        if (line.contains(StringPool.EQUAL) || (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) {

            return new Tuple(getVariableName(line), JavaTerm.TYPE_VARIABLE_PRIVATE_STATIC);
        }

        if (pos != -1) {
            return new Tuple(getConstructorOrMethodName(line, pos), JavaTerm.TYPE_METHOD_PRIVATE_STATIC);
        }
    } else if (line.startsWith(_indent + "private ")) {
        if (line.contains(" @interface ") || line.contains(" class ") || line.contains(" enum ")
                || line.contains(" interface ")) {

            return new Tuple(getClassName(line), JavaTerm.TYPE_CLASS_PRIVATE);
        }

        if (line.contains(StringPool.EQUAL) || (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) {

            return new Tuple(getVariableName(line), JavaTerm.TYPE_VARIABLE_PRIVATE);
        }

        if (pos != -1) {
            int spaceCount = StringUtil.count(line.substring(0, pos), StringPool.SPACE);

            if (spaceCount == 1) {
                return new Tuple(getConstructorOrMethodName(line, pos), JavaTerm.TYPE_CONSTRUCTOR_PRIVATE);
            }

            if (spaceCount > 1) {
                return new Tuple(getConstructorOrMethodName(line, pos), JavaTerm.TYPE_METHOD_PRIVATE);
            }
        }
    } else if (line.startsWith(_indent + "static {")) {
        return new Tuple("static", JavaTerm.TYPE_STATIC_BLOCK);
    }

    return null;
}

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

License:Open Source License

protected String getVariableName(String line) {
    int x = line.indexOf(StringPool.EQUAL);
    int y = line.lastIndexOf(StringPool.SPACE);

    if (x != -1) {
        line = line.substring(0, x);//  www  . j a  v a 2s .co m
        line = StringUtil.trim(line);

        y = line.lastIndexOf(StringPool.SPACE);

        return line.substring(y + 1);
    }

    if (line.endsWith(StringPool.SEMICOLON)) {
        return line.substring(y + 1, line.length() - 1);
    }

    return StringPool.BLANK;
}

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 w  ww. j  ava 2s  .  com
        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 absolutePath, String line,
        String trimmedLine, int lineLength, int lineCount, String previousLine, int lineTabCount,
        int previousLineTabCount) {

    if (Validator.isNull(line) || Validator.isNull(previousLine)
            || isExcluded(_fitOnSingleLineExclusions, absolutePath, lineCount)) {

        return null;
    }/*from  w  w  w  . j  av a  2 s . c  om*/

    String trimmedPreviousLine = StringUtil.trimLeading(previousLine);

    if (line.contains("// ") || line.contains("*/") || line.contains("*/") || previousLine.contains("// ")
            || previousLine.contains("*/") || previousLine.contains("*/")) {

        return null;
    }

    int tabDiff = lineTabCount - previousLineTabCount;

    if (previousLine.endsWith(" extends")) {
        return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount,
                previousLine, "extends", tabDiff, false, false, false);
    }

    if (previousLine.endsWith(" implements")) {
        return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount,
                previousLine, "implements ", tabDiff, false, false, false);
    }

    if (trimmedLine.startsWith("+ ") || trimmedLine.startsWith("- ") || trimmedLine.startsWith("|| ")
            || trimmedLine.startsWith("&& ")) {

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

        String linePart = trimmedLine.substring(0, pos);

        return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount,
                previousLine, linePart, tabDiff, true, true, false);
    }

    int previousLineLength = getLineLength(previousLine);

    if ((trimmedLine.length() + previousLineLength) < _MAX_LINE_LENGTH) {
        if (trimmedPreviousLine.startsWith("for ") && previousLine.endsWith(StringPool.COLON)
                && line.endsWith(StringPool.OPEN_CURLY_BRACE)) {

            return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount,
                    previousLine, null, tabDiff, false, true, false);
        }

        if (line.endsWith(StringPool.SEMICOLON) && !previousLine.endsWith(StringPool.COLON)
                && !previousLine.endsWith(StringPool.OPEN_BRACKET)
                && !previousLine.endsWith(StringPool.OPEN_CURLY_BRACE)
                && !previousLine.endsWith(StringPool.OPEN_PARENTHESIS)
                && !previousLine.endsWith(StringPool.PERIOD) && (lineTabCount == (previousLineTabCount + 1))) {

            return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount,
                    previousLine, null, tabDiff, false, true, false);
        }

        if ((trimmedPreviousLine.startsWith("if ") || trimmedPreviousLine.startsWith("else "))
                && (previousLine.endsWith("||") || previousLine.endsWith("&&"))
                && line.endsWith(StringPool.OPEN_CURLY_BRACE)) {

            return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount,
                    previousLine, null, tabDiff, false, true, false);
        }

        if ((trimmedLine.startsWith("extends ") || trimmedLine.startsWith("implements ")
                || trimmedLine.startsWith("throws"))
                && (line.endsWith(StringPool.OPEN_CURLY_BRACE) || line.endsWith(StringPool.SEMICOLON))
                && (lineTabCount == (previousLineTabCount + 1))) {

            return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount,
                    previousLine, null, tabDiff, false, true, false);
        }

        if (previousLine.endsWith(StringPool.EQUAL) && line.endsWith(StringPool.OPEN_PARENTHESIS)) {

            String nextLine = getNextLine(content, lineCount);

            if (nextLine.endsWith(StringPool.SEMICOLON)) {
                return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount,
                        previousLine, null, tabDiff, false, true, true);
            }
        }
    }

    if (((trimmedLine.length() + previousLineLength) <= _MAX_LINE_LENGTH)
            && (previousLine.endsWith(StringPool.OPEN_BRACKET)
                    || previousLine.endsWith(StringPool.OPEN_PARENTHESIS)
                    || previousLine.endsWith(StringPool.PERIOD))
            && line.endsWith(StringPool.SEMICOLON)) {

        return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount,
                previousLine, null, tabDiff, false, false, false);
    }

    if (previousLine.endsWith(StringPool.EQUAL) && line.endsWith(StringPool.SEMICOLON)) {

        String tempLine = trimmedLine;

        for (int pos = 0;;) {
            pos = tempLine.indexOf(StringPool.DASH);

            if (pos == -1) {
                pos = tempLine.indexOf(StringPool.PLUS);
            }

            if (pos == -1) {
                pos = tempLine.indexOf(StringPool.SLASH);
            }

            if (pos == -1) {
                pos = tempLine.indexOf(StringPool.STAR);
            }

            if (pos == -1) {
                pos = tempLine.indexOf("||");
            }

            if (pos == -1) {
                pos = tempLine.indexOf("&&");
            }

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

            String linePart = tempLine.substring(0, pos);

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

            if (openParenthesisCount == closeParenthesisCount) {
                return null;
            }

            tempLine = tempLine.substring(0, pos) + tempLine.substring(pos + 1);
        }

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

        if (x == 0) {
            x = trimmedLine.indexOf(StringPool.OPEN_PARENTHESIS, 1);
        }

        if (x != -1) {
            int y = trimmedLine.indexOf(StringPool.CLOSE_PARENTHESIS, x);
            int z = trimmedLine.indexOf(StringPool.QUOTE);

            if (((x + 1) != y) && ((z == -1) || (z > x))) {
                char previousChar = trimmedLine.charAt(x - 1);

                if ((previousChar != CharPool.CLOSE_PARENTHESIS) && (previousChar != CharPool.OPEN_PARENTHESIS)
                        && (previousChar != CharPool.SPACE)
                        && (previousLineLength + 1 + x) < _MAX_LINE_LENGTH) {

                    String linePart = trimmedLine.substring(0, x + 1);

                    if (linePart.startsWith(StringPool.OPEN_PARENTHESIS)
                            && !linePart.contains(StringPool.CLOSE_PARENTHESIS)) {

                        return null;
                    }

                    return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount,
                            previousLine, linePart, tabDiff, true, true, false);
                }
            }
        }
    }

    if (previousLine.endsWith(StringPool.COMMA) && (previousLineTabCount == lineTabCount)
            && !previousLine.contains(StringPool.CLOSE_CURLY_BRACE)
            && (line.endsWith(") {") || !line.endsWith(StringPool.OPEN_CURLY_BRACE))) {

        int x = trimmedLine.indexOf(StringPool.COMMA);

        if (x != -1) {
            while ((previousLineLength + 1 + x) < _MAX_LINE_LENGTH) {
                String linePart = trimmedLine.substring(0, x + 1);

                if (isValidJavaParameter(linePart)) {
                    if (trimmedLine.equals(linePart)) {
                        return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength,
                                lineCount, previousLine, null, tabDiff, false, true, false);
                    } else {
                        return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength,
                                lineCount, previousLine, linePart + StringPool.SPACE, tabDiff, true, true,
                                false);
                    }
                }

                String partAfterComma = trimmedLine.substring(x + 1);

                int pos = partAfterComma.indexOf(StringPool.COMMA);

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

                x = x + pos + 1;
            }
        } else if (!line.endsWith(StringPool.OPEN_PARENTHESIS) && !line.endsWith(StringPool.PLUS)
                && !line.endsWith(StringPool.PERIOD)
                && (!trimmedLine.startsWith("new ") || !line.endsWith(StringPool.OPEN_CURLY_BRACE))
                && ((trimmedLine.length() + previousLineLength) < _MAX_LINE_LENGTH)) {

            return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount,
                    previousLine, null, tabDiff, false, true, false);
        }
    }

    if (!previousLine.endsWith(StringPool.OPEN_PARENTHESIS)) {
        return null;
    }

    if (StringUtil.count(previousLine, StringPool.OPEN_PARENTHESIS) > 1) {
        int pos = trimmedPreviousLine.lastIndexOf(StringPool.OPEN_PARENTHESIS,
                trimmedPreviousLine.length() - 2);

        if ((pos > 0) && Character.isLetterOrDigit(trimmedPreviousLine.charAt(pos - 1))) {

            String filePart = trimmedPreviousLine.substring(pos + 1);

            if (!filePart.contains(StringPool.CLOSE_PARENTHESIS) && !filePart.contains(StringPool.QUOTE)) {

                return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount,
                        previousLine, filePart, tabDiff, false, false, false);
            }
        }
    }

    if ((trimmedLine.length() + previousLineLength) > _MAX_LINE_LENGTH) {
        return null;
    }

    if (line.endsWith(StringPool.COMMA)) {
        String strippedQuotesLine = stripQuotes(trimmedLine, CharPool.QUOTE);

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

        if (closeParenthesisCount > openParenthesisCount) {
            return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount,
                    previousLine, null, tabDiff, false, false, false);
        }
    }

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

        return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount,
                previousLine, null, tabDiff, false, false, false);
    }

    return null;
}

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  . j  a  v a 2s  . c  om

        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 void formatPortalProperties(String fileName, String content) throws Exception {

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

    int lineCount = 0;

    String line = null;// w  w w  . j av  a 2  s.com

    int previousPos = -1;

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

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

        if (pos == -1) {
            continue;
        }

        String property = line.substring(0, pos + 1);

        property = property.trim();

        pos = _portalPortalPropertiesContent.indexOf(StringPool.FOUR_SPACES + property);

        if (pos == -1) {
            continue;
        }

        if (pos < previousPos) {
            processErrorMessage(fileName, "sort " + fileName + " " + lineCount);
        }

        previousPos = pos;
    }
}

From source file:com.liferay.util.bridges.php.PHPServletRequest.java

License:Open Source License

public PHPServletRequest(HttpServletRequest request, ServletConfig servletConfig, RenderRequest renderRequest,
        RenderResponse renderResponse, PortletConfig portletConfig, String phpURI, boolean addPortletParams) {

    super(request);

    _servletConfig = servletConfig;// w  w  w  .j  a v a 2s .  c  o  m
    _renderRequest = renderRequest;
    _renderResponse = renderResponse;
    _portletConfig = portletConfig;

    StringBundler sb = new StringBundler();

    int pos = phpURI.indexOf(CharPool.QUESTION);

    if (pos != -1) {
        _path = phpURI.substring(0, pos);

        sb.append(phpURI.substring(pos + 1));
    } else {
        _path = phpURI;
    }

    if (addPortletParams) {
        sb.append(StringPool.AMPERSAND);
        sb.append("portlet_namespace");
        sb.append(StringPool.EQUAL);
        sb.append(_renderResponse.getNamespace());
        sb.append(StringPool.AMPERSAND);
        sb.append("portlet_name");
        sb.append(StringPool.EQUAL);
        sb.append(_portletConfig.getPortletName());
    }

    _queryString = sb.toString();

    request.setAttribute(JavaConstants.JAVAX_SERVLET_INCLUDE_QUERY_STRING, getQueryString());
    request.setAttribute(JavaConstants.JAVAX_SERVLET_INCLUDE_PATH_INFO, getPathInfo());
    request.setAttribute(JavaConstants.JAVAX_SERVLET_INCLUDE_REQUEST_URI, getRequestURI());
    request.setAttribute(JavaConstants.JAVAX_SERVLET_INCLUDE_SERVLET_PATH, _path);
}

From source file:com.liferay.wiki.engine.markdown.pegdown.ast.LiferayPegDownProcessor.java

License:Open Source License

protected String stripIds(String content) {
    int index = content.indexOf("[](id=");

    if (index == -1) {
        return content;
    }/*w w  w . j av a 2s.  c  om*/

    StringBundler sb = new StringBundler();

    do {
        int x = content.indexOf(StringPool.EQUAL, index);

        int y = content.indexOf(StringPool.CLOSE_PARENTHESIS, x);

        if (y != -1) {
            int z = content.indexOf("</h", y);

            if (z != (y + 1)) {
                sb.append(content.substring(0, y + 1));
            } else {
                sb.append(StringUtil.trimTrailing(content.substring(0, index)));
            }

            content = content.substring(y + 1);
        } else {
            if (_log.isWarnEnabled()) {
                String msg = content.substring(index);

                // Get the invalid id text from the content

                int spaceIndex = content.indexOf(StringPool.SPACE);

                if (spaceIndex != -1) {
                    msg = content.substring(index, spaceIndex);
                }

                _log.warn("Missing ')' for web content containing header id " + msg);
            }

            // Since no close parenthesis remains in the content, stop
            // stripping out IDs and simply include all of the remaining
            // content

            break;
        }
    } while ((index = content.indexOf("[](id=")) != -1);

    sb.append(content);

    return sb.toString();
}