(4);
Date firstDepartureTime = null;
String firstDeparture = null;
int firstDepartureId = 0;
Date lastArrivalTime = null;
String lastArrival = null;
int lastArrivalId = 0;
final Matcher mDetCoarse = P_CONNECTION_DETAILS_COARSE.matcher(page);
while (mDetCoarse.find())
{
final Matcher mDetFine = P_CONNECTION_DETAILS_FINE.matcher(mDetCoarse.group(1));
if (mDetFine.matches())
{
int departureId = 0;
String departure = ParserUtils.resolveEntities(mDetFine.group(2));
if (departure == null)
{
departure = lastArrival;
departureId = lastArrivalId;
}
else
{
departureId = Integer.parseInt(mDetFine.group(1));
}
if (departure != null && firstDeparture == null)
{
firstDeparture = departure;
firstDepartureId = departureId;
}
final String min = mDetFine.group(11);
if (min == null)
{
Date departureTime = ParserUtils.joinDateTime(currentDate, ParserUtils.parseTime(mDetFine.group(3)));
if (lastArrivalTime != null && departureTime.before(lastArrivalTime))
departureTime = ParserUtils.addDays(departureTime, 1);
final String departurePosition = mDetFine.group(4);
final String line = normalizeLine(ParserUtils.resolveEntities(mDetFine.group(5)));
final Location destination = new Location(LocationType.ANY, 0, null, ParserUtils.resolveEntities(mDetFine.group(6)));
Date arrivalTime = ParserUtils.joinDateTime(currentDate, ParserUtils.parseTime(mDetFine.group(7)));
if (departureTime.after(arrivalTime))
arrivalTime = ParserUtils.addDays(arrivalTime, 1);
final String arrivalPosition = mDetFine.group(8);
final int arrivalId = Integer.parseInt(mDetFine.group(9));
final String arrival = ParserUtils.resolveEntities(mDetFine.group(10));
parts.add(new Connection.Trip(line, destination, departureTime, departurePosition, departureId, departure, arrivalTime,
arrivalPosition, arrivalId, arrival, null));
if (firstDepartureTime == null)
firstDepartureTime = departureTime;
lastArrival = arrival;
lastArrivalId = arrivalId;
lastArrivalTime = arrivalTime;
}
else
{
final int arrivalId = mDetFine.group(12) != null ? Integer.parseInt(mDetFine.group(12)) : 0;
final String arrival = ParserUtils.resolveEntities(ParserUtils.selectNotNull(mDetFine.group(13), mDetFine.group(16),
mDetFine.group(17)));
final int arrivalLon = mDetFine.group(14) != null ? Integer.parseInt(mDetFine.group(14)) : 0;
final int arrivalLat = mDetFine.group(15) != null ? Integer.parseInt(mDetFine.group(15)) : 0;
if (parts.size() > 0 && parts.get(parts.size() - 1) instanceof Connection.Footway)
{
final Connection.Footway lastFootway = (Connection.Footway) parts.remove(parts.size() - 1);
parts.add(new Connection.Footway(lastFootway.min + Integer.parseInt(min), lastFootway.departureId, lastFootway.departure,
arrivalId, arrival, arrivalLat, arrivalLon));
}
else
{
parts.add(new Connection.Footway(Integer.parseInt(min), departureId, departure, arrivalId, arrival, arrivalLat,
arrivalLon));
}
lastArrival = arrival;
lastArrivalId = arrivalId;
}
}
else
{
throw new IllegalArgumentException("cannot parse '" + mDetCoarse.group(1) + "' on " + uri);
}
}
if (firstDepartureTime != null && lastArrivalTime != null)
return new GetConnectionDetailsResult(currentDate, new Connection(AbstractHafasProvider.extractConnectionId(uri), uri,
firstDepartureTime, lastArrivalTime, firstDepartureId, firstDeparture, lastArrivalId, lastArrival, parts, null));
else
return new GetConnectionDetailsResult(currentDate, null);
}
else
{
throw new IOException(page.toString());
}
}
private static final String DEPARTURE_URL_LIVE = "http://mobil.bvg.de/IstAbfahrtzeiten/index/mobil?";
private String departuresQueryLiveUri(final String stationId)
{
final StringBuilder uri = new StringBuilder();
uri.append(DEPARTURE_URL_LIVE);
uri.append("input=").append(stationId);
return uri.toString();
}
private static final String DEPARTURE_URL_PLAN = "http://mobil.bvg.de/Fahrinfo/bin/stboard.bin/dox/dox?boardType=dep&disableEquivs=yes&start=yes&";
private String departuresQueryPlanUri(final String stationId, final int maxDepartures)
{
final StringBuilder uri = new StringBuilder();
uri.append(DEPARTURE_URL_PLAN);
uri.append("input=").append(stationId);
uri.append("&maxJourneys=").append(maxDepartures != 0 ? maxDepartures : 50);
return uri.toString();
}
private static final Pattern P_DEPARTURES_HEAD = Pattern.compile(".*?" //
+ "(.*?).*?Datum:(.*?)
.*" //
, Pattern.DOTALL);
private static final Pattern P_DEPARTURES_COARSE = Pattern.compile("" //
+ "\\s*((?:| | ).+?)\\s* |
" //
, Pattern.DOTALL);
private static final Pattern P_DEPARTURES_LIVE_FINE = Pattern.compile("" //
+ "\\s*(\\d{1,2}:\\d{2})\\s*" // time
+ "(\\*)?\\s* | \\s*" // planned
+ "\\s*(.*?)\\s* | \\s*" // line
+ ".*?\\s*(.*?)\\s*.*? | " // destination
, Pattern.DOTALL);
private static final Pattern P_DEPARTURES_PLAN_FINE = Pattern.compile("" //
+ "(\\d{1,2}:\\d{2}) | .*?" // time
+ "\\s*(.*?)[\\s\\*]*.*?" // line
+ "(?:\\((Gl\\. " + ParserUtils.P_PLATFORM + ")\\).*?)?" // position
+ "]*>" // destinationId
+ "\\s*(.*?)\\s*.*?" // destination
, Pattern.DOTALL);
private static final Pattern P_DEPARTURES_LIVE_ERRORS = Pattern.compile("(Haltestelle:)|(Wartungsgründen)");
private static final Pattern P_DEPARTURES_PLAN_ERRORS = Pattern.compile("(derzeit leider nicht bearbeitet werden)|(Wartungsarbeiten)");
public QueryDeparturesResult queryDepartures(final String stationId, final int maxDepartures) throws IOException
{
if (stationId.length() == 6) // live
{
final String uri = departuresQueryLiveUri(stationId);
final CharSequence page = ParserUtils.scrape(uri);
final Matcher mError = P_DEPARTURES_LIVE_ERRORS.matcher(page);
if (mError.find())
{
if (mError.group(1) != null)
return new QueryDeparturesResult(Status.INVALID_STATION, Integer.parseInt(stationId));
if (mError.group(2) != null)
return new QueryDeparturesResult(Status.SERVICE_DOWN, Integer.parseInt(stationId));
}
// parse page
final Matcher mHead = P_DEPARTURES_HEAD.matcher(page);
if (mHead.matches())
{
final String location = ParserUtils.resolveEntities(mHead.group(1));
final Date currentTime = parseDate(mHead.group(2));
final List departures = new ArrayList(8);
// choose matcher
final Matcher mDepCoarse = P_DEPARTURES_COARSE.matcher(page);
while (mDepCoarse.find())
{
final Matcher mDepFine = P_DEPARTURES_LIVE_FINE.matcher(mDepCoarse.group(1));
if (mDepFine.matches())
{
final Calendar current = new GregorianCalendar();
current.setTime(currentTime);
final Calendar parsed = new GregorianCalendar();
parsed.setTime(ParserUtils.parseTime(mDepFine.group(1)));
parsed.set(Calendar.YEAR, current.get(Calendar.YEAR));
parsed.set(Calendar.MONTH, current.get(Calendar.MONTH));
parsed.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));
if (ParserUtils.timeDiff(parsed.getTime(), currentTime) < -PARSER_DAY_ROLLOVER_THRESHOLD_MS)
parsed.add(Calendar.DAY_OF_MONTH, 1);
boolean isPlanned = mDepFine.group(2) != null;
Date plannedTime = null;
Date predictedTime = null;
if (!isPlanned)
predictedTime = parsed.getTime();
else
plannedTime = parsed.getTime();
final String line = normalizeLine(ParserUtils.resolveEntities(mDepFine.group(3)));
final String position = null;
final int destinationId = 0;
final String destination = ParserUtils.resolveEntities(mDepFine.group(4));
final Departure dep = new Departure(plannedTime, predictedTime, line, line != null ? lineColors(line) : null, null, position,
destinationId, destination, null);
if (!departures.contains(dep))
departures.add(dep);
}
else
{
throw new IllegalArgumentException("cannot parse '" + mDepCoarse.group(1) + "' on " + uri);
}
}
return new QueryDeparturesResult(new Location(LocationType.STATION, Integer.parseInt(stationId), null, location), departures, null);
}
else
{
throw new IllegalArgumentException("cannot parse '" + page + "' on " + uri);
}
}
else
{
final String uri = departuresQueryPlanUri(stationId, maxDepartures);
final CharSequence page = ParserUtils.scrape(uri);
final Matcher mError = P_DEPARTURES_PLAN_ERRORS.matcher(page);
if (mError.find())
{
if (mError.group(1) != null)
return new QueryDeparturesResult(Status.INVALID_STATION, Integer.parseInt(stationId));
if (mError.group(2) != null)
return new QueryDeparturesResult(Status.SERVICE_DOWN, Integer.parseInt(stationId));
}
// parse page
final Matcher mHead = P_DEPARTURES_HEAD.matcher(page);
if (mHead.matches())
{
final String location = ParserUtils.resolveEntities(mHead.group(1));
final Date currentTime = parseDate(mHead.group(2));
final List departures = new ArrayList(8);
// choose matcher
final Matcher mDepCoarse = P_DEPARTURES_COARSE.matcher(page);
while (mDepCoarse.find())
{
final Matcher mDepFine = P_DEPARTURES_PLAN_FINE.matcher(mDepCoarse.group(1));
if (mDepFine.matches())
{
final Calendar current = new GregorianCalendar();
current.setTime(currentTime);
final Calendar parsed = new GregorianCalendar();
parsed.setTime(ParserUtils.parseTime(mDepFine.group(1)));
parsed.set(Calendar.YEAR, current.get(Calendar.YEAR));
parsed.set(Calendar.MONTH, current.get(Calendar.MONTH));
parsed.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));
if (ParserUtils.timeDiff(parsed.getTime(), currentTime) < -PARSER_DAY_ROLLOVER_THRESHOLD_MS)
parsed.add(Calendar.DAY_OF_MONTH, 1);
final Date plannedTime = parsed.getTime();
final String line = normalizeLine(ParserUtils.resolveEntities(mDepFine.group(2)));
final String position = ParserUtils.resolveEntities(mDepFine.group(3));
final int destinationId = Integer.parseInt(mDepFine.group(4));
final String destination = ParserUtils.resolveEntities(mDepFine.group(5));
final Departure dep = new Departure(plannedTime, null, line, line != null ? lineColors(line) : null, null, position,
destinationId, destination, null);
if (!departures.contains(dep))
departures.add(dep);
}
else
{
throw new IllegalArgumentException("cannot parse '" + mDepCoarse.group(1) + "' on " + uri);
}
}
return new QueryDeparturesResult(new Location(LocationType.STATION, Integer.parseInt(stationId), null, location), departures, null);
}
else
{
throw new IllegalArgumentException("cannot parse '" + page + "' on " + uri);
}
}
}
private static final Date parseDate(String str)
{
try
{
return new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss").parse(str);
}
catch (ParseException x1)
{
try
{
return new SimpleDateFormat("dd.MM.yy").parse(str);
}
catch (ParseException x2)
{
throw new RuntimeException(x2);
}
}
}
private static final Pattern P_NORMALIZE_LINE = Pattern.compile("([A-Za-zÄÖÜäöüßáàâéèêíìîóòôúùû]+)[\\s-]*(.*)");
private static final Pattern P_NORMALIZE_LINE_SPECIAL_NUMBER = Pattern.compile("\\d{4,}");
private static final Pattern P_NORMALIZE_LINE_SPECIAL_BUS = Pattern.compile("Bus[A-Z]");
private static String normalizeLine(final String line)
{
if (line == null || line.length() == 0)
return null;
if (line.startsWith("RE") || line.startsWith("RB") || line.startsWith("NE") || line.startsWith("OE") || line.startsWith("MR")
|| line.startsWith("PE"))
return "R" + line;
if (line.equals("11"))
return "?11";
if (P_NORMALIZE_LINE_SPECIAL_NUMBER.matcher(line).matches())
return "R" + line;
final Matcher m = P_NORMALIZE_LINE.matcher(line);
if (m.matches())
{
final String type = m.group(1);
final String number = m.group(2).replace(" ", "");
if (type.equals("ICE")) // InterCityExpress
return "IICE" + number;
if (type.equals("IC")) // InterCity
return "IIC" + number;
if (type.equals("EC")) // EuroCity
return "IEC" + number;
if (type.equals("EN")) // EuroNight
return "IEN" + number;
if (type.equals("CNL")) // CityNightLine
return "ICNL" + number;
if (type.equals("IR"))
return "RIR" + number;
if (type.equals("IRE"))
return "RIRE" + number;
if (type.equals("Zug"))
return "R" + number;
if (type.equals("ZUG"))
return "R" + number;
if (type.equals("D")) // D-Zug?
return "RD" + number;
if (type.equals("DNZ")) // unklar, aber vermutlich Russland
return "RDNZ" + (number.equals("DNZ") ? "" : number);
if (type.equals("KBS")) // Kursbuchstrecke
return "RKBS" + number;
if (type.equals("BKB")) // Buckower Kleinbahn
return "RBKB" + number;
if (type.equals("Ausfl")) // Umgebung Berlin
return "RAusfl" + number;
if (type.equals("PKP")) // Polen
return "RPKP" + number;
if (type.equals("S"))
return "SS" + number;
if (type.equals("U"))
return "UU" + number;
if (type.equals("Tra") || type.equals("Tram"))
return "T" + number;
if (type.equals("Bus"))
return "B" + number;
if (P_NORMALIZE_LINE_SPECIAL_BUS.matcher(type).matches()) // workaround for weird scheme BusF/526
return "B" + line.substring(3);
if (type.equals("Fäh"))
return "F" + number;
if (type.equals("F"))
return "FF" + 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)
{
throw new UnsupportedOperationException();
}
private static final Map LINES = new HashMap();
static
{
LINES.put("SS1", new int[] { Color.rgb(221, 77, 174), Color.WHITE });
LINES.put("SS2", new int[] { Color.rgb(16, 132, 73), Color.WHITE });
LINES.put("SS25", new int[] { Color.rgb(16, 132, 73), Color.WHITE });
LINES.put("SS3", new int[] { Color.rgb(22, 106, 184), Color.WHITE });
LINES.put("SS41", new int[] { Color.rgb(162, 63, 48), Color.WHITE });
LINES.put("SS42", new int[] { Color.rgb(191, 90, 42), Color.WHITE });
LINES.put("SS45", new int[] { Color.rgb(191, 128, 55), Color.WHITE });
LINES.put("SS46", new int[] { Color.rgb(191, 128, 55), Color.WHITE });
LINES.put("SS47", new int[] { Color.rgb(191, 128, 55), Color.WHITE });
LINES.put("SS5", new int[] { Color.rgb(243, 103, 23), Color.WHITE });
LINES.put("SS7", new int[] { Color.rgb(119, 96, 176), Color.WHITE });
LINES.put("SS75", new int[] { Color.rgb(119, 96, 176), Color.WHITE });
LINES.put("SS8", new int[] { Color.rgb(85, 184, 49), Color.WHITE });
LINES.put("SS85", new int[] { Color.rgb(85, 184, 49), Color.WHITE });
LINES.put("SS9", new int[] { Color.rgb(148, 36, 64), Color.WHITE });
LINES.put("UU1", new int[] { Color.rgb(84, 131, 47), Color.WHITE });
LINES.put("UU2", new int[] { Color.rgb(215, 25, 16), Color.WHITE });
LINES.put("UU3", new int[] { Color.rgb(47, 152, 154), Color.WHITE });
LINES.put("UU4", new int[] { Color.rgb(255, 233, 42), Color.BLACK });
LINES.put("UU5", new int[] { Color.rgb(91, 31, 16), Color.WHITE });
LINES.put("UU55", new int[] { Color.rgb(91, 31, 16), Color.WHITE });
LINES.put("UU6", new int[] { Color.rgb(127, 57, 115), Color.WHITE });
LINES.put("UU7", new int[] { Color.rgb(0, 153, 204), Color.WHITE });
LINES.put("UU8", new int[] { Color.rgb(24, 25, 83), Color.WHITE });
LINES.put("UU9", new int[] { Color.rgb(255, 90, 34), Color.WHITE });
LINES.put("TM1", new int[] { Color.rgb(204, 51, 0), Color.WHITE });
LINES.put("TM2", new int[] { Color.rgb(116, 192, 67), Color.WHITE });
LINES.put("TM4", new int[] { Color.rgb(208, 28, 34), Color.WHITE });
LINES.put("TM5", new int[] { Color.rgb(204, 153, 51), Color.WHITE });
LINES.put("TM6", new int[] { Color.rgb(0, 0, 255), Color.WHITE });
LINES.put("TM8", new int[] { Color.rgb(255, 102, 0), Color.WHITE });
LINES.put("TM10", new int[] { Color.rgb(0, 153, 51), Color.WHITE });
LINES.put("TM13", new int[] { Color.rgb(51, 153, 102), Color.WHITE });
LINES.put("TM17", new int[] { Color.rgb(153, 102, 51), Color.WHITE });
LINES.put("B12", new int[] { Color.rgb(153, 102, 255), Color.WHITE });
LINES.put("B16", new int[] { Color.rgb(0, 0, 255), Color.WHITE });
LINES.put("B18", new int[] { Color.rgb(255, 102, 0), Color.WHITE });
LINES.put("B21", new int[] { Color.rgb(153, 102, 255), Color.WHITE });
LINES.put("B27", new int[] { Color.rgb(153, 102, 51), Color.WHITE });
LINES.put("B37", new int[] { Color.rgb(153, 102, 51), Color.WHITE });
LINES.put("B50", new int[] { Color.rgb(51, 153, 102), Color.WHITE });
LINES.put("B60", new int[] { Color.rgb(0, 153, 51), Color.WHITE });
LINES.put("B61", new int[] { Color.rgb(0, 153, 51), Color.WHITE });
LINES.put("B62", new int[] { Color.rgb(0, 102, 51), Color.WHITE });
LINES.put("B63", new int[] { Color.rgb(51, 153, 102), Color.WHITE });
LINES.put("B67", new int[] { Color.rgb(0, 102, 51), Color.WHITE });
LINES.put("B68", new int[] { Color.rgb(0, 153, 51), Color.WHITE });
LINES.put("FF1", new int[] { Color.BLUE, Color.WHITE }); // Potsdam
LINES.put("FF10", new int[] { Color.BLUE, Color.WHITE });
LINES.put("FF11", new int[] { Color.BLUE, Color.WHITE });
LINES.put("FF12", new int[] { Color.BLUE, Color.WHITE });
LINES.put("FF21", new int[] { Color.BLUE, Color.WHITE });
LINES.put("FF23", new int[] { Color.BLUE, Color.WHITE });
LINES.put("FF24", new int[] { Color.BLUE, Color.WHITE });
// Regional lines Brandenburg:
LINES.put("RRE1", new int[] { Color.parseColor("#EE1C23"), Color.WHITE });
LINES.put("RRE2", new int[] { Color.parseColor("#FFD403"), Color.BLACK });
LINES.put("RRE3", new int[] { Color.parseColor("#F57921"), Color.WHITE });
LINES.put("RRE4", new int[] { Color.parseColor("#952D4F"), Color.WHITE });
LINES.put("RRE5", new int[] { Color.parseColor("#0072BC"), Color.WHITE });
LINES.put("RRE6", new int[] { Color.parseColor("#DB6EAB"), Color.WHITE });
LINES.put("RRE7", new int[] { Color.parseColor("#00854A"), Color.WHITE });
LINES.put("RRE10", new int[] { Color.parseColor("#A7653F"), Color.WHITE });
LINES.put("RRE11", new int[] { Color.parseColor("#059EDB"), Color.WHITE });
LINES.put("RRE11", new int[] { Color.parseColor("#EE1C23"), Color.WHITE });
LINES.put("RRE15", new int[] { Color.parseColor("#FFD403"), Color.BLACK });
LINES.put("RRE18", new int[] { Color.parseColor("#00A65E"), Color.WHITE });
LINES.put("RRB10", new int[] { Color.parseColor("#60BB46"), Color.WHITE });
LINES.put("RRB12", new int[] { Color.parseColor("#A3238E"), Color.WHITE });
LINES.put("RRB13", new int[] { Color.parseColor("#F68B1F"), Color.WHITE });
LINES.put("RRB13", new int[] { Color.parseColor("#00A65E"), Color.WHITE });
LINES.put("RRB14", new int[] { Color.parseColor("#A3238E"), Color.WHITE });
LINES.put("RRB20", new int[] { Color.parseColor("#00854A"), Color.WHITE });
LINES.put("RRB21", new int[] { Color.parseColor("#5E6DB3"), Color.WHITE });
LINES.put("RRB22", new int[] { Color.parseColor("#0087CB"), Color.WHITE });
LINES.put("ROE25", new int[] { Color.parseColor("#0087CB"), Color.WHITE });
LINES.put("RNE26", new int[] { Color.parseColor("#00A896"), Color.WHITE });
LINES.put("RNE27", new int[] { Color.parseColor("#EE1C23"), Color.WHITE });
LINES.put("RRB30", new int[] { Color.parseColor("#00A65E"), Color.WHITE });
LINES.put("RRB31", new int[] { Color.parseColor("#60BB46"), Color.WHITE });
LINES.put("RMR33", new int[] { Color.parseColor("#EE1C23"), Color.WHITE });
LINES.put("ROE35", new int[] { Color.parseColor("#5E6DB3"), Color.WHITE });
LINES.put("ROE36", new int[] { Color.parseColor("#A7653F"), Color.WHITE });
LINES.put("RRB43", new int[] { Color.parseColor("#5E6DB3"), Color.WHITE });
LINES.put("RRB45", new int[] { Color.parseColor("#FFD403"), Color.BLACK });
LINES.put("ROE46", new int[] { Color.parseColor("#DB6EAB"), Color.WHITE });
LINES.put("RMR51", new int[] { Color.parseColor("#DB6EAB"), Color.WHITE });
LINES.put("RRB51", new int[] { Color.parseColor("#DB6EAB"), Color.WHITE });
LINES.put("RRB54", new int[] { Color.parseColor("#FFD403"), Color.parseColor("#333333") });
LINES.put("RRB55", new int[] { Color.parseColor("#F57921"), Color.WHITE });
LINES.put("ROE60", new int[] { Color.parseColor("#60BB46"), Color.WHITE });
LINES.put("ROE63", new int[] { Color.parseColor("#FFD403"), Color.BLACK });
LINES.put("ROE65", new int[] { Color.parseColor("#0072BC"), Color.WHITE });
LINES.put("RRB66", new int[] { Color.parseColor("#60BB46"), Color.WHITE });
LINES.put("RPE70", new int[] { Color.parseColor("#FFD403"), Color.BLACK });
LINES.put("RPE73", new int[] { Color.parseColor("#00A896"), Color.WHITE });
LINES.put("RPE74", new int[] { Color.parseColor("#0072BC"), Color.WHITE });
LINES.put("T89", new int[] { Color.parseColor("#EE1C23"), Color.WHITE });
LINES.put("RRB91", new int[] { Color.parseColor("#A7653F"), Color.WHITE });
LINES.put("RRB93", new int[] { Color.parseColor("#A7653F"), Color.WHITE });
}
@Override
public int[] lineColors(final String line)
{
final int[] lineColors = LINES.get(line);
if (lineColors != null)
return lineColors;
else
return super.lineColors(line);
}
}