KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > vcard > Vcard


1 package net.suberic.pooka.vcard;
2 import java.io.*;
3 import java.util.Properties JavaDoc;
4 import javax.mail.internet.InternetAddress JavaDoc;
5
6 /**
7  * A class which represents a vcard address book entry.
8  */

9 public class Vcard implements Comparable JavaDoc, net.suberic.pooka.AddressBookEntry {
10
11   public static final int SORT_BY_ADDRESS = 0;
12   
13   public static final int SORT_BY_LAST_NAME = 1;
14   
15   public static final int SORT_BY_FIRST_NAME = 2;
16   
17   public static final int SORT_BY_PERSONAL_NAME = 3;
18
19   public static final int SORT_BY_ID = 4;
20   
21   Properties JavaDoc properties;
22   
23   private int sortingMethod = SORT_BY_ID;
24   
25   private InternetAddress JavaDoc[] addresses;
26
27   /**
28    * Creates a new Vcard from the given properties.
29    */

30   public Vcard(Properties JavaDoc newProps) {
31     properties = newProps;
32   }
33   
34   /**
35    * Gets a property on the Vcard.
36    */

37   public String JavaDoc getProperty(String JavaDoc propertyName) {
38     return properties.getProperty(propertyName);
39   }
40   
41   /**
42    * Sets a property on the Vcard.
43    */

44   public void setProperty(String JavaDoc propertyName, String JavaDoc newValue) {
45     properties.setProperty(propertyName, newValue);
46     
47     // if pretty much anything changes on here, then we should probably
48
// null out the address objects.
49
addresses = null;
50   }
51   
52   /**
53    * Gets a Properties representation of the values in the AddressBookEntry.
54    */

55   public java.util.Properties JavaDoc getProperties() {
56     Properties JavaDoc returnValue = new Properties JavaDoc();
57     // we need five settings: "personalName", "firstName", "lastName",
58
// "address", and "id".
59

60     returnValue.setProperty("currentAddress.personalName", getPersonalName());
61     returnValue.setProperty("currentAddress.firstName", getFirstName());
62     returnValue.setProperty("currentAddress.lastName", getLastName());
63     returnValue.setProperty("currentAddress.address", getAddressString());
64     returnValue.setProperty("currentAddress.id", getID());
65     return returnValue;
66   }
67
68   /**
69    * Gets the InternetAddress associated with this Vcard.
70    */

71   public InternetAddress JavaDoc[] getAddresses() {
72     try {
73       if (addresses == null) {
74     addresses = InternetAddress.parse(properties.getProperty("email;internet"), false);
75       }
76       return addresses;
77     } catch (javax.mail.internet.AddressException JavaDoc ae) {
78       ae.printStackTrace();
79       return null;
80     }
81   }
82
83   /**
84    * Gets the String that's a proper representation of the address(es)
85    * in this AddressBookEntry.
86    */

87   public String JavaDoc getAddressString() {
88     String JavaDoc returnValue = properties.getProperty("email;internet");
89     if (returnValue != null)
90       return returnValue;
91     else
92       return "";
93   }
94   
95   /**
96    * Sets the InternetAddress associated with this Vcard.
97    */

98   public void setAddress(InternetAddress JavaDoc newAddress) {
99     setAddresses(new InternetAddress JavaDoc[] { newAddress });
100   }
101
102   /**
103    * Sets the InternetAddress associated with this Vcard.
104    */

105   public void setAddresses(InternetAddress JavaDoc newAddresses[]) {
106     if (newAddresses != null) {
107       addresses = newAddresses;
108       properties.setProperty("email;internet", InternetAddress.toString(newAddresses));
109     }
110   }
111
112   /**
113    * Gets the PersonalName property associated with this Vcard.
114    */

115   public String JavaDoc getPersonalName() {
116     String JavaDoc returnValue = properties.getProperty("fn");
117     if (returnValue != null)
118       return returnValue;
119     else
120       return "";
121   }
122
123   /**
124    * Gets the PersonalName property associated with this Vcard.
125    */

126   public void setPersonalName(String JavaDoc newName) {
127     properties.setProperty("fn", newName);
128   }
129
130   /**
131    * Gets the FirstName property associated with this Vcard.
132    */

133   public String JavaDoc getFirstName() {
134     String JavaDoc name = properties.getProperty("n");
135     if (name != null) {
136       int index = name.indexOf(";");
137       if (index >= 0)
138     return name.substring(index + 1);
139     }
140
141     return "";
142   }
143
144   /**
145    * Gets the FirstName property associated with this Vcard.
146    */

147   public void setFirstName(String JavaDoc newName) {
148     String JavaDoc oldName = properties.getProperty("n");
149     if (oldName != null) {
150       int index = oldName.indexOf(";");
151       if (index > 0)
152     properties.setProperty("n", oldName.substring(0, index) + ";" + newName);
153       else if (index == 0) {
154     properties.setProperty("n", ";" + newName);
155
156       }
157     } else {
158       properties.setProperty("n", ";" + newName);
159     }
160   }
161
162   /**
163    * Gets the LastName property associated with this Vcard.
164    */

165   public String JavaDoc getLastName() {
166     String JavaDoc name = properties.getProperty("n");
167     if (name != null) {
168       int index = name.indexOf(";");
169       if (index >= 0)
170     return name.substring(0, index);
171     }
172
173     return "";
174   }
175
176   /**
177    * Gets the LastName property associated with this Vcard.
178    */

179   public void setLastName(String JavaDoc newName) {
180     String JavaDoc name = properties.getProperty("n");
181     if (name != null) {
182       int index = name.indexOf(";");
183       if (index >= 0)
184     properties.setProperty("n", newName + ";" + ((index + 1 < name.length()) ? name.substring(index + 1) : ""));
185     } else {
186       properties.setProperty("n", newName + ";");
187     }
188
189   }
190   
191   /**
192    * Gets the user information, last name first.
193    */

194   public String JavaDoc getLastFirst() {
195     return getLastName() + ", " + getFirstName();
196   }
197   
198   /**
199    * Gets the user information, first name first.
200    */

201   public String JavaDoc getFirstLast() {
202     return getFirstName() + " " + getLastName();
203   }
204     
205   /**
206    * Gets the email address (as a string) associated with this Vcard.
207    */

208   public String JavaDoc getEmailAddress() {
209     return getAddressString();
210   }
211
212   /**
213    * <p>Gets the ID of this AddressBookEntry. This is the ID that will
214    * be searched by default, that can be entered into the To: field, etc.</p>
215    */

216   public String JavaDoc getID() {
217     return getPersonalName();
218   }
219
220   /**
221    * <p>Sets the ID of this AddressBookEntry. This is the ID that will
222    * be searched by default, that can be entered into the To: field, etc.</p>
223    */

224   public void setID(String JavaDoc newID) {
225     setPersonalName(newID);
226   }
227
228   //---- Comparable ----//
229

230   /**
231    * Compares this Vcard either to another Vcard, or a String which matches
232    * the Vcard. This checks the sortingMethod setting to decide how to
233    * compare to the Vcard or String.
234    *
235    * @param o the Object to be compared.
236    * @return a negative integer, zero, or a positive integer as this object
237    * is less than, equal to, or greater than the specified object.
238    *
239    * @throws ClassCastException if the specified object's type prevents it
240    * from being compared to this Object.
241    */

242   
243   public int compareTo(Object JavaDoc o) {
244     if (o instanceof Vcard) {
245       Vcard target = (Vcard) o;
246       
247       switch (sortingMethod) {
248       case (SORT_BY_ADDRESS):
249     int returnValue = getAddressString().compareTo(target.getAddressString());
250     return returnValue;
251       case (SORT_BY_LAST_NAME):
252     return getLastFirst().compareTo(target.getLastFirst());
253       case (SORT_BY_FIRST_NAME):
254     return getFirstLast().compareTo(target.getFirstLast());
255       case (SORT_BY_PERSONAL_NAME):
256     return getPersonalName().compareTo(target.getPersonalName());
257       default:
258     return getID().compareTo(target.getID());
259       }
260     } else if (o instanceof String JavaDoc) {
261       String JavaDoc compareString = null;
262       String JavaDoc matchString = (String JavaDoc) o;
263       
264       switch (sortingMethod) {
265       case (SORT_BY_ADDRESS):
266     compareString = getAddressString();
267     break;
268       case (SORT_BY_LAST_NAME):
269     compareString = getLastFirst();
270     break;
271       case (SORT_BY_FIRST_NAME):
272     compareString = getFirstLast();
273     break;
274       default:
275     compareString = getID();
276       }
277       
278       // see if the string to be matched is shorter; if so, match
279
// with just that length.
280

281
282       int origSize = compareString.length();
283       int matchSize = matchString.length();
284       if (matchSize < origSize) {
285     int returnValue = compareString.substring(0,matchSize).compareTo(matchString);
286     return returnValue;
287       } else {
288     int returnValue = compareString.compareTo(matchString);
289     return returnValue;
290       }
291     }
292     
293     return -1;
294   }
295   
296   //---- parser ----//
297

298   /**
299    * Parses a vcard from a BufferedReader.
300    */

301   public static Vcard parse(BufferedReader reader) throws java.text.ParseException JavaDoc {
302     
303     try {
304       Properties JavaDoc newProps = new Properties JavaDoc();
305       
306       boolean isDone = false;
307       
308       String JavaDoc line = getNextLine(reader);
309       if (line != null) {
310     String JavaDoc[] current = parseLine(line);
311     if (current[0] != null && current[1] != null) {
312       if (! (current[0].equalsIgnoreCase("begin") && current[1].equalsIgnoreCase("vcard")))
313         throw new java.text.ParseException JavaDoc("No beginning", 0);
314     }
315     else {
316       newProps.put(current[0], current[1]);
317     }
318       } else {
319     return null;
320       }
321       
322       while (!isDone) {
323     line = getNextLine(reader);
324     if (line != null) {
325       String JavaDoc[] current = parseLine(line);
326       if (current[0] != null && current[1] != null) {
327         if (current[0].equalsIgnoreCase("end")) {
328           if (current[1].equalsIgnoreCase("vcard"))
329         isDone = true;
330           else
331         throw new java.text.ParseException JavaDoc("incorrect end tag", 0);
332         } else {
333           newProps.put(current[0], current[1]);
334         }
335       }
336     } else {
337       isDone = true;
338     }
339       }
340       return new Vcard(newProps);
341     } catch (IOException ioe) {
342       throw new java.text.ParseException JavaDoc(ioe.getMessage(), 0);
343     }
344   }
345   
346   /**
347    * Parses a name/value pair from an rfc2425 stream.
348    */

349   private static String JavaDoc getNextLine(BufferedReader reader) throws IOException {
350     String JavaDoc firstLine = reader.readLine();
351     boolean isDone = false;
352     if (firstLine != null) {
353       while (! isDone) {
354     reader.mark(256);
355     String JavaDoc nextLine = reader.readLine();
356     if (nextLine != null && nextLine.length() > 0) {
357       if (! Character.isWhitespace(nextLine.charAt(0))) {
358         isDone = true;
359         reader.reset();
360       } else {
361         firstLine = firstLine + nextLine.substring(1);
362       }
363     } else {
364       isDone = true;
365     }
366       }
367     }
368     return firstLine;
369   }
370   
371   private static String JavaDoc[] parseLine(String JavaDoc firstLine) {
372     String JavaDoc[] returnValue = new String JavaDoc[2];
373     
374     int dividerLoc = firstLine.indexOf(':');
375     returnValue[0] = firstLine.substring(0, dividerLoc);
376     returnValue[1] = firstLine.substring(dividerLoc + 1);
377     
378     return returnValue;
379   }
380
381   /**
382    * Writes this Vcard out to the given BufferedWriter.
383    */

384   public void write(BufferedWriter out) throws java.io.IOException JavaDoc {
385     out.write("begin:vcard");
386     out.newLine();
387     java.util.Enumeration JavaDoc propNames = properties.propertyNames();
388     while (propNames.hasMoreElements()) {
389       String JavaDoc currentName = (String JavaDoc) propNames.nextElement();
390       out.write(currentName);
391       out.write(":");
392       out.write(properties.getProperty(currentName));
393       out.newLine();
394     }
395     out.write("end:vcard");
396     out.newLine();
397   }
398 }
399
Popular Tags