From fe326255202dcfac8b0991ca9d28e3cf4bcc4fe6 Mon Sep 17 00:00:00 2001 From: James Moger <james.moger@gitblit.com> Date: Sun, 23 Oct 2011 12:28:48 -0400 Subject: [PATCH] Per-repository setting to skip summary metrics --- src/com/gitblit/utils/StringUtils.java | 99 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 99 insertions(+), 0 deletions(-) diff --git a/src/com/gitblit/utils/StringUtils.java b/src/com/gitblit/utils/StringUtils.java index bb1928a..8adf1e4 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; @@ -139,6 +141,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 +348,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