From 5c1ae2385f9e6c0c2050e5b0cb505d25bdbe27e0 Mon Sep 17 00:00:00 2001 From: James Moger <james.moger@gitblit.com> Date: Sun, 02 Oct 2011 17:21:20 -0400 Subject: [PATCH] Delete the test user account as part of cleanup. --- src/com/gitblit/utils/StringUtils.java | 95 +++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 95 insertions(+), 0 deletions(-) diff --git a/src/com/gitblit/utils/StringUtils.java b/src/com/gitblit/utils/StringUtils.java index bb1928a..f4e9256 100644 --- a/src/com/gitblit/utils/StringUtils.java +++ b/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; @@ -342,4 +344,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); + } + }); + } } -- Gitblit v1.9.1