Example usage for com.liferay.portal.kernel.util StringUtil count

List of usage examples for com.liferay.portal.kernel.util StringUtil count

Introduction

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

Prototype

public static int count(String s, String text) 

Source Link

Document

Returns the number of times the text appears in the string.

Usage

From source file:com.liferay.message.boards.web.internal.portlet.action.MBAdminConfigurationAction.java

License:Open Source License

protected boolean isValidUserRank(String rank) {
    if ((StringUtil.count(rank, CharPool.EQUAL) != 1) || rank.startsWith(StringPool.EQUAL)
            || rank.endsWith(StringPool.EQUAL)) {

        return false;
    }//  www.  j av a  2s . com

    return true;
}

From source file:com.liferay.portlet.messageboards.util.BBCodeUtil.java

License:Open Source License

private static String[] _getListItems(String tagElement) {
    List<String> items = new ArrayList<String>();

    StringBuilder sb = new StringBuilder();

    int nestLevel = 0;

    for (String item : StringUtil.split(tagElement, "[*]")) {
        item = item.trim();/*w  w w.  jav a 2 s .c  o  m*/

        if (item.length() == 0) {
            continue;
        }

        int begTagCount = StringUtil.count(item, "[list");

        if (begTagCount > 0) {
            nestLevel += begTagCount;
        }

        int endTagCount = StringUtil.count(item, "[/list]");

        if (endTagCount > 0) {
            nestLevel -= endTagCount;
        }

        if (nestLevel == 0) {
            if ((begTagCount == 0) && (endTagCount == 0)) {
                items.add(item);
            } else if (endTagCount > 0) {
                if (sb.length() > 0) {
                    sb.append("[*]");
                }

                sb.append(item);

                items.add(sb.toString());

                sb.delete(0, sb.length());
            }
        } else {
            if (sb.length() > 0) {
                sb.append("[*]");
            }

            sb.append(item);
        }
    }

    return items.toArray(new String[items.size()]);
}

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

License:Open Source License

protected void checkIfClauseParentheses(String ifClause, String fileName, int lineCount) {

    int quoteCount = StringUtil.count(ifClause, StringPool.QUOTE);

    if ((quoteCount % 2) == 1) {
        return;/*from ww  w. jav  a  2 s .  c om*/
    }

    ifClause = stripQuotes(ifClause, CharPool.QUOTE);

    ifClause = stripQuotes(ifClause, CharPool.APOSTROPHE);

    if (ifClause.contains(StringPool.DOUBLE_SLASH) || ifClause.contains("/*") || ifClause.contains("*/")) {

        return;
    }

    if (hasRedundantParentheses(ifClause, "||", "&&") || hasRedundantParentheses(ifClause, "&&", "||")) {

        processErrorMessage(fileName, "redundant parentheses: " + fileName + " " + lineCount);
    }

    ifClause = stripRedundantParentheses(ifClause);

    int level = 0;
    int max = StringUtil.count(ifClause, StringPool.OPEN_PARENTHESIS);
    int previousParenthesisPos = -1;

    int[] levels = new int[max];

    for (int i = 0; i < ifClause.length(); i++) {
        char c = ifClause.charAt(i);

        if ((c == CharPool.OPEN_PARENTHESIS) || (c == CharPool.CLOSE_PARENTHESIS)) {

            if (previousParenthesisPos != -1) {
                String s = ifClause.substring(previousParenthesisPos + 1, i);

                if (hasMissingParentheses(s)) {
                    processErrorMessage(fileName, "missing parentheses: " + fileName + " " + lineCount);
                }
            }

            previousParenthesisPos = i;

            if (c == CharPool.OPEN_PARENTHESIS) {
                levels[level] = i;

                level += 1;
            } else {
                int posOpenParenthesis = levels[level - 1];

                if (level > 1) {
                    char nextChar = ifClause.charAt(i + 1);
                    char previousChar = ifClause.charAt(posOpenParenthesis - 1);

                    if (!Character.isLetterOrDigit(nextChar) && (nextChar != CharPool.PERIOD)
                            && !Character.isLetterOrDigit(previousChar)) {

                        String s = ifClause.substring(posOpenParenthesis + 1, i);

                        if (hasRedundantParentheses(s)) {
                            processErrorMessage(fileName,
                                    "redundant parentheses: " + fileName + " " + lineCount);
                        }
                    }

                    if ((previousChar == CharPool.OPEN_PARENTHESIS)
                            && (nextChar == CharPool.CLOSE_PARENTHESIS)) {

                        processErrorMessage(fileName, "redundant parentheses: " + fileName + " " + lineCount);
                    }
                }

                level -= 1;
            }
        }
    }
}

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

License:Open Source License

