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

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

Introduction

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

Prototype

String LESS_THAN

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

Click Source Link

Usage

From source file:com.liferay.alloy.util.TypeUtil.java

License:Open Source License

private String _removeGenericsType(String type) {
    String genericsType = _getGenericsType(type);

    if (Validator.isNotNull(genericsType)) {
        type = type.replace(StringPool.LESS_THAN.concat(genericsType).concat(StringPool.GREATER_THAN),
                StringPool.BLANK);/* ww  w .j  a  v a 2 s  .  c o m*/
    }

    return type;
}

From source file:com.liferay.mail.mailbox.IMAPMailbox.java

License:Open Source License

public InternetAddress[] parseAddresses(String addresses) throws PortalException {

    InternetAddress[] internetAddresses = new InternetAddress[0];

    try {/*from  w w w . j a v  a 2 s . c  o m*/
        internetAddresses = InternetAddress.parse(addresses, true);

        for (int i = 0; i < internetAddresses.length; i++) {
            InternetAddress internetAddress = internetAddresses[i];

            if (!Validator.isEmailAddress(internetAddress.getAddress())) {
                StringBundler sb = new StringBundler(4);

                sb.append(internetAddress.getPersonal());
                sb.append(StringPool.LESS_THAN);
                sb.append(internetAddress.getAddress());
                sb.append(StringPool.GREATER_THAN);

                throw new MailException(MailException.MESSAGE_INVALID_ADDRESS, sb.toString());
            }
        }
    } catch (AddressException ae) {
        throw new MailException(MailException.MESSAGE_INVALID_ADDRESS, ae, addresses);
    }

    return internetAddresses;
}

From source file:com.liferay.newsletter.service.impl.NewsletterCampaignLocalServiceImpl.java

License:Open Source License

protected void sendEmail(long contactId, NewsletterCampaign campaign) throws PortalException, SystemException {

    String passwordString = PortletProps.get(PropsKeys.MAIL_SESSION_MAIL_SMTP_PASSWORD);
    String userString = PortletProps.get(PropsKeys.MAIL_SESSION_MAIL_SMTP_USER);
    String host = PortletProps.get(PropsKeys.MAIL_SESSION_MAIL_SMTP_HOST);
    String port = PortletProps.get(PropsKeys.MAIL_SESSION_MAIL_SMTP_PORT);

    if (passwordString.isEmpty()) {
        passwordString = PropsUtil.get(PropsKeys.MAIL_SESSION_MAIL_SMTP_HOST);
    }/*from  ww w.j a v a 2  s . c  o m*/

    if (userString.isEmpty()) {
        userString = PropsUtil.get(PropsKeys.MAIL_SESSION_MAIL_SMTP_HOST);
    }

    if (host.isEmpty()) {
        host = PropsUtil.get(PropsKeys.MAIL_SESSION_MAIL_SMTP_HOST);
    }

    if (port.isEmpty()) {
        port = PropsUtil.get(PropsKeys.MAIL_SESSION_MAIL_SMTP_HOST);
    }

    Properties properties = new Properties();

    properties.put(NewsletterConstants.MAIL_TRANSPORT_PROTOCOL, "smtp");
    properties.put(NewsletterConstants.MAIL_SMTP_HOST, host);
    properties.put(NewsletterConstants.MAIL_SMTP_SOCKET_FACTORY_PORT, port);
    properties.put(NewsletterConstants.MAIL_SMTP_PORT, port);
    properties.put(NewsletterConstants.MAIL_SMTP_SOCKET_FACTORY_FALLBACK, "false");
    properties.put(NewsletterConstants.MAIL_SMTP_STARTTLS_ENABLE, "true");
    properties.put(NewsletterConstants.MAIL_SMTP_AUTH, "true");

    MailAuthenticator mailAuthenticator = new MailAuthenticator(userString, passwordString);

    Session session = Session.getInstance(properties, mailAuthenticator);

    String senderEmail = campaign.getSenderEmail();
    String emailSubject = campaign.getEmailSubject();

    NewsletterContent content = campaign.getContent();

    String senderName = campaign.getSenderName();

    StringBundler sb = new StringBundler(10);

    sb.append(StringPool.QUOTE);
    sb.append(senderName);
    sb.append(StringPool.QUOTE);
    sb.append(StringPool.SPACE);
    sb.append(StringPool.LESS_THAN);
    sb.append(senderEmail);
    sb.append(StringPool.GREATER_THAN);

    String from = sb.toString();

    NewsletterContact contact = newsletterContactLocalService.getContact(contactId);

    try {
        MimeMessage message = new MimeMessage(session);

        message.setFrom(new InternetAddress(from));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(contact.getEmail()));
        message.setSentDate(new Date());
        message.setSubject(emailSubject);
        message.setContent(content.getContent(), "text/html");

        Transport.send(message);
    } catch (Exception e) {
        throw new SendEmailException(e.getMessage());
    }
}

