001: /*
002: * AddressBookEntry.java
003: *
004: * Copyright (C) 2000-2002 Peter Graves
005: * $Id: AddressBookEntry.java,v 1.1.1.1 2002/09/24 16:10:02 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j.mail;
023:
024: import org.armedbear.j.FastStringBuffer;
025:
026: public final class AddressBookEntry {
027: private String personal;
028: private String address;
029:
030: public AddressBookEntry(String personal, String address) {
031: this .personal = personal;
032: this .address = address;
033: }
034:
035: // Constructs an address book entry from a line from ~/.j/addresses.
036: public static AddressBookEntry parseAddressBookEntry(String s) {
037: int index = s.lastIndexOf('<');
038: if (index >= 0) {
039: String personal = s.substring(0, index).trim();
040: if (personal.length() == 0)
041: personal = null;
042: String address = s.substring(index);
043: // Strip '<' and '>'.
044: address = address.substring(1, address.length() - 1);
045: return new AddressBookEntry(personal, address);
046: } else
047: return new AddressBookEntry(null, s);
048: }
049:
050: public String getPersonal() {
051: return personal;
052: }
053:
054: public void setPersonal(String s) {
055: personal = s;
056: }
057:
058: // Returns canonical form of personal name.
059: public static String canonicalizePersonal(String s) {
060: if (s == null)
061: return null;
062: // Reject the name if it looks like an address.
063: if (s.indexOf('@') >= 0)
064: return null;
065: s = s.trim();
066: // Strip single quotes.
067: if (s.length() >= 2 && s.charAt(0) == '\''
068: && s.charAt(s.length() - 1) == '\'')
069: s = s.substring(1, s.length() - 1);
070: if (s.length() == 0)
071: return null;
072: // Strip any bogus text starting with '<'.
073: int index = s.indexOf('<');
074: if (index >= 0)
075: s = s.substring(0, index).trim();
076: if (s.length() == 0)
077: return null;
078: // First name should come first.
079: index = s.indexOf(',');
080: if (index >= 0) {
081: // Last name first.
082: String lastName = s.substring(0, index);
083: String firstName = s.substring(index + 1).trim();
084: return firstName + ' ' + lastName;
085: }
086: return s;
087: }
088:
089: public static String canonicalizeAddress(String s) {
090: if (s == null)
091: return null;
092: if (s.length() == 0)
093: return null;
094: return s;
095: }
096:
097: public String getAddress() {
098: return address;
099: }
100:
101: public void setAddress(String s) {
102: address = s;
103: }
104:
105: public boolean equals(Object object) {
106: if (this == object)
107: return true;
108: if (object instanceof AddressBookEntry) {
109: AddressBookEntry entry = (AddressBookEntry) object;
110: if (personal != null) {
111: if (!personal.equals(entry.personal))
112: return false;
113: } else if (entry.personal != null)
114: return false;
115: if (address != null) {
116: if (!address.equals(entry.address))
117: return false;
118: } else if (entry.address != null)
119: return false;
120: return true;
121: }
122: return false;
123: }
124:
125: public String toString() {
126: FastStringBuffer sb = new FastStringBuffer();
127: if (personal != null) {
128: sb.append(personal);
129: sb.append(' ');
130: }
131: if (address != null) {
132: if (sb.length() > 0) {
133: sb.append('<');
134: sb.append(address);
135: sb.append('>');
136: } else {
137: // No personal name. Don't use angle brackets.
138: sb.append(address);
139: }
140: }
141: return sb.toString();
142: }
143: }
|