();
while (mAddresses.find())
{
final String address = ParserUtils.resolveEntities(mAddresses.group(1)).trim();
if (!addresses.contains(address))
addresses.add(new Location(LocationType.ANY, 0, null, address + "!"));
}
if (type.equals("REQ0JourneyStopsS0K"))
fromAddresses = addresses;
else if (type.equals("REQ0JourneyStopsZ0K"))
toAddresses = addresses;
else if (type.equals("REQ0JourneyStops1.0K"))
viaAddresses = addresses;
else
throw new IllegalStateException(type);
}
if (fromAddresses != null || viaAddresses != null || toAddresses != null)
return new QueryConnectionsResult(fromAddresses, viaAddresses, toAddresses);
else
return queryConnections(baseUri, page);
}
@Override
public QueryConnectionsResult queryMoreConnections(final String uri) throws IOException
{
final CharSequence page = ParserUtils.scrape(uri, false, null, null, "NSC_");
final Matcher mError = P_QUERY_CONNECTIONS_ERROR.matcher(page);
if (mError.find())
{
if (mError.group(1) != null)
return QueryConnectionsResult.NO_CONNECTIONS;
if (mError.group(2) != null)
return QueryConnectionsResult.INVALID_DATE;
if (mError.group(3) != null)
throw new SessionExpiredException();
}
return queryConnections(uri, page);
}
private static final Pattern P_CONNECTIONS_ALL_DETAILS = Pattern.compile("" //
+ "");
private static final Pattern P_CONNECTIONS_HEAD = Pattern.compile(".*?" //
+ "von:\n\\s*(.*?)\\s*.*?" // from
+ "nach:\n\\s*(.*?)\\s*.*?" // to
+ "\nDatum:\n\n.., (\\d{2}\\.\\d{2}\\.\\d{2}).*?" // date
+ "(?:]*>\n(.*?)\n" //
+ "\n(.*?)Seitenanfang.*?
" //
, Pattern.DOTALL);
private static final Pattern P_CONNECTIONS_FINE = Pattern.compile(".*?" //
+ "]*>(\\d{2}\\.\\d{2}\\.\\d{2})" // departureDate
+ "(?: (\\d{2}\\.\\d{2}\\.\\d{2}))?.*?" // arrivalDate
+ "(\\d{1,2}:\\d{2}) ab.*?" // departureTime
+ "(\\d{1,2}:\\d{2}) an.*?" // arrivalTime
, Pattern.DOTALL);
private static final Pattern P_CONNECTION_DETAILS_COARSE = Pattern.compile("" //
+ " | \n(.*?)
\n" //
+ "\n(.*?)
\n" //
+ "\n(.*?)
\n" //
, Pattern.DOTALL);
private static final Pattern P_CONNECTION_DETAILS_FINE = Pattern.compile(".*?" //
+ "\n?(?:]*>)?" // departureId
+ "([^\n<]*).*?" // departure
+ " | (?:(\\d{2}\\.\\d{2}\\.\\d{2})| ) | .*?" // departureDate
+ "\n?ab (\\d{2}:\\d{2}).*?" // departureTime
+ " | \\s*(?: |(.*?))\\s* | .*?" // departurePosition
+ "
]*>(.*?).*?)?" // line
+ "\n?(?:]*>)?" // arrivalId
+ "([^\n<]*).*?" // arrival
+ " | (?:(\\d{2}\\.\\d{2}\\.\\d{2})| ) | .*?" // arrivalDate
+ "\n?an (\\d{2}:\\d{2}).*?" // arrivalTime
+ " | \\s*(?: |(.*?))\\s* | .*?" // arrivalPosition
+ "]* class=\"section_remarks\">(?:.*?Richtung\\:\\s*([^\n]*)\n)?.*? | ?.*?" // destination
, Pattern.DOTALL);
private QueryConnectionsResult queryConnections(final String firstUri, final CharSequence firstPage) throws IOException
{
// ugly workaround to fetch all details
final Matcher mAllDetailsAction = P_CONNECTIONS_ALL_DETAILS.matcher(firstPage);
if (!mAllDetailsAction.find())
throw new IOException("cannot find all details link in '" + firstPage + "' on " + firstUri);
final String allDetailsUri = mAllDetailsAction.group(1);
final CharSequence page = ParserUtils.scrape(allDetailsUri, false, null, null, "NSC_");
final Matcher mError = P_QUERY_CONNECTIONS_ERROR.matcher(page);
if (mError.find())
{
if (mError.group(1) != null)
return QueryConnectionsResult.NO_CONNECTIONS;
if (mError.group(2) != null)
return QueryConnectionsResult.INVALID_DATE;
if (mError.group(3) != null)
throw new SessionExpiredException();
}
// parse page
final Matcher mHead = P_CONNECTIONS_HEAD.matcher(page);
if (mHead.matches())
{
final Location from = new Location(LocationType.ANY, 0, null, ParserUtils.resolveEntities(mHead.group(1)));
final Location to = new Location(LocationType.ANY, 0, null, ParserUtils.resolveEntities(mHead.group(2)));
final Calendar time = new GregorianCalendar(timeZone());
time.clear();
ParserUtils.parseGermanDate(time, mHead.group(3));
// final String linkEarlier = mHead.group(4) != null ? ParserUtils.resolveEntities(mHead.group(4)) : null;
final String linkLater = mHead.group(5) != null ? ParserUtils.resolveEntities(mHead.group(5)) : null;
final List connections = new ArrayList();
final Matcher mConCoarse = P_CONNECTIONS_COARSE.matcher(page);
while (mConCoarse.find())
{
final String id = mConCoarse.group(1);
final String overview = mConCoarse.group(2);
final String details = mConCoarse.group(3);
final Matcher mConFine = P_CONNECTIONS_FINE.matcher(overview);
if (mConFine.matches())
{
final Calendar overviewDepartureTime = new GregorianCalendar(timeZone());
overviewDepartureTime.clear();
ParserUtils.parseGermanDate(overviewDepartureTime, mConFine.group(1));
ParserUtils.parseEuropeanTime(overviewDepartureTime, mConFine.group(3));
final Calendar overviewArrivalTime = new GregorianCalendar(timeZone());
overviewArrivalTime.setTimeInMillis(overviewDepartureTime.getTimeInMillis());
if (mConFine.group(2) != null)
ParserUtils.parseGermanDate(overviewArrivalTime, mConFine.group(2));
ParserUtils.parseEuropeanTime(overviewArrivalTime, mConFine.group(4));
final String link = allDetailsUri; // TODO use print link?
final Connection connection = new Connection(id, link, overviewDepartureTime.getTime(), overviewArrivalTime.getTime(), from, to,
new ArrayList(1), null, null);
connections.add(connection);
final Matcher mDetCoarse = P_CONNECTION_DETAILS_COARSE.matcher(details);
while (mDetCoarse.find())
{
final String set = mDetCoarse.group(1) + mDetCoarse.group(2) + mDetCoarse.group(3);
final Matcher mDetFine = P_CONNECTION_DETAILS_FINE.matcher(set);
if (mDetFine.matches())
{
final int departureId = mDetFine.group(1) != null ? Integer.parseInt(mDetFine.group(1)) : 0;
final Location departure = new Location(departureId != 0 ? LocationType.STATION : LocationType.ANY, departureId, null,
ParserUtils.resolveEntities(mDetFine.group(2)));
if (mDetFine.group(3) != null)
ParserUtils.parseGermanDate(time, mDetFine.group(3));
ParserUtils.parseEuropeanTime(time, mDetFine.group(4));
final Date detailsDepartureTime = time.getTime();
final String lineType = mDetFine.group(6);
final int arrivalId = mDetFine.group(8) != null ? Integer.parseInt(mDetFine.group(8)) : 0;
final Location arrival = new Location(arrivalId != 0 ? LocationType.STATION : LocationType.ANY, arrivalId, null,
ParserUtils.resolveEntities(mDetFine.group(9)));
if (mDetFine.group(10) != null)
ParserUtils.parseGermanDate(time, mDetFine.group(10));
ParserUtils.parseEuropeanTime(time, mDetFine.group(11));
final Date detailsArrivalTime = time.getTime();
if (!("fuss".equals(lineType) || "transfer".equals(lineType)))
{
if (departureId == 0)
throw new IllegalStateException("departureId");
final String departurePosition = mDetFine.group(5) != null ? ParserUtils.resolveEntities(mDetFine.group(5)) : null;
final String lineStr = normalizeLine(lineType, ParserUtils.resolveEntities(mDetFine.group(7)));
final Line line = new Line(null, lineStr, lineColors(lineStr));
if (arrivalId == 0)
throw new IllegalStateException("arrivalId");
final String arrivalPosition = mDetFine.group(12) != null ? ParserUtils.resolveEntities(mDetFine.group(12)) : null;
final Location destination = mDetFine.group(13) != null ? new Location(LocationType.ANY, 0, null,
ParserUtils.resolveEntities(mDetFine.group(13))) : null;
final Connection.Trip trip = new Connection.Trip(line, destination, detailsDepartureTime, departurePosition,
departure, detailsArrivalTime, arrivalPosition, arrival, null, null);
connection.parts.add(trip);
}
else
{
final int min = (int) (detailsArrivalTime.getTime() - detailsDepartureTime.getTime()) / 1000 / 60;
final Connection.Footway footway = new Connection.Footway(min, departure, arrival, null);
connection.parts.add(footway);
}
}
else
{
throw new IllegalArgumentException("cannot parse '" + set + "' on " + allDetailsUri);
}
}
}
else
{
throw new IllegalArgumentException("cannot parse '" + overview + "' on " + allDetailsUri);
}
}
return new QueryConnectionsResult(allDetailsUri, from, null, to, linkLater, connections);
}
else
{
throw new IllegalArgumentException("cannot parse '" + page + "' on " + allDetailsUri);
}
}
@Override
public GetConnectionDetailsResult getConnectionDetails(final String connectionUri) throws IOException
{
throw new UnsupportedOperationException();
}
private static final Pattern P_NORMALIZE_LINE_AND_TYPE = Pattern.compile("([^#]*)#(.*)");
private static final Pattern P_NORMALIZE_LINE_NUMBER = Pattern.compile("\\d{2,5}");
@Override
protected String normalizeLine(final String line)
{
final Matcher m = P_NORMALIZE_LINE_AND_TYPE.matcher(line);
if (m.matches())
{
final String number = m.group(1).replaceAll("\\s+", " ");
final String type = m.group(2);
if (type.length() == 0)
{
if (number.length() == 0)
return "?";
if (P_NORMALIZE_LINE_NUMBER.matcher(number).matches())
return "?" + number;
}
else
{
final char normalizedType = normalizeType(type);
if (normalizedType != 0)
return normalizedType + number;
}
throw new IllegalStateException("cannot normalize type " + type + " number " + number + " line " + line);
}
throw new IllegalStateException("cannot normalize line " + line);
}
@Override
protected char normalizeType(final String type)
{
final String ucType = type.toUpperCase();
if (ucType.equals("INT")) // Rußland, Connections only?
return 'I';
if (ucType.equals("RR")) // Finnland, Connections only?
return 'I';
if (ucType.equals("EE")) // Rumänien, Connections only?
return 'I';
if (ucType.equals("OZ")) // Schweden, Oeresundzug, Connections only?
return 'I';
if (ucType.equals("UUU")) // Italien, Nacht, Connections only?
return 'I';
if (ucType.equals("S2")) // Helsinki-Turku, Connections only?
return 'R';
if (ucType.equals("RE")) // RegionalExpress Deutschland
return 'R';
if (ucType.equals("DPN")) // Connections only? TODO nicht evtl. doch eher ne S-Bahn?
return 'R';
if (ucType.equals("E")) // Budapest, Ungarn
return 'R';
if (ucType.equals("IP")) // Ozd, Ungarn
return 'R';
if (ucType.equals("N")) // Frankreich, Tours
return 'R';
if (ucType.equals("DPF")) // VX=Vogtland Express, Connections only?
return 'R';
// if (ucType.equals("SBE")) // Zittau-Seifhennersdorf, via JSON API
// return 'R';
// if (ucType.equals("RNV")) // Rhein-Neckar-Verkehr GmbH, via JSON API
// return 'R';
if ("UAU".equals(ucType)) // Rußland
return 'R';
if (ucType.equals("RSB")) // Schnellbahn Wien
return 'S';
// if (ucType.equals("DPN")) // S3 Bad Reichenhall-Freilassing, via JSON API
// return 'S';
if (ucType.equals("LKB")) // Connections only?
return 'T';
// if (ucType.equals("WLB")) // via JSON API
// return 'T';
if (ucType.equals("OBU")) // Connections only?
return 'B';
// if (ucType.equals("ASTSV")) // via JSON API
// return 'B';
if (ucType.equals("ICB")) // ÖBB ICBus
return 'B';
if (ucType.equals("BSV")) // Deutschland, Connections only?
return 'B';
if (ucType.equals("O-BUS")) // Stadtbus
return 'B';
if (ucType.equals("SCH")) // Connections only?
return 'F';
if (ucType.equals("F")) // Fähre
return 'F';
if (ucType.equals("LIF"))
return 'C';
if (ucType.equals("SSB")) // Graz Schlossbergbahn
return 'C';
// if (ucType.equals("HBB")) // Innsbruck Hungerburgbahn, via JSON API
// return 'C';
final char t = super.normalizeType(type);
if (t != 0)
return t;
if (ucType.equals("U70")) // U.K., Connections only?
return '?';
if (ucType.equals("X70")) // U.K., Connections only?
return '?';
if (ucType.equals("R84")) // U.K., Connections only?
return '?';
if (ucType.equals("S84")) // U.K., Connections only?
return '?';
if (ucType.equals("T84")) // U.K., Connections only?
return '?';
return 0;
}
}