* @throws NullPointerException
* @throws UnsupportedEncodingException
*/
public static InternetAddress parseAddress(String sNamePlusEMail)
throws AddressException,NullPointerException,UnsupportedEncodingException {
InternetAddress oRetAdr = null;
String sAddr = sNamePlusEMail.trim();
int iLeftAng = sAddr.indexOf('<');
int iRightAng= sAddr.indexOf('>');
int iLeftPar = sAddr.indexOf('(');
int iRightPar= sAddr.indexOf(')');
int iLeftQuo = sAddr.indexOf('"');
int iRightQuo;
if (iLeftQuo>=0) iRightQuo = sAddr.indexOf('"',iLeftQuo+1); else iRightQuo = -1;
if (iRightAng<iLeftAng) throw new AddressException("Misplaced right angle");
if (iLeftAng<0 && iRightAng>=0) throw new AddressException("Missing left angle");
if (iLeftAng>=0 && iRightAng<0) throw new AddressException("Missing right angle");
if (iLeftPar<0 && iRightPar>=0) throw new AddressException("Missing left parenthesis");
if (iLeftPar>=0 && iRightPar<0) throw new AddressException("Missing right parenthesis");
if (iRightPar<iLeftPar) throw new AddressException("Misplaced right parenthesis");
if (iLeftQuo>=0 && iRightQuo<0) throw new AddressException("Unclosed quote");
if (iLeftAng>=0 && iRightAng>=0 && iLeftPar>=0 && iRightPar>=0) {
// Address is (Name) <user@domain.com> or <user@domain.com> (Name)
oRetAdr = new InternetAddress(sAddr.substring(iLeftAng+1,iRightAng),sAddr.substring(iLeftPar+1,iRightPar));
} else if (iLeftAng>=0 && iRightAng>=0 && iLeftQuo>=0 && iRightQuo>=0) {
// Address is "Name" <user@domain.com> or "Name" <user@domain.com>
oRetAdr = new InternetAddress(sAddr.substring(iLeftAng+1,iRightAng),sAddr.substring(iLeftQuo+1,iRightQuo));
} else if (iLeftAng>=0 && iRightAng>=0) {
// Address is Name <user@domain.com> or <user@domain.com> Name
if (0==iLeftAng)
oRetAdr = new InternetAddress(sAddr.substring(1,iRightAng),sAddr.substring(iRightAng+1));
else
oRetAdr = new InternetAddress(sAddr.substring(iLeftAng+1,iRightAng),sAddr.substring(0,iLeftAng));
} else {
oRetAdr = new InternetAddress(sAddr);
}
return oRetAdr;
}