FileDocCategorySizeDatePackage
EmailFieldParser.javaAPI DocphoneME MR2 API (J2ME)5575Wed May 02 18:00:42 BST 2007gov.nist.javax.sdp.parser

EmailFieldParser

public class EmailFieldParser extends SDPParser
Parser for Email Field.
version
JAIN-SIP-1.1 This code is in the public domain.

Fields Summary
Constructors Summary
public EmailFieldParser(String emailField)
Creates new EmailFieldParser.

param
emailField the email field to be parsed

        lexer = new Lexer("charLexer", emailField);
    
protected EmailFieldParser()
Default constructor.

        super();
    
Methods Summary
public EmailFieldemailField()
Perform the email address field parsing

return
the parsed email address field
exception
ParseException if a parsing error occurs

        lexer.match('e");
        lexer.SPorHT();
        lexer.match('=");
        lexer.SPorHT();

        EmailField emailField = new EmailField();
        EmailAddress emailAddress = new EmailAddress();

        String rest = lexer.getRest();

        String displayName = getDisplayName(rest.trim());
        emailAddress.setDisplayName(displayName);
        Email email = getEmail(rest);
        emailAddress.setEmail(email);

        emailField.setEmailAddress(emailAddress);
        return emailField;
    
public java.lang.StringgetDisplayName(java.lang.String rest)
Gets the user friendly display name.

param
rest the remaining string to be parsed
return
the display name

        String retval = null;

        int begin = rest.indexOf("(");
        int end = rest.indexOf(")");

        if (begin != -1 && end != -1 && end > begin) {
            // e=mjh@isi.edu (Mark Handley)
            retval = rest.substring(begin+1, end);
        } else {
            // The alternative RFC822 name quoting convention 
            // is also allowed for
            // email addresses. ex: e=Mark Handley <mjh@isi.edu>
            begin = rest.indexOf("<");
            if (begin != -1) {
                retval = rest.substring(0, begin);
            } else {
                // There is no display name !!!
            }
        }
        return retval;
    
public EmailgetEmail(java.lang.String rest)
Gets the email address.

param
rest the remaining string to be parsed
return
the email address

        Email email = new Email();

        int begin = rest.indexOf("(");
        try {
            if (begin != -1) {
                // e=mjh@isi.edu (Mark Handley)
                String emailTemp = rest.substring(0, begin);
                int i = emailTemp.indexOf("@");
                if (i != -1) {
                    email.setUserName(emailTemp.substring(0, i));
                    email.setHostName(emailTemp.substring(i+1));
                } else {
                    // Pb: the email is not well formatted
                }
            } else {
                // The alternative RFC822 name quoting convention is 
                // also allowed for
                // email addresses. ex: e=Mark Handley <mjh@isi.edu>
                int ind = rest.indexOf("<");
                int end = rest.indexOf(">");

                if (ind != -1) {
                    String emailTemp = rest.substring(ind+1, end);
                    int i = emailTemp.indexOf("@");
                    if (i != -1) {
                        email.setUserName(emailTemp.substring(0, i));
                        email.setHostName(emailTemp.substring(i+1));
                    } else {
                        // Pb: the email is not well formatted
                    }

                } else {
                    int i = rest.indexOf("@");
                    int j = rest.indexOf("\n");
                    if (i != -1) {
                        email.setUserName(rest.substring(0, i));
                        email.setHostName(rest.substring(i+1, j));
                    } else {
                        // Pb: the email is not well formatted
                    }
                }
            }
            return email;
        } catch (IndexOutOfBoundsException iobe) {
            return new Email();
        }
    
public SDPFieldparse()
Perform the email address field parsing

return
the parsed email address field
exception
ParseException if a parsing error occurs

        return this.emailField();