From 2250ac20eddf10b8bd118e8551709ab82838a87f Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 26 Feb 2015 08:55:53 +0100 Subject: [PATCH] Make VCard XML null value safe The user is able to set (most) values to null. We must take that into consideration when transforming the VCard to XML. --- .../smackx/vcardtemp/packet/VCard.java | 42 ++++++++++++++----- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/vcardtemp/packet/VCard.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/vcardtemp/packet/VCard.java index 303510031..fd18638e4 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/vcardtemp/packet/VCard.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/vcardtemp/packet/VCard.java @@ -565,23 +565,27 @@ public class VCard extends IQ { xml.rightAngleBracket(); if (hasNameField()) { xml.openElement("N"); - xml.element("FAMILY", lastName); - xml.element("GIVEN", firstName); - xml.element("MIDDLE", middleName); + xml.optElement("FAMILY", lastName); + xml.optElement("GIVEN", firstName); + xml.optElement("MIDDLE", middleName); xml.closeElement("N"); } if (hasOrganizationFields()) { xml.openElement("ORG"); - xml.element("ORGNAME", organization); - xml.element("ORGUNIT", organizationUnit); + xml.optElement("ORGNAME", organization); + xml.optElement("ORGUNIT", organizationUnit); xml.closeElement("ORG"); } for (Entry entry : otherSimpleFields.entrySet()) { - xml.element(entry.getKey(), entry.getValue()); + xml.optElement(entry.getKey(), entry.getValue()); } for (Entry entry : otherUnescapableFields.entrySet()) { + final String value = entry.getValue(); + if (value == null) { + continue; + } xml.openElement(entry.getKey()); - xml.append(entry.getValue()); + xml.append(value); xml.closeElement(entry.getKey()); } if (photoBinval != null) { @@ -607,24 +611,36 @@ public class VCard extends IQ { xml.closeElement("EMAIL"); } for (Entry phone : workPhones.entrySet()) { + final String number = phone.getValue(); + if (number == null) { + continue; + } xml.openElement("TEL"); xml.emptyElement("WORK"); xml.emptyElement(phone.getKey()); - xml.element("NUMBER", phone.getValue()); + xml.element("NUMBER", number); xml.closeElement("TEL"); } for (Entry phone : homePhones.entrySet()) { + final String number = phone.getValue(); + if (number == null) { + continue; + } xml.openElement("TEL"); xml.emptyElement("HOME"); xml.emptyElement(phone.getKey()); - xml.element("NUMBER", phone.getValue()); + xml.element("NUMBER", number); xml.closeElement("TEL"); } if (!workAddr.isEmpty()) { xml.openElement("ADR"); xml.emptyElement("WORK"); for (Entry entry : workAddr.entrySet()) { - xml.element(entry.getKey(), entry.getValue()); + final String value = entry.getValue(); + if (value == null) { + continue; + } + xml.element(entry.getKey(), value); } xml.closeElement("ADR"); } @@ -632,7 +648,11 @@ public class VCard extends IQ { xml.openElement("ADR"); xml.emptyElement("HOME"); for (Entry entry : homeAddr.entrySet()) { - xml.element(entry.getKey(), entry.getValue()); + final String value = entry.getValue(); + if (value == null) { + continue; + } + xml.element(entry.getKey(), value); } xml.closeElement("ADR"); }