Get rid of XmlPullUtil.jumpToStartTag()

This commit is contained in:
Andreas Schildbach 2013-12-18 21:13:46 +01:00
parent 33bacbd588
commit 067fb7752c
3 changed files with 375 additions and 464 deletions

View file

@ -958,7 +958,9 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider
pp.setInput(is, null);
final ResultHeader header = enterItdRequest(pp);
if (!XmlPullUtil.jumpToStartTag(pp, null, "itdOdv") || !"dm".equals(pp.getAttributeValue(null, "usage")))
XmlPullUtil.enter(pp, "itdDepartureMonitorRequest");
if (!XmlPullUtil.test(pp, "itdOdv") || !"dm".equals(pp.getAttributeValue(null, "usage")))
throw new IllegalStateException("cannot find <itdOdv usage=\"dm\" />");
XmlPullUtil.enter(pp, "itdOdv");
@ -972,9 +974,11 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider
final Location ownLocation = processOdvNameElem(pp, place);
final Location ownStation = ownLocation.type == LocationType.STATION ? ownLocation : null;
XmlPullUtil.exit(pp, "itdOdvName");
final List<Location> stations = new ArrayList<Location>();
if (XmlPullUtil.jumpToStartTag(pp, null, "itdOdvAssignedStops"))
if (XmlPullUtil.test(pp, "itdOdvAssignedStops"))
{
XmlPullUtil.enter(pp, "itdOdvAssignedStops");
while (XmlPullUtil.test(pp, "itdOdvAssignedStop"))
@ -987,6 +991,8 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider
XmlPullUtil.exit(pp, "itdOdvAssignedStops");
}
XmlPullUtil.exit(pp, "itdOdv");
if (ownStation != null && !stations.contains(ownStation))
stations.add(ownStation);
@ -2409,18 +2415,22 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider
XmlPullUtil.next(pp);
}
XmlPullUtil.exit(pp, "itdDateTime");
XmlPullUtil.exit(pp, "itdTripDateTime");
XmlPullUtil.enter(pp, "itdTripOptions");
XmlPullUtil.exit(pp, "itdTripOptions");
XmlPullUtil.enter(pp, "itdItinerary");
final Calendar time = new GregorianCalendar(timeZone());
final List<Trip> trips = new ArrayList<Trip>();
if (XmlPullUtil.jumpToStartTag(pp, null, "itdRouteList"))
{
XmlPullUtil.enter(pp, "itdRouteList");
while (XmlPullUtil.test(pp, "itdRoute"))
{
final String id = useRouteIndexAsTripId ? pp.getAttributeValue(null, "routeIndex") + "-"
+ pp.getAttributeValue(null, "routeTripIndex") : null;
final String id = useRouteIndexAsTripId ? pp.getAttributeValue(null, "routeIndex") + "-" + pp.getAttributeValue(null, "routeTripIndex")
: null;
final int numChanges = XmlPullUtil.intAttr(pp, "changes");
XmlPullUtil.enter(pp, "itdRoute");
@ -2535,8 +2545,7 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider
{
final String destinationName = normalizeLocationName(pp.getAttributeValue(null, "destination"));
final String destinationIdStr = pp.getAttributeValue(null, "destID");
final int destinationId = (destinationIdStr != null && destinationIdStr.length() > 0) ? Integer.parseInt(destinationIdStr)
: 0;
final int destinationId = (destinationIdStr != null && destinationIdStr.length() > 0) ? Integer.parseInt(destinationIdStr) : 0;
final Location destination = new Location(destinationId > 0 ? LocationType.STATION : LocationType.ANY,
destinationId > 0 ? destinationId : 0, null, destinationName);
final String lineLabel;
@ -2813,14 +2822,10 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider
}
XmlPullUtil.exit(pp, "itdRouteList");
XmlPullUtil.exit(pp, "itdItinerary");
return new QueryTripsResult(header, uri, from, via, to, new Context(commandLink((String) context, requestId)), trips);
}
else
{
return new QueryTripsResult(header, QueryTripsResult.Status.NO_TRIPS);
}
}
private QueryTripsResult queryTripsMobile(final String uri, final Location from, final Location via, final Location to, final InputStream is)
throws XmlPullParserException, IOException

View file

