(4);
Date lastTime = currentDate;
Date firstDepartureTime = null;
String firstDeparture = null;
Date lastArrivalTime = null;
String lastArrival = null;
final Matcher mDetCoarse = P_CONNECTION_DETAILS_COARSE.matcher(page);
while (mDetCoarse.find())
{
final String set = mDetCoarse.group(2) + mDetCoarse.group(3) + mDetCoarse.group(4);
if (!set.contains(SITZENBLEIBER))
{
final Matcher mDetFine = P_CONNECTION_DETAILS_FINE.matcher(set);
if (mDetFine.matches())
{
if (mDetFine.group(8) == null)
{
final Date departureTime = upTime(lastTime, ParserUtils.joinDateTime(currentDate, ParserUtils
.parseTime(mDetFine.group(1))));
final String departure = ParserUtils.resolveEntities(mDetFine.group(2));
if (departure != null && firstDeparture == null)
firstDeparture = departure;
final String product = ParserUtils.resolveEntities(mDetFine.group(3));
final String line = ParserUtils.resolveEntities(mDetFine.group(4));
final String destination = ParserUtils.resolveEntities(mDetFine.group(5));
final Date arrivalTime = upTime(lastTime, ParserUtils.joinDateTime(currentDate, ParserUtils.parseTime(mDetFine.group(6))));
final String arrival = ParserUtils.resolveEntities(mDetFine.group(7));
final String normalizedLine = normalizeLine(product, line);
parts.add(new Connection.Trip(normalizedLine, LINES.get(normalizedLine), destination, departureTime, null, 0, departure,
arrivalTime, null, 0, arrival));
if (firstDepartureTime == null)
firstDepartureTime = departureTime;
lastArrival = arrival;
lastArrivalTime = arrivalTime;
}
else
{
final String departure = ParserUtils.resolveEntities(mDetFine.group(8));
if (departure != null && firstDeparture == null)
firstDeparture = departure;
final String min = mDetFine.group(9);
final String arrival = ParserUtils.resolveEntities(mDetFine.group(10));
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.departure, arrival));
}
else
{
parts.add(new Connection.Footway(Integer.parseInt(min), departure, arrival));
}
lastArrival = arrival;
}
}
else
{
throw new IllegalArgumentException("cannot parse '" + set + "' on " + uri);
}
}
}
if (firstDepartureTime == null && lastArrivalTime == null && parts.size() == 1 && parts.get(0) instanceof Connection.Footway)
{
final Calendar calendar = new GregorianCalendar();
firstDepartureTime = calendar.getTime();
calendar.add(Calendar.MINUTE, ((Connection.Footway) parts.get(0)).min);
lastArrivalTime = calendar.getTime();
}
return new GetConnectionDetailsResult(new Date(), new Connection(ParserUtils.extractId(uri), uri, firstDepartureTime, lastArrivalTime,
null, null, 0, firstDeparture, 0, lastArrival, parts));
}
else
{
if (P_CONNECTION_DETAILS_ERRORS.matcher(page).find())
throw new SessionExpiredException();
else
throw new IOException(page.toString());
}
}
private static Date upTime(final Date lastTime, Date time)
{
while (time.before(lastTime))
time = ParserUtils.addDays(time, 1);
lastTime.setTime(time.getTime());
return time;
}
private static final String DEPARTURE_URL = "http://efa.mvv-muenchen.de/mobile/XSLT_DM_REQUEST?typeInfo_dm=stopID&mode=direct&nameInfo_dm=";
public String departuresQueryUri(final String stationId, final int maxDepartures)
{
return DEPARTURE_URL + stationId;
}
private static final Pattern P_DEPARTURES_HEAD_COARSE = Pattern.compile(".*?(.*?Linie/Richtung.*?).*?", Pattern.DOTALL);
private static final Pattern P_DEPARTURES_HEAD_FINE = Pattern.compile(".*?" //
+ "Von:[\\xa0\\s]*(.*?)
.*?" // location
+ "Datum:[\\xa0\\s]*\\w{2}\\.,\\s(\\d+)\\.\\s(\\w{3})\\.[\\xa0\\s]+(\\d{4})
.*?" // date
, Pattern.DOTALL);
private static final Pattern P_DEPARTURES_COARSE = Pattern.compile("(.+?)
", Pattern.DOTALL);
private static final Pattern P_DEPARTURES_FINE = Pattern.compile(".*?" //
+ "(?:[\\xa0\\s]*[\\xa0\\s]*(\\d+)\\.(\\d+)\\.[\\xa0\\s]*)?" // date
+ "(\\d+):(\\d+).*?" // time
+ "(?:
.*?)?" // product
+ "\\s*([^<]*?)[\\xa0\\s]*(?:.*?)?" // line
+ " \\s*(.*?)\\s* .*?" // destination
, Pattern.DOTALL);
private static final Pattern P_DEPARTURES_URI_STATION_ID = Pattern.compile("nameInfo_dm=(\\d+)");
public QueryDeparturesResult queryDepartures(final String uri) throws IOException
{
final CharSequence page = ParserUtils.scrape(uri);
final Matcher mStationId = P_DEPARTURES_URI_STATION_ID.matcher(uri);
if (!mStationId.find())
throw new IllegalStateException(uri);
final int stationId = Integer.parseInt(mStationId.group(1));
final Matcher mHeadCoarse = P_DEPARTURES_HEAD_COARSE.matcher(page);
if (mHeadCoarse.matches())
{
final Matcher mHeadFine = P_DEPARTURES_HEAD_FINE.matcher(mHeadCoarse.group(1));
if (mHeadFine.matches())
{
final String location = ParserUtils.resolveEntities(mHeadFine.group(1));
final Date currentTime = parseDate(mHeadFine.group(2), mHeadFine.group(3), mHeadFine.group(4));
final List departures = new ArrayList(8);
final Calendar calendar = new GregorianCalendar();
final Matcher mDepCoarse = P_DEPARTURES_COARSE.matcher(mHeadCoarse.group(1));
while (mDepCoarse.find())
{
final Matcher mDepFine = P_DEPARTURES_FINE.matcher(mDepCoarse.group(1));
if (mDepFine.matches())
{
calendar.setTime(currentTime);
final String day = mDepFine.group(1);
if (day != null)
calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
final String month = mDepFine.group(2);
if (month != null)
calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1);
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(mDepFine.group(3)));
calendar.set(Calendar.MINUTE, Integer.parseInt(mDepFine.group(4)));
final String normalizedLine = normalizeLine(mDepFine.group(5), mDepFine.group(6));
final String destination = normalizeStationName(mDepFine.group(7));
final Departure departure = new Departure(calendar.getTime(), normalizedLine, LINES.get(normalizedLine), 0, destination);
departures.add(departure);
}
else
{
throw new IllegalArgumentException("cannot parse '" + mDepCoarse.group(1) + "' on " + uri);
}
}
return new QueryDeparturesResult(uri, stationId, location, currentTime, departures);
}
else
{
throw new IllegalArgumentException("cannot parse '" + mHeadCoarse.group(1) + "' on " + uri);
}
}
else
{
return new QueryDeparturesResult(uri, Status.NO_INFO);
}
}
private static final Pattern P_STATION_NAME_WHITESPACE = Pattern.compile("\\s+");
private String normalizeStationName(String name)
{
return P_STATION_NAME_WHITESPACE.matcher(name).replaceAll(" ");
}
private static final Pattern P_NORMALIZE_LINE_TRAM = Pattern.compile("[12]\\d");
private static final Pattern P_NORMALIZE_LINE_NACHTTRAM = Pattern.compile("N[12]\\d");
private static final Pattern P_NORMALIZE_LINE_METROBUS = Pattern.compile("[56]\\d");
private static final Pattern P_NORMALIZE_LINE_STADTBUS = Pattern.compile("1\\d{2}");
private static final Pattern P_NORMALIZE_LINE_NACHTBUS = Pattern.compile("N[48]\\d");
private static final Pattern P_NORMALIZE_LINE_REGIONALBUS = Pattern.compile("\\d{3}[A-Z]?");
private String normalizeLine(final String product, final String line)
{
if (product == null)
{
if (P_NORMALIZE_LINE_METROBUS.matcher(line).matches())
return "B" + line;
if (P_NORMALIZE_LINE_STADTBUS.matcher(line).matches())
return "B" + line;
if (P_NORMALIZE_LINE_NACHTBUS.matcher(line).matches())
return "B" + line;
if (line.equals("N117")) // Ersatzbus für N17
return "BN117";
if (P_NORMALIZE_LINE_REGIONALBUS.matcher(line).matches())
return "B" + line;
if (line.equals("Schienenersatzverkehr"))
return "BSEV";
if (P_NORMALIZE_LINE_TRAM.matcher(line).matches())
return "T" + line;
if (P_NORMALIZE_LINE_NACHTTRAM.matcher(line).matches())
return "T" + line;
if (LINES.containsKey("S" + line))
return "S" + line;
if (line.equals("S20/27"))
return "S" + line;
if (LINES.containsKey("U" + line))
return "U" + line;
if (line.startsWith("RE "))
return "R" + line;
if (line.startsWith("RB "))
return "R" + line;
if (line.startsWith("ALX ")) // Alex
return "R" + line;
if (line.startsWith("BOB ")) // Bayerische Oberlandbahn
return "R" + line;
if (line.startsWith("VBG ")) // Vogtlandbahn
return "R" + line;
if (line.startsWith("ICE "))
return "I" + line;
if (line.startsWith("IC "))
return "I" + line;
if (line.startsWith("EC "))
return "I" + line;
if (line.startsWith("CNL "))
return "I" + line;
throw new IllegalStateException("cannot normalize null product, line " + line);
}
else if (product.equals("Bus"))
{
if (line.startsWith("Bus"))
return "B" + line.substring(4);
else if (line.startsWith("StadtBus"))
return "B" + line.substring(9);
else if (line.startsWith("MetroBus"))
return "B" + line.substring(9);
else if (line.startsWith("Regionalbus"))
return "B" + line.substring(12);
else
return "B" + line;
}
else if (product.equals("Tram"))
{
if (line.startsWith("Tram"))
return "T" + line.substring(5);
else
return "T" + line;
}
else if (product.equals("U-Bahn"))
{
if (line.startsWith("U-Bahn"))
return "U" + line.substring(7);
else
return "U" + line;
}
else if (product.equals("S-Bahn"))
{
if (line.startsWith("S-Bahn"))
return "S" + line.substring(7);
else
return "S" + line;
}
else if (product.equals("Zug"))
{
final String[] lineParts = line.split("\\s+");
final String type = lineParts[0];
final String number = lineParts[1];
if (type.equals("IC"))
return "I" + type + number;
if (type.equals("ICE"))
return "I" + type + number;
if (type.equals("EC"))
return "I" + type + number;
if (type.equals("EN")) // EuroNight
return "I" + type + number;
if (type.equals("CNL"))
return "I" + type + number;
if (type.equals("RJ")) // Railjet, Österreich
return "I" + type + number;
if (type.equals("IRE")) // Franken-Sachsen-Express
return "I" + type + number;
if (type.equals("RB"))
return "R" + type + number;
if (type.equals("RE"))
return "R" + type + number;
if (type.equals("D"))
return "R" + type + number;
if (type.equals("BOB"))
return "R" + type + number;
if (type.equals("BRB")) // Bayerische Regiobahn
return "R" + type + number;
if (type.equals("ALX")) // Länderbahn und Vogtlandbahn
return "R" + type + number;
throw new IllegalStateException("cannot normalize product " + product + " line " + line);
}
else if (product.equals("Schiff"))
{
return "F" + line;
}
else if (product.equals("Seilbahn")) // strangely marked as 'Seilbahn', but means 'Schienenersatzverkehr'
{
return "BSEV" + line;
}
throw new IllegalStateException("cannot normalize product " + product + " line " + line);
}
private static Date parseDate(final String day, final String month, final String year)
{
final Calendar calendar = new GregorianCalendar();
calendar.clear();
calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
calendar.set(Calendar.MONTH, parseMonth(month));
calendar.set(Calendar.YEAR, Integer.parseInt(year));
return calendar.getTime();
}
private final static String[] MONTHS = new String[] { "Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez" };
private static int parseMonth(final String month)
{
for (int m = 0; m < MONTHS.length; m++)
if (MONTHS[m].equals(month))
return m;
throw new IllegalArgumentException("cannot parse month: " + month);
}
private static final Map LINES = new HashMap();
static
{
LINES.put("I", new int[] { Color.WHITE, Color.RED, Color.RED }); // generic
LINES.put("R", new int[] { Color.WHITE, Color.RED, Color.RED }); // generic
LINES.put("S", new int[] { Color.parseColor("#006e34"), Color.WHITE }); // generic
LINES.put("U", new int[] { Color.parseColor("#003090"), Color.WHITE }); // generic
LINES.put("SS1", new int[] { Color.parseColor("#00ccff"), Color.WHITE });
LINES.put("SS2", new int[] { Color.parseColor("#66cc00"), Color.WHITE });
LINES.put("SS3", new int[] { Color.parseColor("#880099"), Color.WHITE });
LINES.put("SS4", new int[] { Color.parseColor("#ff0033"), Color.WHITE });
LINES.put("SS6", new int[] { Color.parseColor("#00aa66"), Color.WHITE });
LINES.put("SS7", new int[] { Color.parseColor("#993333"), Color.WHITE });
LINES.put("SS8", new int[] { Color.BLACK, Color.parseColor("#ffcc00") });
LINES.put("SS20", new int[] { Color.BLACK, Color.parseColor("#ffaaaa") });
LINES.put("SS27", new int[] { Color.parseColor("#ffaaaa"), Color.WHITE });
LINES.put("SA", new int[] { Color.parseColor("#231f20"), Color.WHITE });
LINES.put("T12", new int[] { Color.parseColor("#883388"), Color.WHITE });
LINES.put("T15", new int[] { Color.parseColor("#3366CC"), Color.WHITE });
LINES.put("T16", new int[] { Color.parseColor("#CC8833"), Color.WHITE });
LINES.put("T17", new int[] { Color.parseColor("#993333"), Color.WHITE });
LINES.put("T18", new int[] { Color.parseColor("#66bb33"), Color.WHITE });
LINES.put("T19", new int[] { Color.parseColor("#cc0000"), Color.WHITE });
LINES.put("T20", new int[] { Color.parseColor("#00bbee"), Color.WHITE });
LINES.put("T21", new int[] { Color.parseColor("#33aa99"), Color.WHITE });
LINES.put("T23", new int[] { Color.parseColor("#fff000"), Color.WHITE });
LINES.put("T25", new int[] { Color.parseColor("#ff9999"), Color.WHITE });
LINES.put("T27", new int[] { Color.parseColor("#ff6600"), Color.WHITE });
LINES.put("TN17", new int[] { Color.parseColor("#999999"), Color.parseColor("#ffff00") });
LINES.put("TN19", new int[] { Color.parseColor("#999999"), Color.parseColor("#ffff00") });
LINES.put("TN20", new int[] { Color.parseColor("#999999"), Color.parseColor("#ffff00") });
LINES.put("TN27", new int[] { Color.parseColor("#999999"), Color.parseColor("#ffff00") });
LINES.put("UU1", new int[] { Color.parseColor("#227700"), Color.WHITE });
LINES.put("UU2", new int[] { Color.parseColor("#bb0000"), Color.WHITE });
LINES.put("UU2E", new int[] { Color.parseColor("#bb0000"), Color.WHITE });
LINES.put("UU3", new int[] { Color.parseColor("#ee8800"), Color.WHITE });
LINES.put("UU4", new int[] { Color.parseColor("#00ccaa"), Color.WHITE });
LINES.put("UU5", new int[] { Color.parseColor("#bb7700"), Color.WHITE });
LINES.put("UU6", new int[] { Color.parseColor("#0000cc"), Color.WHITE });
}
public int[] lineColors(final String line)
{
return LINES.get(line);
}
}
|