mirror of
https://gitlab.com/oeffi/public-transport-enabler.git
synced 2025-07-19 00:39:58 +00:00
Switch to modern code format. Organize imports. No functional changes.
Java sources use Eclipse 4.5 'Java Conventions' with indentation changed to 4 spaces only per level.
This commit is contained in:
parent
db59817a05
commit
931dafb628
191 changed files with 24827 additions and 27703 deletions
|
@ -15,262 +15,229 @@ import org.xmlpull.v1.XmlPullParserException;
|
|||
/**
|
||||
* Handy functions that combines XmlPull API into higher level functionality.
|
||||
*/
|
||||
public final class XmlPullUtil
|
||||
{
|
||||
public static boolean test(final XmlPullParser pp, final String tagName) throws XmlPullParserException, IOException
|
||||
{
|
||||
skipWhitespace(pp);
|
||||
public final class XmlPullUtil {
|
||||
public static boolean test(final XmlPullParser pp, final String tagName)
|
||||
throws XmlPullParserException, IOException {
|
||||
skipWhitespace(pp);
|
||||
|
||||
return pp.getEventType() == XmlPullParser.START_TAG && pp.getName().equals(tagName);
|
||||
}
|
||||
return pp.getEventType() == XmlPullParser.START_TAG && pp.getName().equals(tagName);
|
||||
}
|
||||
|
||||
public static void require(final XmlPullParser pp, final String tagName) throws XmlPullParserException, IOException
|
||||
{
|
||||
skipWhitespace(pp);
|
||||
public static void require(final XmlPullParser pp, final String tagName)
|
||||
throws XmlPullParserException, IOException {
|
||||
skipWhitespace(pp);
|
||||
|
||||
pp.require(XmlPullParser.START_TAG, null, tagName);
|
||||
}
|
||||
pp.require(XmlPullParser.START_TAG, null, tagName);
|
||||
}
|
||||
|
||||
public static void enter(final XmlPullParser pp) throws XmlPullParserException, IOException
|
||||
{
|
||||
skipWhitespace(pp);
|
||||
public static void enter(final XmlPullParser pp) throws XmlPullParserException, IOException {
|
||||
skipWhitespace(pp);
|
||||
|
||||
if (pp.getEventType() != XmlPullParser.START_TAG)
|
||||
throw new IllegalStateException("expecting start tag to enter");
|
||||
if (pp.isEmptyElementTag())
|
||||
throw new IllegalStateException("cannot enter empty tag");
|
||||
if (pp.getEventType() != XmlPullParser.START_TAG)
|
||||
throw new IllegalStateException("expecting start tag to enter");
|
||||
if (pp.isEmptyElementTag())
|
||||
throw new IllegalStateException("cannot enter empty tag");
|
||||
|
||||
pp.next();
|
||||
}
|
||||
pp.next();
|
||||
}
|
||||
|
||||
public static void enter(final XmlPullParser pp, final String tagName) throws XmlPullParserException, IOException
|
||||
{
|
||||
skipWhitespace(pp);
|
||||
public static void enter(final XmlPullParser pp, final String tagName) throws XmlPullParserException, IOException {
|
||||
skipWhitespace(pp);
|
||||
|
||||
pp.require(XmlPullParser.START_TAG, null, tagName);
|
||||
enter(pp);
|
||||
}
|
||||
pp.require(XmlPullParser.START_TAG, null, tagName);
|
||||
enter(pp);
|
||||
}
|
||||
|
||||
public static void skipExit(final XmlPullParser pp) throws XmlPullParserException, IOException
|
||||
{
|
||||
skipToEnd(pp);
|
||||
public static void skipExit(final XmlPullParser pp) throws XmlPullParserException, IOException {
|
||||
skipToEnd(pp);
|
||||
|
||||
if (pp.getEventType() != XmlPullParser.END_TAG)
|
||||
throw new IllegalStateException("expecting end tag to exit");
|
||||
if (pp.getEventType() != XmlPullParser.END_TAG)
|
||||
throw new IllegalStateException("expecting end tag to exit");
|
||||
|
||||
pp.next();
|
||||
}
|
||||
pp.next();
|
||||
}
|
||||
|
||||
public static void skipExit(final XmlPullParser pp, final String tagName) throws XmlPullParserException, IOException
|
||||
{
|
||||
skipToEnd(pp);
|
||||
public static void skipExit(final XmlPullParser pp, final String tagName)
|
||||
throws XmlPullParserException, IOException {
|
||||
skipToEnd(pp);
|
||||
|
||||
pp.require(XmlPullParser.END_TAG, null, tagName);
|
||||
pp.next();
|
||||
}
|
||||
pp.require(XmlPullParser.END_TAG, null, tagName);
|
||||
pp.next();
|
||||
}
|
||||
|
||||
public static void exit(final XmlPullParser pp, final String tagName) throws XmlPullParserException, IOException
|
||||
{
|
||||
pp.require(XmlPullParser.END_TAG, null, tagName);
|
||||
pp.next();
|
||||
}
|
||||
public static void exit(final XmlPullParser pp, final String tagName) throws XmlPullParserException, IOException {
|
||||
pp.require(XmlPullParser.END_TAG, null, tagName);
|
||||
pp.next();
|
||||
}
|
||||
|
||||
private static void skipToEnd(final XmlPullParser pp) throws XmlPullParserException, IOException
|
||||
{
|
||||
while (pp.getEventType() != XmlPullParser.END_TAG)
|
||||
{
|
||||
if (pp.getEventType() == XmlPullParser.START_TAG)
|
||||
next(pp);
|
||||
else if (pp.getEventType() == XmlPullParser.TEXT)
|
||||
pp.next();
|
||||
else
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
private static void skipToEnd(final XmlPullParser pp) throws XmlPullParserException, IOException {
|
||||
while (pp.getEventType() != XmlPullParser.END_TAG) {
|
||||
if (pp.getEventType() == XmlPullParser.START_TAG)
|
||||
next(pp);
|
||||
else if (pp.getEventType() == XmlPullParser.TEXT)
|
||||
pp.next();
|
||||
else
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
|
||||
private static void skipWhitespace(final XmlPullParser pp) throws XmlPullParserException, IOException
|
||||
{
|
||||
if (pp.getEventType() == XmlPullParser.START_DOCUMENT)
|
||||
pp.next();
|
||||
if (pp.getEventType() == XmlPullParser.TEXT && pp.isWhitespace())
|
||||
pp.next();
|
||||
}
|
||||
private static void skipWhitespace(final XmlPullParser pp) throws XmlPullParserException, IOException {
|
||||
if (pp.getEventType() == XmlPullParser.START_DOCUMENT)
|
||||
pp.next();
|
||||
if (pp.getEventType() == XmlPullParser.TEXT && pp.isWhitespace())
|
||||
pp.next();
|
||||
}
|
||||
|
||||
public static void requireSkip(final XmlPullParser pp, final String tagName) throws XmlPullParserException, IOException
|
||||
{
|
||||
require(pp, tagName);
|
||||
public static void requireSkip(final XmlPullParser pp, final String tagName)
|
||||
throws XmlPullParserException, IOException {
|
||||
require(pp, tagName);
|
||||
|
||||
if (!pp.isEmptyElementTag())
|
||||
{
|
||||
enter(pp, tagName);
|
||||
skipExit(pp, tagName);
|
||||
}
|
||||
else
|
||||
{
|
||||
next(pp);
|
||||
}
|
||||
}
|
||||
if (!pp.isEmptyElementTag()) {
|
||||
enter(pp, tagName);
|
||||
skipExit(pp, tagName);
|
||||
} else {
|
||||
next(pp);
|
||||
}
|
||||
}
|
||||
|
||||
public static void optSkip(final XmlPullParser pp, final String tagName) throws XmlPullParserException, IOException
|
||||
{
|
||||
if (test(pp, tagName))
|
||||
requireSkip(pp, tagName);
|
||||
}
|
||||
public static void optSkip(final XmlPullParser pp, final String tagName)
|
||||
throws XmlPullParserException, IOException {
|
||||
if (test(pp, tagName))
|
||||
requireSkip(pp, tagName);
|
||||
}
|
||||
|
||||
public static void next(final XmlPullParser pp) throws XmlPullParserException, IOException
|
||||
{
|
||||
skipSubTree(pp);
|
||||
pp.next();
|
||||
}
|
||||
public static void next(final XmlPullParser pp) throws XmlPullParserException, IOException {
|
||||
skipSubTree(pp);
|
||||
pp.next();
|
||||
}
|
||||
|
||||
public static String attr(final XmlPullParser pp, final String attrName)
|
||||
{
|
||||
final String attr = optAttr(pp, attrName, null);
|
||||
public static String attr(final XmlPullParser pp, final String attrName) {
|
||||
final String attr = optAttr(pp, attrName, null);
|
||||
|
||||
if (attr != null)
|
||||
return attr;
|
||||
else
|
||||
throw new IllegalStateException("expecting attribute: " + attrName);
|
||||
}
|
||||
if (attr != null)
|
||||
return attr;
|
||||
else
|
||||
throw new IllegalStateException("expecting attribute: " + attrName);
|
||||
}
|
||||
|
||||
public static String optAttr(final XmlPullParser pp, final String attrName, final @Nullable String defaultValue)
|
||||
{
|
||||
final String attr = pp.getAttributeValue(null, attrName);
|
||||
public static String optAttr(final XmlPullParser pp, final String attrName, final @Nullable String defaultValue) {
|
||||
final String attr = pp.getAttributeValue(null, attrName);
|
||||
|
||||
if (attr != null)
|
||||
{
|
||||
final String trimmedAttr = attr.trim();
|
||||
if (attr != null) {
|
||||
final String trimmedAttr = attr.trim();
|
||||
|
||||
if (trimmedAttr.length() > 0)
|
||||
return trimmedAttr;
|
||||
}
|
||||
if (trimmedAttr.length() > 0)
|
||||
return trimmedAttr;
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public static int intAttr(final XmlPullParser pp, final String attrName)
|
||||
{
|
||||
return Integer.parseInt(attr(pp, attrName));
|
||||
}
|
||||
public static int intAttr(final XmlPullParser pp, final String attrName) {
|
||||
return Integer.parseInt(attr(pp, attrName));
|
||||
}
|
||||
|
||||
public static int optIntAttr(final XmlPullParser pp, final String attrName, final int defaultValue)
|
||||
{
|
||||
final String attr = optAttr(pp, attrName, null);
|
||||
if (attr != null)
|
||||
return Integer.parseInt(attr);
|
||||
else
|
||||
return defaultValue;
|
||||
}
|
||||
public static int optIntAttr(final XmlPullParser pp, final String attrName, final int defaultValue) {
|
||||
final String attr = optAttr(pp, attrName, null);
|
||||
if (attr != null)
|
||||
return Integer.parseInt(attr);
|
||||
else
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public static double floatAttr(final XmlPullParser pp, final String attrName)
|
||||
{
|
||||
return Double.parseDouble(attr(pp, attrName));
|
||||
}
|
||||
public static double floatAttr(final XmlPullParser pp, final String attrName) {
|
||||
return Double.parseDouble(attr(pp, attrName));
|
||||
}
|
||||
|
||||
public static double optFloatAttr(final XmlPullParser pp, final String attrName, final float defaultValue)
|
||||
{
|
||||
final String attr = optAttr(pp, attrName, null);
|
||||
if (attr != null)
|
||||
return Double.parseDouble(attr);
|
||||
else
|
||||
return defaultValue;
|
||||
}
|
||||
public static double optFloatAttr(final XmlPullParser pp, final String attrName, final float defaultValue) {
|
||||
final String attr = optAttr(pp, attrName, null);
|
||||
if (attr != null)
|
||||
return Double.parseDouble(attr);
|
||||
else
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public static void requireAttr(final XmlPullParser pp, final String attrName, final String requiredValue)
|
||||
{
|
||||
if (!requiredValue.equals(attr(pp, attrName)))
|
||||
throw new IllegalStateException("cannot find " + attrName + "=\"" + requiredValue + "\" />");
|
||||
}
|
||||
public static void requireAttr(final XmlPullParser pp, final String attrName, final String requiredValue) {
|
||||
if (!requiredValue.equals(attr(pp, attrName)))
|
||||
throw new IllegalStateException("cannot find " + attrName + "=\"" + requiredValue + "\" />");
|
||||
}
|
||||
|
||||
public static String valueTag(final XmlPullParser pp, final String tagName) throws XmlPullParserException, IOException
|
||||
{
|
||||
XmlPullUtil.enter(pp, tagName);
|
||||
final String value = pp.getText();
|
||||
XmlPullUtil.skipExit(pp, tagName);
|
||||
public static String valueTag(final XmlPullParser pp, final String tagName)
|
||||
throws XmlPullParserException, IOException {
|
||||
XmlPullUtil.enter(pp, tagName);
|
||||
final String value = pp.getText();
|
||||
XmlPullUtil.skipExit(pp, tagName);
|
||||
|
||||
return value != null ? value.trim() : null;
|
||||
}
|
||||
return value != null ? value.trim() : null;
|
||||
}
|
||||
|
||||
public static String optValueTag(final XmlPullParser pp, final String tagName, final @Nullable 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;
|
||||
}
|
||||
}
|
||||
public static String optValueTag(final XmlPullParser pp, final String tagName, final @Nullable 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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Skip sub tree that is currently porser positioned on. <br>
|
||||
* NOTE: parser must be on START_TAG and when funtion returns parser will be positioned on corresponding END_TAG
|
||||
*/
|
||||
public static void skipSubTree(final XmlPullParser pp) throws XmlPullParserException, IOException
|
||||
{
|
||||
pp.require(XmlPullParser.START_TAG, null, null);
|
||||
/**
|
||||
* Skip sub tree that is currently porser positioned on. <br>
|
||||
* NOTE: parser must be on START_TAG and when funtion returns parser will be positioned on corresponding
|
||||
* END_TAG
|
||||
*/
|
||||
public static void skipSubTree(final XmlPullParser pp) throws XmlPullParserException, IOException {
|
||||
pp.require(XmlPullParser.START_TAG, null, null);
|
||||
|
||||
int level = 1;
|
||||
while (level > 0)
|
||||
{
|
||||
final int eventType = pp.next();
|
||||
if (eventType == XmlPullParser.END_TAG)
|
||||
--level;
|
||||
else if (eventType == XmlPullParser.START_TAG)
|
||||
++level;
|
||||
}
|
||||
}
|
||||
int level = 1;
|
||||
while (level > 0) {
|
||||
final int eventType = pp.next();
|
||||
if (eventType == XmlPullParser.END_TAG)
|
||||
--level;
|
||||
else if (eventType == XmlPullParser.START_TAG)
|
||||
++level;
|
||||
}
|
||||
}
|
||||
|
||||
public static void skipUntil(final XmlPullParser pp, final String tagName) throws XmlPullParserException, IOException
|
||||
{
|
||||
while (!XmlPullUtil.test(pp, tagName))
|
||||
{
|
||||
if (!pp.isEmptyElementTag())
|
||||
{
|
||||
XmlPullUtil.enter(pp);
|
||||
XmlPullUtil.skipExit(pp);
|
||||
}
|
||||
else
|
||||
{
|
||||
XmlPullUtil.next(pp);
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void skipUntil(final XmlPullParser pp, final String tagName)
|
||||
throws XmlPullParserException, IOException {
|
||||
while (!XmlPullUtil.test(pp, tagName)) {
|
||||
if (!pp.isEmptyElementTag()) {
|
||||
XmlPullUtil.enter(pp);
|
||||
XmlPullUtil.skipExit(pp);
|
||||
} else {
|
||||
XmlPullUtil.next(pp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read text content of element ith given namespace and name (use null namespace do indicate that nemspace should
|
||||
* not be checked)
|
||||
*/
|
||||
/**
|
||||
* Read text content of element ith given namespace and name (use null namespace do indicate that nemspace
|
||||
* should not be checked)
|
||||
*/
|
||||
|
||||
public static String nextText(final XmlPullParser pp, final String namespace, final String name) throws IOException, XmlPullParserException
|
||||
{
|
||||
pp.require(XmlPullParser.START_TAG, namespace, name);
|
||||
final String text = pp.nextText();
|
||||
public static String nextText(final XmlPullParser pp, final String namespace, final String name)
|
||||
throws IOException, XmlPullParserException {
|
||||
pp.require(XmlPullParser.START_TAG, namespace, name);
|
||||
final String text = pp.nextText();
|
||||
|
||||
// work around http://code.google.com/p/android/issues/detail?id=21425
|
||||
if (pp.getEventType() != XmlPullParser.END_TAG)
|
||||
pp.nextTag();
|
||||
// work around http://code.google.com/p/android/issues/detail?id=21425
|
||||
if (pp.getEventType() != XmlPullParser.END_TAG)
|
||||
pp.nextTag();
|
||||
|
||||
pp.require(XmlPullParser.END_TAG, namespace, name);
|
||||
pp.require(XmlPullParser.END_TAG, namespace, name);
|
||||
|
||||
return text;
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
public static void requireEndDocument(final XmlPullParser pp) throws XmlPullParserException, IOException
|
||||
{
|
||||
skipWhitespace(pp);
|
||||
public static void requireEndDocument(final XmlPullParser pp) throws XmlPullParserException, IOException {
|
||||
skipWhitespace(pp);
|
||||
|
||||
if (pp.getEventType() != XmlPullParser.END_DOCUMENT)
|
||||
throw new IllegalStateException("expecting end of document: " + pp.getPositionDescription());
|
||||
}
|
||||
if (pp.getEventType() != XmlPullParser.END_DOCUMENT)
|
||||
throw new IllegalStateException("expecting end of document: " + pp.getPositionDescription());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue