1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-26 20:47:58 +01:00

Add utility methods

ParserUtils: getShortAttribute
XmlStringBuilder: attribute(String, long), optAttribute(String, Long)
This commit is contained in:
Paul Schaub 2018-04-30 13:54:52 +02:00 committed by Florian Schmaus
parent 84f282befe
commit 27c77fcb1c
2 changed files with 28 additions and 0 deletions

View file

@ -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);

View file

@ -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)}