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

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

Introduction

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

Prototype

String COMMA

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

Click Source Link

Usage

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

License:Open Source License

private Properties _getProperties() throws Exception {
    String fileName = "source-formatter.properties";

    Properties properties = new Properties();

    List<Properties> propertiesList = new ArrayList<Properties>();

    int level = 2;

    if (portalSource) {
        level = 3;//from  ww w  . jav  a  2  s . c om
    }

    for (int i = 0; i <= level; i++) {
        try {
            InputStream inputStream = new FileInputStream(fileName);

            Properties props = new Properties();

            props.load(inputStream);

            propertiesList.add(props);
        } catch (FileNotFoundException fnfe) {
        }

        fileName = "../" + fileName;
    }

    if (propertiesList.isEmpty()) {
        return properties;
    }

    properties = propertiesList.get(0);

    if (propertiesList.size() == 1) {
        return properties;
    }

    for (int i = 1; i < propertiesList.size(); i++) {
        Properties props = propertiesList.get(i);

        Enumeration<String> enu = (Enumeration<String>) props.propertyNames();

        while (enu.hasMoreElements()) {
            String key = enu.nextElement();

            String value = props.getProperty(key);

            if (Validator.isNull(value)) {
                continue;
            }

            if (key.contains("excludes")) {
                String existingValue = properties.getProperty(key);

                if (Validator.isNotNull(existingValue)) {
                    value = existingValue + StringPool.COMMA + value;
                }

                properties.put(key, value);
            } else if (!properties.containsKey(key)) {
                properties.put(key, value);
            }
        }
    }

    return properties;
}

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

License:Open Source License

protected void fixTabsAndIncorrectEmptyLines(JavaTerm javaTerm) {
    if (!javaTerm.isConstructor() && !javaTerm.isMethod()) {
        return;/*from  ww w .  j  av  a  2 s .c o m*/
    }

    String javaTermContent = "\n" + javaTerm.getContent();

    Pattern methodNameAndParametersPattern = Pattern
            .compile("\n" + _indent + "(private |protected |public )(.|\n)*?(\\{|;)\n");

    Matcher matcher = methodNameAndParametersPattern.matcher(javaTermContent);

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

    String methodNameAndParameters = matcher.group();

    String[] lines = StringUtil.splitLines(methodNameAndParameters);

    if (lines.length == 1) {
        if (methodNameAndParameters.endsWith("{\n") && javaTermContent.contains(methodNameAndParameters + "\n")
                && !javaTermContent.contains(methodNameAndParameters + "\n" + _indent + StringPool.TAB + "/*")
                && !javaTermContent
                        .contains(methodNameAndParameters + "\n" + _indent + StringPool.TAB + "// ")) {

            String trimmedJavaTermContent = StringUtil.trimTrailing(javaTermContent);

            if (!trimmedJavaTermContent.endsWith("\n\n" + _indent + StringPool.CLOSE_CURLY_BRACE)) {

                _content = StringUtil.replace(_content, methodNameAndParameters + "\n",
                        methodNameAndParameters);
            }
        }

        return;
    }

    if (methodNameAndParameters.endsWith("{\n") && !javaTermContent.contains(methodNameAndParameters + "\n")
            && !javaTermContent.contains(methodNameAndParameters + _indent + StringPool.CLOSE_CURLY_BRACE)) {

        _content = StringUtil.replace(_content, methodNameAndParameters, methodNameAndParameters + "\n");
    }

    boolean throwsException = methodNameAndParameters.contains(_indent + "throws ");

    String newMethodNameAndParameters = methodNameAndParameters;

    int expectedTabCount = -1;

    for (int i = 0; i < lines.length; i++) {
        String line = lines[i];

        if (line.contains(_indent + "throws ")) {
            newMethodNameAndParameters = fixLeadingTabs(newMethodNameAndParameters, line, _indent.length() + 1);

            break;
        }

        if (expectedTabCount == -1) {
            if (line.endsWith(StringPool.OPEN_PARENTHESIS)) {
                expectedTabCount = Math.max(JavaSourceProcessor.getLeadingTabCount(line), _indent.length()) + 1;

                if (throwsException && (expectedTabCount == (_indent.length() + 1))) {

                    expectedTabCount += 1;
                }
            }
        } else {
            String previousLine = lines[i - 1];

            if (previousLine.endsWith(StringPool.COMMA) || previousLine.endsWith(StringPool.OPEN_PARENTHESIS)) {

                newMethodNameAndParameters = fixLeadingTabs(newMethodNameAndParameters, line, expectedTabCount);
            } else {
                newMethodNameAndParameters = fixLeadingTabs(newMethodNameAndParameters, line,
                        JavaSourceProcessor.getLeadingTabCount(previousLine) + 1);
            }
        }
    }

    _content = StringUtil.replace(_content, methodNameAndParameters, newMethodNameAndParameters);
}

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

License:Open Source License

protected void checkRegexPattern(String regexPattern, String fileName, int lineCount) {

    int i = regexPattern.indexOf("Pattern.compile(");

    if (i == -1) {
        return;//from   w ww  .  j  a  va 2  s . c  o  m
    }

    regexPattern = regexPattern.substring(i + 16);

    regexPattern = stripQuotes(regexPattern, CharPool.QUOTE);

    i = regexPattern.indexOf(StringPool.COMMA);

    if (i != -1) {
        regexPattern = regexPattern.substring(0, i);
    } else {
        regexPattern = StringUtil.replaceLast(regexPattern, ");", StringPool.BLANK);
    }

    regexPattern = StringUtil.replace(regexPattern, StringPool.PLUS, StringPool.BLANK);

    if (Validator.isNull(regexPattern)) {
        processErrorMessage(fileName, "create pattern as global var: " + fileName + " " + lineCount);
    }
}

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 av  a2 s. c  om*/
        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 ava  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.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  . ja v a 2 s. co 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.JavaSourceProcessor.java

