Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2008, Red Hat, Inc., and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * http://www.apache.org/licenses/LICENSE-2.0
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.jboss.weld.metadata;


Algorithm taken from Apache Ant's SelectorUtils class and modified to handle class hierarchies.

Author(s):
Pete Muir
Apache Ant Team
 
 public class Selectors {

    
The pattern that matches an arbitrary number of directories.

Since:
Ant 1.8.0
 
     public static final String DEEP_TREE_MATCH = "**";
 
     private Selectors() {
     }

    
Tests whether or not a given path matches a given pattern.

If you need to call this method multiple times with the same pattern you should rather use TokenizedPath

Parameters:
pattern The pattern to match against. Must not be null .
str The path to match, as a String. Must not be null.
Returns:
true if the pattern matches against the string, or false otherwise.
See also:
TokenizedPath
 
     public static boolean matchPath(String patternString str) {
         String[] patDirs = tokenize(pattern);
         return matchPath(patDirstokenize(str), true);
     }

    
Tests whether or not a given path matches a given pattern.

If you need to call this method multiple times with the same pattern you should rather use TokenizedPattern

Parameters:
pattern The pattern to match against. Must not be null .
str The path to match, as a String. Must not be null.
isCaseSensitive Whether or not matching should be performed case sensitively.
Returns:
true if the pattern matches against the string, or false otherwise.
See also:
TokenizedPattern
 
     public static boolean matchPath(String patternString strboolean isCaseSensitive) {
         String[] patDirs = tokenize(pattern);
         return matchPath(patDirstokenize(str), isCaseSensitive);
     }

    
Core implementation of matchPath. It is isolated so that it can be called from TokenizedPattern.
 
     static boolean matchPath(String[] tokenizedPatternString[] strDirsboolean isCaseSensitive) {
         int patIdxStart = 0;
         int patIdxEnd = tokenizedPattern.length - 1;
         int strIdxStart = 0;
         int strIdxEnd = strDirs.length - 1;
 
         // up to first '**'
         while (patIdxStart <= patIdxEnd && strIdxStart <= strIdxEnd) {
             String patDir = tokenizedPattern[patIdxStart];
             if (patDir.equals()) {
                 break;
             }
             if (!match(patDirstrDirs[strIdxStart], isCaseSensitive)) {
                 return false;
             }
             patIdxStart++;
             strIdxStart++;
         }
         if (strIdxStart > strIdxEnd) {
            // String is exhausted
            for (int i = patIdxStarti <= patIdxEndi++) {
                if (!tokenizedPattern[i].equals()) {
                    return false;
                }
            }
            return true;
        } else {
            if (patIdxStart > patIdxEnd) {
                // String not exhausted, but pattern is. Failure.
                return false;
            }
        }
        // up to last '**'
        while (patIdxStart <= patIdxEnd && strIdxStart <= strIdxEnd) {
            String patDir = tokenizedPattern[patIdxEnd];
            if (patDir.equals()) {
                break;
            }
            if (!match(patDirstrDirs[strIdxEnd], isCaseSensitive)) {
                return false;
            }
            patIdxEnd--;
            strIdxEnd--;
        }
        if (strIdxStart > strIdxEnd) {
            // String is exhausted
            for (int i = patIdxStarti <= patIdxEndi++) {
                if (!tokenizedPattern[i].equals()) {
                    return false;
                }
            }
            return true;
        }
        while (patIdxStart != patIdxEnd && strIdxStart <= strIdxEnd) {
            int patIdxTmp = -1;
            for (int i = patIdxStart + 1; i <= patIdxEndi++) {
                if (tokenizedPattern[i].equals()) {
                    patIdxTmp = i;
                    break;
                }
            }
            if (patIdxTmp == patIdxStart + 1) {
                // '**/**' situation, so skip one
                patIdxStart++;
                continue;
            }
            // Find the pattern between padIdxStart & padIdxTmp in str between
            // strIdxStart & strIdxEnd
            int patLength = (patIdxTmp - patIdxStart - 1);
            int strLength = (strIdxEnd - strIdxStart + 1);
            int foundIdx = -1;
            strLoop:
            for (int i = 0; i <= strLength - patLengthi++) {
                for (int j = 0; j < patLengthj++) {
                    String subPat = tokenizedPattern[patIdxStart + j + 1];
                    String subStr = strDirs[strIdxStart + i + j];
                    if (!match(subPatsubStrisCaseSensitive)) {
                        continue strLoop;
                    }
                }
                foundIdx = strIdxStart + i;
                break;
            }
            if (foundIdx == -1) {
                return false;
            }
            patIdxStart = patIdxTmp;
            strIdxStart = foundIdx + patLength;
        }
        for (int i = patIdxStarti <= patIdxEndi++) {
            if (!tokenizedPattern[i].equals()) {
                return false;
            }
        }
        return true;
    }
    private static boolean different(boolean caseSensitivechar chchar other) {
        return caseSensitive ? ch != other : Character.toUpperCase(ch) != Character.toUpperCase(other);
    }

    
Tests whether or not a string matches against a pattern. The pattern may contain two special characters:
'*' means zero or more characters
'?' means one and only one character

Parameters:
pattern The pattern to match against. Must not be null.
str The string which must be matched against the pattern. Must not be null.
caseSensitive Whether or not matching should be performed case sensitively.
Returns:
true if the string matches against the pattern, or false otherwise.
    public static boolean match(String patternString str,
                                boolean caseSensitive) {
        char[] patArr = pattern.toCharArray();
        char[] strArr = str.toCharArray();
        int patIdxStart = 0;
        int patIdxEnd = patArr.length - 1;
        int strIdxStart = 0;
        int strIdxEnd = strArr.length - 1;
        char ch;
        boolean containsStar = false;
        for (int i = 0; i < patArr.lengthi++) {
            if (patArr[i] == '*') {
                containsStar = true;
                break;
            }
        }
        if (!containsStar) {
            // No '*'s, so we make a shortcut
            if (patIdxEnd != strIdxEnd) {
                return false// Pattern and string do not have the same size
            }
            for (int i = 0; i <= patIdxEndi++) {
                ch = patArr[i];
                if (ch != '?') {
                    if (different(caseSensitivechstrArr[i])) {
                        return false// Character mismatch
                    }
                }
            }
            return true// String matches against pattern
        }
        if (patIdxEnd == 0) {
            return true// Pattern contains only '*', which matches anything
        }
        // Process characters before first star
        while (true) {
            ch = patArr[patIdxStart];
            if (ch == '*' || strIdxStart > strIdxEnd) {
                break;
            }
            if (ch != '?') {
                if (different(caseSensitivechstrArr[strIdxStart])) {
                    return false// Character mismatch
                }
            }
            patIdxStart++;
            strIdxStart++;
        }
        if (strIdxStart > strIdxEnd) {
            // All characters in the string are used. Check if only '*'s are
            // left in the pattern. If so, we succeeded. Otherwise failure.
            return allStars(patArrpatIdxStartpatIdxEnd);
        }
        // Process characters after last star
        while (true) {
            ch = patArr[patIdxEnd];
            if (ch == '*' || strIdxStart > strIdxEnd) {
                break;
            }
            if (ch != '?') {
                if (different(caseSensitivechstrArr[strIdxEnd])) {
                    return false// Character mismatch
                }
            }
            patIdxEnd--;
            strIdxEnd--;
        }
        if (strIdxStart > strIdxEnd) {
            // All characters in the string are used. Check if only '*'s are
            // left in the pattern. If so, we succeeded. Otherwise failure.
            return allStars(patArrpatIdxStartpatIdxEnd);
        }
        // process pattern between stars. padIdxStart and patIdxEnd point
        // always to a '*'.
        while (patIdxStart != patIdxEnd && strIdxStart <= strIdxEnd) {
            int patIdxTmp = -1;
            for (int i = patIdxStart + 1; i <= patIdxEndi++) {
                if (patArr[i] == '*') {
                    patIdxTmp = i;
                    break;
                }
            }
            if (patIdxTmp == patIdxStart + 1) {
                // Two stars next to each other, skip the first one.
                patIdxStart++;
                continue;
            }
            // Find the pattern between padIdxStart & padIdxTmp in str between
            // strIdxStart & strIdxEnd
            int patLength = (patIdxTmp - patIdxStart - 1);
            int strLength = (strIdxEnd - strIdxStart + 1);
            int foundIdx = -1;
            strLoop:
            for (int i = 0; i <= strLength - patLengthi++) {
                for (int j = 0; j < patLengthj++) {
                    ch = patArr[patIdxStart + j + 1];
                    if (ch != '?') {
                        if (different(caseSensitivech,
                                strArr[strIdxStart + i + j])) {
                            continue strLoop;
                        }
                    }
                }
                foundIdx = strIdxStart + i;
                break;
            }
            if (foundIdx == -1) {
                return false;
            }
            patIdxStart = patIdxTmp;
            strIdxStart = foundIdx + patLength;
        }
        // All characters in the string are used. Check if only '*'s are left
        // in the pattern. If so, we succeeded. Otherwise failure.
        return allStars(patArrpatIdxStartpatIdxEnd);
    }
    private static boolean allStars(char[] charsint startint end) {
        for (int i = starti <= end; ++i) {
            if (chars[i] != '*') {
                return false;
            }
        }
        return true;
    }


    
Tokenize the the string as a package hierarchy

Parameters:
str
Returns:
    static String[] tokenize(String str) {
        char sep = '.';
        int start = 0;
        int len = str.length();
        int count = 0;
        for (int pos = 0; pos < lenpos++) {
            if (str.charAt(pos) == sep) {
                if (pos != start) {
                    count++;
                }
                start = pos + 1;
            }
        }
        if (len != start) {
            count++;
        }
        String[] l = new String[count];
        count = 0;
        start = 0;
        for (int pos = 0; pos < lenpos++) {
            if (str.charAt(pos) == sep) {
                if (pos != start) {
                    String tok = str.substring(startpos);
                    l[count++] = tok;
                }
                start = pos + 1;
            }
        }
        if (len != start) {
            String tok = str.substring(start);
            l[count/* ++ */] = tok;
        }
        return l;
    }
New to GrepCode? Check out our FAQ X