Add XmlPullUtil.optEnter() and use it where possible.

This commit is contained in:
Andreas Schildbach 2017-03-23 16:51:50 +01:00
parent 12a2093912
commit 9398b454a5
2 changed files with 225 additions and 291 deletions

View file

@ -413,12 +413,8 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
final List<SuggestedLocation> locations = new ArrayList<>(); final List<SuggestedLocation> locations = new ArrayList<>();
XmlPullUtil.require(pp, "sf"); XmlPullUtil.require(pp, "sf");
if (!pp.isEmptyElementTag()) { if (XmlPullUtil.optEnter(pp, "sf")) {
XmlPullUtil.enter(pp, "sf"); while (XmlPullUtil.optEnter(pp, "p")) {
while (XmlPullUtil.test(pp, "p")) {
XmlPullUtil.enter(pp, "p");
final String name = normalizeLocationName(XmlPullUtil.valueTag(pp, "n")); final String name = normalizeLocationName(XmlPullUtil.valueTag(pp, "n"));
final String u = XmlPullUtil.valueTag(pp, "u"); final String u = XmlPullUtil.valueTag(pp, "u");
if (!"sf".equals(u)) if (!"sf".equals(u))
@ -462,8 +458,6 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
} }
XmlPullUtil.skipExit(pp, "sf"); XmlPullUtil.skipExit(pp, "sf");
} else {
XmlPullUtil.next(pp);
} }
result.set(new SuggestLocationsResult(header, locations)); result.set(new SuggestLocationsResult(header, locations));
@ -529,9 +523,7 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
final List<Location> locations = new ArrayList<>(); final List<Location> locations = new ArrayList<>();
if (XmlPullUtil.test(pp, "coordInfoItemList")) { if (XmlPullUtil.optEnter(pp, "coordInfoItemList")) {
XmlPullUtil.enter(pp, "coordInfoItemList");
while (XmlPullUtil.test(pp, "coordInfoItem")) { while (XmlPullUtil.test(pp, "coordInfoItem")) {
final String type = XmlPullUtil.attr(pp, "type"); final String type = XmlPullUtil.attr(pp, "type");
final LocationType locationType; final LocationType locationType;
@ -601,12 +593,8 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
final List<Location> stations = new ArrayList<>(); final List<Location> stations = new ArrayList<>();
if (XmlPullUtil.test(pp, "pis")) { if (XmlPullUtil.optEnter(pp, "pis")) {
XmlPullUtil.enter(pp, "pis"); while (XmlPullUtil.optEnter(pp, "pi")) {
while (XmlPullUtil.test(pp, "pi")) {
XmlPullUtil.enter(pp, "pi");
final String name = normalizeLocationName(XmlPullUtil.optValueTag(pp, "de", null)); final String name = normalizeLocationName(XmlPullUtil.optValueTag(pp, "de", null));
final String type = XmlPullUtil.valueTag(pp, "ty"); final String type = XmlPullUtil.valueTag(pp, "ty");
final LocationType locationType; final LocationType locationType;
@ -714,9 +702,7 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
XmlPullUtil.optSkip(pp, "itdPoiHierarchyRoot"); XmlPullUtil.optSkip(pp, "itdPoiHierarchyRoot");
if (XmlPullUtil.test(pp, "itdOdvAssignedStops")) { if (XmlPullUtil.optEnter(pp, "itdOdvAssignedStops")) {
XmlPullUtil.enter(pp, "itdOdvAssignedStops");
while (XmlPullUtil.test(pp, "itdOdvAssignedStop")) { while (XmlPullUtil.test(pp, "itdOdvAssignedStop")) {
final Location stop = processItdOdvAssignedStop(pp); final Location stop = processItdOdvAssignedStop(pp);
@ -1540,8 +1526,7 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
} }
XmlPullUtil.require(pp, "itdDepartureList"); XmlPullUtil.require(pp, "itdDepartureList");
if (!pp.isEmptyElementTag()) { if (XmlPullUtil.optEnter(pp, "itdDepartureList")) {
XmlPullUtil.enter(pp, "itdDepartureList");
final Calendar plannedDepartureTime = new GregorianCalendar(timeZone); final Calendar plannedDepartureTime = new GregorianCalendar(timeZone);
final Calendar predictedDepartureTime = new GregorianCalendar(timeZone); final Calendar predictedDepartureTime = new GregorianCalendar(timeZone);
@ -1594,8 +1579,6 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
} }
XmlPullUtil.skipExit(pp, "itdDepartureList"); XmlPullUtil.skipExit(pp, "itdDepartureList");
} else {
XmlPullUtil.next(pp);
} }
result.set(r); result.set(r);
@ -1630,15 +1613,11 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
final QueryDeparturesResult r = new QueryDeparturesResult(header); final QueryDeparturesResult r = new QueryDeparturesResult(header);
XmlPullUtil.require(pp, "dps"); XmlPullUtil.require(pp, "dps");
if (!pp.isEmptyElementTag()) { if (XmlPullUtil.optEnter(pp, "dps")) {
XmlPullUtil.enter(pp, "dps");
final Calendar plannedDepartureTime = new GregorianCalendar(timeZone); final Calendar plannedDepartureTime = new GregorianCalendar(timeZone);
final Calendar predictedDepartureTime = new GregorianCalendar(timeZone); final Calendar predictedDepartureTime = new GregorianCalendar(timeZone);
while (XmlPullUtil.test(pp, "dp")) { while (XmlPullUtil.optEnter(pp, "dp")) {
XmlPullUtil.enter(pp, "dp");
// misc // misc
/* final String stationName = */normalizeLocationName(XmlPullUtil.valueTag(pp, "n")); /* final String stationName = */normalizeLocationName(XmlPullUtil.valueTag(pp, "n"));
/* final boolean isRealtime = */XmlPullUtil.valueTag(pp, "realtime").equals("1"); /* final boolean isRealtime = */XmlPullUtil.valueTag(pp, "realtime").equals("1");
@ -2251,8 +2230,7 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
XmlPullUtil.enter(pp, "itdTripDateTime"); XmlPullUtil.enter(pp, "itdTripDateTime");
XmlPullUtil.enter(pp, "itdDateTime"); XmlPullUtil.enter(pp, "itdDateTime");
XmlPullUtil.require(pp, "itdDate"); XmlPullUtil.require(pp, "itdDate");
if (!pp.isEmptyElementTag()) { if (XmlPullUtil.optEnter(pp, "itdDate")) {
XmlPullUtil.enter(pp, "itdDate");
if (XmlPullUtil.test(pp, "itdMessage")) { if (XmlPullUtil.test(pp, "itdMessage")) {
final String message = XmlPullUtil.nextText(pp, null, "itdMessage"); final String message = XmlPullUtil.nextText(pp, null, "itdMessage");
@ -2262,8 +2240,6 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
throw new IllegalStateException("unknown message: " + message); throw new IllegalStateException("unknown message: " + message);
} }
XmlPullUtil.skipExit(pp, "itdDate"); XmlPullUtil.skipExit(pp, "itdDate");
} else {
XmlPullUtil.next(pp);
} }
XmlPullUtil.skipExit(pp, "itdDateTime"); XmlPullUtil.skipExit(pp, "itdDateTime");
XmlPullUtil.skipExit(pp, "itdTripDateTime"); XmlPullUtil.skipExit(pp, "itdTripDateTime");
@ -2274,14 +2250,10 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
final List<Trip> trips = new ArrayList<>(); final List<Trip> trips = new ArrayList<>();
XmlPullUtil.require(pp, "itdItinerary"); XmlPullUtil.require(pp, "itdItinerary");
if (!pp.isEmptyElementTag()) { if (XmlPullUtil.optEnter(pp, "itdItinerary")) {
XmlPullUtil.enter(pp, "itdItinerary");
XmlPullUtil.optSkip(pp, "itdLegTTs"); XmlPullUtil.optSkip(pp, "itdLegTTs");
if (XmlPullUtil.test(pp, "itdRouteList")) { if (XmlPullUtil.optEnter(pp, "itdRouteList")) {
XmlPullUtil.enter(pp, "itdRouteList");
final Calendar calendar = new GregorianCalendar(timeZone); final Calendar calendar = new GregorianCalendar(timeZone);
while (XmlPullUtil.test(pp, "itdRoute")) { while (XmlPullUtil.test(pp, "itdRoute")) {
@ -2397,53 +2369,44 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
XmlPullUtil.skipExit(pp, "itdPartialRouteList"); XmlPullUtil.skipExit(pp, "itdPartialRouteList");
final List<Fare> fares = new ArrayList<>(2); final List<Fare> fares = new ArrayList<>(2);
if (XmlPullUtil.test(pp, "itdFare")) { if (XmlPullUtil.optEnter(pp, "itdFare")) {
if (!pp.isEmptyElementTag()) { if (XmlPullUtil.test(pp, "itdSingleTicket")) {
XmlPullUtil.enter(pp, "itdFare"); final String net = XmlPullUtil.optAttr(pp, "net", null);
if (XmlPullUtil.test(pp, "itdSingleTicket")) { if (net != null) {
final String net = XmlPullUtil.optAttr(pp, "net", null); final Currency currency = parseCurrency(XmlPullUtil.attr(pp, "currency"));
if (net != null) { final String fareAdult = XmlPullUtil.optAttr(pp, "fareAdult", null);
final Currency currency = parseCurrency(XmlPullUtil.attr(pp, "currency")); final String fareChild = XmlPullUtil.optAttr(pp, "fareChild", null);
final String fareAdult = XmlPullUtil.optAttr(pp, "fareAdult", null); final String unitName = XmlPullUtil.optAttr(pp, "unitName", null);
final String fareChild = XmlPullUtil.optAttr(pp, "fareChild", null); final String unitsAdult = XmlPullUtil.optAttr(pp, "unitsAdult", null);
final String unitName = XmlPullUtil.optAttr(pp, "unitName", null); final String unitsChild = XmlPullUtil.optAttr(pp, "unitsChild", null);
final String unitsAdult = XmlPullUtil.optAttr(pp, "unitsAdult", null); final String levelAdult = XmlPullUtil.optAttr(pp, "levelAdult", null);
final String unitsChild = XmlPullUtil.optAttr(pp, "unitsChild", null); final String levelChild = XmlPullUtil.optAttr(pp, "levelChild", null);
final String levelAdult = XmlPullUtil.optAttr(pp, "levelAdult", null); if (fareAdult != null)
final String levelChild = XmlPullUtil.optAttr(pp, "levelChild", null); fares.add(new Fare(net.toUpperCase(), Type.ADULT, currency,
if (fareAdult != null) Float.parseFloat(fareAdult) * fareCorrectionFactor,
fares.add(new Fare(net.toUpperCase(), Type.ADULT, currency, levelAdult != null ? null : unitName,
Float.parseFloat(fareAdult) * fareCorrectionFactor, levelAdult != null ? levelAdult : unitsAdult));
levelAdult != null ? null : unitName, if (fareChild != null)
levelAdult != null ? levelAdult : unitsAdult)); fares.add(new Fare(net.toUpperCase(), Type.CHILD, currency,
if (fareChild != null) Float.parseFloat(fareChild) * fareCorrectionFactor,
fares.add(new Fare(net.toUpperCase(), Type.CHILD, currency, levelChild != null ? null : unitName,
Float.parseFloat(fareChild) * fareCorrectionFactor, levelChild != null ? levelChild : unitsChild));
levelChild != null ? null : unitName,
levelChild != null ? levelChild : unitsChild));
if (!pp.isEmptyElementTag()) { if (XmlPullUtil.optEnter(pp, "itdSingleTicket")) {
XmlPullUtil.enter(pp, "itdSingleTicket"); if (XmlPullUtil.optEnter(pp, "itdGenericTicketList")) {
if (XmlPullUtil.test(pp, "itdGenericTicketList")) { while (XmlPullUtil.test(pp, "itdGenericTicketGroup")) {
XmlPullUtil.enter(pp, "itdGenericTicketList"); final Fare fare = processItdGenericTicketGroup(pp, net.toUpperCase(),
while (XmlPullUtil.test(pp, "itdGenericTicketGroup")) { currency);
final Fare fare = processItdGenericTicketGroup(pp, net.toUpperCase(), if (fare != null)
currency); fares.add(fare);
if (fare != null)
fares.add(fare);
}
XmlPullUtil.skipExit(pp, "itdGenericTicketList");
} }
XmlPullUtil.skipExit(pp, "itdSingleTicket"); XmlPullUtil.skipExit(pp, "itdGenericTicketList");
} else {
XmlPullUtil.next(pp);
} }
XmlPullUtil.skipExit(pp, "itdSingleTicket");
} }
} }
XmlPullUtil.skipExit(pp, "itdFare");
} else {
XmlPullUtil.next(pp);
} }
XmlPullUtil.skipExit(pp, "itdFare");
} }
XmlPullUtil.skipExit(pp, "itdRoute"); XmlPullUtil.skipExit(pp, "itdRoute");
@ -2458,8 +2421,6 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
XmlPullUtil.skipExit(pp, "itdRouteList"); XmlPullUtil.skipExit(pp, "itdRouteList");
} }
XmlPullUtil.skipExit(pp, "itdItinerary"); XmlPullUtil.skipExit(pp, "itdItinerary");
} else {
XmlPullUtil.next(pp);
} }
return new QueryTripsResult(header, url.toString(), from, via, to, return new QueryTripsResult(header, url.toString(), from, via, to,
@ -2550,29 +2511,23 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
boolean lowFloorVehicle = false; boolean lowFloorVehicle = false;
String message = null; String message = null;
if (XmlPullUtil.test(pp, "itdInfoTextList")) { if (XmlPullUtil.optEnter(pp, "itdInfoTextList")) {
if (!pp.isEmptyElementTag()) { while (XmlPullUtil.test(pp, "infoTextListElem")) {
XmlPullUtil.enter(pp, "itdInfoTextList"); final String text = XmlPullUtil.valueTag(pp, "infoTextListElem");
while (XmlPullUtil.test(pp, "infoTextListElem")) { if (text != null) {
final String text = XmlPullUtil.valueTag(pp, "infoTextListElem"); final String lcText = text.toLowerCase();
if (text != null) { if (lcText.startsWith("niederflurwagen")) // KVV
final String lcText = text.toLowerCase(); lowFloorVehicle = true;
if (lcText.startsWith("niederflurwagen")) // KVV else if (lcText.contains("ruf") || lcText.contains("anmeld")) // Bedarfsverkehr
lowFloorVehicle = true; message = text;
else if (lcText.contains("ruf") || lcText.contains("anmeld")) // Bedarfsverkehr
message = text;
}
} }
XmlPullUtil.skipExit(pp, "itdInfoTextList");
} else {
XmlPullUtil.next(pp);
} }
XmlPullUtil.skipExit(pp, "itdInfoTextList");
} }
XmlPullUtil.optSkip(pp, "itdFootPathInfo"); XmlPullUtil.optSkip(pp, "itdFootPathInfo");
while (XmlPullUtil.test(pp, "infoLink")) { while (XmlPullUtil.optEnter(pp, "infoLink")) {
XmlPullUtil.enter(pp, "infoLink");
XmlPullUtil.optSkip(pp, "paramList"); XmlPullUtil.optSkip(pp, "paramList");
final String infoLinkText = XmlPullUtil.valueTag(pp, "infoLinkText"); final String infoLinkText = XmlPullUtil.valueTag(pp, "infoLinkText");
if (message == null) if (message == null)
@ -2581,8 +2536,7 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
} }
List<Stop> intermediateStops = null; List<Stop> intermediateStops = null;
if (XmlPullUtil.test(pp, "itdStopSeq")) { if (XmlPullUtil.optEnter(pp, "itdStopSeq")) {
XmlPullUtil.enter(pp, "itdStopSeq");
intermediateStops = new LinkedList<>(); intermediateStops = new LinkedList<>();
while (XmlPullUtil.test(pp, "itdPoint")) { while (XmlPullUtil.test(pp, "itdPoint")) {
final Location stopLocation = processItdPointAttributes(pp); final Location stopLocation = processItdPointAttributes(pp);
@ -2650,10 +2604,8 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
path = processItdPathCoordinates(pp); path = processItdPathCoordinates(pp);
boolean wheelChairAccess = false; boolean wheelChairAccess = false;
if (XmlPullUtil.test(pp, "genAttrList")) { if (XmlPullUtil.optEnter(pp, "genAttrList")) {
XmlPullUtil.enter(pp, "genAttrList"); while (XmlPullUtil.optEnter(pp, "genAttrElem")) {
while (XmlPullUtil.test(pp, "genAttrElem")) {
XmlPullUtil.enter(pp, "genAttrElem");
final String name = XmlPullUtil.valueTag(pp, "name"); final String name = XmlPullUtil.valueTag(pp, "name");
final String value = XmlPullUtil.valueTag(pp, "value"); final String value = XmlPullUtil.valueTag(pp, "value");
XmlPullUtil.skipExit(pp, "genAttrElem"); XmlPullUtil.skipExit(pp, "genAttrElem");
@ -2666,8 +2618,7 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
XmlPullUtil.skipExit(pp, "genAttrList"); XmlPullUtil.skipExit(pp, "genAttrList");
} }
if (XmlPullUtil.test(pp, "nextDeps")) { if (XmlPullUtil.optEnter(pp, "nextDeps")) {
XmlPullUtil.enter(pp, "nextDeps");
while (XmlPullUtil.test(pp, "itdDateTime")) { while (XmlPullUtil.test(pp, "itdDateTime")) {
processItdDateTime(pp, calendar); processItdDateTime(pp, calendar);
/* final Date nextDepartureTime = */calendar.getTime(); /* final Date nextDepartureTime = */calendar.getTime();
@ -2704,220 +2655,194 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
final List<Trip> trips = new ArrayList<>(); final List<Trip> trips = new ArrayList<>();
if (XmlPullUtil.test(pp, "ts")) { if (XmlPullUtil.optEnter(pp, "ts")) {
if (!pp.isEmptyElementTag()) { while (XmlPullUtil.optEnter(pp, "tp")) {
XmlPullUtil.enter(pp, "ts"); XmlPullUtil.optSkip(pp, "attrs");
while (XmlPullUtil.test(pp, "tp")) { XmlPullUtil.valueTag(pp, "d"); // duration
XmlPullUtil.enter(pp, "tp"); final int numChanges = Integer.parseInt(XmlPullUtil.valueTag(pp, "ic"));
final String tripId = XmlPullUtil.valueTag(pp, "de");
XmlPullUtil.optValueTag(pp, "optval", null);
XmlPullUtil.optValueTag(pp, "alt", null);
XmlPullUtil.optValueTag(pp, "gix", null);
XmlPullUtil.optSkip(pp, "attrs"); XmlPullUtil.enter(pp, "ls");
XmlPullUtil.valueTag(pp, "d"); // duration final List<Trip.Leg> legs = new LinkedList<>();
final int numChanges = Integer.parseInt(XmlPullUtil.valueTag(pp, "ic")); Location firstDepartureLocation = null;
final String tripId = XmlPullUtil.valueTag(pp, "de"); Location lastArrivalLocation = null;
XmlPullUtil.optValueTag(pp, "optval", null);
XmlPullUtil.optValueTag(pp, "alt", null);
XmlPullUtil.optValueTag(pp, "gix", null);
XmlPullUtil.enter(pp, "ls"); while (XmlPullUtil.test(pp, "l")) {
XmlPullUtil.enter(pp, "l");
final List<Trip.Leg> legs = new LinkedList<>(); XmlPullUtil.enter(pp, "ps");
Location firstDepartureLocation = null;
Location lastArrivalLocation = null;
while (XmlPullUtil.test(pp, "l")) { Stop departure = null;
XmlPullUtil.enter(pp, "l"); Stop arrival = null;
XmlPullUtil.enter(pp, "ps"); while (XmlPullUtil.optEnter(pp, "p")) {
final String name = XmlPullUtil.valueTag(pp, "n");
final String usage = XmlPullUtil.valueTag(pp, "u");
XmlPullUtil.optValueTag(pp, "de", null);
Stop departure = null; XmlPullUtil.requireSkip(pp, "dt");
Stop arrival = null;
while (XmlPullUtil.test(pp, "p")) { parseMobileSt(pp, plannedTimeCal, predictedTimeCal);
XmlPullUtil.enter(pp, "p");
final String name = XmlPullUtil.valueTag(pp, "n"); XmlPullUtil.requireSkip(pp, "lis");
final String usage = XmlPullUtil.valueTag(pp, "u");
XmlPullUtil.optValueTag(pp, "de", null);
XmlPullUtil.requireSkip(pp, "dt"); XmlPullUtil.enter(pp, "r");
final String id = XmlPullUtil.valueTag(pp, "id");
XmlPullUtil.optValueTag(pp, "a", null);
final Position position = parsePosition(XmlPullUtil.optValueTag(pp, "pl", null));
final String place = normalizeLocationName(XmlPullUtil.optValueTag(pp, "pc", null));
final Point coord = parseCoord(XmlPullUtil.optValueTag(pp, "c", null));
XmlPullUtil.skipExit(pp, "r");
parseMobileSt(pp, plannedTimeCal, predictedTimeCal); final Location location;
if (id.equals("99999997") || id.equals("99999998"))
XmlPullUtil.requireSkip(pp, "lis"); location = new Location(LocationType.ADDRESS, null, coord, place, name);
XmlPullUtil.enter(pp, "r");
final String id = XmlPullUtil.valueTag(pp, "id");
XmlPullUtil.optValueTag(pp, "a", null);
final Position position = parsePosition(XmlPullUtil.optValueTag(pp, "pl", null));
final String place = normalizeLocationName(XmlPullUtil.optValueTag(pp, "pc", null));
final Point coord = parseCoord(XmlPullUtil.optValueTag(pp, "c", null));
XmlPullUtil.skipExit(pp, "r");
final Location location;
if (id.equals("99999997") || id.equals("99999998"))
location = new Location(LocationType.ADDRESS, null, coord, place, name);
else
location = new Location(LocationType.STATION, id, coord, place, name);
XmlPullUtil.skipExit(pp, "p");
final Date plannedTime = plannedTimeCal.isSet(Calendar.HOUR_OF_DAY)
? plannedTimeCal.getTime() : null;
final Date predictedTime = predictedTimeCal.isSet(Calendar.HOUR_OF_DAY)
? predictedTimeCal.getTime() : null;
if ("departure".equals(usage)) {
departure = new Stop(location, true, plannedTime, predictedTime, position, null);
if (firstDepartureLocation == null)
firstDepartureLocation = location;
} else if ("arrival".equals(usage)) {
arrival = new Stop(location, false, plannedTime, predictedTime, position, null);
lastArrivalLocation = location;
} else {
throw new IllegalStateException("unknown usage: " + usage);
}
}
checkState(departure != null);
checkState(arrival != null);
XmlPullUtil.skipExit(pp, "ps");
final boolean isRealtime = XmlPullUtil.valueTag(pp, "realtime").equals("1");
final LineDestination lineDestination = parseMobileM(pp, false);
final List<Point> path;
if (XmlPullUtil.test(pp, "pt"))
path = processCoordinateStrings(pp, "pt");
else else
path = null; location = new Location(LocationType.STATION, id, coord, place, name);
XmlPullUtil.require(pp, "pss"); XmlPullUtil.skipExit(pp, "p");
final List<Stop> intermediateStops; final Date plannedTime = plannedTimeCal.isSet(Calendar.HOUR_OF_DAY) ? plannedTimeCal.getTime()
: null;
final Date predictedTime = predictedTimeCal.isSet(Calendar.HOUR_OF_DAY)
? predictedTimeCal.getTime() : null;
if (!pp.isEmptyElementTag()) { if ("departure".equals(usage)) {
XmlPullUtil.enter(pp, "pss"); departure = new Stop(location, true, plannedTime, predictedTime, position, null);
if (firstDepartureLocation == null)
firstDepartureLocation = location;
} else if ("arrival".equals(usage)) {
arrival = new Stop(location, false, plannedTime, predictedTime, position, null);
lastArrivalLocation = location;
} else {
throw new IllegalStateException("unknown usage: " + usage);
}
}
intermediateStops = new LinkedList<>(); checkState(departure != null);
checkState(arrival != null);
while (XmlPullUtil.test(pp, "s")) { XmlPullUtil.skipExit(pp, "ps");
plannedTimeCal.clear();
predictedTimeCal.clear();
final String s = XmlPullUtil.valueTag(pp, "s"); final boolean isRealtime = XmlPullUtil.valueTag(pp, "realtime").equals("1");
final String[] intermediateParts = s.split(";");
final String id = intermediateParts[0];
if (!id.equals(departure.location.id) && !id.equals(arrival.location.id)) {
final String name = normalizeLocationName(intermediateParts[1]);
if (!("0000-1".equals(intermediateParts[2]) final LineDestination lineDestination = parseMobileM(pp, false);
&& "000-1".equals(intermediateParts[3]))) {
ParserUtils.parseIsoDate(plannedTimeCal, intermediateParts[2]);
ParserUtils.parseIsoTime(plannedTimeCal, intermediateParts[3]);
if (isRealtime) { final List<Point> path;
ParserUtils.parseIsoDate(predictedTimeCal, intermediateParts[2]); if (XmlPullUtil.test(pp, "pt"))
ParserUtils.parseIsoTime(predictedTimeCal, intermediateParts[3]); path = processCoordinateStrings(pp, "pt");
else
path = null;
if (intermediateParts.length > 5 && intermediateParts[5].length() > 0) { final List<Stop> intermediateStops;
final int delay = Integer.parseInt(intermediateParts[5]); XmlPullUtil.require(pp, "pss");
predictedTimeCal.add(Calendar.MINUTE, delay); if (XmlPullUtil.optEnter(pp, "pss")) {
} intermediateStops = new LinkedList<>();
while (XmlPullUtil.test(pp, "s")) {
plannedTimeCal.clear();
predictedTimeCal.clear();
final String s = XmlPullUtil.valueTag(pp, "s");
final String[] intermediateParts = s.split(";");
final String id = intermediateParts[0];
if (!id.equals(departure.location.id) && !id.equals(arrival.location.id)) {
final String name = normalizeLocationName(intermediateParts[1]);
if (!("0000-1".equals(intermediateParts[2]) && "000-1".equals(intermediateParts[3]))) {
ParserUtils.parseIsoDate(plannedTimeCal, intermediateParts[2]);
ParserUtils.parseIsoTime(plannedTimeCal, intermediateParts[3]);
if (isRealtime) {
ParserUtils.parseIsoDate(predictedTimeCal, intermediateParts[2]);
ParserUtils.parseIsoTime(predictedTimeCal, intermediateParts[3]);
if (intermediateParts.length > 5 && intermediateParts[5].length() > 0) {
final int delay = Integer.parseInt(intermediateParts[5]);
predictedTimeCal.add(Calendar.MINUTE, delay);
} }
} }
final String coordPart = intermediateParts[4]; }
final String coordPart = intermediateParts[4];
final Point coords; final Point coords;
if (!"::".equals(coordPart)) { if (!"::".equals(coordPart)) {
final String[] coordParts = coordPart.split(":"); final String[] coordParts = coordPart.split(":");
if ("WGS84".equals(coordParts[2])) { if ("WGS84".equals(coordParts[2])) {
final int lat = (int) Math.round(Double.parseDouble(coordParts[1])); final int lat = (int) Math.round(Double.parseDouble(coordParts[1]));
final int lon = (int) Math.round(Double.parseDouble(coordParts[0])); final int lon = (int) Math.round(Double.parseDouble(coordParts[0]));
coords = new Point(lat, lon); coords = new Point(lat, lon);
} else {
coords = null;
}
} else { } else {
coords = null; coords = null;
} }
final Location location = new Location(LocationType.STATION, id, coords, null, } else {
name); coords = null;
final Date plannedTime = plannedTimeCal.isSet(Calendar.HOUR_OF_DAY)
? plannedTimeCal.getTime() : null;
final Date predictedTime = predictedTimeCal.isSet(Calendar.HOUR_OF_DAY)
? predictedTimeCal.getTime() : null;
final Stop stop = new Stop(location, false, plannedTime, predictedTime, null, null);
intermediateStops.add(stop);
} }
final Location location = new Location(LocationType.STATION, id, coords, null, name);
final Date plannedTime = plannedTimeCal.isSet(Calendar.HOUR_OF_DAY)
? plannedTimeCal.getTime() : null;
final Date predictedTime = predictedTimeCal.isSet(Calendar.HOUR_OF_DAY)
? predictedTimeCal.getTime() : null;
final Stop stop = new Stop(location, false, plannedTime, predictedTime, null, null);
intermediateStops.add(stop);
} }
XmlPullUtil.skipExit(pp, "pss");
} else {
intermediateStops = null;
XmlPullUtil.next(pp);
} }
XmlPullUtil.optSkip(pp, "interchange"); XmlPullUtil.skipExit(pp, "pss");
XmlPullUtil.requireSkip(pp, "ns");
// TODO messages
XmlPullUtil.skipExit(pp, "l");
if (lineDestination.line == Line.FOOTWAY) {
legs.add(new Trip.Individual(Trip.Individual.Type.WALK, departure.location,
departure.getDepartureTime(), arrival.location, arrival.getArrivalTime(), path, 0));
} else if (lineDestination.line == Line.TRANSFER) {
legs.add(new Trip.Individual(Trip.Individual.Type.TRANSFER, departure.location,
departure.getDepartureTime(), arrival.location, arrival.getArrivalTime(), path, 0));
} else if (lineDestination.line == Line.SECURE_CONNECTION
|| lineDestination.line == Line.DO_NOT_CHANGE) {
// ignore
} else {
legs.add(new Trip.Public(lineDestination.line, lineDestination.destination, departure,
arrival, intermediateStops, path, null));
}
}
XmlPullUtil.skipExit(pp, "ls");
XmlPullUtil.optSkip(pp, "seqroutes");
final List<Fare> fares;
if (XmlPullUtil.test(pp, "tcs")) {
if (!pp.isEmptyElementTag()) {
XmlPullUtil.enter(pp, "tcs");
fares = new ArrayList<>(2);
XmlPullUtil.optSkipMultiple(pp, "tc"); // TODO fares
XmlPullUtil.skipExit(pp, "tcs");
} else {
fares = null;
XmlPullUtil.next(pp);
}
} else { } else {
fares = null; intermediateStops = null;
} }
final Trip trip = new Trip(tripId, firstDepartureLocation, lastArrivalLocation, legs, fares, null, XmlPullUtil.optSkip(pp, "interchange");
numChanges);
trips.add(trip);
XmlPullUtil.skipExit(pp, "tp"); XmlPullUtil.requireSkip(pp, "ns");
// TODO messages
XmlPullUtil.skipExit(pp, "l");
if (lineDestination.line == Line.FOOTWAY) {
legs.add(new Trip.Individual(Trip.Individual.Type.WALK, departure.location,
departure.getDepartureTime(), arrival.location, arrival.getArrivalTime(), path, 0));
} else if (lineDestination.line == Line.TRANSFER) {
legs.add(new Trip.Individual(Trip.Individual.Type.TRANSFER, departure.location,
departure.getDepartureTime(), arrival.location, arrival.getArrivalTime(), path, 0));
} else if (lineDestination.line == Line.SECURE_CONNECTION
|| lineDestination.line == Line.DO_NOT_CHANGE) {
// ignore
} else {
legs.add(new Trip.Public(lineDestination.line, lineDestination.destination, departure, arrival,
intermediateStops, path, null));
}
} }
XmlPullUtil.skipExit(pp, "ts"); XmlPullUtil.skipExit(pp, "ls");
} else {
XmlPullUtil.next(pp); XmlPullUtil.optSkip(pp, "seqroutes");
final List<Fare> fares;
if (XmlPullUtil.optEnter(pp, "tcs")) {
fares = new ArrayList<>(2);
XmlPullUtil.optSkipMultiple(pp, "tc"); // TODO fares
XmlPullUtil.skipExit(pp, "tcs");
} else {
fares = null;
}
final Trip trip = new Trip(tripId, firstDepartureLocation, lastArrivalLocation, legs, fares, null,
numChanges);
trips.add(trip);
XmlPullUtil.skipExit(pp, "tp");
} }
XmlPullUtil.skipExit(pp, "ts");
} }
if (trips.size() > 0) { if (trips.size() > 0) {
@ -2969,9 +2894,7 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
XmlPullUtil.enter(pp, "itdCoordinateBaseElemList"); XmlPullUtil.enter(pp, "itdCoordinateBaseElemList");
while (XmlPullUtil.test(pp, "itdCoordinateBaseElem")) { while (XmlPullUtil.optEnter(pp, "itdCoordinateBaseElem")) {
XmlPullUtil.enter(pp, "itdCoordinateBaseElem");
final int lon = (int) Math.round(Double.parseDouble(XmlPullUtil.valueTag(pp, "x"))); final int lon = (int) Math.round(Double.parseDouble(XmlPullUtil.valueTag(pp, "x")));
final int lat = (int) Math.round(Double.parseDouble(XmlPullUtil.valueTag(pp, "y"))); final int lat = (int) Math.round(Double.parseDouble(XmlPullUtil.valueTag(pp, "y")));
path.add(new Point(lat, lon)); path.add(new Point(lat, lon));
@ -3015,9 +2938,7 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
Type type = null; Type type = null;
float fare = 0; float fare = 0;
while (XmlPullUtil.test(pp, "itdGenericTicket")) { while (XmlPullUtil.optEnter(pp, "itdGenericTicket")) {
XmlPullUtil.enter(pp, "itdGenericTicket");
final String key = XmlPullUtil.valueTag(pp, "ticket"); final String key = XmlPullUtil.valueTag(pp, "ticket");
final String value = XmlPullUtil.valueTag(pp, "value"); final String value = XmlPullUtil.valueTag(pp, "value");
@ -3192,8 +3113,7 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider {
XmlPullUtil.enter(pp, "pas"); XmlPullUtil.enter(pp, "pas");
while (XmlPullUtil.test(pp, "pa")) { while (XmlPullUtil.optEnter(pp, "pa")) {
XmlPullUtil.enter(pp, "pa");
final String name = XmlPullUtil.valueTag(pp, "n"); final String name = XmlPullUtil.valueTag(pp, "n");
final String value = XmlPullUtil.valueTag(pp, "v"); final String value = XmlPullUtil.valueTag(pp, "v");
params.put(name, value); params.put(name, value);

View file

@ -48,6 +48,20 @@ public final class XmlPullUtil {
enter(pp); enter(pp);
} }
public static boolean optEnter(final XmlPullParser pp, final String tagName)
throws XmlPullParserException, IOException {
if (!test(pp, tagName))
return false;
final boolean isEmpty = pp.isEmptyElementTag();
pp.next(); // Skip start tag
if (isEmpty) {
pp.next(); // Skip (virtual) end tag
return false;
} else {
return true;
}
}
public static void skipExit(final XmlPullParser pp) throws XmlPullParserException, IOException { public static void skipExit(final XmlPullParser pp) throws XmlPullParserException, IOException {
skipToEnd(pp); skipToEnd(pp);