📄 clearspacevcardtranslator.java
字号:
// Get the mail type, i.e. HOME or WORK String mailType = getFieldType(oldValue); // Adds the mail type to the new value newValue = addFieldType(newValue, mailType); // Now the old and new values can be compared if (!oldValue.equalsIgnoreCase(newValue)) { value.setText(newValue == null ? "" : newValue); action = Action.MODIFY; } // Removes the value from the map to mark that is was used vCardValues.remove(VCardField.EMAIL_USERID); break; case PHONE: // Get all the phones numbers String newHomePhone = vCardValues.get(VCardField.PHONE_HOME); String newWorkPhone = vCardValues.get(VCardField.PHONE_WORK); String newWorkFax = vCardValues.get(VCardField.FAX_WORK); String newWorkMobile = vCardValues.get(VCardField.MOBILE_WORK); String newWorkPager = vCardValues.get(VCardField.PAGER_WORK); newValue = null; oldValue = value.getTextTrim(); String oldType = getFieldType(oldValue); // Modifies the phone field that is of the same type if ("work".equalsIgnoreCase(oldType)) { newValue = addFieldType(newWorkPhone, oldType); } else if ("home".equalsIgnoreCase(oldType)) { newValue = addFieldType(newHomePhone, oldType); } else if ("fax".equalsIgnoreCase(oldType)) { newValue = addFieldType(newWorkFax, oldType); } else if ("mobile".equalsIgnoreCase(oldType)) { newValue = addFieldType(newWorkMobile, oldType); } else if ("pager".equalsIgnoreCase(oldType)) { newValue = addFieldType(newWorkPager, oldType); } else if ("other".equalsIgnoreCase(oldType)) { // No phone to update // Removes the values from the map to mark that is was used vCardValues.remove(VCardField.PHONE_HOME); vCardValues.remove(VCardField.PHONE_WORK); vCardValues.remove(VCardField.FAX_WORK); vCardValues.remove(VCardField.MOBILE_WORK); vCardValues.remove(VCardField.PAGER_WORK); break; } // If newValue and oldValue are different the update the field if (!oldValue.equals(newValue)) { value.setText(newValue == null ? "" : newValue); action = Action.MODIFY; } // Removes the values from the map to mark that is was used vCardValues.remove(VCardField.PHONE_HOME); vCardValues.remove(VCardField.PHONE_WORK); vCardValues.remove(VCardField.FAX_WORK); vCardValues.remove(VCardField.MOBILE_WORK); vCardValues.remove(VCardField.PAGER_WORK); break; } } // Add new profiles that remains in the vCardValues, those are new profiles. if (vCardValues.containsKey(VCardField.TITLE)) { String newValue = vCardValues.get(VCardField.TITLE); if (addProfile(profiles, ClearspaceField.TITLE, newValue)) { action = Action.MODIFY; } } if (vCardValues.containsKey(VCardField.ORG_ORGUNIT)) { String newValue = vCardValues.get(VCardField.ORG_ORGUNIT); if (addProfile(profiles, ClearspaceField.DEPARTMENT, newValue)) { action = Action.MODIFY; } } if (vCardValues.containsKey(VCardField.ADR_WORK)) { String newValue = vCardValues.get(VCardField.ADR_WORK); if (addProfile(profiles, ClearspaceField.ADDRESS, newValue)) { action = Action.MODIFY; } } if (vCardValues.containsKey(VCardField.ADR_HOME)) { String newValue = vCardValues.get(VCardField.ADR_HOME); if (addProfile(profiles, ClearspaceField.HOME_ADDRESS, newValue)) { action = Action.MODIFY; } } if (vCardValues.containsKey(VCardField.TZ)) { String newValue = vCardValues.get(VCardField.TZ); if (addProfile(profiles, ClearspaceField.TIME_ZONE, newValue)) { action = Action.MODIFY; } } if (vCardValues.containsKey(VCardField.URL)) { String newValue = vCardValues.get(VCardField.URL); if (addProfile(profiles, ClearspaceField.URL, newValue)) { action = Action.MODIFY; } } if (vCardValues.containsKey(VCardField.EMAIL_USERID)) { String newValue = vCardValues.get(VCardField.EMAIL_USERID); newValue = addFieldType(newValue, "work"); if (addProfile(profiles, ClearspaceField.ALT_EMAIL, newValue)) { action = Action.MODIFY; } } // Adds just one phone number, the first one. Clearspace doesn't support more than one. if (vCardValues.containsKey(VCardField.PHONE_WORK)) { String newValue = vCardValues.get(VCardField.PHONE_WORK); newValue = addFieldType(newValue, "work"); if (addProfile(profiles, ClearspaceField.PHONE, newValue)) { action = Action.MODIFY; } } else if (vCardValues.containsKey(VCardField.PHONE_HOME)) { String newValue = vCardValues.get(VCardField.PHONE_HOME); newValue = addFieldType(newValue, "home"); if (addProfile(profiles, ClearspaceField.PHONE, newValue)) { action = Action.MODIFY; } } else if (vCardValues.containsKey(VCardField.FAX_WORK)) { String newValue = vCardValues.get(VCardField.FAX_WORK); newValue = addFieldType(newValue, "fax"); if (addProfile(profiles, ClearspaceField.PHONE, newValue)) { action = Action.MODIFY; } } else if (vCardValues.containsKey(VCardField.MOBILE_WORK)) { String newValue = vCardValues.get(VCardField.MOBILE_WORK); newValue = addFieldType(newValue, "mobile"); if (addProfile(profiles, ClearspaceField.PHONE, newValue)) { action = Action.MODIFY; } } else if (vCardValues.containsKey(VCardField.PAGER_WORK)) { String newValue = vCardValues.get(VCardField.PAGER_WORK); newValue = addFieldType(newValue, "pager"); if (addProfile(profiles, ClearspaceField.PHONE, newValue)) { action = Action.MODIFY; } } return action; } /** * Adds a profiles element to the profiles if it is not empty * * @param profiles the profiles to add a profile to * @param field the field type to add * @param newValue the value to add * @return true if the field was added */ private boolean addProfile(Element profiles, ClearspaceField field, String newValue) { // Don't add empty vales if (newValue == null || "".equals(newValue.trim())) { return false; } Element profile = profiles.addElement("profiles"); profile.addElement("fieldID").setText(String.valueOf(field.getId())); if (field.isMultipleValues()) { profile.addElement("values").setText(newValue); } else { profile.addElement("value").setText(newValue); } return true; } /** * Modifies the value of a profile if it is different from the original one. * * @param vCardValues the vCard values with the new values * @param value the current value of the profile * @param vCardField the vCard field * @return true if the field was modified */ private boolean modifyProfileValue(Map<VCardField, String> vCardValues, Element value, VCardField vCardField) { boolean modified = false; String newValue = vCardValues.get(vCardField); // Modifies or deletes the value if (!value.getTextTrim().equals(newValue)) { value.setText(newValue == null ? "" : newValue); modified = true; } // Remove the vCard value to mark that it was used vCardValues.remove(vCardField); return modified; } /** * Adds the field type to the field value. Returns null if the value is empty. * * @param value the value * @param type the type * @return the field value with the type */ private String addFieldType(String value, String type) { if (value == null || "".equals(value.trim())) { return null; } return value + "|" + type; } /** * Returns the field type of a field. Return null if the field doesn't * contains a type * * @param field the field with the type * @return the field type */ private String getFieldType(String field) { int i = field.indexOf("|"); if (i == -1) { return null; } else { return field.substring(i + 1); } } /** * Returns the field value of a field. Return the field if the field doesn't * contains a type. * * @param field the field * @return the field value */ private String getFieldValue(String field) { int i = field.indexOf("|"); if (i == -1) { return field; } else { return field.substring(0, i); } } /** * Collects the vCard values and store them into a map. * They are stored with the VCardField enum. * * @param vCardElement the vCard with the information * @return a map of the value of the vCard. */ private Map<VCardField, String> collectVCardValues(Element vCardElement) { Map<VCardField, String> vCardValues = new HashMap<VCardField, String>(); // Add the Title vCardValues.put(VCardField.TITLE, vCardElement.elementText("TITLE")); // Add the Department Element orgElement = vCardElement.element("ORG"); if (orgElement != null) { vCardValues.put(VCardField.ORG_ORGUNIT, orgElement.elementText("ORGUNIT")); } // Add the home and work address List<Element> addressElements = (List<Element>) vCardElement.elements("ADR"); if (addressElements != null) { for (Element address : addressElements) { if (address.element("WORK") != null) { vCardValues.put(VCardField.ADR_WORK, translateAddress(address)); } else if (address.element("HOME") != null) { vCardValues.put(VCardField.ADR_HOME, translateAddress(address)); } } } // Add the URL vCardValues.put(VCardField.URL, vCardElement.elementText("URL")); // Add the preferred and alternative email address List<Element> emailsElement = (List<Element>) vCardElement.elements("EMAIL"); if (emailsElement != null) { for (Element emailElement : emailsElement) { if (emailElement.element("PREF") == null) { vCardValues.put(VCardField.EMAIL_USERID, emailElement.elementText("USERID")); } else { vCardValues.put(VCardField.EMAIL_PREF_USERID, emailElement.elementText("USERID")); } } } // Add the full name vCardValues.put(VCardField.FN, vCardElement.elementText("FN")); // Add the time zone vCardValues.put(VCardField.TZ, vCardElement.elementText("TZ")); // Add the photo Element photoElement = vCardElement.element("PHOTO"); if (photoElement != null) { vCardValues.put(VCardField.PHOTO_TYPE, photoElement.elementText("TYPE")); vCardValues.put(VCardField.PHOTO_BINVAL, photoElement.elementText("BINVAL")); } // Add the home and work tel List<Element> telElements = (List<Element>) vCardElement.elements("TEL"); if (telElements != null) { for (Element tel : telElements) { String number = tel.elementText("NUMBER");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -