001    package org.hackystat.sensorbase.uripattern;
002    
003    
004    /**
005     * A utility class which does pattern matches.
006     *
007     * @author Qin Zhang
008     */
009    public class PatternMatcher {
010    
011      /**
012       * Determines whether a file name matches a pattern. The pattern rule follows ant file set
013       * selections rules that is used in ant. <p>
014       * Some systems use slash, while others use back-slash as path separator. This function will
015       * not distinguish them (effectively,they are treated as the same character). <p>
016       * If a pattern ends with a path separator, then '**' will be append to it before matching.
017       * (i.e. if pattern is 'src/', then it will be treated as 'src/**'.
018       *
019       * @param pattern The pattern. If null, then the method will always return true.
020       * @param fileName The file name to be tested against the pattern.
021       * @param isCaseSensitive Whether fileName is case sensitive.
022       *
023       * @return True if the file name matches the pattern.
024       */
025      static boolean matchesFilePath(String pattern, String fileName, boolean isCaseSensitive) {
026        //PatternMatcher does not work well with file name starts with "/"
027        //e.g. PatternMatcherImpl.matchPath("**", "/d/d/file", ture) returns false.
028        //everything's ok if path does not start with a slash.
029        //Fix: get rid of the starting "/" in fileName, if pattern starts with **
030        if (pattern == null) {
031          return true;
032        }
033        else {
034          
035    //      pattern = pattern.replace('/', File.separatorChar).replace('\\', File.separatorChar);
036    //      fileName = fileName.replace('/', File.separatorChar).replace('\\', File.separatorChar);
037    //      if (pattern.endsWith(File.separator)) {
038    //        pattern += "**";
039    //      }
040    //      //our fix.
041    //      if (pattern.startsWith("**") && fileName.length() > 0 
042    //          && fileName.charAt(0) == File.separatorChar) {
043    //        fileName = fileName.substring(1);
044    //      }
045          return PatternMatcherImpl.matchPath(pattern, fileName, isCaseSensitive);
046        }
047      }
048    
049      /**
050       * Tests whether a string matches a pattern or not. Wild characters are:
051       * <ul>
052       *   <li>'*': zero or more characters. </li>
053       *   <li>'?': one and only one character.</li>
054       * </ul>
055       * <p>
056       * Note that this method is NOT designed to match file path, use 
057       * <code>matchesFilePath</code> instead.
058       * 
059       * @param pattern The pattern. If null, the function always returns true.
060       * @param str The string to be matched against the pattern.
061       * @param caseSensitive Whether the matches is case sensitive or not.
062       *
063       * @return True if string matches the pattern.
064       */
065      public static boolean matchesPattern(String pattern, String str, boolean caseSensitive) {
066        if (pattern == null) {
067          return true;
068        }
069        else {
070          return PatternMatcherImpl.match(pattern, str, caseSensitive);
071        }
072      }
073    }