From 5272680d477937b96c11353e320847a8fe8dc6c7 Mon Sep 17 00:00:00 2001 From: Vyacheslav Blinov Date: Mon, 30 Mar 2015 19:24:56 +0300 Subject: [PATCH] Add XmlStringBuilder API for Date Fixes XEP-0080 GeoLoc timestamp element formating --- .../smack/util/XmlStringBuilder.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java b/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java index 2787a37e6..49d1a43a5 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java @@ -17,10 +17,12 @@ package org.jivesoftware.smack.util; import java.util.Collection; +import java.util.Date; import org.jivesoftware.smack.packet.Element; import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.packet.ExtensionElement; +import org.jxmpp.util.XmppDateTime; public class XmlStringBuilder implements Appendable, CharSequence { public static final String RIGHT_ANGLE_BRACKET = Character.toString('>'); @@ -65,6 +67,19 @@ public class XmlStringBuilder implements Appendable, CharSequence { } /** + * Add a new element to this builder, with the {@link java.util.Date} instance as its content, + * which will get formated with {@link XmppDateTime#formatXEP0082Date(Date)}. + * + * @param name element name + * @param content content of element + * @return this XmlStringBuilder + */ + public XmlStringBuilder element(String name, Date content) { + assert content != null; + return element(name, XmppDateTime.formatXEP0082Date(content)); + } + + /** * Add a new element to this builder. * * @param name @@ -93,6 +108,22 @@ public class XmlStringBuilder implements Appendable, CharSequence { return this; } + /** + * Add a new element to this builder, with the {@link java.util.Date} instance as its content, + * which will get formated with {@link XmppDateTime#formatXEP0082Date(Date)} + * if {@link java.util.Date} instance is not null. + * + * @param name element name + * @param content content of element + * @return this XmlStringBuilder + */ + public XmlStringBuilder optElement(String name, Date content) { + if (content != null) { + element(name, content); + } + return this; + } + public XmlStringBuilder optElement(String name, CharSequence content) { if (content != null) { element(name, content.toString()); @@ -171,6 +202,7 @@ public class XmlStringBuilder implements Appendable, CharSequence { /** * Add a right angle bracket '>'. + * * @return a reference to this object * @deprecated use {@link #rightAngleBracket()} instead */ @@ -194,6 +226,19 @@ public class XmlStringBuilder implements Appendable, CharSequence { return this; } + /** + * Add a new attribute to this builder, with the {@link java.util.Date} instance as its value, + * which will get formated with {@link XmppDateTime#formatXEP0082Date(Date)}. + * + * @param name name of attribute + * @param value value of attribute + * @return this XmlStringBuilder + */ + public XmlStringBuilder attribute(String name, Date value) { + assert value != null; + return attribute(name, XmppDateTime.formatXEP0082Date(value)); + } + public XmlStringBuilder attribute(String name, CharSequence value) { return attribute(name, value.toString()); } @@ -216,6 +261,22 @@ public class XmlStringBuilder implements Appendable, CharSequence { return this; } + /** + * Add a new attribute to this builder, with the {@link java.util.Date} instance as its value, + * which will get formated with {@link XmppDateTime#formatXEP0082Date(Date)} + * if {@link java.util.Date} instance is not null. + * + * @param name attribute name + * @param value value of this attribute + * @return this XmlStringBuilder + */ + public XmlStringBuilder optAttribute(String name, Date value) { + if (value != null) { + attribute(name, value); + } + return this; + } + public XmlStringBuilder optAttribute(String name, CharSequence value) { if (value != null) { attribute(name, value.toString());