package soph.collab.util;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.regex.Pattern;

public final class StringUtils {

    public static final String ENCODING = "UTF-8";
    public static final String EMPTY = "";
    public static final String SPACE = " ";
    public static final String COMMA = ",";
    public static final Pattern SPACE_PATTERN = Pattern.compile(SPACE);
    public static final Pattern COMMA_PATTERN = Pattern.compile(COMMA);
    public static final Pattern EOL_PATTERN = Pattern.compile("\n");

    /**
     * Escape the given as HTML.
     * Newlines are converted to <code>&lt;br/&gt;</code>.
     * Ampersands are converted to <code>&amp;amp;</code>.
     * Double quotes are converted to <code>&amp;quot;</code>.
     * Less than signs are converted to <code>&amp;lt;</code>.
     * Greater than signs are converted to <code>&amp;gt;</code>.
     *
     * @param s text to escape
     * @return empty string if given string is null, otherwise the converted
     *         version if the given string.
     */
    public static String htmlEncode(String s) {
        if (s == null)
            return "";

        StringBuilder sb = new StringBuilder();
        char c;
        for (int i = 0; i < s.length(); i++) {
            c = s.charAt(i);
            switch (c) {
                case '"':
                    sb.append("\"");
                    break;
                case '&':
                    sb.append("&");
                    break;
                case '<':
                    sb.append("<");
                    break;
                case '>':
                    sb.append(">");
                    break;
                case '\n':
                    sb.append("<br/>");
                    break;
                default:
                    sb.append(c);
                    break;
            }
        }
        return sb.toString();
    }

    public static String urlEncode(String s) {
        try {
            return URLEncoder.encode(s, ENCODING);
        } catch (Exception e) {
            return s;
        }
    }

    public static String urlDecode(String url) {
        try {
            return URLDecoder.decode(url, ENCODING);
        } catch (Exception e) {
            return url;
        }
    }

    public static boolean hasText(String s) {
        return org.springframework.util.StringUtils.hasText(s);
    }

    public static String join(Collection<String> collection) {
        return join(collection, COMMA);
    }
    public static String join(Collection<String> collection, String delimiter) {
        return org.springframework.util.StringUtils.collectionToDelimitedString(collection, delimiter);
    }
}

@@ -72,24 +72,49 @@
 URLEncoder;%0A
+import java.util.Arrays;%0A
 import java.
@@ -153,23 +153,136 @@
 l.regex.
-Pattern
+Matcher;%0Aimport java.util.regex.Pattern;%0Aimport java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.List
 ;%0A%0Apubli
@@ -706,16 +706,419 @@
 %22%5Cn%22);%0A%0A
+    public static final String VALID_NAME_CHAR = %22%5B-a-zA-Z ,%5D%22;%0A    public static final String INVALID_NAME_CHAR = %22%5B%5E%22 + VALID_NAME_CHAR.substring(1);%0A    public static final Pattern INVALID_NAME_CHAR_PATTERN = Pattern.compile(StringUtils.INVALID_NAME_CHAR);%0A    public static final Pattern LAST_NAME_FIRST_PATTERN%0A      = Pattern.compile(%22%5E(%22 + VALID_NAME_CHAR + %22*), ?(%22 + VALID_NAME_CHAR + %22*)$%22);%0A%0A
     /**%0A
@@ -3273,11 +3273,1507 @@
 );%0A    %7D
+%0A%0A    public static List%3CString%3E spaceDelimitedList(String s) %7B%0A        return delimitedList(s, SPACE);%0A    %7D%0A%0A    public static List%3CString%3E delimitedList(String s, String delimiter) %7B%0A        return Arrays.asList(org.springframework.util.StringUtils.delimitedListToStringArray(s, delimiter));%0A    %7D%0A%0A    /**%0A     * Remove leading and trailing whitespace, then replace an internal%0A     * sequences of white space with a single space character.%0A     */%0A    public static String normalizeWhitespace(String s) %7B%0A        return org.apache.commons.lang.StringUtils.normalizeSpace(s);%0A    %7D%0A%0A    public static List%3CString%3E parseName(String name) %7B%0A        if (name == null %7C%7C !hasText(name))%0A            return Collections.emptyList();%0A%0A        // remove leading and trailing whitespace and condense internal spaces%0A        name = normalizeWhitespace(name);%0A%0A        List%3CString%3E nameParts = new ArrayList%3C%3E();%0A        String last = %22%22;%0A        String rest = name;%0A        // Check if name fits 'last name, first name' pattern%0A        Matcher m = LAST_NAME_FIRST_PATTERN.matcher(name);%0A        if (m.matches()) %7B%0A            last = m.group(1).trim();%0A            rest = m.group(2).trim();%0A        %7D%0A%0A        for (String part : StringUtils.SPACE_PATTERN.split(rest)) %7B%0A            if (part == null %7C%7C !StringUtils.hasText(part))%0A                continue;%0A            nameParts.add(part);%0A        %7D%0A        if (StringUtils.hasText(last))%0A            nameParts.add(last);%0A%0A        return nameParts;%0A    %7D
 %0A%7D%0A

Add some convenience methods to StringUtils utility class.