From source file:com.liferay.portlet.wiki.service.persistence.WikiPageFinderImpl.java

License:Open Source License

public int countByCreateDate(long nodeId, Timestamp createDate, boolean before) throws SystemException {

    Session session = null;//from   w  w w  .  j av a 2s.  c  o  m

    try {
        session = openSession();

        String createDateComparator = StringPool.GREATER_THAN;

        if (before) {
            createDateComparator = StringPool.LESS_THAN;
        }

        String sql = CustomSQLUtil.get(COUNT_BY_CREATE_DATE);

        sql = StringUtil.replace(sql, "[$CREATE_DATE_COMPARATOR$]", createDateComparator);

        SQLQuery q = session.createSQLQuery(sql);

        q.addScalar(COUNT_COLUMN_NAME, Type.LONG);

        QueryPos qPos = QueryPos.getInstance(q);

        qPos.add(nodeId);
        qPos.add(createDate);
        qPos.add(true);
        qPos.add(WorkflowConstants.STATUS_APPROVED);

        Iterator<Long> itr = q.iterate();

        if (itr.hasNext()) {
            Long count = itr.next();

            if (count != null) {
                return count.intValue();
            }
        }

        return 0;
    } catch (Exception e) {
        throw new SystemException(e);
    } finally {
        closeSession(session);
    }
}

From source file:com.liferay.portlet.wiki.service.persistence.WikiPageFinderImpl.java

License:Open Source License

public List<WikiPage> findByCreateDate(long nodeId, Timestamp createDate, boolean before, int start, int end)
        throws SystemException {

    Session session = null;//www .  j ava  2  s.  c  o m

    try {
        session = openSession();

        String createDateComparator = StringPool.GREATER_THAN;

        if (before) {
            createDateComparator = StringPool.LESS_THAN;
        }

        String sql = CustomSQLUtil.get(FIND_BY_CREATE_DATE);

        sql = StringUtil.replace(sql, "[$CREATE_DATE_COMPARATOR$]", createDateComparator);

        SQLQuery q = session.createSQLQuery(sql);

        q.addEntity("WikiPage", WikiPageImpl.class);

        QueryPos qPos = QueryPos.getInstance(q);

        qPos.add(nodeId);
        qPos.add(createDate);
        qPos.add(true);
        qPos.add(WorkflowConstants.STATUS_APPROVED);

        return (List<WikiPage>) QueryUtil.list(q, getDialect(), start, end);
    } catch (Exception e) {
        throw new SystemException(e);
    } finally {
        closeSession(session);
    }
}

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 w  w  w .j  a va2 s. co 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 String getClassName(String line) {
    int pos = line.indexOf(" extends ");

    if (pos == -1) {
        pos = line.indexOf(" implements ");
    }/*from www  .ja v  a2 s . c  o m*/

    if (pos == -1) {
        pos = line.indexOf(StringPool.OPEN_CURLY_BRACE);
    }

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

    pos = line.indexOf(StringPool.LESS_THAN);

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

    line = line.trim();

    pos = line.lastIndexOf(StringPool.SPACE);

    return line.substring(pos + 1);
}

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  www  . java 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 getTruncateLongLinesContent(String content, String line, String trimmedLine, int lineCount) {

    String indent = StringPool.BLANK;

    for (int i = 0; i < getLeadingTabCount(line); i++) {
        indent += StringPool.TAB;/*w  ww.j a 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 boolean isValidJavaParameter(String javaParameter) {
    if (javaParameter.contains(" implements ") || javaParameter.contains(" throws ")) {

        return false;
    }//from  w ww.  j  a va 2s  . c o m

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

    if ((quoteCount % 2) == 1) {
        return false;
    }

    javaParameter = stripQuotes(javaParameter, CharPool.QUOTE);

    int openParenthesisCount = StringUtil.count(javaParameter, StringPool.OPEN_PARENTHESIS);
    int closeParenthesisCount = StringUtil.count(javaParameter, StringPool.CLOSE_PARENTHESIS);
    int lessThanCount = StringUtil.count(javaParameter, StringPool.LESS_THAN);
    int greaterThanCount = StringUtil.count(javaParameter, StringPool.GREATER_THAN);
    int openCurlyBraceCount = StringUtil.count(javaParameter, StringPool.OPEN_CURLY_BRACE);
    int closeCurlyBraceCount = StringUtil.count(javaParameter, StringPool.CLOSE_CURLY_BRACE);

    if ((openParenthesisCount == closeParenthesisCount) && (lessThanCount == greaterThanCount)
            && (openCurlyBraceCount == closeCurlyBraceCount)) {

        return true;
    }

    return false;
}