Example usage for org.apache.commons.io IOCase checkIndexOf

List of usage examples for org.apache.commons.io IOCase checkIndexOf

Introduction

In this page you can find the example usage for org.apache.commons.io IOCase checkIndexOf.

Prototype

public int checkIndexOf(String str, int strStartIndex, String search) 

Source Link

Document

Checks if one string contains another starting at a specific index using the case-sensitivity rule.

Usage

From source file:com.qwazr.utils.WildcardMatcher.java

/**
 * Match the passed name with the current pattern
 *
 * @param name            the string to test
 * @param caseSensitivity//from   w w w . j a v a  2  s.  c om
 * @return true if the name match the pattern
 */
public boolean match(String name, IOCase caseSensitivity) {
    if (name == null && wcs == null) {
        return true;
    }
    if (name == null || wcs == null) {
        return false;
    }
    if (caseSensitivity == null) {
        caseSensitivity = IOCase.SENSITIVE;
    }
    int length = name.length();
    boolean anyChars = false;
    int textIdx = 0;
    int wcsIdx = 0;
    Stack<int[]> backtrack = new Stack<int[]>();

    // loop around a backtrack stack, to handle complex * matching
    do {
        if (backtrack.size() > 0) {
            int[] array = backtrack.pop();
            wcsIdx = array[0];
            textIdx = array[1];
            anyChars = true;
        }

        // loop whilst tokens and text left to process
        while (wcsIdx < wcs.length) {

            if (wcs[wcsIdx].equals("?")) {
                // ? so move to next text char
                textIdx++;
                if (textIdx > length) {
                    break;
                }
                anyChars = false;

            } else if (wcs[wcsIdx].equals("*")) {
                // set any chars status
                anyChars = true;
                if (wcsIdx == wcs.length - 1) {
                    textIdx = length;
                }

            } else {
                // matching text token
                if (anyChars) {
                    // any chars then try to locate text token
                    textIdx = caseSensitivity.checkIndexOf(name, textIdx, wcs[wcsIdx]);
                    if (textIdx == -1) {
                        // token not found
                        break;
                    }
                    int repeat = caseSensitivity.checkIndexOf(name, textIdx + 1, wcs[wcsIdx]);
                    if (repeat >= 0) {
                        backtrack.push(new int[] { wcsIdx, repeat });
                    }
                } else {
                    // matching from current position
                    if (!caseSensitivity.checkRegionMatches(name, textIdx, wcs[wcsIdx])) {
                        // couldnt match token
                        break;
                    }
                }

                // matched text token, move text index to end of matched token
                textIdx += wcs[wcsIdx].length();
                anyChars = false;
            }

            wcsIdx++;
        }

        // full match
        if (wcsIdx == wcs.length && textIdx == length) {
            return true;
        }

    } while (backtrack.size() > 0);

    return false;
}

From source file:com.matteoveroni.model.copy.FilenameUtils.java

/**
 * Checks a filename to see if it matches the specified wildcard matcher
 * allowing control over case-sensitivity.
 * <p>/*from   w w  w.j a  v  a  2  s .  com*/
 * The wildcard matcher uses the characters '?' and '*' to represent a
 * single or multiple (zero or more) wildcard characters.
 * N.B. the sequence "*?" does not work properly at present in match strings.
 * 
 * @param filename  the filename to match on
 * @param wildcardMatcher  the wildcard string to match against
 * @param caseSensitivity  what case sensitivity rule to use, null means case-sensitive
 * @return true if the filename matches the wilcard string
 * @since 1.3
 */
public static boolean wildcardMatch(String filename, String wildcardMatcher, IOCase caseSensitivity) {
    if (filename == null && wildcardMatcher == null) {
        return true;
    }
    if (filename == null || wildcardMatcher == null) {
        return false;
    }
    if (caseSensitivity == null) {
        caseSensitivity = IOCase.SENSITIVE;
    }
    String[] wcs = splitOnTokens(wildcardMatcher);
    boolean anyChars = false;
    int textIdx = 0;
    int wcsIdx = 0;
    Stack<int[]> backtrack = new Stack<int[]>();

    // loop around a backtrack stack, to handle complex * matching
    do {
        if (backtrack.size() > 0) {
            int[] array = backtrack.pop();
            wcsIdx = array[0];
            textIdx = array[1];
            anyChars = true;
        }

        // loop whilst tokens and text left to process
        while (wcsIdx < wcs.length) {

            if (wcs[wcsIdx].equals("?")) {
                // ? so move to next text char
                textIdx++;
                if (textIdx > filename.length()) {
                    break;
                }
                anyChars = false;

            } else if (wcs[wcsIdx].equals("*")) {
                // set any chars status
                anyChars = true;
                if (wcsIdx == wcs.length - 1) {
                    textIdx = filename.length();
                }

            } else {
                // matching text token
                if (anyChars) {
                    // any chars then try to locate text token
                    textIdx = caseSensitivity.checkIndexOf(filename, textIdx, wcs[wcsIdx]);
                    if (textIdx == -1) {
                        // token not found
                        break;
                    }
                    int repeat = caseSensitivity.checkIndexOf(filename, textIdx + 1, wcs[wcsIdx]);
                    if (repeat >= 0) {
                        backtrack.push(new int[] { wcsIdx, repeat });
                    }
                } else {
                    // matching from current position
                    if (!caseSensitivity.checkRegionMatches(filename, textIdx, wcs[wcsIdx])) {
                        // couldnt match token
                        break;
                    }
                }

                // matched text token, move text index to end of matched token
                textIdx += wcs[wcsIdx].length();
                anyChars = false;
            }

            wcsIdx++;
        }

        // full match
        if (wcsIdx == wcs.length && textIdx == filename.length()) {
            return true;
        }

    } while (backtrack.size() > 0);

    return false;
}