Consolidate XmlPullUtil API for pure value tags.

This commit is contained in:
Andreas Schildbach 2014-07-04 11:19:37 +02:00
parent 7f9755d896
commit 0dc08c103a
3 changed files with 114 additions and 183 deletions

View file

@ -174,20 +174,34 @@ public final class XmlPullUtil
throw new IllegalStateException("cannot find " + attrName + "=\"" + requiredValue + "\" />");
}
public static String text(final XmlPullParser pp) throws XmlPullParserException, IOException
public static String valueTag(final XmlPullParser pp, final String tagName) throws XmlPullParserException, IOException
{
if (pp.getEventType() != XmlPullParser.START_TAG || pp.isEmptyElementTag())
throw new IllegalStateException("expecting start tag to get text from");
XmlPullUtil.enter(pp, tagName);
final String value = pp.getText();
XmlPullUtil.exit(pp, tagName);
enter(pp);
return value != null ? value.trim() : null;
}
String text = "";
if (pp.getEventType() == XmlPullParser.TEXT)
text = pp.getText();
exit(pp);
return text;
public static String optValueTag(final XmlPullParser pp, final String tagName, final String defaultValue) throws XmlPullParserException,
IOException
{
if (XmlPullUtil.test(pp, tagName))
{
if (!pp.isEmptyElementTag())
{
return valueTag(pp, tagName);
}
else
{
pp.next();
return defaultValue;
}
}
else
{
return defaultValue;
}
}
/**