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

📄 vcardprovider.java

📁 JBother是纯Java开发的Jabber(即时消息开源软件)客户端。支持群组聊天
💻 JAVA
字号:
/** * $RCSfile$ * $Revision: 2813 $ * $Date: 2005-09-13 13:55:41 -0500 (Tue, 13 Sep 2005) $ * * Copyright 2003-2004 Jive Software. * * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.valhalla.jbother.jabber.smack.provider;import org.jivesoftware.smack.packet.IQ;import com.valhalla.jbother.jabber.smack.*;import org.jivesoftware.smack.provider.IQProvider;import org.w3c.dom.*;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import java.io.ByteArrayInputStream;import java.io.IOException;/** * Created by IntelliJ IDEA. * User: Gaston * Date: Jun 18, 2005 * Time: 1:00:57 AM * To change this template use File | Settings | File Templates. * * @author     synic * @created    October 13, 2005 */public class VCardProvider implements IQProvider {    /**     *  Description of the Method     *     * @param  parser         Description of the Parameter     * @return                Description of the Return Value     * @exception  Exception  Description of the Exception     */    public IQ parseIQ(XmlPullParser parser) throws Exception {        StringBuffer sb = new StringBuffer();        try {            int event = parser.getEventType();            // get the content            while (true) {                switch (event) {                    case XmlPullParser.TEXT:                        sb.append(parser.getText());                        break;                    case XmlPullParser.START_TAG:                        sb.append('<' + parser.getName() + '>');                        break;                    case XmlPullParser.END_TAG:                        sb.append("</" + parser.getName() + '>');                        break;                    default:                }                if (event == XmlPullParser.END_TAG && "vCard".equals(parser.getName())) {                    break;                }                event = parser.next();            }        } catch (XmlPullParserException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        String xmlText = sb.toString();        VCard vCard = new VCard();        try {            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();            Document document = documentBuilder.parse(new ByteArrayInputStream(xmlText.getBytes()));            new VCardReader(vCard, document).initializeFields();        } catch (Exception e) {            e.printStackTrace(System.err);        }        return vCard;    }    /**     *  Description of the Class     *     * @author     synic     * @created    October 13, 2005     */    private class VCardReader {        private final VCard vCard;        private final Document document;        /**         *Constructor for the VCardReader object         *         * @param  vCard     Description of the Parameter         * @param  document  Description of the Parameter         */        VCardReader(VCard vCard, Document document) {            this.vCard = vCard;            this.document = document;        }        /**         *  Description of the Method         */        public void initializeFields() {            vCard.setFirstName(getTagContents("GIVEN"));            vCard.setLastName(getTagContents("FAMILY"));            vCard.setMiddleName(getTagContents("MIDDLE"));            vCard.setEncodedImage(getTagContents("BINVAL"));            setupEmails();            vCard.setOrganization(getTagContents("ORGNAME"));            vCard.setOrganizationUnit(getTagContents("ORGUNIT"));            setupSimpleFields();            //setupPhones("WORK", true);            setupPhones("HOME", false);            //setupAddress("WORK", true);            setupAddress("HOME", false);        }        /**         *  Description of the Method         */        private void setupEmails() {            NodeList nodes = document.getElementsByTagName("USERID");            if (nodes == null) {                return;            }            for (int i = 0; i < nodes.getLength(); i++) {                Element element = (Element) nodes.item(i);                if ("WORK".equals(element.getParentNode().getFirstChild().getNodeName())) {                    vCard.setEmailWork(getTextContent(element));                } else {                    vCard.setEmailHome(getTextContent(element));                }            }        }        /**         *  Description of the Method         *         * @param  type  Description of the Parameter         * @param  work  Description of the Parameter         */        private void setupPhones(String type, boolean work) {            NodeList allPhones = document.getElementsByTagName("TEL");            if (allPhones == null) {                return;            }            for (int i = 0; i < allPhones.getLength(); i++) {                Element node = (Element) allPhones.item(i);                if (true) { //type.equals(node.getChildNodes().item(0).getNodeName())) {                    String code = node.getFirstChild().getNodeName();                    String value = getTextContent(node.getChildNodes().item(0));                    //if (work) {                        vCard.setPhoneWork("VOICE", value);                    //} else {                        vCard.setPhoneHome("VOICE", value);                    //}                }            }        }        /**         *  Description of the Method         *         * @param  type  Description of the Parameter         * @param  work  Description of the Parameter         */        private void setupAddress(String type, boolean work) {            NodeList allAddresses = document.getElementsByTagName("ADR");            work = false;            if (allAddresses == null) {                return;            }            for (int i = 0; i < allAddresses.getLength(); i++) {                Element node = (Element) allAddresses.item(i);                NodeList childNodes = node.getChildNodes();                if (true) { //type.equals(childNodes.item(0).getNodeName())) {                    for (int j = 0; j < childNodes.getLength(); j++) {                        Node item = childNodes.item(j);                        if (item instanceof Element) {                            if (work) {                                vCard.setAddressFieldWork(item.getNodeName(), getTextContent(item));                            } else {                                vCard.setAddressFieldHome(item.getNodeName(), getTextContent(item));                            }                        }                    }                }            }        }        /**         *  Gets the tagContents attribute of the VCardReader object         *         * @param  tag  Description of the Parameter         * @return      The tagContents value         */        private String getTagContents(String tag) {            NodeList nodes = document.getElementsByTagName(tag);            if (nodes != null && nodes.getLength() == 1) {                return getTextContent(nodes.item(0));            }            return null;        }        /**         *  Description of the Method         */        private void setupSimpleFields() {            NodeList childNodes = document.getDocumentElement().getChildNodes();            for (int i = 0; i < childNodes.getLength(); i++) {                Node node = childNodes.item(i);                if (node instanceof Element) {                    Element element = (Element) node;                    if ("FN".equals(element.getNodeName())) {                        continue;                    }                    if (element.getChildNodes().getLength() == 0) {                        vCard.setField(element.getNodeName(), "");                    } else if (element.getChildNodes().getLength() == 1 &&                            element.getChildNodes().item(0) instanceof Text) {                        vCard.setField(element.getNodeName(), getTextContent(element));                    }                }            }        }        /**         *  Gets the textContent attribute of the VCardReader object         *         * @param  node  Description of the Parameter         * @return       The textContent value         */        private String getTextContent(Node node) {            StringBuffer result = new StringBuffer();            appendText(result, node);            return result.toString();        }        /**         *  Description of the Method         *         * @param  result  Description of the Parameter         * @param  node    Description of the Parameter         */        private void appendText(StringBuffer result, Node node) {            NodeList childNodes = node.getChildNodes();            for (int i = 0; i < childNodes.getLength(); i++) {                Node nd = childNodes.item(i);                String nodeValue = nd.getNodeValue();                if (nodeValue != null) {                    result.append(nodeValue);                }                appendText(result, nd);            }        }    }}

⌨️ 快捷键说明

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