James Moger
2011-11-11 d65f712ea3d8941f4b9145c0630c30c20af80d13
src/com/gitblit/utils/StringUtils.java
@@ -19,6 +19,8 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.PatternSyntaxException;
@@ -31,6 +33,8 @@
public class StringUtils {
   public static final String MD5_TYPE = "MD5:";
   public static final String COMBINED_MD5_TYPE = "CMD5:";
   /**
    * Returns true if the string is null or empty.
@@ -139,6 +143,10 @@
      StringBuilder sb = new StringBuilder();
      for (String value : values) {
         sb.append(value).append(separator);
      }
      if (sb.length() > 0) {
         // truncate trailing separator
         sb.setLength(sb.length() - separator.length());
      }
      return sb.toString().trim();
   }
@@ -342,4 +350,97 @@
      }
      return strings;
   }
   /**
    * Validates that a name is composed of letters, digits, or limited other
    * characters.
    *
    * @param name
    * @return the first invalid character found or null if string is acceptable
    */
   public static Character findInvalidCharacter(String name) {
      char[] validChars = { '/', '.', '_', '-' };
      for (char c : name.toCharArray()) {
         if (!Character.isLetterOrDigit(c)) {
            boolean ok = false;
            for (char vc : validChars) {
               ok |= c == vc;
            }
            if (!ok) {
               return c;
            }
         }
      }
      return null;
   }
   /**
    * Simple fuzzy string comparison. This is a case-insensitive check. A
    * single wildcard * value is supported.
    *
    * @param value
    * @param pattern
    * @return true if the value matches the pattern
    */
   public static boolean fuzzyMatch(String value, String pattern) {
      if (value.equalsIgnoreCase(pattern)) {
         return true;
      }
      if (pattern.contains("*")) {
         boolean prefixMatches = false;
         boolean suffixMatches = false;
         int wildcard = pattern.indexOf('*');
         String prefix = pattern.substring(0, wildcard).toLowerCase();
         prefixMatches = value.toLowerCase().startsWith(prefix);
         if (pattern.length() > (wildcard + 1)) {
            String suffix = pattern.substring(wildcard + 1).toLowerCase();
            suffixMatches = value.toLowerCase().endsWith(suffix);
            return prefixMatches && suffixMatches;
         }
         return prefixMatches || suffixMatches;
      }
      return false;
   }
   /**
    * Compare two repository names for proper group sorting.
    *
    * @param r1
    * @param r2
    * @return
    */
   public static int compareRepositoryNames(String r1, String r2) {
      // sort root repositories first, alphabetically
      // then sort grouped repositories, alphabetically
      int s1 = r1.indexOf('/');
      int s2 = r2.indexOf('/');
      if (s1 == -1 && s2 == -1) {
         // neither grouped
         return r1.compareTo(r2);
      } else if (s1 > -1 && s2 > -1) {
         // both grouped
         return r1.compareTo(r2);
      } else if (s1 == -1) {
         return -1;
      } else if (s2 == -1) {
         return 1;
      }
      return 0;
   }
   /**
    * Sort grouped repository names.
    *
    * @param list
    */
   public static void sortRepositorynames(List<String> list) {
      Collections.sort(list, new Comparator<String>() {
         @Override
         public int compare(String o1, String o2) {
            return compareRepositoryNames(o1, o2);
         }
      });
   }
}