From 831469ba89ea8bca3bfbd1d662dbdd2c9f233798 Mon Sep 17 00:00:00 2001 From: James Moger Date: Mon, 12 Sep 2011 15:37:55 -0400 Subject: Largely completed, uber-cool federation feature. --- src/com/gitblit/utils/StringUtils.java | 53 ++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'src/com/gitblit/utils/StringUtils.java') diff --git a/src/com/gitblit/utils/StringUtils.java b/src/com/gitblit/utils/StringUtils.java index bb1928a2..77d3cbbc 100644 --- a/src/com/gitblit/utils/StringUtils.java +++ b/src/com/gitblit/utils/StringUtils.java @@ -342,4 +342,57 @@ public class StringUtils { } 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; + } } -- cgit v1.2.3