protected void checkStringBundler(String line, String fileName, int lineCount) {

    if ((!line.startsWith("sb.append(") && !line.contains("SB.append(")) || !line.endsWith(");")) {

        return;//from w w  w  . ja  va  2  s.c o m
    }

    int pos = line.indexOf(".append(");

    line = line.substring(pos + 8, line.length() - 2);

    line = stripQuotes(line, CharPool.QUOTE);

    if (!line.contains(" + ")) {
        return;
    }

    String[] lineParts = StringUtil.split(line, " + ");

    for (String linePart : lineParts) {
        int closeParenthesesCount = StringUtil.count(linePart, StringPool.CLOSE_PARENTHESIS);
        int openParenthesesCount = StringUtil.count(linePart, StringPool.OPEN_PARENTHESIS);

        if (closeParenthesesCount != openParenthesesCount) {
            return;
        }

        if (Validator.isNumber(linePart)) {
            return;
        }
    }

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

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

License:Open Source License

protected boolean hasRedundantParentheses(String s, String operator1, String operator2) {

    String[] parts = StringUtil.split(s, operator1);

    if (parts.length < 3) {
        return false;
    }//w w  w .ja v  a2 s. c o m

    for (int i = 1; i < (parts.length - 1); i++) {
        String part = parts[i];

        if (part.contains(operator2) || part.contains("!(")) {
            continue;
        }

        int closeParenthesesCount = StringUtil.count(part, StringPool.CLOSE_PARENTHESIS);
        int openParenthesesCount = StringUtil.count(part, StringPool.OPEN_PARENTHESIS);

        if (Math.abs(closeParenthesesCount - openParenthesesCount) == 1) {
            return true;
        }
    }

    return false;
}

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;/*www.java 2s. c o  m*/

    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 checkUnusedParameters(JavaTerm javaTerm) {
    if (!javaTerm.isPrivate() || !javaTerm.isMethod()) {
        return;//  w ww  .  ja v a 2  s.  c o  m
    }

    for (String parameterName : javaTerm.getParameterNames()) {
        if (StringUtil.count(javaTerm.getContent(), parameterName) == 1) {
            BaseSourceProcessor.processErrorMessage(_fileName,
                    "Unused parameter " + parameterName + ": " + _fileName + " " + javaTerm.getLineCount());
        }
    }
}

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;// w  w w  . j a va  2s  . c  om
        } 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 boolean isValidJavaTerm(String content) {
    if (content.startsWith(_indent + "static {")) {
        return true;
    }//from  w  w w  . j a v  a2 s .c o  m

    while (!content.startsWith(_indent + "private") && !content.startsWith(_indent + "protected")
            && !content.startsWith(_indent + "public")) {

        content = content.substring(content.indexOf("\n") + 1);
    }

    int indentLinesCount = StringUtil.count(content, "\n" + _indent)
            - StringUtil.count(content, "\n" + _indent + StringPool.TAB);

    content = StringUtil.trim(content);

    if (content.endsWith(StringPool.CLOSE_CURLY_BRACE)
            && ((indentLinesCount == 1) || (((indentLinesCount == 2) || (indentLinesCount == 3))
                    && content.contains("\n" + _indent + "static {")))) {

        return true;
    } else if ((content.endsWith("};") && (indentLinesCount == 1))
            || (content.endsWith(StringPool.SEMICOLON) && (indentLinesCount == 0))) {

        return true;
    }

    return false;
}

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

License:Open Source License

protected static String sortAnnotations(String content, String indent) throws IOException {

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

    String line = null;//  w w  w  .  j ava2 s  .c o m

    String annotation = StringPool.BLANK;
    String previousAnnotation = StringPool.BLANK;

    while ((line = unsyncBufferedReader.readLine()) != null) {
        if (line.equals(indent + StringPool.CLOSE_CURLY_BRACE)) {
            return content;
        }

        if (StringUtil.count(line, StringPool.TAB) == indent.length()) {
            if (Validator.isNotNull(previousAnnotation) && (previousAnnotation.compareTo(annotation) > 0)) {

                content = StringUtil.replaceFirst(content, previousAnnotation, annotation);
                content = StringUtil.replaceLast(content, annotation, previousAnnotation);

                return sortAnnotations(content, indent);
            }

            if (line.startsWith(indent + StringPool.AT)) {
                if (Validator.isNotNull(annotation)) {
                    previousAnnotation = annotation;
                }

                annotation = line + "\n";
            } else {
                annotation = StringPool.BLANK;
                previousAnnotation = StringPool.BLANK;
            }
        } else {
            if (Validator.isNull(annotation)) {
                return content;
            }

            annotation += line + "\n";
        }
    }

    return content;
}