1 package net.suberic.pooka.vcard; 2 import java.io.*; 3 import java.util.Properties ; 4 import javax.mail.internet.InternetAddress ; 5 6 9 public class Vcard implements Comparable , 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 properties; 22 23 private int sortingMethod = SORT_BY_ID; 24 25 private InternetAddress [] addresses; 26 27 30 public Vcard(Properties newProps) { 31 properties = newProps; 32 } 33 34 37 public String getProperty(String propertyName) { 38 return properties.getProperty(propertyName); 39 } 40 41 44 public void setProperty(String propertyName, String newValue) { 45 properties.setProperty(propertyName, newValue); 46 47 addresses = null; 50 } 51 52 55 public java.util.Properties getProperties() { 56 Properties returnValue = new Properties (); 57 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 71 public InternetAddress [] 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 ae) { 78 ae.printStackTrace(); 79 return null; 80 } 81 } 82 83 87 public String getAddressString() { 88 String returnValue = properties.getProperty("email;internet"); 89 if (returnValue != null) 90 return returnValue; 91 else 92 return ""; 93 } 94 95 98 public void setAddress(InternetAddress newAddress) { 99 setAddresses(new InternetAddress [] { newAddress }); 100 } 101 102 105 public void setAddresses(InternetAddress newAddresses[]) { 106 if (newAddresses != null) { 107 addresses = newAddresses; 108 properties.setProperty("email;internet", InternetAddress.toString(newAddresses)); 109 } 110 } 111 112 115 public String getPersonalName() { 116 String returnValue = properties.getProperty("fn"); 117 if (returnValue != null) 118 return returnValue; 119 else 120 return ""; 121 } 122 123 126 public void setPersonalName(String newName) { 127 properties.setProperty("fn", newName); 128 } 129 130 133 public String getFirstName() { 134 String 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 147 public void setFirstName(String newName) { 148 String 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 165 public String getLastName() { 166 String 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 179 public void setLastName(String newName) { 180 String 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 194 public String getLastFirst() { 195 return getLastName() + ", " + getFirstName(); 196 } 197 198 201 public String getFirstLast() { 202 return getFirstName() + " " + getLastName(); 203 } 204 205 208 public String getEmailAddress() { 209 return getAddressString(); 210 } 211 212 216 public String getID() { 217 return getPersonalName(); 218 } 219 220 224 public void setID(String newID) { 225 setPersonalName(newID); 226 } 227 228 230 242 243 public int compareTo(Object 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 ) { 261 String compareString = null; 262 String matchString = (String ) 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 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 298 301 public static Vcard parse(BufferedReader reader) throws java.text.ParseException { 302 303 try { 304 Properties newProps = new Properties (); 305 306 boolean isDone = false; 307 308 String line = getNextLine(reader); 309 if (line != null) { 310 String [] 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 ("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 [] 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 ("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 (ioe.getMessage(), 0); 343 } 344 } 345 346 349 private static String getNextLine(BufferedReader reader) throws IOException { 350 String firstLine = reader.readLine(); 351 boolean isDone = false; 352 if (firstLine != null) { 353 while (! isDone) { 354 reader.mark(256); 355 String 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 [] parseLine(String firstLine) { 372 String [] returnValue = new String [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 384 public void write(BufferedWriter out) throws java.io.IOException { 385 out.write("begin:vcard"); 386 out.newLine(); 387 java.util.Enumeration propNames = properties.propertyNames(); 388 while (propNames.hasMoreElements()) { 389 String currentName = (String ) 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 |