KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > AddressBookEntry


1 /*
2  * AddressBookEntry.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: AddressBookEntry.java,v 1.1.1.1 2002/09/24 16:10:02 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j.mail;
23
24 import org.armedbear.j.FastStringBuffer;
25
26 public final class AddressBookEntry
27 {
28     private String JavaDoc personal;
29     private String JavaDoc address;
30
31     public AddressBookEntry(String JavaDoc personal, String JavaDoc address)
32     {
33         this.personal = personal;
34         this.address = address;
35     }
36
37     // Constructs an address book entry from a line from ~/.j/addresses.
38
public static AddressBookEntry parseAddressBookEntry(String JavaDoc s)
39     {
40         int index = s.lastIndexOf('<');
41         if (index >= 0) {
42             String JavaDoc personal = s.substring(0, index).trim();
43             if (personal.length() == 0)
44                 personal = null;
45             String JavaDoc address = s.substring(index);
46             // Strip '<' and '>'.
47
address = address.substring(1, address.length()-1);
48             return new AddressBookEntry(personal, address);
49         } else
50             return new AddressBookEntry(null, s);
51     }
52
53     public String JavaDoc getPersonal()
54     {
55         return personal;
56     }
57
58     public void setPersonal(String JavaDoc s)
59     {
60         personal = s;
61     }
62
63     // Returns canonical form of personal name.
64
public static String JavaDoc canonicalizePersonal(String JavaDoc s)
65     {
66         if (s == null)
67             return null;
68         // Reject the name if it looks like an address.
69
if (s.indexOf('@') >= 0)
70             return null;
71         s = s.trim();
72         // Strip single quotes.
73
if (s.length() >= 2 && s.charAt(0) == '\'' && s.charAt(s.length()-1) == '\'')
74             s = s.substring(1, s.length()-1);
75         if (s.length() == 0)
76             return null;
77         // Strip any bogus text starting with '<'.
78
int index = s.indexOf('<');
79         if (index >= 0)
80             s = s.substring(0, index).trim();
81         if (s.length() == 0)
82             return null;
83         // First name should come first.
84
index = s.indexOf(',');
85         if (index >= 0) {
86             // Last name first.
87
String JavaDoc lastName = s.substring(0, index);
88             String JavaDoc firstName = s.substring(index+1).trim();
89             return firstName + ' ' + lastName;
90         }
91         return s;
92     }
93
94     public static String JavaDoc canonicalizeAddress(String JavaDoc s)
95     {
96         if (s == null)
97             return null;
98         if (s.length() == 0)
99             return null;
100         return s;
101     }
102
103     public String JavaDoc getAddress()
104     {
105         return address;
106     }
107
108     public void setAddress(String JavaDoc s)
109     {
110         address = s;
111     }
112
113     public boolean equals(Object JavaDoc object)
114     {
115         if (this == object)
116             return true;
117         if (object instanceof AddressBookEntry) {
118             AddressBookEntry entry = (AddressBookEntry) object;
119             if (personal != null) {
120                 if (!personal.equals(entry.personal))
121                     return false;
122             } else if (entry.personal != null)
123                 return false;
124             if (address != null) {
125                 if (!address.equals(entry.address))
126                     return false;
127             } else if (entry.address != null)
128                 return false;
129             return true;
130         }
131         return false;
132     }
133
134     public String JavaDoc toString()
135     {
136         FastStringBuffer sb = new FastStringBuffer();
137         if (personal != null) {
138             sb.append(personal);
139             sb.append(' ');
140         }
141         if (address != null) {
142             if (sb.length() > 0) {
143                 sb.append('<');
144                 sb.append(address);
145                 sb.append('>');
146             } else {
147                 // No personal name. Don't use angle brackets.
148
sb.append(address);
149             }
150         }
151         return sb.toString();
152     }
153 }
154
Popular Tags