/*
 * @(#)EmailAddressList.java
 *
 * Copyright (c) 2003 JSCAPE
 * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * JSCAPE. ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 * the terms of the license agreement you entered into with JSCAPE.
 */
package com.jscape.inet.email;

import java.util.Enumeration;
import java.util.Vector;


/**
 * Provides methods for parsing a comma delimited list of email addresses.
 */
@SuppressWarnings("unchecked")
public class EmailAddressList {

    private static final char QUOTE = '"';
    private static final char COMMA = ',';
    private static final char POINT_COMMA = ';';
    private static final char AT = '@';
    private static final char OPEN_PAREN = '(';
    private static final char CLOSE_PAREN = ')';
    private Vector emails = new Vector();

    /**
     * Creates a new EmailAddressList instance.
     *
     * @param list a comma delimited list of email addresses
     */
    public EmailAddressList(String list) {
        if (list != null) {
            parse(list);
        }
    }

    /**
     * Gets parsed list of email addresses.
     *
     * @return and Enumeration of EmailAddress
     */
    public Enumeration getEmailAddresses() {
        return emails.elements();
    }

    /**
     * Parses list of email addresses.
     *
     * @param list the list of email addresses each separated by a comma delimiter
     */
    private void parse(String list) {
        char[] data = list.toCharArray();
        boolean openParen = false;
        boolean closeParen = false;
        boolean openQuote = false;
        boolean closeQuote = false;
        boolean at = false;
        StringBuffer email = new StringBuffer();

        for (int i = 0; i < data.length; ++i) {
            char c = data[i];

            switch (c) {
                case AT:
                    at = true;
                    email.append(c);

                    break;

                case OPEN_PAREN:
                    openParen = true;
                    email.append(c);

                    break;

                case CLOSE_PAREN:
                    closeParen = true;
                    email.append(c);

                    break;

                case QUOTE:

                    if (!openQuote) {
                        openQuote = true;
                    } else {
                        closeQuote = true;
                    }

                    email.append(c);

                    break;

                case COMMA:
                case POINT_COMMA:

                    if (at) {
                        if (!openQuote && !openParen) {
                            emails.addElement(new EmailAddress(
                                    email.toString().trim()));
                            openParen = false;
                            closeParen = false;
                            openQuote = false;
                            closeQuote = false;
                            at = false;
                            email = new StringBuffer();
                        } else if (openQuote && !closeQuote) {
                            email.append(c);
                        } else if (openParen && !closeParen) {
                            email.append(c);
                        } else if (openQuote && closeQuote) {
                            emails.addElement(new EmailAddress(
                                    email.toString().trim()));
                            openParen = false;
                            closeParen = false;
                            openQuote = false;
                            closeQuote = false;
                            at = false;
                            email = new StringBuffer();
                        } else if (openParen && closeParen) {
                            emails.addElement(new EmailAddress(
                                    email.toString().trim()));
                            openParen = false;
                            closeParen = false;
                            openQuote = false;
                            closeQuote = false;
                            at = false;
                            email = new StringBuffer();
                        }
                    } else {
                        email.append(c);
                    }

                    break;

                default:
                    email.append(c);

                    break;
            }
        }

        String remainder = email.toString().trim();

        if (remainder.length() > 0) {
            emails.addElement(new EmailAddress(remainder));
        }
    }
}
