mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-27 00:32:07 +01:00
Fixed vCard issues. SMACK-116
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@3315 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
07f5fc9ef6
commit
237ab44301
3 changed files with 145 additions and 60 deletions
|
@ -124,13 +124,35 @@ public class VCard extends IQ {
|
||||||
/**
|
/**
|
||||||
* Set generic VCard field.
|
* Set generic VCard field.
|
||||||
*
|
*
|
||||||
* @param field value of field. Possible values: NICKNAME, PHOTO, BDAY, JABBERID, MAILER, TZ,
|
* @param field value of field. Possible values: FN, NICKNAME, PHOTO, BDAY, JABBERID, MAILER, TZ,
|
||||||
* GEO, TITLE, ROLE, LOGO, NOTE, PRODID, REV, SORT-STRING, SOUND, UID, URL, DESC.
|
* GEO, TITLE, ROLE, LOGO, NOTE, PRODID, REV, SORT-STRING, SOUND, UID, URL, DESC.
|
||||||
*/
|
*/
|
||||||
public String getField(String field) {
|
public String getField(String field) {
|
||||||
|
if ("FN".equals(field)) {
|
||||||
|
return buildFullName();
|
||||||
|
}
|
||||||
return (String) otherSimpleFields.get(field);
|
return (String) otherSimpleFields.get(field);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String buildFullName() {
|
||||||
|
if (otherSimpleFields.containsKey("FN")) {
|
||||||
|
return otherSimpleFields.get("FN").toString().trim();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
StringBuffer sb = new StringBuffer();
|
||||||
|
if (firstName != null) {
|
||||||
|
sb.append(firstName).append(' ');
|
||||||
|
}
|
||||||
|
if (middleName != null) {
|
||||||
|
sb.append(middleName).append(' ');
|
||||||
|
}
|
||||||
|
if (lastName != null) {
|
||||||
|
sb.append(lastName);
|
||||||
|
}
|
||||||
|
return sb.toString().trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set generic VCard field.
|
* Set generic VCard field.
|
||||||
*
|
*
|
||||||
|
@ -162,6 +184,13 @@ public class VCard extends IQ {
|
||||||
return middleName;
|
return middleName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the full name of the user, associated with this VCard.
|
||||||
|
*/
|
||||||
|
public String getFullName() {
|
||||||
|
return getField("FN");
|
||||||
|
}
|
||||||
|
|
||||||
public void setMiddleName(String middleName) {
|
public void setMiddleName(String middleName) {
|
||||||
this.middleName = middleName;
|
this.middleName = middleName;
|
||||||
}
|
}
|
||||||
|
@ -540,7 +569,7 @@ public class VCard extends IQ {
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean hasNameField() {
|
private boolean hasNameField() {
|
||||||
return firstName != null || lastName != null || middleName != null;
|
return firstName != null || lastName != null || middleName != null || otherSimpleFields.containsKey("FN");
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean hasOrganizationFields() {
|
private boolean hasOrganizationFields() {
|
||||||
|
@ -638,7 +667,7 @@ public class VCard extends IQ {
|
||||||
|
|
||||||
private void buildActualContent() {
|
private void buildActualContent() {
|
||||||
if (hasNameField()) {
|
if (hasNameField()) {
|
||||||
appendFN();
|
appendTag("FN", getFullName());
|
||||||
appendN();
|
appendN();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -706,7 +735,9 @@ public class VCard extends IQ {
|
||||||
Iterator it = otherSimpleFields.entrySet().iterator();
|
Iterator it = otherSimpleFields.entrySet().iterator();
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
Map.Entry entry = (Map.Entry) it.next();
|
Map.Entry entry = (Map.Entry) it.next();
|
||||||
appendTag(entry.getKey().toString(), (String) entry.getValue());
|
String tag = entry.getKey().toString();
|
||||||
|
if ("FN".equals(tag)) continue;
|
||||||
|
appendTag(tag, (String) entry.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -721,28 +752,6 @@ public class VCard extends IQ {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void appendField(String tag) {
|
|
||||||
String value = (String) otherSimpleFields.get(tag);
|
|
||||||
appendTag(tag, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void appendFN() {
|
|
||||||
final ContentBuilder contentBuilder = new ContentBuilder() {
|
|
||||||
public void addTagContent() {
|
|
||||||
if (firstName != null) {
|
|
||||||
sb.append(firstName + ' ');
|
|
||||||
}
|
|
||||||
if (middleName != null) {
|
|
||||||
sb.append(middleName + ' ');
|
|
||||||
}
|
|
||||||
if (lastName != null) {
|
|
||||||
sb.append(lastName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
appendTag("FN", true, contentBuilder);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void appendN() {
|
private void appendN() {
|
||||||
appendTag("N", true, new ContentBuilder() {
|
appendTag("N", true, new ContentBuilder() {
|
||||||
public void addTagContent() {
|
public void addTagContent() {
|
||||||
|
|
|
@ -31,6 +31,8 @@ import javax.xml.parsers.DocumentBuilder;
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by IntelliJ IDEA.
|
* Created by IntelliJ IDEA.
|
||||||
|
@ -52,10 +54,10 @@ public class VCardProvider implements IQProvider {
|
||||||
sb.append(parser.getText());
|
sb.append(parser.getText());
|
||||||
break;
|
break;
|
||||||
case XmlPullParser.START_TAG:
|
case XmlPullParser.START_TAG:
|
||||||
sb.append('<' + parser.getName() + '>');
|
sb.append('<').append(parser.getName()).append('>');
|
||||||
break;
|
break;
|
||||||
case XmlPullParser.END_TAG:
|
case XmlPullParser.END_TAG:
|
||||||
sb.append("</" + parser.getName() + '>');
|
sb.append("</").append(parser.getName()).append('>');
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
@ -71,6 +73,10 @@ public class VCardProvider implements IQProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
String xmlText = sb.toString();
|
String xmlText = sb.toString();
|
||||||
|
return _createVCardFromXml(xmlText);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static VCard _createVCardFromXml(String xmlText) {
|
||||||
VCard vCard = new VCard();
|
VCard vCard = new VCard();
|
||||||
try {
|
try {
|
||||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||||
|
@ -85,7 +91,7 @@ public class VCardProvider implements IQProvider {
|
||||||
return vCard;
|
return vCard;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class VCardReader {
|
private static class VCardReader {
|
||||||
private final VCard vCard;
|
private final VCard vCard;
|
||||||
private final Document document;
|
private final Document document;
|
||||||
|
|
||||||
|
@ -106,58 +112,88 @@ public class VCardProvider implements IQProvider {
|
||||||
vCard.setOrganizationUnit(getTagContents("ORGUNIT"));
|
vCard.setOrganizationUnit(getTagContents("ORGUNIT"));
|
||||||
|
|
||||||
setupSimpleFields();
|
setupSimpleFields();
|
||||||
setupPhones("WORK", true);
|
|
||||||
setupPhones("HOME", false);
|
|
||||||
|
|
||||||
setupAddress("WORK", true);
|
setupPhones();
|
||||||
setupAddress("HOME", false);
|
setupAddresses();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupEmails() {
|
private void setupEmails() {
|
||||||
NodeList nodes = document.getElementsByTagName("USERID");
|
NodeList nodes = document.getElementsByTagName("USERID");
|
||||||
|
if (nodes == null) return;
|
||||||
for (int i = 0; i < nodes.getLength(); i++) {
|
for (int i = 0; i < nodes.getLength(); i++) {
|
||||||
Element element = (Element) nodes.item(i);
|
Element element = (Element) nodes.item(i);
|
||||||
if ("WORK".equals(element.getParentNode().getFirstChild().getNodeName())) {
|
if ("HOME".equals(element.getParentNode().getFirstChild().getNodeName())) {
|
||||||
vCard.setEmailWork(getTextContent(element));
|
|
||||||
} else {
|
|
||||||
vCard.setEmailHome(getTextContent(element));
|
vCard.setEmailHome(getTextContent(element));
|
||||||
|
} else {
|
||||||
|
vCard.setEmailWork(getTextContent(element));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupPhones(String type, boolean work) {
|
private void setupPhones() {
|
||||||
NodeList allPhones = document.getElementsByTagName("TEL");
|
NodeList allPhones = document.getElementsByTagName("TEL");
|
||||||
|
if (allPhones == null) return;
|
||||||
for (int i = 0; i < allPhones.getLength(); i++) {
|
for (int i = 0; i < allPhones.getLength(); i++) {
|
||||||
Element node = (Element) allPhones.item(i);
|
NodeList nodes = allPhones.item(i).getChildNodes();
|
||||||
if (type.equals(node.getChildNodes().item(1).getNodeName())) {
|
String type = null;
|
||||||
String code = node.getFirstChild().getNodeName();
|
String code = null;
|
||||||
String value = getTextContent(node.getChildNodes().item(2));
|
String value = null;
|
||||||
if (work) {
|
for (int j = 0; j < nodes.getLength(); j++) {
|
||||||
vCard.setPhoneWork(code, value);
|
Node node = nodes.item(j);
|
||||||
|
if (node.getNodeType() != Node.ELEMENT_NODE) continue;
|
||||||
|
String nodeName = node.getNodeName();
|
||||||
|
if ("NUMBER".equals(nodeName)) {
|
||||||
|
value = getTextContent(node);
|
||||||
|
}
|
||||||
|
else if (isWorkHome(nodeName)) {
|
||||||
|
type = nodeName;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
code = nodeName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (code == null || value == null) continue;
|
||||||
|
if ("HOME".equals(type)) {
|
||||||
vCard.setPhoneHome(code, value);
|
vCard.setPhoneHome(code, value);
|
||||||
}
|
}
|
||||||
|
else { // By default, setup work phone
|
||||||
|
vCard.setPhoneWork(code, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupAddress(String type, boolean work) {
|
private boolean isWorkHome(String nodeName) {
|
||||||
|
return "HOME".equals(nodeName) || "WORK".equals(nodeName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupAddresses() {
|
||||||
NodeList allAddresses = document.getElementsByTagName("ADR");
|
NodeList allAddresses = document.getElementsByTagName("ADR");
|
||||||
|
if (allAddresses == null) return;
|
||||||
for (int i = 0; i < allAddresses.getLength(); i++) {
|
for (int i = 0; i < allAddresses.getLength(); i++) {
|
||||||
Element node = (Element) allAddresses.item(i);
|
Element addressNode = (Element) allAddresses.item(i);
|
||||||
NodeList childNodes = node.getChildNodes();
|
|
||||||
if (type.equals(childNodes.item(0).getNodeName())) {
|
String type = null;
|
||||||
for (int j = 1; j < childNodes.getLength(); j++) {
|
List code = new ArrayList();
|
||||||
Node item = childNodes.item(j);
|
List value = new ArrayList();
|
||||||
if (item instanceof Element) {
|
NodeList childNodes = addressNode.getChildNodes();
|
||||||
if (work) {
|
for(int j = 0; j < childNodes.getLength(); j++) {
|
||||||
vCard.setAddressFieldWork(item.getNodeName(), getTextContent(item));
|
Node node = childNodes.item(j);
|
||||||
|
if (node.getNodeType() != Node.ELEMENT_NODE) continue;
|
||||||
|
String nodeName = node.getNodeName();
|
||||||
|
if (isWorkHome(nodeName)) {
|
||||||
|
type = nodeName;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
vCard.setAddressFieldHome(item.getNodeName(), getTextContent(item));
|
code.add(nodeName);
|
||||||
|
value.add(getTextContent(node));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for (int j = 0; j < value.size(); j++) {
|
||||||
|
if ("HOME".equals(type)) {
|
||||||
|
vCard.setAddressFieldHome((String) code.get(j), (String) value.get(j));
|
||||||
|
}
|
||||||
|
else { // By default, setup work address
|
||||||
|
vCard.setAddressFieldWork((String) code.get(j), (String) value.get(j));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -165,7 +201,7 @@ public class VCardProvider implements IQProvider {
|
||||||
|
|
||||||
private String getTagContents(String tag) {
|
private String getTagContents(String tag) {
|
||||||
NodeList nodes = document.getElementsByTagName(tag);
|
NodeList nodes = document.getElementsByTagName(tag);
|
||||||
if (nodes.getLength() == 1) {
|
if (nodes != null && nodes.getLength() == 1) {
|
||||||
return getTextContent(nodes.item(0));
|
return getTextContent(nodes.item(0));
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -177,13 +213,13 @@ public class VCardProvider implements IQProvider {
|
||||||
Node node = childNodes.item(i);
|
Node node = childNodes.item(i);
|
||||||
if (node instanceof Element) {
|
if (node instanceof Element) {
|
||||||
Element element = (Element) node;
|
Element element = (Element) node;
|
||||||
if ("FN".equals(element.getNodeName())) continue;
|
|
||||||
|
|
||||||
|
String field = element.getNodeName();
|
||||||
if (element.getChildNodes().getLength() == 0) {
|
if (element.getChildNodes().getLength() == 0) {
|
||||||
vCard.setField(element.getNodeName(), "");
|
vCard.setField(field, "");
|
||||||
} else if (element.getChildNodes().getLength() == 1 &&
|
} else if (element.getChildNodes().getLength() == 1 &&
|
||||||
element.getChildNodes().item(0) instanceof Text) {
|
element.getChildNodes().item(0) instanceof Text) {
|
||||||
vCard.setField(element.getNodeName(), getTextContent(element));
|
vCard.setField(field, getTextContent(element));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
package org.jivesoftware.smackx;
|
package org.jivesoftware.smackx;
|
||||||
|
|
||||||
import org.jivesoftware.smack.test.SmackTestCase;
|
|
||||||
import org.jivesoftware.smack.XMPPException;
|
import org.jivesoftware.smack.XMPPException;
|
||||||
|
import org.jivesoftware.smack.test.SmackTestCase;
|
||||||
import org.jivesoftware.smackx.packet.VCard;
|
import org.jivesoftware.smackx.packet.VCard;
|
||||||
|
import org.jivesoftware.smackx.provider.VCardProvider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by IntelliJ IDEA.
|
* Created by IntelliJ IDEA.
|
||||||
|
@ -23,13 +24,18 @@ public class VCardTest extends SmackTestCase {
|
||||||
origVCard.setFirstName("kir");
|
origVCard.setFirstName("kir");
|
||||||
origVCard.setLastName("max");
|
origVCard.setLastName("max");
|
||||||
origVCard.setEmailHome("foo@fee.bar");
|
origVCard.setEmailHome("foo@fee.bar");
|
||||||
|
origVCard.setEmailWork("foo@fee.www.bar");
|
||||||
|
|
||||||
origVCard.setJabberId("jabber@id.org");
|
origVCard.setJabberId("jabber@id.org");
|
||||||
origVCard.setOrganization("Jetbrains, s.r.o");
|
origVCard.setOrganization("Jetbrains, s.r.o");
|
||||||
origVCard.setNickName("KIR");
|
origVCard.setNickName("KIR");
|
||||||
|
|
||||||
origVCard.setField("TITLE", "Mr");
|
origVCard.setField("TITLE", "Mr");
|
||||||
origVCard.setAddressFieldHome("STREET", "Some street");
|
origVCard.setAddressFieldHome("STREET", "Some street");
|
||||||
|
origVCard.setAddressFieldWork("STREET", "Some street work");
|
||||||
|
|
||||||
origVCard.setPhoneWork("FAX", "3443233");
|
origVCard.setPhoneWork("FAX", "3443233");
|
||||||
|
origVCard.setPhoneHome("VOICE", "3443233");
|
||||||
|
|
||||||
origVCard.save(getConnection(0));
|
origVCard.save(getConnection(0));
|
||||||
|
|
||||||
|
@ -41,7 +47,7 @@ public class VCardTest extends SmackTestCase {
|
||||||
fail(e.getMessage());
|
fail(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals("Should load own VCard successfully", origVCard, loaded);
|
assertEquals("Should load own VCard successfully", origVCard.toString(), loaded.toString());
|
||||||
|
|
||||||
loaded = new VCard();
|
loaded = new VCard();
|
||||||
try {
|
try {
|
||||||
|
@ -51,7 +57,41 @@ public class VCardTest extends SmackTestCase {
|
||||||
fail(e.getMessage());
|
fail(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals("Should load another user's VCard successfully", origVCard, loaded);
|
assertEquals("Should load another user's VCard successfully", origVCard.toString(), loaded.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testNoWorkHomeSpecifier_EMAIL() throws Throwable {
|
||||||
|
VCard card = VCardProvider._createVCardFromXml("<vcard><EMAIL><USERID>foo@fee.www.bar</USERID></EMAIL></vcard>");
|
||||||
|
assertEquals("foo@fee.www.bar", card.getEmailWork());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testNoWorkHomeSpecifier_TEL() throws Throwable {
|
||||||
|
VCard card = VCardProvider._createVCardFromXml("<vcard><TEL><FAX/><NUMBER>3443233</NUMBER></TEL></vcard>");
|
||||||
|
assertEquals("3443233", card.getPhoneWork("FAX"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testNoWorkHomeSpecifier_ADDR() throws Throwable {
|
||||||
|
VCard card = VCardProvider._createVCardFromXml("<vcard><ADR><STREET>Some street</STREET><FF>ddss</FF></ADR></vcard>");
|
||||||
|
assertEquals("Some street", card.getAddressFieldWork("STREET"));
|
||||||
|
assertEquals("ddss", card.getAddressFieldWork("FF"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testFN() throws Throwable {
|
||||||
|
VCard card = VCardProvider._createVCardFromXml("<vcard><FN>kir max</FN></vcard>");
|
||||||
|
assertEquals("kir max", card.getField("FN"));
|
||||||
|
assertEquals("kir max", card.getFullName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testFullName() throws Throwable {
|
||||||
|
VCard card = new VCard();
|
||||||
|
card.setFirstName("kir");
|
||||||
|
assertEquals("kir", card.getFullName());
|
||||||
|
|
||||||
|
card.setLastName("maximov");
|
||||||
|
assertEquals("kir maximov", card.getFullName());
|
||||||
|
|
||||||
|
card.setField("FN", "some name");
|
||||||
|
assertEquals("some name", card.getFullName());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int getMaxConnections() {
|
protected int getMaxConnections() {
|
||||||
|
|
Loading…
Reference in a new issue