From 483c122ecab4402720e9f9d3b18ddb3c87a2b34a Mon Sep 17 00:00:00 2001 From: adam Date: Tue, 17 Jan 2006 03:42:34 +0000 Subject: [PATCH] better Address parsing darcs-hash:20060117034234-5007d-55fa277a4329f91f0586359d33d6f9ddb431b8c1.gz --- src/org/ibex/mail/Address.java | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/org/ibex/mail/Address.java b/src/org/ibex/mail/Address.java index 1bf6b04..b54dcfa 100644 --- a/src/org/ibex/mail/Address.java +++ b/src/org/ibex/mail/Address.java @@ -19,24 +19,36 @@ public class Address extends JSReflection implements Serializable { public static Address parse(String s) { try { return s==null?null:new Address(s); } catch (Malformed _) { return null; } } public Address(String user, String host, String description) {this.user=user;this.host=host;this.description=description;} public Address(String s0) throws Address.Malformed { - String s = s0.trim(); - if (s.indexOf('<') == -1) { - if (s.indexOf(' ') == -1) { - description = ""; + boolean good = false; + try { + String s = s0.trim(); + if (s.indexOf('<') == -1) { + if (s.indexOf(' ') == -1) { + description = ""; + } else { + description = s.substring(s.indexOf(' ')).trim(); + s = s.substring(0, s.indexOf(' ')); + } } else { - description = s.substring(s.indexOf(' ')).trim(); - s = s.substring(0, s.indexOf(' ')); + if (s.indexOf('>') == -1) { throw new Malformed("found open-angle-bracket (<) but not close-angle-bracket (>)"); } + description = s.substring(0, s.indexOf('<')) + s.substring(s.indexOf('>') + 1); + s = s.substring(s.indexOf('<') + 1, s.indexOf('>')); } - } else { - if (s.indexOf('>') == -1) { throw new Malformed("found open-angle-bracket (<) but not close-angle-bracket (>)"); } - description = s.substring(0, s.indexOf('<')) + s.substring(s.indexOf('>') + 1); - s = s.substring(s.indexOf('<') + 1, s.indexOf('>')); + if (s.trim().length() == 0) { + user = null; + host = null; + good = true; + } else if (s.indexOf('@') == -1) { throw new Malformed("no @-sign in email address \""+s0+"\""); + } else { + user = s.substring(0, s.indexOf('@')); + host = s.substring(s.indexOf('@')+1); + good = true; + } + } finally { + if (!good) Log.error(Address.class, "problem parsing: \""+s0+"\""); } - if (s.indexOf('@') == -1) { throw new Malformed("no @-sign in email address \""+s0+"\""); } - user = s.substring(0, s.indexOf('@')); - host = s.substring(s.indexOf('@')+1); } - public String toString() { return user + "@" + host; } + public String toString() { return (user == null && host == null) ? "" : (user + "@" + host); } public String coerceToString() { return toString(); } public String toString(boolean desc) { return desc && description != null && description.length() > 0 ? (description+" <" + toString() + ">") : toString(); } -- 1.7.10.4