📄 vcard.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 org.jivesoftware.smackx.packet;import org.jivesoftware.smack.PacketCollector;import org.jivesoftware.smack.SmackConfiguration;import org.jivesoftware.smack.XMPPConnection;import org.jivesoftware.smack.XMPPException;import org.jivesoftware.smack.filter.PacketIDFilter;import org.jivesoftware.smack.packet.IQ;import org.jivesoftware.smack.packet.XMPPError;import org.jivesoftware.smack.util.StringUtils;import java.io.IOException;import java.io.InputStream;import java.lang.reflect.Field;import java.lang.reflect.Modifier;import java.net.URL;import java.util.HashMap;import java.util.Iterator;import java.util.Map;/** * A VCard class for use with the * <a href="http://www.jivesoftware.org/smack/" target="_blank">SMACK jabber library</a>.<p> * <p/> * You should refer to the * <a href="http://www.jabber.org/jeps/jep-0054.html" target="_blank">JEP-54 documentation</a>.<p> * <p/> * Please note that this class is incomplete but it does provide the most commonly found * information in vCards. Also remember that VCard transfer is not a standard, and the protocol * may change or be replaced.<p> * <p/> * <b>Usage:</b> * <pre> * <p/> * // To save VCard: * <p/> * VCard vCard = new VCard(); * vCard.setFirstName("kir"); * vCard.setLastName("max"); * vCard.setEmailHome("foo@fee.bar"); * vCard.setJabberId("jabber@id.org"); * vCard.setOrganization("Jetbrains, s.r.o"); * vCard.setNickName("KIR"); * <p/> * vCard.setField("TITLE", "Mr"); * vCard.setAddressFieldHome("STREET", "Some street"); * vCard.setAddressFieldWork("CTRY", "US"); * vCard.setPhoneWork("FAX", "3443233"); * <p/> * vCard.save(connection); * <p/> * // To load VCard: * <p/> * VCard vCard = new VCard(); * vCard.load(conn); // load own VCard * vCard.load(conn, "joe@foo.bar"); // load someone's VCard * </pre> * * @author Kirill Maximov (kir@maxkir.com) */public class VCard extends IQ { /** * Phone types: * VOICE?, FAX?, PAGER?, MSG?, CELL?, VIDEO?, BBS?, MODEM?, ISDN?, PCS?, PREF? */ private Map homePhones = new HashMap(); private Map workPhones = new HashMap(); /** * Address types: * POSTAL?, PARCEL?, (DOM | INTL)?, PREF?, POBOX?, EXTADR?, STREET?, LOCALITY?, * REGION?, PCODE?, CTRY? */ private Map homeAddr = new HashMap(); private Map workAddr = new HashMap(); private String firstName; private String lastName; private String middleName; private String emailHome; private String emailWork; private String organization; private String organizationUnit; private String avatar; /** * Such as DESC ROLE GEO etc.. see JEP-0054 */ private Map otherSimpleFields = new HashMap(); public VCard() { } /** * Set generic VCard field. * * @param field value of field. Possible values: NICKNAME, PHOTO, BDAY, JABBERID, MAILER, TZ, * GEO, TITLE, ROLE, LOGO, NOTE, PRODID, REV, SORT-STRING, SOUND, UID, URL, DESC. */ public String getField(String field) { return (String) otherSimpleFields.get(field); } /** * Set generic VCard field. * * @param value value of field * @param field field to set. See {@link #getField(String)} * @see #getField(String) */ public void setField(String field, String value) { otherSimpleFields.put(field, value); } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getMiddleName() { return middleName; } public void setMiddleName(String middleName) { this.middleName = middleName; } public String getNickName() { return (String) otherSimpleFields.get("NICKNAME"); } public void setNickName(String nickName) { otherSimpleFields.put("NICKNAME", nickName); } public String getEmailHome() { return emailHome; } public void setEmailHome(String email) { this.emailHome = email; } public String getEmailWork() { return emailWork; } public void setEmailWork(String emailWork) { this.emailWork = emailWork; } public String getJabberId() { return (String) otherSimpleFields.get("JABBERID"); } public void setJabberId(String jabberId) { otherSimpleFields.put("JABBERID", jabberId); } public String getOrganization() { return organization; } public void setOrganization(String organization) { this.organization = organization; } public String getOrganizationUnit() { return organizationUnit; } public void setOrganizationUnit(String organizationUnit) { this.organizationUnit = organizationUnit; } /** * Get home address field * * @param addrField one of POSTAL, PARCEL, (DOM | INTL), PREF, POBOX, EXTADR, STREET, * LOCALITY, REGION, PCODE, CTRY */ public String getAddressFieldHome(String addrField) { return (String) homeAddr.get(addrField); } /** * Set home address field * * @param addrField one of POSTAL, PARCEL, (DOM | INTL), PREF, POBOX, EXTADR, STREET, * LOCALITY, REGION, PCODE, CTRY */ public void setAddressFieldHome(String addrField, String value) { homeAddr.put(addrField, value); } /** * Get work address field * * @param addrField one of POSTAL, PARCEL, (DOM | INTL), PREF, POBOX, EXTADR, STREET, * LOCALITY, REGION, PCODE, CTRY */ public String getAddressFieldWork(String addrField) { return (String) workAddr.get(addrField); } /** * Set work address field * * @param addrField one of POSTAL, PARCEL, (DOM | INTL), PREF, POBOX, EXTADR, STREET, * LOCALITY, REGION, PCODE, CTRY */ public void setAddressFieldWork(String addrField, String value) { workAddr.put(addrField, value); } /** * Set home phone number * * @param phoneType one of VOICE, FAX, PAGER, MSG, CELL, VIDEO, BBS, MODEM, ISDN, PCS, PREF * @param phoneNum phone number */ public void setPhoneHome(String phoneType, String phoneNum) { homePhones.put(phoneType, phoneNum); } /** * Get home phone number * * @param phoneType one of VOICE, FAX, PAGER, MSG, CELL, VIDEO, BBS, MODEM, ISDN, PCS, PREF */ public String getPhoneHome(String phoneType) { return (String) homePhones.get(phoneType); } /** * Set work phone number * * @param phoneType one of VOICE, FAX, PAGER, MSG, CELL, VIDEO, BBS, MODEM, ISDN, PCS, PREF * @param phoneNum phone number */ public void setPhoneWork(String phoneType, String phoneNum) { workPhones.put(phoneType, phoneNum); } /** * Get work phone number * * @param phoneType one of VOICE, FAX, PAGER, MSG, CELL, VIDEO, BBS, MODEM, ISDN, PCS, PREF */ public String getPhoneWork(String phoneType) { return (String) workPhones.get(phoneType); } /** * Set the avatar for the VCard by specifying the url to the image. * * @param avatarURL the url to the image(png,jpeg,gif,bmp) */ public void setAvatar(URL avatarURL) { byte[] bytes = new byte[0]; try { bytes = getBytes(avatarURL); } catch (IOException e) { e.printStackTrace(); } String encodedImage = StringUtils.encodeBase64(bytes); setField("PHOTO", "<TYPE>image/jpeg</TYPE><BINVAL>" + encodedImage + "</BINVAL>"); } /** * Specify the bytes for the avatar to use. * @param bytes the bytes of the avatar. */ public void setAvatar(byte[] bytes) { String encodedImage = StringUtils.encodeBase64(bytes); setField("PHOTO", "<TYPE>image/jpeg</TYPE><BINVAL>" + encodedImage + "</BINVAL>"); } /** * Set the encoded avatar string. This is used by the provider. * * @param encodedAvatar the encoded avatar string. */ public void setEncodedImage(String encodedAvatar) { //TODO Move VCard and VCardProvider into a vCard package. this.avatar = encodedAvatar; } /** * Return the byte representation of the avatar(if one exists), otherwise returns null if * no avatar could be found. * <b>Example 1</b> * <pre> * // Load Avatar from VCard * byte[] avatarBytes = vCard.getAvatar(); * <p/> * // To create an ImageIcon for Swing applications * ImageIcon icon = new ImageIcon(avatar); * <p/> * // To create just an image object from the bytes * ByteArrayInputStream bais = new ByteArrayInputStream(avatar); * try { * Image image = ImageIO.read(bais); * } * catch (IOException e) { * e.printStackTrace(); * } * </pre> * * @return byte representation of avatar. */ public byte[] getAvatar() { if (avatar == null) { return null; } if (avatar != null) { return StringUtils.decodeBase64(avatar); } return null; } /** * Common code for getting the bytes of a url. * * @param url the url to read. */ public static byte[] getBytes(URL url) throws IOException { InputStream in = url.openStream(); final byte[] buffer = new byte[4096]; while (true) { final int bytesRead = in.read(buffer); if (bytesRead < 0) { break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -