package org.lantern;

import java.util.Locale;

import org.apache.commons.lang.StringUtils;

public class EmailAddressUtils {

    public static class NormalizationException extends Exception {
        public NormalizationException(String s, Throwable t) {
            super(s, t);
        }
    }

    /**
     * Given an email (or the userid part of a jabberid), returns a normalized version with
     * the following transformations applied:
     *
     *     - lowercase
     *     - '.' characters removed from the username part
     *     - plus-extensions removed from the username
     *     - domains of 'googlemail.com' replaced with 'gmail.com'. other
     *       domains left as-is.
     *
     * If a string that is not of the expected form is passed in, no guarantee is
     * made about the result.
     *
     * @param input The email address to normalize.
     * @return The normalized email address.
     */
    public static String normalizedEmail(String input) throws NormalizationException {
        try {
            input = input.toLowerCase(Locale.ENGLISH);
            int idx = input.indexOf("@");
            String beforeAt = input.substring(0, idx);
            String rest = input.substring(idx);
            beforeAt = beforeAt.replaceAll("\\.", "");
            beforeAt = StringUtils.substringBefore(beforeAt, "+");
            rest = rest.replaceAll("googlemail.com$", "gmail.com");
            return beforeAt + rest;
        } catch (Exception e) {
            throw new EmailAddressUtils.NormalizationException(
                    "Couldn't normalize email: ", e);
        }
    }
}

@@ -120,16 +120,411 @@
 tils %7B%0A%0A
+    // Extending RuntimeException so we don't have to catch this explicitly.%0A    // It's not clear what could trigger this, if anything, nor what to do%0A    // about it most of the time.  We were rethrowing this as%0A    // a RuntimeException every time anyway, a real PITA.  If you know of some%0A    // other sensible way to handle these in a particular situation, you can%0A    // still catch this.%0A
     publ
@@ -570,16 +570,23 @@
 extends 
+Runtime
 Exceptio

Make NormalizationException a RuntimeException.