⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clearspacevcardtranslator.java

📁 openfire 服务器源码下载
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/** * $Revision$ * $Date$ * * Copyright (C) 2008 Jive Software. All rights reserved. * * This software is published under the terms of the GNU Public License (GPL), * a copy of which is included in this distribution, or a commercial license * agreement with Jive. */package org.jivesoftware.openfire.clearspace;import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.jivesoftware.openfire.XMPPServer;import org.jivesoftware.openfire.user.User;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.StringTokenizer;/** * @author Gabriel Guardincerri */class ClearspaceVCardTranslator {    // Represents the type of action that was performed.    enum Action {        MODIFY, CREATE, UPDATE, DELETE, NO_ACTION;    }    /**     * Represents the default fields of Clearspace, this is only a subset of the fields. It only includes     * the fields that have in common with VCards.     */    enum ClearspaceField {        TITLE("Title"),        DEPARTMENT("Department"),        TIME_ZONE("Time Zone"),        ADDRESS("Address"),        HOME_ADDRESS("Home Address"),        ALT_EMAIL("Alternate Email", true), //multiple - primary comes from user obj        URL("URL", true), //multiple with primary        PHONE("Phone Number", true); //multiple with primary        // Used to get a field from its ID        private static Map<Long, ClearspaceField> idMap = new HashMap<Long, ClearspaceField>();        // Used to get a field from its name        private static Map<String, ClearspaceField> nameMap = new HashMap<String, ClearspaceField>();        static {            nameMap.put(TITLE.getName(), TITLE);            nameMap.put(DEPARTMENT.getName(), DEPARTMENT);            nameMap.put(TIME_ZONE.getName(), TIME_ZONE);            nameMap.put(ADDRESS.getName(), ADDRESS);            nameMap.put(HOME_ADDRESS.getName(), HOME_ADDRESS);            nameMap.put(ALT_EMAIL.getName(), ALT_EMAIL);            nameMap.put(URL.getName(), URL);            nameMap.put(PHONE.getName(), PHONE);        }        // The name is fixed and can be used as ID        private final String name;        // The id may change, so it is updated        private long id;        // True if the field supports multiple values.        private final boolean multipleValues;        /**         * Constructs a new field with a name         *         * @param name the name of the field         */        ClearspaceField(String name) {            this(name, false);        }        /**         * Constructs a new field with a name and if it has multiple values         *         * @param name           the name of the field         * @param multipleValues true if it has multiple values         */        ClearspaceField(String name, boolean multipleValues) {            this.name = name;            this.multipleValues = multipleValues;        }        public String getName() {            return name;        }        public long getId() {            return id;        }        public boolean isMultipleValues() {            return multipleValues;        }        public void setId(long id) {            this.id = id;            idMap.put(id, this);        }        public static ClearspaceField valueOf(long id) {            return idMap.get(id);        }        public static ClearspaceField valueOfName(String name) {            return nameMap.get(name);        }    }    /**     * Represents the fields of the VCard, this is only a subset of the fields. It only includes     * the fields that have in common with Clearspace.     */    enum VCardField {        TITLE, ORG_ORGUNIT, ADR_WORK, ADR_HOME, EMAIL_USERID, EMAIL_PREF_USERID, FN,        PHOTO_TYPE, PHOTO_BINVAL, URL, TZ, PHONE_HOME, PHONE_WORK, FAX_WORK, MOBILE_WORK, PAGER_WORK;    }    private static ClearspaceVCardTranslator instance = new ClearspaceVCardTranslator();    /**     * Returns the instance of the translator     *     * @return the instance.     */    protected static ClearspaceVCardTranslator getInstance() {        return instance;    }    /**     * Init the fields of clearspace based on they name.     *     * @param fields the fields information     */    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);            }        }    }    /**     * Translates a VCard of Openfire into profiles, user information and a avatar of Clearspace.     * Returns what can of action has been made over the the profilesElement, userElement and avatarElement/     *     * @param vCardElement    the VCard information     * @param profilesElement the profile to add/modify/delete the information     * @param userElement     the user to add/modify/delete the information     * @param avatarElement   the avatar to add/modify/delete the information     * @return a list of actions performed over the profilesElement, userElement and avatarElement     */    protected Action[] translateVCard(Element vCardElement, Element profilesElement, Element userElement, Element avatarElement) {        Action[] actions = new Action[3];        // Gets the vCard values        Map<VCardField, String> vCardValues = collectVCardValues(vCardElement);        // Updates the profiles values with the vCard values        actions[0] = updateProfilesValues(profilesElement, vCardValues);        actions[1] = updateUserValues(userElement, vCardValues);        actions[2] = updateAvatarValues(avatarElement, vCardValues);        return actions;    }    /**     * 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     */    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;    }    /**     * Updates the user values based on the vCard values     *     * @param userElement the user element to update     * @param vCardValues the vCard values     * @return the action performed     */    private Action updateUserValues(Element userElement, Map<VCardField, String> vCardValues) {        Action action = Action.NO_ACTION;        String fullName = vCardValues.get(VCardField.FN);        boolean emptyName = fullName == null || "".equals(fullName.trim());        // if the new value is not empty then update. The name can't be deleted by an empty string        if (!emptyName) {            WSUtils.modifyElementText(userElement, "name", fullName);            action = Action.MODIFY;        }        String email = vCardValues.get(VCardField.EMAIL_PREF_USERID);        boolean emptyEmail = email == null || "".equals(email.trim());        // if the new value is not empty then update. The email can't be deleted by an empty string        if (!emptyEmail) {            WSUtils.modifyElementText(userElement, "email", email);            action = Action.MODIFY;        }        return action;    }    /**     * Updates the values of the profiles with the values of the vCard     *     * @param profiles    the profiles element to update     * @param vCardValues the vCard values     * @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();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -