From 27c77fcb1cb90716a0850a97488873d7e0a42de4 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 30 Apr 2018 13:54:52 +0200 Subject: [PATCH] Add utility methods ParserUtils: getShortAttribute XmlStringBuilder: attribute(String, long), optAttribute(String, Long) --- .../org/jivesoftware/smack/util/ParserUtils.java | 16 ++++++++++++++++ .../smack/util/XmlStringBuilder.java | 12 ++++++++++++ 2 files changed, 28 insertions(+) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/ParserUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/ParserUtils.java index 28440bdbd..472ca1ddb 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/ParserUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/ParserUtils.java @@ -224,6 +224,22 @@ public class ParserUtils { } } + public static Short getShortAttribute(XmlPullParser parser, String name) { + String valueString = parser.getAttributeValue("", name); + if (valueString == null) { + return null; + } + return Short.valueOf(valueString); + } + + public static short getShortAttribute(XmlPullParser parser, String name, short defaultValue) { + Short s = getShortAttribute(parser, name); + if (s == null) { + return defaultValue; + } + return s; + } + public static Date getDateFromNextText(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException { String dateString = parser.nextText(); return XmppDateTime.parseDate(dateString); 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 0fdaceeb8..3d2bc0ed6 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 @@ -274,6 +274,11 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element { return attribute(name, String.valueOf(value)); } + public XmlStringBuilder attribute(String name, long value) { + assert name != null; + return attribute(name, String.valueOf(value)); + } + public XmlStringBuilder optAttribute(String name, String value) { if (value != null) { attribute(name, value); @@ -281,6 +286,13 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element { return this; } + public XmlStringBuilder optAttribute(String name, Long value) { + if (value != null) { + attribute(name, value); + } + return this; + } + /** * Add a new attribute to this builder, with the {@link java.util.Date} instance as its value, * which will get formatted with {@link XmppDateTime#formatXEP0082Date(Date)}