@ -318,64 +318,6 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider
return new Position(platformText);
}
public List<Location> xmlLocValReq(final CharSequence constraint) throws IOException
{
final String locValReq = "<LocValReq id=\"req\" maxNr=\"20\"><ReqLoc match=\"" + constraint + "\" type=\"ALLTYPE\"/></LocValReq>";
final String request = wrapReqC(locValReq, null);
// System.out.println(ParserUtils.scrape(apiUri, true, request, null, false));
Reader reader = null;
try
{
reader = new InputStreamReader(ParserUtils.scrapeInputStream(queryEndpoint, request, null, null, null, 3), ISO_8859_1);
final List<Location> results = new ArrayList<Location>();
final XmlPullParserFactory factory = XmlPullParserFactory.newInstance(System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null);
final XmlPullParser pp = factory.newPullParser();
pp.setInput(reader);
assertResC(pp);
XmlPullUtil.enter(pp, "ResC");
XmlPullUtil.require(pp, "LocValRes");
XmlPullUtil.requireAttr(pp, "id", "req");
XmlPullUtil.enter(pp, "LocalValRes");
while (pp.getEventType() == XmlPullParser.START_TAG)
{
final String tag = pp.getName();
if ("Station".equals(tag))
results.add(parseStation(pp));
else if ("Poi".equals(tag))
results.add(parsePoi(pp));
else if ("Address".equals(tag))
results.add(parseAddress(pp));
else if ("ReqLoc".equals(tag))
/* results.add(parseReqLoc(pp)) */;
else
System.out.println("cannot handle tag: " + tag);
XmlPullUtil.next(pp);
}
XmlPullUtil.exit(pp, "LocalValRes");
return results;
}
catch (final XmlPullParserException x)
{
throw new RuntimeException(x);
}
finally
{
if (reader != null)
reader.close();
}
}
protected StringBuilder jsonGetStopsParameters(final CharSequence constraint)
{
final StringBuilder parameters = new StringBuilder();
@ -542,7 +484,7 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider
final String mlcReq = "<MLcReq><MLc n=\"" + constraint + "?\" t=\"ALLTYPE\" /></MLcReq>";
final String request = wrapReqC(mlcReq, xmlMlcResEncoding);
// ParserUtils.printXml(ParserUtils.scrape(apiUri, request, xmlMlcResEncoding, null));
// ParserUtils.printXml(ParserUtils.scrape(queryEndpoint, request, xmlMlcResEncoding, null));
Reader reader = null;
@ -556,7 +498,6 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider
final List<Location> results = new ArrayList<Location>();
assertResC(pp);
XmlPullUtil.enter(pp, "ResC");
XmlPullUtil.enter(pp, "MLcRes");
@ -1068,7 +1009,7 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider
final XmlPullParser pp = factory.newPullParser();
pp.setInput(reader);
assertResC(pp);
XmlPullUtil.require(pp, "ResC");
final String product = XmlPullUtil.attr(pp, "prod").split(" ")[0];
final ResultHeader header = new ResultHeader(SERVER_PRODUCT, product, 0, null);
XmlPullUtil.enter(pp, "ResC");
@ -3059,10 +3000,4 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider
return new Line(null, lineStr, lineStyle(null, lineStr), attrSet, comment);
}
}
private void assertResC(final XmlPullParser pp) throws XmlPullParserException, IOException
{
if (!XmlPullUtil.jumpToStartTag(pp, null, "ResC"))
throw new IOException("cannot find <ResC />");
}
}

View file

@ -15,27 +15,23 @@ import org.xmlpull.v1.XmlPullParserException;
*/
public final class XmlPullUtil
{
public static boolean test(final XmlPullParser pp, final String tagName) throws XmlPullParserException, IOException
{
if (pp.getEventType() == XmlPullParser.TEXT && pp.isWhitespace())
pp.next();
skipWhitespace(pp);
return pp.getEventType() == XmlPullParser.START_TAG && pp.getName().equals(tagName);
}
public static void require(final XmlPullParser pp, final String tagName) throws XmlPullParserException, IOException
{
if (pp.getEventType() == XmlPullParser.TEXT && pp.isWhitespace())
pp.next();
skipWhitespace(pp);
pp.require(XmlPullParser.START_TAG, null, tagName);
}
public static void enter(final XmlPullParser pp) throws XmlPullParserException, IOException
{
if (pp.getEventType() == XmlPullParser.TEXT && pp.isWhitespace())
pp.next();
skipWhitespace(pp);
if (pp.getEventType() != XmlPullParser.START_TAG)
throw new IllegalStateException("expecting start tag to enter");
@ -47,8 +43,7 @@ public final class XmlPullUtil
public static void enter(final XmlPullParser pp, final String tagName) throws XmlPullParserException, IOException
{
if (pp.getEventType() == XmlPullParser.TEXT && pp.isWhitespace())
pp.next();
skipWhitespace(pp);
pp.require(XmlPullParser.START_TAG, null, tagName);
enter(pp);
@ -85,6 +80,14 @@ public final class XmlPullUtil
}
}
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);
@ -227,36 +230,4 @@ public final class XmlPullUtil
return text;
}
/**
* This method bypasses all events until it finds a start tag that has passed in namespace (if not null) and
* namespace (if not null).
*
* @return true if such START_TAG was found or false otherwise (and parser is on END_DOCUMENT).
*/
public static boolean jumpToStartTag(final XmlPullParser pp, final String tagNamespace, final String tagName) throws XmlPullParserException,
IOException
{
if (tagNamespace == null && tagName == null)
throw new IllegalArgumentException("namespace and name argument can not be both null:" + pp.getPositionDescription());
while (true)
{
final int eventType = pp.next();
if (eventType == XmlPullParser.START_TAG)
{
final String name = pp.getName();
final String namespace = pp.getNamespace();
boolean matches = (tagNamespace != null && tagNamespace.equals(namespace)) || (tagName != null && tagName.equals(name));
if (matches)
return true;
}
else if (eventType == XmlPullParser.END_DOCUMENT)
{
return false;
}
}
}
}