1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-12 19:02:06 +01:00

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.
This commit is contained in:
Florian Schmaus 2015-02-26 08:55:53 +01:00
parent 49e1c837b2
commit 2250ac20ed

View file

@ -565,23 +565,27 @@ public class VCard extends IQ {
xml.rightAngleBracket(); xml.rightAngleBracket();
if (hasNameField()) { if (hasNameField()) {
xml.openElement("N"); xml.openElement("N");
xml.element("FAMILY", lastName); xml.optElement("FAMILY", lastName);
xml.element("GIVEN", firstName); xml.optElement("GIVEN", firstName);
xml.element("MIDDLE", middleName); xml.optElement("MIDDLE", middleName);
xml.closeElement("N"); xml.closeElement("N");
} }
if (hasOrganizationFields()) { if (hasOrganizationFields()) {
xml.openElement("ORG"); xml.openElement("ORG");
xml.element("ORGNAME", organization); xml.optElement("ORGNAME", organization);
xml.element("ORGUNIT", organizationUnit); xml.optElement("ORGUNIT", organizationUnit);
xml.closeElement("ORG"); xml.closeElement("ORG");
} }
for (Entry<String, String> entry : otherSimpleFields.entrySet()) { for (Entry<String, String> entry : otherSimpleFields.entrySet()) {
xml.element(entry.getKey(), entry.getValue()); xml.optElement(entry.getKey(), entry.getValue());
} }
for (Entry<String, String> entry : otherUnescapableFields.entrySet()) { for (Entry<String, String> entry : otherUnescapableFields.entrySet()) {
final String value = entry.getValue();
if (value == null) {
continue;
}
xml.openElement(entry.getKey()); xml.openElement(entry.getKey());
xml.append(entry.getValue()); xml.append(value);
xml.closeElement(entry.getKey()); xml.closeElement(entry.getKey());
} }
if (photoBinval != null) { if (photoBinval != null) {
@ -607,24 +611,36 @@ public class VCard extends IQ {
xml.closeElement("EMAIL"); xml.closeElement("EMAIL");
} }
for (Entry<String, String> phone : workPhones.entrySet()) { for (Entry<String, String> phone : workPhones.entrySet()) {
final String number = phone.getValue();
if (number == null) {
continue;
}
xml.openElement("TEL"); xml.openElement("TEL");
xml.emptyElement("WORK"); xml.emptyElement("WORK");
xml.emptyElement(phone.getKey()); xml.emptyElement(phone.getKey());
xml.element("NUMBER", phone.getValue()); xml.element("NUMBER", number);
xml.closeElement("TEL"); xml.closeElement("TEL");
} }
for (Entry<String, String> phone : homePhones.entrySet()) { for (Entry<String, String> phone : homePhones.entrySet()) {
final String number = phone.getValue();
if (number == null) {
continue;
}
xml.openElement("TEL"); xml.openElement("TEL");
xml.emptyElement("HOME"); xml.emptyElement("HOME");
xml.emptyElement(phone.getKey()); xml.emptyElement(phone.getKey());
xml.element("NUMBER", phone.getValue()); xml.element("NUMBER", number);
xml.closeElement("TEL"); xml.closeElement("TEL");
} }
if (!workAddr.isEmpty()) { if (!workAddr.isEmpty()) {
xml.openElement("ADR"); xml.openElement("ADR");
xml.emptyElement("WORK"); xml.emptyElement("WORK");
for (Entry<String, String> entry : workAddr.entrySet()) { for (Entry<String, String> 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"); xml.closeElement("ADR");
} }
@ -632,7 +648,11 @@ public class VCard extends IQ {
xml.openElement("ADR"); xml.openElement("ADR");
xml.emptyElement("HOME"); xml.emptyElement("HOME");
for (Entry<String, String> entry : homeAddr.entrySet()) { for (Entry<String, String> 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"); xml.closeElement("ADR");
} }