License:Open Source License

protected String sortExceptions(String line) {
    if (!line.endsWith(StringPool.OPEN_CURLY_BRACE) && !line.endsWith(StringPool.SEMICOLON)) {

        return line;
    }/*from w  w w .  j a  v a2 s.co  m*/

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

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

    String previousException = StringPool.BLANK;

    String[] exceptions = StringUtil.split(line.substring(x), CharPool.SPACE);

    for (int i = 1; i < exceptions.length; i++) {
        String exception = exceptions[i];

        if (exception.equals(StringPool.OPEN_CURLY_BRACE)) {
            break;
        }

        if (exception.endsWith(StringPool.COMMA) || exception.endsWith(StringPool.SEMICOLON)) {

            exception = exception.substring(0, exception.length() - 1);
        }

        if (Validator.isNotNull(previousException) && (previousException.compareToIgnoreCase(exception) > 0)) {

            line = StringUtil.replace(line, previousException + ", " + exception,
                    exception + ", " + previousException);

            return sortExceptions(line);
        }

        previousException = exception;
    }

    return line;
}

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  o m*/
    }

    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.PropertiesSourceProcessor.java

License:Open Source License

protected void formatSourceFormatterProperties(String fileName, String content) throws Exception {

    String path = StringPool.BLANK;

    int pos = fileName.lastIndexOf(StringPool.SLASH);

    if (pos != -1) {
        path = fileName.substring(0, pos + 1);
    }/*from   w w  w .ja  va 2  s. c om*/

    Properties properties = new Properties();

    InputStream inputStream = new FileInputStream(fileName);

    properties.load(inputStream);

    Enumeration<String> enu = (Enumeration<String>) properties.propertyNames();

    while (enu.hasMoreElements()) {
        String key = enu.nextElement();

        if (!key.endsWith("excludes.files")) {
            continue;
        }

        String value = properties.getProperty(key);

        if (Validator.isNull(value)) {
            continue;
        }

        List<String> propertyFileNames = ListUtil.fromString(value, StringPool.COMMA);

        for (String propertyFileName : propertyFileNames) {
            pos = propertyFileName.indexOf(StringPool.AT);

            if (pos != -1) {
                propertyFileName = propertyFileName.substring(0, pos);
            }

            if (!fileUtil.exists(path + propertyFileName)) {
                processErrorMessage(fileName, "Incorrect property value: " + propertyFileName + " " + fileName);
            }
        }
    }
}

From source file:com.liferay.twitter.service.persistence.impl.FeedPersistenceImpl.java

License:Open Source License

@Override
public Map<Serializable, Feed> fetchByPrimaryKeys(Set<Serializable> primaryKeys) {
    if (primaryKeys.isEmpty()) {
        return Collections.emptyMap();
    }//from  w w w .ja  va 2s. c  om

    Map<Serializable, Feed> map = new HashMap<Serializable, Feed>();

    if (primaryKeys.size() == 1) {
        Iterator<Serializable> iterator = primaryKeys.iterator();

        Serializable primaryKey = iterator.next();

        Feed feed = fetchByPrimaryKey(primaryKey);

        if (feed != null) {
            map.put(primaryKey, feed);
        }

        return map;
    }

    Set<Serializable> uncachedPrimaryKeys = null;

    for (Serializable primaryKey : primaryKeys) {
        Feed feed = (Feed) EntityCacheUtil.getResult(FeedModelImpl.ENTITY_CACHE_ENABLED, FeedImpl.class,
                primaryKey);

        if (feed == null) {
            if (uncachedPrimaryKeys == null) {
                uncachedPrimaryKeys = new HashSet<Serializable>();
            }

            uncachedPrimaryKeys.add(primaryKey);
        } else {
            map.put(primaryKey, feed);
        }
    }

    if (uncachedPrimaryKeys == null) {
        return map;
    }

    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) + 1);

    query.append(_SQL_SELECT_FEED_WHERE_PKS_IN);

    for (Serializable primaryKey : uncachedPrimaryKeys) {
        query.append(String.valueOf(primaryKey));

        query.append(StringPool.COMMA);
    }

    query.setIndex(query.index() - 1);

    query.append(StringPool.CLOSE_PARENTHESIS);

    String sql = query.toString();

    Session session = null;

    try {
        session = openSession();

        Query q = session.createQuery(sql);

        for (Feed feed : (List<Feed>) q.list()) {
            map.put(feed.getPrimaryKeyObj(), feed);

            cacheResult(feed);

            uncachedPrimaryKeys.remove(feed.getPrimaryKeyObj());
        }

        for (Serializable primaryKey : uncachedPrimaryKeys) {
            EntityCacheUtil.putResult(FeedModelImpl.ENTITY_CACHE_ENABLED, FeedImpl.class, primaryKey,
                    _nullFeed);
        }
    } catch (Exception e) {
        throw processException(e);
    } finally {
        closeSession(session);
    }

    return map;
}