adapt to API change

git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@733 0924bc21-9374-b0fa-ee44-9ff1593b38f0
This commit is contained in:
andreas.schildbach@gmail.com 2011-07-22 09:29:14 +00:00
parent 37bec3b3cb
commit 457e29ff49

View file

@ -145,31 +145,24 @@ public class ShProvider extends AbstractHafasProvider
return uri.toString(); return uri.toString();
} }
private static final Pattern P_DEPARTURES_HEAD_COARSE = Pattern private static final Pattern P_DEPARTURES_HEAD_COARSE = Pattern.compile(".*?" //
.compile( + "(?:" //
".*?" // + "Bhf\\./Haltest\\.:</span>\\n<span class=\"output\">([^<]*)<.*?" // location
+ "(?:" // + "Fahrplan:</span>\\n<span class=\"output\">\n" //
+ "<table class=\"hafasResult\"[^>]*>(.+?)</table>.*?" // + "(\\d{2}\\.\\d{2}\\.\\d{2})[^\n]*,\n" // date
+ "(?:<table cellspacing=\"0\" class=\"hafasResult\"[^>]*>(.+?)</table>|(verkehren an dieser Haltestelle keine))"//
+ "|(Eingabe kann nicht interpretiert)|(Verbindung zum Server konnte leider nicht hergestellt werden|kann vom Server derzeit leider nicht bearbeitet werden))" //
+ ".*?" //
, Pattern.DOTALL);
private static final Pattern P_DEPARTURES_HEAD_FINE = Pattern.compile(".*?" //
+ "<td class=\"querysummary screennowrap\">\\s*(.*?)\\s*<.*?" // location
+ "(\\d{2}\\.\\d{2}\\.\\d{2}).*?" // date
+ "Abfahrt (\\d{1,2}:\\d{2}).*?" // time + "Abfahrt (\\d{1,2}:\\d{2}).*?" // time
+ "<table class=\"resultTable\"[^>]*>(.+?)</table>" // content
+ "|(verkehren an dieser Haltestelle keine)" //
+ "|(Eingabe kann nicht interpretiert)" //
+ "|(Verbindung zum Server konnte leider nicht hergestellt werden|kann vom Server derzeit leider nicht bearbeitet werden))" //
+ ".*?" //
, Pattern.DOTALL); , Pattern.DOTALL);
private static final Pattern P_DEPARTURES_COARSE = Pattern.compile("<tr class=\"(depboard-\\w*)\">(.*?)</tr>", Pattern.DOTALL); private static final Pattern P_DEPARTURES_COARSE = Pattern.compile("<tr class=\"(depboard-\\w*)\">(.*?)</tr>", Pattern.DOTALL);
private static final Pattern P_DEPARTURES_FINE = Pattern.compile(".*?" // private static final Pattern P_DEPARTURES_FINE = Pattern.compile("\n" //
+ "<td class=\"[\\w ]*\">(\\d{1,2}:\\d{2})</td>\n" // plannedTime + "<td class=\"time\">(\\d{1,2}:\\d{2}).*?" // plannedTime
+ "(?:<td class=\"[\\w ]*prognosis[\\w ]*\">\n" // + "<img class=\"product\" src=\"/hafas-res/img/products/(\\w+)_pic\\.png\"[^>]*>\\s*([^<]*)<.*?" // type,line
+ "(?:&nbsp;|<span class=\"rtLimit\\d\">(p&#252;nktlich|\\d{1,2}:\\d{2})</span>)\n</td>\n" // predictedTime + "<a href=\"http://nah\\.sh\\.hafas\\.de/bin/stboard\\.exe/dn\\?input=(\\d+)&[^>]*>\n" // destinationId
+ ")?.*?" // + "([^\n]*)\n.*?" // destination
+ "<img src=\"/hafas-res/img/(\\w+)_pic\\.gif\"[^>]*>\\s*(.*?)\\s*</.*?" // type, line
+ "<span class=\"bold\">\n" //
+ "<a href=\"http://scout\\.hafas\\.de/bin/stboard\\.exe/dn\\?input=(\\d+)&[^>]*>" // destinationId
+ "\\s*(.*?)\\s*</a>\n" // destination
+ "</span>.*?" //
+ "(?:<td class=\"center sepline top\">\n(" + ParserUtils.P_PLATFORM + ").*?)?" // position + "(?:<td class=\"center sepline top\">\n(" + ParserUtils.P_PLATFORM + ").*?)?" // position
, Pattern.DOTALL); , Pattern.DOTALL);
@ -186,96 +179,69 @@ public class ShProvider extends AbstractHafasProvider
if (mHeadCoarse.matches()) if (mHeadCoarse.matches())
{ {
// messages // messages
if (mHeadCoarse.group(3) != null) if (mHeadCoarse.group(5) != null)
{ {
result.stationDepartures.add(new StationDepartures(new Location(LocationType.STATION, stationId), result.stationDepartures.add(new StationDepartures(new Location(LocationType.STATION, stationId),
Collections.<Departure> emptyList(), null)); Collections.<Departure> emptyList(), null));
return result; return result;
} }
else if (mHeadCoarse.group(4) != null) else if (mHeadCoarse.group(6) != null)
return new QueryDeparturesResult(Status.INVALID_STATION); return new QueryDeparturesResult(Status.INVALID_STATION);
else if (mHeadCoarse.group(5) != null) else if (mHeadCoarse.group(7) != null)
return new QueryDeparturesResult(Status.SERVICE_DOWN); return new QueryDeparturesResult(Status.SERVICE_DOWN);
final Matcher mHeadFine = P_DEPARTURES_HEAD_FINE.matcher(mHeadCoarse.group(1)); final String location = ParserUtils.resolveEntities(mHeadCoarse.group(1));
if (mHeadFine.matches()) final Calendar currentTime = new GregorianCalendar(timeZone());
{ currentTime.clear();
final String location = ParserUtils.resolveEntities(mHeadFine.group(1)); ParserUtils.parseGermanDate(currentTime, mHeadCoarse.group(2));
final Calendar currentTime = new GregorianCalendar(timeZone()); ParserUtils.parseEuropeanTime(currentTime, mHeadCoarse.group(3));
currentTime.clear();
ParserUtils.parseGermanDate(currentTime, mHeadFine.group(2));
ParserUtils.parseEuropeanTime(currentTime, mHeadFine.group(3));
final List<Departure> departures = new ArrayList<Departure>(8);
String oldZebra = null;
final Matcher mDepCoarse = P_DEPARTURES_COARSE.matcher(mHeadCoarse.group(2)); final List<Departure> departures = new ArrayList<Departure>(8);
while (mDepCoarse.find()) String oldZebra = null;
final Matcher mDepCoarse = P_DEPARTURES_COARSE.matcher(mHeadCoarse.group(4));
while (mDepCoarse.find())
{
final String zebra = mDepCoarse.group(1);
if (oldZebra != null && zebra.equals(oldZebra))
throw new IllegalArgumentException("missed row? last:" + zebra);
else
oldZebra = zebra;
final Matcher mDepFine = P_DEPARTURES_FINE.matcher(mDepCoarse.group(2));
if (mDepFine.matches())
{ {
final String zebra = mDepCoarse.group(1); final Calendar plannedTime = new GregorianCalendar(timeZone());
if (oldZebra != null && zebra.equals(oldZebra)) plannedTime.setTimeInMillis(currentTime.getTimeInMillis());
throw new IllegalArgumentException("missed row? last:" + zebra); ParserUtils.parseEuropeanTime(plannedTime, mDepFine.group(1));
else
oldZebra = zebra;
final Matcher mDepFine = P_DEPARTURES_FINE.matcher(mDepCoarse.group(2)); if (plannedTime.getTimeInMillis() - currentTime.getTimeInMillis() < -PARSER_DAY_ROLLOVER_THRESHOLD_MS)
if (mDepFine.matches()) plannedTime.add(Calendar.DAY_OF_MONTH, 1);
{
final Calendar plannedTime = new GregorianCalendar(timeZone());
plannedTime.setTimeInMillis(currentTime.getTimeInMillis());
ParserUtils.parseEuropeanTime(plannedTime, mDepFine.group(1));
if (plannedTime.getTimeInMillis() - currentTime.getTimeInMillis() < -PARSER_DAY_ROLLOVER_THRESHOLD_MS) final String lineType = mDepFine.group(2);
plannedTime.add(Calendar.DAY_OF_MONTH, 1);
final Calendar predictedTime; final String line = normalizeLine(lineType, ParserUtils.resolveEntities(mDepFine.group(3).trim()));
final String prognosis = ParserUtils.resolveEntities(mDepFine.group(2));
if (prognosis != null)
{
predictedTime = new GregorianCalendar(timeZone());
if (prognosis.equals("pünktlich"))
{
predictedTime.setTimeInMillis(plannedTime.getTimeInMillis());
}
else
{
predictedTime.setTimeInMillis(currentTime.getTimeInMillis());
ParserUtils.parseEuropeanTime(predictedTime, prognosis);
}
}
else
{
predictedTime = null;
}
final String lineType = mDepFine.group(3); final int destinationId = mDepFine.group(4) != null ? Integer.parseInt(mDepFine.group(4)) : 0;
final String line = normalizeLine(lineType, ParserUtils.resolveEntities(mDepFine.group(4))); final String destination = ParserUtils.resolveEntities(mDepFine.group(5));
final int destinationId = mDepFine.group(5) != null ? Integer.parseInt(mDepFine.group(5)) : 0; final String position = mDepFine.group(6) != null ? "Gl. " + ParserUtils.resolveEntities(mDepFine.group(6)) : null;
final String destination = ParserUtils.resolveEntities(mDepFine.group(6)); final Departure dep = new Departure(plannedTime.getTime(), null, new Line(null, line, line != null ? lineColors(line) : null),
position, destinationId, destination, null, null);
final String position = mDepFine.group(7) != null ? "Gl. " + ParserUtils.resolveEntities(mDepFine.group(7)) : null; if (!departures.contains(dep))
departures.add(dep);
final Departure dep = new Departure(plannedTime.getTime(), predictedTime != null ? predictedTime.getTime() : null, new Line(
null, line, line != null ? lineColors(line) : null), position, destinationId, destination, null, null);
if (!departures.contains(dep))
departures.add(dep);
}
else
{
throw new IllegalArgumentException("cannot parse '" + mDepCoarse.group(2) + "' on " + stationId);
}
} }
else
{
throw new IllegalArgumentException("cannot parse '" + mDepCoarse.group(2) + "' on " + stationId);
}
}
result.stationDepartures.add(new StationDepartures(new Location(LocationType.STATION, stationId, null, location), departures, null)); result.stationDepartures.add(new StationDepartures(new Location(LocationType.STATION, stationId, null, location), departures, null));
return result; return result;
}
else
{
throw new IllegalArgumentException("cannot parse '" + mHeadCoarse.group(1) + "' on " + stationId);
}
} }
else else
{ {