1 18 19 package sync4j.syncclient.blackberry.parser; 20 21 import java.util.Hashtable ; 22 import java.util.Vector ; 23 24 import javax.microedition.pim.Contact; 25 import javax.microedition.pim.ContactList; 26 import javax.microedition.pim.PIMItem; 27 28 import sync4j.syncclient.common.StringTools; 29 30 import sync4j.syncclient.util.StaticDataHelper; 31 32 import net.rim.device.api.ui.component.Dialog; 33 34 42 43 public class XMLContactParser implements ContactParser{ 44 45 47 private static final String FIRST_NAME = "FirstName" ; 48 private static final String LAST_NAME = "LastName" ; 49 private static final String TITLE = "JobTitle" ; 50 private static final String COMPANY = "CompanyName" ; 51 private static final String EMAIL_1 = "Email1Address" ; 52 private static final String INSTANT_MESSENGER = "IstantMessenger" ; 53 private static final String WEB_PAGE = "WebPage" ; 54 private static final String MOBILE_TEL = "MobileTelephoneNumber" ; 55 private static final String HOME_FAX = "HomeFaxNumber" ; 56 private static final String BUSINESS_FAX = "BusinessFaxNumber" ; 57 private static final String BUSINESS_TEL = "BusinessTelephoneNumber" ; 58 private static final String BUSINESS_TEL2 = "Business2TelephoneNumber" ; 59 private static final String HOME_TEL = "HomeTelephoneNumber" ; 60 private static final String BUSINESS_STREET = "BusinessAddressStreet" ; 61 private static final String BUSINESS_CITY = "BusinessAddressCity" ; 62 private static final String BUSINESS_STATE = "BusinessAddressState" ; 63 private static final String BUSINESS_COUNTRY = "BusinessAddressCountry" ; 64 private static final String BUSINESS_PO = "BusinessAddressPostalCode" ; 65 private static final String HOME_STREET = "HomeAddressStreet" ; 66 private static final String HOME_CITY = "HomeAddressCity" ; 67 private static final String HOME_STATE = "HomeAddressState" ; 68 private static final String HOME_COUNTRY = "HomeAddressCountry" ; 69 private static final String HOME_PO = "HomeAddressPostalCode" ; 70 private static final String NOTE = "Body" ; 71 72 private static final String START_MARKER = "<contact>" ; 73 private static final String END_MARKER = "</contact>" ; 74 75 77 private String [] tagArray = { 78 FIRST_NAME , 79 LAST_NAME , 80 EMAIL_1 , 81 COMPANY , 82 TITLE , 83 INSTANT_MESSENGER , 84 WEB_PAGE , 85 MOBILE_TEL , 86 HOME_FAX , 87 BUSINESS_FAX , 88 BUSINESS_TEL , 89 BUSINESS_TEL2 , 90 HOME_TEL , 91 BUSINESS_STREET , 92 BUSINESS_CITY , 93 BUSINESS_STATE , 94 BUSINESS_COUNTRY , 95 BUSINESS_PO , 96 HOME_STREET , 97 HOME_CITY , 98 HOME_STATE , 99 HOME_COUNTRY , 100 HOME_PO , 101 NOTE 102 }; 103 104 private String [] telAttrs = { 105 MOBILE_TEL , 106 BUSINESS_FAX , 107 BUSINESS_TEL , 108 HOME_TEL 109 }; 110 111 private ContactList list; 112 private Contact contact; 113 private boolean modify; 114 115 123 public XMLContactParser(ContactList list, Contact contact, boolean modify) { 124 this.list = list; 125 this.contact = contact; 126 this.modify = modify; 127 } 128 129 137 public Contact parseContact(String contactString) { 138 if(modify) 139 return modifyContact(contactString); 140 else 141 return addContact(contactString); 142 } 143 144 152 public Contact addContact(String contactString) { 153 154 Hashtable contactMap = buildMapFromXML(contactString); 155 156 if (contactMap != null) { 157 try { 158 String [] arrayField = new String [5]; 159 160 arrayField[Contact.NAME_FAMILY] = getValue(contactMap, LAST_NAME); 161 162 arrayField[Contact.NAME_GIVEN] = getValue(contactMap, FIRST_NAME); 163 164 if (list.isSupportedField(Contact.NAME)) { 165 contact.addStringArray(Contact.NAME, PIMItem.ATTR_NONE, arrayField); 166 } 167 168 String field = getValue(contactMap, EMAIL_1); 169 170 if (isSupportedField(Contact.EMAIL, field)) { 171 contact.addString(Contact.EMAIL, Contact.ATTR_NONE, field); 172 } 173 174 field = getValue(contactMap, NOTE); 175 if (isSupportedField(Contact.NOTE, field)) { 176 contact.addString(Contact.NOTE, Contact.ATTR_NONE, field); 177 } 178 179 field = getValue(contactMap, TITLE); 180 if (isSupportedField(Contact.TITLE, field)) { 181 contact.addString(Contact.TITLE, Contact.ATTR_NONE, field); 182 } 183 184 field = getValue(contactMap, COMPANY); 185 if (isSupportedField(Contact.ORG, field)) { 186 contact.addString(Contact.ORG, Contact.ATTR_NONE, field); 187 } 188 189 field = getValue(contactMap, WEB_PAGE); 190 if (isSupportedField(Contact.URL, field)) { 191 contact.addString(Contact.URL, Contact.ATTR_NONE, field); 192 } 193 194 field = getValue(contactMap, HOME_TEL); 195 if (isSupportedAtrributedField(Contact.TEL, Contact.ATTR_HOME, field)) { 196 contact.addString(Contact.TEL, Contact.ATTR_HOME, field); 197 } 198 199 field = getValue(contactMap, MOBILE_TEL); 200 if (isSupportedAtrributedField(Contact.TEL, Contact.ATTR_MOBILE, field)) { 201 contact.addString(Contact.TEL, Contact.ATTR_MOBILE, field); 202 } 203 204 field = getValue(contactMap, BUSINESS_TEL); 205 if (isSupportedAtrributedField(Contact.TEL, Contact.ATTR_WORK, field)) { 206 contact.addString(Contact.TEL, Contact.ATTR_WORK, field); 207 } 208 209 field = getValue(contactMap, BUSINESS_FAX); 210 211 if (isSupportedAtrributedField(Contact.TEL, Contact.ATTR_FAX, field)) { 212 contact.addString(Contact.TEL, Contact.ATTR_FAX, field); 213 } 214 arrayField = new String [7]; 215 arrayField[Contact.ADDR_COUNTRY] = getValue(contactMap, HOME_COUNTRY ); 216 arrayField[Contact.ADDR_STREET] = getValue(contactMap, HOME_STREET ); 217 arrayField[Contact.ADDR_REGION] = getValue(contactMap, HOME_STATE ); 218 arrayField[Contact.ADDR_LOCALITY] = getValue(contactMap, HOME_CITY ); 219 arrayField[Contact.ADDR_POSTALCODE] = getValue(contactMap, HOME_PO ); 220 221 if (list.isSupportedField(Contact.ADDR)) { 222 contact.addStringArray(Contact.ADDR, Contact.ATTR_WORK, arrayField); 223 } 224 225 arrayField = new String [7]; 226 arrayField[Contact.ADDR_COUNTRY] = getValue(contactMap, HOME_COUNTRY ); 227 arrayField[Contact.ADDR_STREET] = getValue(contactMap, HOME_STREET ); 228 arrayField[Contact.ADDR_REGION] = getValue(contactMap, HOME_STATE ); 229 arrayField[Contact.ADDR_LOCALITY] = getValue(contactMap, HOME_CITY ); 230 arrayField[Contact.ADDR_POSTALCODE] = getValue(contactMap, HOME_PO ); 231 if (list.isSupportedField(Contact.ADDR) && list.isSupportedAttribute(Contact.ADDR, Contact.ATTR_HOME)) 232 contact.addStringArray(Contact.ADDR, Contact.ATTR_HOME, arrayField); 233 234 return contact; 235 } catch (Exception e) { 236 e.printStackTrace(); 237 return null; 238 } 239 } 240 return null; 241 } 242 243 251 public Contact modifyContact(String contactString) { 252 253 Hashtable contactMap = buildMapFromXML(contactString); 254 if (contactMap != null) { 255 try { 256 String [] arrayField = new String [5]; 257 arrayField[Contact.NAME_FAMILY] = getValue(contactMap, LAST_NAME); 258 arrayField[Contact.NAME_GIVEN] = getValue(contactMap, FIRST_NAME); 259 if(contact.countValues(Contact.NAME) > 0) { 260 contact.setStringArray(Contact.NAME, 0, Contact.ATTR_NONE, arrayField); 261 } else { 262 contact.addStringArray(Contact.NAME, Contact.ATTR_NONE, arrayField); 263 } 264 265 String field = getValue(contactMap, EMAIL_1); 266 if (isSupportedField(Contact.EMAIL, field) && contact.countValues(Contact.EMAIL) > 0) { 267 contact.setString(Contact.EMAIL, 0, Contact.ATTR_NONE, field); 268 } else { 269 contact.addString(Contact.EMAIL, Contact.ATTR_NONE, field); 270 } 271 272 field = getValue(contactMap, NOTE); 273 if (isSupportedAtrributedField(Contact.NOTE, Contact.ATTR_NONE, field)) { 274 contact.removeValue(Contact.NOTE, 0); 275 contact.addString(Contact.NOTE, Contact.ATTR_NONE, field); 276 } 277 278 field = getValue(contactMap, COMPANY); 279 if (isSupportedField(Contact.ORG, field) && contact.countValues(Contact.ORG) > 0) { 280 contact.setString(Contact.ORG, 0, Contact.ATTR_NONE, field); 281 } else { 282 contact.addString(Contact.ORG, Contact.ATTR_NONE, field); 283 } 284 285 field = getValue(contactMap, TITLE); 286 if (isSupportedAtrributedField(Contact.TITLE, Contact.ATTR_NONE, field)) { 287 contact.removeValue(Contact.TITLE, 0); 288 contact.addString(Contact.TITLE, Contact.ATTR_NONE, field); 289 } 290 291 if( isSupportedField(Contact.URL, field) && contact.countValues(Contact.URL) > 0) { 292 contact.removeValue(Contact.URL, 0); 293 field = getValue(contactMap, WEB_PAGE); 294 contact.addString(Contact.URL, Contact.ATTR_NONE, field); 295 } 296 297 int telValues = contact.countValues(Contact.TEL); 298 for(int i=0; i<telValues;i++) { 299 contact.removeValue(Contact.TEL, 0); 300 } 301 302 field = getValue(contactMap, HOME_TEL); 303 if (isSupportedAtrributedField(Contact.TEL, Contact.ATTR_HOME, field)) 304 contact.addString(Contact.TEL, Contact.ATTR_HOME, field); 305 306 field = getValue(contactMap, MOBILE_TEL); 307 if (isSupportedAtrributedField(Contact.TEL, Contact.ATTR_MOBILE, field)) 308 contact.addString(Contact.TEL, Contact.ATTR_MOBILE, field); 309 310 field = getValue(contactMap, BUSINESS_TEL); 311 if (isSupportedAtrributedField(Contact.TEL, Contact.ATTR_WORK, field)) 312 contact.addString(Contact.TEL, Contact.ATTR_WORK, field); 313 314 field = getValue(contactMap, BUSINESS_FAX); 315 if (isSupportedAtrributedField(Contact.TEL, Contact.ATTR_FAX, field)) 316 contact.addString(Contact.TEL, Contact.ATTR_FAX, field); 317 318 arrayField = new String [7]; 319 arrayField[Contact.ADDR_COUNTRY] = getValue(contactMap, BUSINESS_COUNTRY ); 320 arrayField[Contact.ADDR_STREET] = getValue(contactMap, BUSINESS_STREET ); 321 arrayField[Contact.ADDR_REGION] = getValue(contactMap, BUSINESS_STATE ); 322 arrayField[Contact.ADDR_LOCALITY] = getValue(contactMap, BUSINESS_CITY ); 323 arrayField[Contact.ADDR_POSTALCODE] = getValue(contactMap, BUSINESS_PO ); 324 325 if (list.isSupportedField(Contact.ADDR) && contact.countValues(Contact.ADDR) > 0) 326 contact.setStringArray(Contact.ADDR, 0, Contact.ATTR_WORK, arrayField); 327 else 328 contact.addStringArray(Contact.ADDR, Contact.ATTR_WORK, arrayField); 329 330 return contact; 331 } catch (Exception e) { 332 e.printStackTrace(); 333 return null; 334 } 335 } 336 return null; 337 } 338 339 346 public String toString(Contact contact) { 347 348 StringBuffer contactBuffer = new StringBuffer (); 349 350 contactBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" ); 351 352 contactBuffer.append("<contact>\n"); 353 354 appendName (contactBuffer , 355 contact ); 356 357 appendField (contactBuffer , 358 contact , 359 Contact.ORG , 360 COMPANY ); 361 362 appendField (contactBuffer , 363 contact , 364 Contact.NOTE , 365 NOTE ); 366 367 appendField (contactBuffer , 368 contact , 369 Contact.TITLE , 370 TITLE ); 371 372 appendField (contactBuffer , 373 contact , 374 Contact.EMAIL , 375 EMAIL_1 ); 376 377 appendField (contactBuffer , 378 contact , 379 Contact.URL , 380 WEB_PAGE ); 381 382 appendTelephoneFields (contactBuffer , 383 contact ); 384 385 appendAddress (contactBuffer , 386 contact ); 387 388 contactBuffer.append("</contact>"); 389 390 return contactBuffer.toString(); 391 } 392 393 395 405 private void appendField(StringBuffer contactBuffer, Contact contact, int field, String tag) { 406 407 String value = null; 408 409 if (list.isSupportedField(field)) { 410 411 value = ""; 412 413 try { 414 value = contact.getString(field, 0); 415 } catch (Exception e) { 416 StaticDataHelper.log("Not set field: " + tag); 417 } 418 419 appendToContact(contactBuffer, tag, value); 420 421 } 422 } 423 424 433 private void appendToContact(StringBuffer contactBuffer, String tag, String value) { 434 435 value = (value == null) ? "" : value; 436 437 if (value.length() > 0) { 438 value = StringTools.escapeXml(value); 439 } 440 441 contactBuffer.append("<").append(tag).append(">").append(value); 442 contactBuffer.append("</").append(tag).append(">\n"); 443 444 } 445 446 453 private void appendName(StringBuffer buffer, Contact contact) { 454 String [] name = contact.getStringArray(Contact.NAME, 0); 455 appendToContact(buffer, FIRST_NAME, name[Contact.NAME_GIVEN]); 456 appendToContact(buffer, LAST_NAME, name[Contact.NAME_FAMILY]); 457 } 458 459 466 private void appendTelephoneFields(StringBuffer buffer , Contact contact) { 467 int totalTelFields = contact.countValues(Contact.TEL); 468 Vector addedAttrs = new Vector (); 469 for(int i=0;i<totalTelFields;i++) { 470 String value = contact.getString(Contact.TEL, i); 471 int attr = contact.getAttributes(Contact.TEL, i); 472 String tag = ""; 473 switch(attr) { 474 case Contact.ATTR_MOBILE: 475 tag = MOBILE_TEL; 476 break; 477 case Contact.ATTR_FAX: 478 tag = BUSINESS_FAX; 479 break; 480 case Contact.ATTR_WORK: 481 tag = BUSINESS_TEL; 482 break; 483 case Contact.ATTR_HOME: 484 tag = HOME_TEL; 485 break; 486 } 487 if(!tag.equals("")) { 488 appendToContact(buffer, tag, value); 489 addedAttrs.addElement(tag); 490 } 491 } 492 493 for (int i = 0; i < telAttrs.length; i++) { 494 if(!addedAttrs.contains(telAttrs[i])) 495 appendToContact(buffer, telAttrs[i], ""); 496 } 497 } 498 499 506 private void appendAddress(StringBuffer buffer, Contact contact) { 507 if (list.isSupportedField(Contact.ADDR) && contact.countValues(Contact.ADDR) > 0) { 508 String [] addr = contact.getStringArray(Contact.ADDR, 0); 509 appendToContact(buffer, BUSINESS_STREET , addr[Contact.ADDR_STREET] ); 510 appendToContact(buffer, BUSINESS_STATE , addr[Contact.ADDR_REGION] ); 511 appendToContact(buffer, BUSINESS_CITY , addr[Contact.ADDR_LOCALITY] ); 512 appendToContact(buffer, BUSINESS_PO , addr[Contact.ADDR_POSTALCODE] ); 513 appendToContact(buffer, BUSINESS_COUNTRY , addr[Contact.ADDR_COUNTRY] ); 514 } else { 515 appendToContact(buffer, BUSINESS_STREET , ""); 516 appendToContact(buffer, BUSINESS_STATE , ""); 517 appendToContact(buffer, BUSINESS_CITY , ""); 518 appendToContact(buffer, BUSINESS_PO , ""); 519 appendToContact(buffer, BUSINESS_COUNTRY , ""); 520 } 521 } 522 523 531 private String getValue(Hashtable contactMap, String key) { 532 Object val = contactMap.get(key); 533 String value = val == null ? "" : val.toString(); 534 return value.trim(); 535 } 536 537 547 private boolean isSupportedAtrributedField(int field, int attribute, String value) { 548 return isSupportedField(field, value) && list.isSupportedAttribute(field, attribute); 549 } 550 551 559 private boolean isSupportedField(int type, String value) { 560 return list.isSupportedField(type); 561 } 562 563 570 private Hashtable buildMapFromXML(String contactString) { 571 int startMarkerIndex = contactString.indexOf(START_MARKER); 572 int endMarkerIndex = contactString.indexOf(END_MARKER); 573 574 if(startMarkerIndex == -1 || endMarkerIndex == -1) { 575 Dialog.inform("Improper data from the server."); 576 return null; 577 } 578 579 Hashtable contactMap = new Hashtable (); 580 String startTag = ""; 581 String endTag = ""; 582 String degeneratedTag = ""; 583 String data = ""; 584 585 for (int i = 0; i < tagArray.length; i++) { 586 String tag = tagArray[i]; 587 588 startTag = "<" +tag + ">"; 589 590 int startTagIndex = contactString.indexOf(startTag); 591 if(startTagIndex == -1) { 592 continue; 593 } 594 595 degeneratedTag = "<" + tag +"/>"; 596 int degeneratedTagIndex = contactString.indexOf(degeneratedTag); 597 if(degeneratedTagIndex != -1) { 598 continue; 599 } 600 601 endTag = "</"+tag+">"; 602 603 int endTagIndex = contactString.indexOf(endTag); 604 if(endTagIndex != -1) { 605 data = contactString.substring(startTagIndex+startTag.length(), endTagIndex); 606 } else { 607 data = ""; 608 } 609 610 if (data.length() > 0) { 611 data = StringTools.unescapeXml(data); 612 } 613 614 contactMap.put(tag, data); 615 } 616 return contactMap; 617 } 618 } 619 | Popular Tags |