Example usage for org.apache.commons.lang3 StringUtils lastIndexOf

List of usage examples for org.apache.commons.lang3 StringUtils lastIndexOf

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils lastIndexOf.

Prototype

public static int lastIndexOf(final CharSequence seq, final CharSequence searchSeq, final int startPos) 

Source Link

Document

Finds the last index within a CharSequence, handling null .

Usage

From source file:kenh.expl.functions.LastIndexOf.java

public int process(String seq, String searchSeq, int startPos) {
    return StringUtils.lastIndexOf(seq, searchSeq, startPos);
}

From source file:org.asqatasun.processing.ProcessRemarkServiceImpl.java

/**
* 
* @param originalElementHtml/*www.  jav  a 2s . c  om*/
* @param truncatedElementHtml
* @param indexOfElementToClose
* @return 
*/
private String closeInnerElement(String originalElementHtml, String truncatedElementHtml) {

    int startPos = truncatedElementHtml.length();

    int indexOfElementCloser = StringUtils.indexOf(originalElementHtml, ESCAPED_CLOSE_TAG, startPos);
    int indexOfElementAutoCloser = StringUtils.indexOf(originalElementHtml, AUTO_CLOSE_TAG_OCCUR, startPos);

    String innerClosedElement = StringUtils.substring(originalElementHtml, 0,
            (indexOfElementCloser + ESCAPED_CLOSE_TAG.length()));

    // if the current element is auto-close, return current well-closed element
    if (indexOfElementAutoCloser == (indexOfElementCloser - 1)) {
        return innerClosedElement;
    }

    // if the current element is not auto-close, get the name of it and 
    // and properly close it
    int indexOfElementOpenTagOpener = StringUtils.lastIndexOf(originalElementHtml, ESCAPED_OPEN_TAG, startPos);

    int indexOfElementOpenTagClose = StringUtils.indexOf(originalElementHtml, ' ', indexOfElementOpenTagOpener);

    String elementName = StringUtils.substring(originalElementHtml,
            indexOfElementOpenTagOpener + ESCAPED_OPEN_TAG.length(), indexOfElementOpenTagClose);

    return closeElement(innerClosedElement, elementName);
}

From source file:org.xwiki.editor.tool.autocomplete.internal.AutoCompletionResource.java

/**
 * Parses the passed content and return the autocompletion hints for the passed cursor position.
 *
 * @param content the Velocity content to autocomplete
 * @param offset the position of the cursor relative to the Velocity content
 * @return the list of autocompletion hints
 *///from   w  ww  .  ja v a  2 s.  co  m
private Hints getHints(String content, int offset) {
    Hints results = new Hints();

    // General algorithm:
    // - We start parsing at the first dollar before the cursor
    // - We get the full reference (a reference in VTL can be a variable, a property or a method call, see
    // http://velocity.apache.org/engine/devel/user-guide.html#References)
    // - We split the reference on "." and handle first the case of a variable. If there's no "." then it means
    // we're autocompleting a variable and we find all matching Velocity context variables and return them.
    // - If there's at least one "." then we parse the whole chain of method calls till the last dot to find the
    // return type of the last method call. This allows us to know the full list of methods for autocompletion.

    // Find the dollar sign before the current position
    char[] chars = content.toCharArray();
    VelocityContext velocityContext = getVelocityContext();
    int dollarPos = StringUtils.lastIndexOf(content, '$', offset);

    if (dollarPos == -1) {
        return results;
    }

    // Special case for when there's no variable after the dollar position since the Velocity Parser doesn't
    // support parsing this case.
    if (isCursorDirectlyAfterDollar(chars, dollarPos, offset)) {
        // Find all objects bound to the Velocity Context. We need to also look in the chained context since
        // this is where we store Velocity Tools
        results = getVelocityContextKeys("", velocityContext);
    } else {
        // The cursor is not directly after the dollar sign.
        try {
            // Get all the references after the dollar sign. For example if the input is "$a.b().ccc" then
            // we get "a.b().ccc".
            VelocityParserContext context = new VelocityParserContext();
            StringBuffer reference = new StringBuffer();
            StringBuffer identifier = new StringBuffer();
            int endPos = this.parser.getVar(chars, dollarPos, identifier, reference, context);

            // If endPos matches the current cursor position then it means we have a valid token for
            // autocompletion. Otherwise we don't autocomplete (for example there could be spaces between the
            // reference and the cursor position).
            // Note: We need to handle the special when the cursor is just after the '.' char.
            if (endPos + 1 == offset && chars[endPos] == '.') {
                endPos++;
                reference.append('.');
            }
            if (endPos == offset) {
                // Find out if we're autocompleting a variable. In this case there's no "." in the reference
                int methodPos = reference.indexOf(".");
                if (methodPos > -1) {
                    // Autocomplete a method!
                    results = getHintsForMethodCall(chars, dollarPos + methodPos, identifier.toString());
                } else {
                    // Autocomplete a variable! Find all matching variables.
                    results = getVelocityContextKeys(identifier.toString(), velocityContext);
                }
            }
        } catch (InvalidVelocityException e) {
            this.logger.debug("Failed to get autocomplete hints for content [{}] at offset [{}]",
                    new Object[] { content, offset, e });
        }
    }

    return results;
}