List of usage examples for org.dom4j Element elementText
String elementText(QName qname);
From source file:org.jivesoftware.openfire.clearspace.ClearspaceVCardProvider.java
License:Open Source License
/** * Returns an element that can be used as a parameter to create an avatar. * This element has the user's avatar information. * * @param userID the id of user./*from w ww . j a v a 2s . c o m*/ * @return the element with that can be used to create an Avatar. * @throws UserNotFoundException if the userID is invalid. * @throws Exception if there is problem doing the request. */ private Element getAvatarCreateParams(long userID) throws Exception { // Creates response element Element avatarCreateParams = DocumentHelper.createDocument().addElement("createAvatar"); // Gets current avatar Element avatarResponse = getAvatar(userID); // Translates from the response to create params Element avatar = avatarResponse.element("return"); if (avatar != null) { // Sets the owner avatarCreateParams.addElement("ownerID").setText(avatar.elementText("owner")); // Sets the attachment values Element attachment = avatar.element("attachment"); if (attachment != null) { avatarCreateParams.addElement("name").setText(attachment.elementText("name")); avatarCreateParams.addElement("contentType").setText(attachment.elementText("contentType")); avatarCreateParams.addElement("data").setText(attachment.elementText("data")); } } return avatarCreateParams; }
From source file:org.jivesoftware.openfire.clearspace.ClearspaceVCardProvider.java
License:Open Source License
/** * Adds the profiles elements from one profile to the other one. * * @param currentProfile the profile with the information. * @param newProfiles the profile to copy the information to. *//*from ww w .ja va 2 s . c o m*/ private void addProfiles(Element currentProfile, Element newProfiles) { // Gets current fields List<Element> fields = (List<Element>) currentProfile.elements("return"); // Iterate over current fields for (Element field : fields) { // Get the fieldID and values String fieldID = field.elementText("fieldID"); // The value name of the value field could be value or values Element value = field.element("value"); boolean multiValues = false; if (value == null) { value = field.element("values"); if (value != null) { multiValues = true; } } // Don't add empty field. Field id 0 means no field. if ("0".equals(fieldID)) { continue; } // Adds the profile to the new profiles element Element newProfile = newProfiles.addElement("profiles"); newProfile.addElement("fieldID").setText(fieldID); // adds the value if it is not empty if (value != null) { if (multiValues) { newProfile.addElement("values").setText(value.getText()); } else { newProfile.addElement("value").setText(value.getText()); } } } }
From source file:org.jivesoftware.openfire.clearspace.ClearspaceVCardTranslator.java
License:Open Source License
/** * Init the fields of clearspace based on they name. * * @param fields the fields information/*from w ww. j ava2 s.c o m*/ */ protected void initClearspaceFieldsId(Element fields) { List<Element> fieldsList = fields.elements("return"); for (Element field : fieldsList) { String fieldName = field.elementText("name"); long fieldID = Long.valueOf(field.elementText("ID")); ClearspaceField f = ClearspaceField.valueOfName(fieldName); if (f != null) { f.setId(fieldID); } } }
From source file:org.jivesoftware.openfire.clearspace.ClearspaceVCardTranslator.java
License:Open Source License
/** * Updates the avatar values based on the vCard values * * @param avatarElement the avatar element to update * @param vCardValues the vCard values with the information * @return the action performed/*from w w w. j ava 2s .co m*/ */ private Action updateAvatarValues(Element avatarElement, Map<VCardField, String> vCardValues) { Action action = Action.NO_ACTION; // Gets the current avatar information String currContentType = avatarElement.elementText("contentType"); String currdata = avatarElement.elementText("data"); // Gets the vCard photo information String newContentType = vCardValues.get(VCardField.PHOTO_TYPE); String newData = vCardValues.get(VCardField.PHOTO_BINVAL); // Compares them if (currContentType == null && newContentType != null) { // new avatar avatarElement.addElement("contentType").setText(newContentType); avatarElement.addElement("data").setText(newData); action = Action.CREATE; } else if (currContentType != null && newContentType == null) { // delete action = Action.DELETE; } else if (currdata != null && !currdata.equals(newData)) { // modify avatarElement.element("contentType").setText(newContentType); avatarElement.element("data").setText(newData); action = Action.MODIFY; } return action; }
From source file:org.jivesoftware.openfire.clearspace.ClearspaceVCardTranslator.java
License:Open Source License
/** * Updates the values of the profiles with the values of the vCard * * @param profiles the profiles element to update * @param vCardValues the vCard values/*from w w w. j a v a 2 s . co m*/ * @return the action performed */ private Action updateProfilesValues(Element profiles, Map<VCardField, String> vCardValues) { Action action = Action.NO_ACTION; List<Element> profilesList = profiles.elements("profiles"); // Modify or delete current profiles for (Element profile : profilesList) { int fieldID = Integer.valueOf(profile.elementText("fieldID")); ClearspaceField field = ClearspaceField.valueOf(fieldID); // If the field is unknown, then continue with the next one if (field == null) { continue; } // Gets the field value, it could have "value" of "values" Element value = profile.element("value"); if (value == null) { value = profile.element("values"); // It's OK if the value still null. It could need to be modified } // Modify or delete each field type. If newValue is null it will empty the field. String newValue; String oldValue; switch (field) { case TITLE: if (modifyProfileValue(vCardValues, value, VCardField.TITLE)) { action = Action.MODIFY; } break; case DEPARTMENT: if (modifyProfileValue(vCardValues, value, VCardField.ORG_ORGUNIT)) { action = Action.MODIFY; } break; case ADDRESS: if (modifyProfileValue(vCardValues, value, VCardField.ADR_WORK)) { action = Action.MODIFY; } break; case HOME_ADDRESS: if (modifyProfileValue(vCardValues, value, VCardField.ADR_HOME)) { action = Action.MODIFY; } break; case TIME_ZONE: if (modifyProfileValue(vCardValues, value, VCardField.TZ)) { action = Action.MODIFY; } break; case URL: if (modifyProfileValue(vCardValues, value, VCardField.URL)) { action = Action.MODIFY; } break; case ALT_EMAIL: // Get the new value newValue = vCardValues.get(VCardField.EMAIL_USERID); // Get the old value oldValue = value.getTextTrim(); // 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; }
From source file:org.jivesoftware.openfire.clearspace.ClearspaceVCardTranslator.java
License:Open Source License
/** * 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. *//*from w ww .j a v a2s . c om*/ 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"); if (tel.element("WORK") != null) { if (tel.element("VOICE") != null) { vCardValues.put(VCardField.PHONE_WORK, number); } else if (tel.element("FAX") != null) { vCardValues.put(VCardField.FAX_WORK, number); } else if (tel.element("CELL") != null) { vCardValues.put(VCardField.MOBILE_WORK, number); } else if (tel.element("PAGER") != null) { vCardValues.put(VCardField.PAGER_WORK, number); } } else if (tel.element("HOME") != null && tel.element("VOICE") != null) { vCardValues.put(VCardField.PHONE_HOME, number); } } } return vCardValues; }
From source file:org.jivesoftware.openfire.clearspace.ClearspaceVCardTranslator.java
License:Open Source License
/** * Translates the profile information to the vCard * * @param profiles the profile information * @param vCard the vCard to add the information to *///from ww w . ja va 2 s. c o m private void translateProfileInformation(Element profiles, Element vCard) { // Translate the profile XML /* Profile response sample <ns1:getProfileResponse xmlns:ns1="http://jivesoftware.com/clearspace/webservices"> <return> <fieldID>2</fieldID> <value>RTC</value> </return> <return> <fieldID>9</fieldID> <value>-300</value> </return> <return> <fieldID>11</fieldID> <value>street1:San Martin,street2:1650,city:Cap Fed,state:Buenos Aires,country:Argentina,zip:1602,type:HOME</value> </return> <return> <fieldID>1</fieldID> <value>Mr.</value> </return> <return> <fieldID>3</fieldID> <value>street1:Alder 2345,city:Portland,state:Oregon,country:USA,zip:32423,type:WORK</value> </return> <return> <fieldID>10</fieldID> <values>gguardin@gmail.com|work</values> </return> <return> <fieldID>5</fieldID> <values>http://www.gguardin.com.ar</values> </return> </ns1:getProfileResponse> */ List<Element> profilesList = (List<Element>) profiles.elements("return"); for (Element profileElement : profilesList) { long fieldID = Long.valueOf(profileElement.elementText("fieldID")); ClearspaceField field = ClearspaceField.valueOf(fieldID); // If the field is not known, skip it if (field == null) { continue; } // The value name of the value field could be value or values String fieldText = profileElement.elementText("value"); if (fieldText == null) { fieldText = profileElement.elementText("values"); // if it is an empty field, continue with the next field if (fieldText == null) { continue; } } String fieldType = getFieldType(fieldText); String fieldValue = getFieldValue(fieldText); switch (field) { case TITLE: vCard.addElement("TITLE").setText(fieldValue); break; case DEPARTMENT: vCard.addElement("ORG").addElement("ORGUNIT").setText(fieldValue); break; case TIME_ZONE: vCard.addElement("TZ").setText(fieldValue); break; case ADDRESS: Element workAdr = vCard.addElement("ADR"); workAdr.addElement("WORK"); translateAddress(fieldValue, workAdr); break; case HOME_ADDRESS: Element homeAdr = vCard.addElement("ADR"); homeAdr.addElement("HOME"); translateAddress(fieldValue, homeAdr); break; case URL: vCard.addElement("URL").setText(fieldValue); break; case ALT_EMAIL: fieldValue = getFieldValue(fieldValue); Element email = vCard.addElement("EMAIL"); email.addElement("USERID").setText(fieldValue); email.addElement("INTERNET").setText(fieldValue); if ("work".equalsIgnoreCase(fieldType)) { email.addElement("WORK"); } else if ("home".equalsIgnoreCase(fieldType)) { email.addElement("HOME"); } break; case PHONE: Element tel = vCard.addElement("TEL"); tel.addElement("NUMBER").setText(fieldValue); if ("home".equalsIgnoreCase(fieldType)) { tel.addElement("HOME"); tel.addElement("VOICE"); } else if ("work".equalsIgnoreCase(fieldType)) { tel.addElement("WORK"); tel.addElement("VOICE"); } else if ("fax".equalsIgnoreCase(fieldType)) { tel.addElement("WORK"); tel.addElement("FAX"); } else if ("mobile".equalsIgnoreCase(fieldType)) { tel.addElement("WORK"); tel.addElement("CELL"); } else if ("pager".equalsIgnoreCase(fieldType)) { tel.addElement("WORK"); tel.addElement("PAGER"); } else if ("other".equalsIgnoreCase(fieldType)) { // don't send } break; } } }
From source file:org.jivesoftware.openfire.clearspace.ClearspaceVCardTranslator.java
License:Open Source License
/** * Translates the avatar information to the vCard. * * @param avatarResponse the avatar information * @param vCard the vCard to add the information to *///from ww w .java 2 s . c om private void translateAvatarInformation(Element avatarResponse, Element vCard) { Element avatar = avatarResponse.element("return"); if (avatar != null) { Element attachment = avatar.element("attachment"); if (attachment != null) { String contentType = attachment.elementText("contentType"); String data = attachment.elementText("data"); // Add the avatar to the vCard Element photo = vCard.addElement("PHOTO"); photo.addElement("TYPE").setText(contentType); photo.addElement("BINVAL").setText(data); } } }
From source file:org.jivesoftware.openfire.fastpath.history.ChatTranscriptManager.java
License:Open Source License
/** * Return the plain text version of a chat transcript. * * @param sessionID the sessionID of the <code>ChatSession</code> * @return the plain text version of a chat transcript. *//* w w w. j a va2s .c om*/ public static String getTextTranscriptFromSessionID(String sessionID) { String transcript = null; Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; try { con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(GET_SESSION_TRANSCRIPT); pstmt.setString(1, sessionID); rs = pstmt.executeQuery(); if (rs.next()) { transcript = DbConnectionManager.getLargeTextField(rs, 1); } } catch (Exception ex) { Log.error(ex.getMessage(), ex); } finally { DbConnectionManager.closeConnection(rs, pstmt, con); } if (transcript == null || "".equals(transcript)) { return ""; } // Define time zone used in the transcript. SimpleDateFormat UTC_FORMAT = new SimpleDateFormat(JiveConstants.XMPP_DELAY_DATETIME_FORMAT); UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC")); final SimpleDateFormat formatter = new SimpleDateFormat("h:mm a"); Document element = null; try { element = DocumentHelper.parseText(transcript); } catch (DocumentException e) { Log.error(e.getMessage(), e); } StringBuilder buf = new StringBuilder(); // Add the Messages and Presences contained in the retrieved transcript element for (Iterator<Element> it = element.getRootElement().elementIterator(); it.hasNext();) { Element packet = it.next(); String name = packet.getName(); String message = ""; String from = ""; if ("presence".equals(name)) { String type = packet.attributeValue("type"); from = new JID(packet.attributeValue("from")).getResource(); if (type == null) { message = from + " has joined the room"; } else { message = from + " has left the room"; } } else if ("message".equals(name)) { from = new JID(packet.attributeValue("from")).getResource(); message = packet.elementText("body"); message = StringUtils.escapeHTMLTags(message); } List<Element> el = packet.elements("x"); for (Element ele : el) { if ("jabber:x:delay".equals(ele.getNamespaceURI())) { String stamp = ele.attributeValue("stamp"); try { String formattedDate; synchronized (UTC_FORMAT) { Date d = UTC_FORMAT.parse(stamp); formattedDate = formatter.format(d); } if ("presence".equals(name)) { buf.append("[").append(formattedDate).append("] ").append(message).append("\n"); } else { buf.append("[").append(formattedDate).append("] ").append(from).append(": ") .append(message).append("\n"); } } catch (ParseException e) { Log.error(e.getMessage(), e); } } } } return buf.toString(); }
From source file:org.jivesoftware.openfire.fastpath.history.ChatTranscriptManager.java
License:Open Source License
/** * Formats a given XML Chat Transcript.//from w w w.j a v a 2s . c o m * * @param transcript the XMP ChatTranscript. * @return the pretty-version of a transcript. */ public static String formatTranscript(String transcript) { if (transcript == null || "".equals(transcript)) { return ""; } final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat(JiveConstants.XMPP_DELAY_DATETIME_FORMAT); UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC")); final SimpleDateFormat formatter = new SimpleDateFormat("h:mm a"); Document element = null; try { element = DocumentHelper.parseText(transcript); } catch (DocumentException e) { Log.error(e.getMessage(), e); } StringBuilder buf = new StringBuilder(); String conv1 = null; // Add the Messages and Presences contained in the retrieved transcript element for (Iterator<Element> it = element.getRootElement().elementIterator(); it.hasNext();) { Element packet = it.next(); String name = packet.getName(); String message = ""; String from = ""; if ("presence".equals(name)) { String type = packet.attributeValue("type"); from = new JID(packet.attributeValue("from")).getResource(); if (type == null) { message = from + " has joined the room"; } else { message = from + " has left the room"; } } else if ("message".equals(name)) { from = new JID(packet.attributeValue("from")).getResource(); message = packet.elementText("body"); message = StringUtils.escapeHTMLTags(message); if (conv1 == null) { conv1 = from; } } List<Element> el = packet.elements("x"); for (Element ele : el) { if ("jabber:x:delay".equals(ele.getNamespaceURI())) { String stamp = ele.attributeValue("stamp"); try { String formattedDate; synchronized (UTC_FORMAT) { Date d = UTC_FORMAT.parse(stamp); formattedDate = formatter.format(d); } if ("presence".equals(name)) { buf.append("<tr valign=\"top\"><td class=\"notification-label\" colspan=2 nowrap>[") .append(formattedDate).append("] ").append(message).append("</td></tr>"); } else { String cssClass = conv1.equals(from) ? "conversation-label1" : "conversation-label2"; buf.append("<tr valign=\"top\"><td width=1% class=\"" + cssClass + "\" nowrap>[") .append(formattedDate).append("] ").append(from) .append(":</td><td class=\"conversation-body\">").append(message) .append("</td></tr>"); } } catch (ParseException e) { Log.error(e.getMessage(), e); } } } } return buf.toString(); }