From b105904d0980a649f4aef25d6c48d1c1f8d1e957 Mon Sep 17 00:00:00 2001 From: "andreas.schildbach@gmail.com" Date: Thu, 31 Mar 2011 14:30:41 +0000 Subject: [PATCH] parse messages git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@543 0924bc21-9374-b0fa-ee44-9ff1593b38f0 --- src/de/schildbach/pte/BvgProvider.java | 66 +++++++++++++++++++------- 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/src/de/schildbach/pte/BvgProvider.java b/src/de/schildbach/pte/BvgProvider.java index 960ee60a..3c5fa38e 100644 --- a/src/de/schildbach/pte/BvgProvider.java +++ b/src/de/schildbach/pte/BvgProvider.java @@ -608,18 +608,12 @@ public final class BvgProvider extends AbstractHafasProvider return uri.toString(); } - private static final Pattern P_DEPARTURES_HEAD = Pattern.compile(".*?" // + private static final Pattern P_DEPARTURES_PLAN_HEAD = Pattern.compile(".*?" // + "(.*?).*?Datum:\\s*([^<\n]+)[<\n].*?" // , Pattern.DOTALL); - private static final Pattern P_DEPARTURES_COARSE = Pattern.compile("" // + private static final Pattern P_DEPARTURES_PLAN_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 @@ -627,11 +621,32 @@ public final class BvgProvider extends AbstractHafasProvider + "]*>" // destinationId + "\\s*(.*?)\\s*.*?" // destination , Pattern.DOTALL); - private static final Pattern P_DEPARTURES_LIVE_ERRORS = Pattern.compile("(Haltestelle:)|(Wartungsgründen)|(http-equiv=\"refresh\")", - Pattern.CASE_INSENSITIVE); private static final Pattern P_DEPARTURES_PLAN_ERRORS = Pattern.compile("(derzeit leider nicht bearbeitet werden)|(Wartungsarbeiten)|" + "(http-equiv=\"refresh\")", Pattern.CASE_INSENSITIVE); + private static final Pattern P_DEPARTURES_LIVE_HEAD = Pattern.compile(".*?" // + + "(.*?).*?Datum:\\s*([^<\n]+)[<\n].*?" // + , Pattern.DOTALL); + private static final Pattern P_DEPARTURES_LIVE_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_LIVE_MSGS_COARSE = Pattern.compile("" // + + "\\s*(.+?)\\s*" // + , Pattern.DOTALL); + private static final Pattern P_DEPARTURES_LIVE_MSGS_FINE = Pattern.compile("" // + + "\\s*(.*?)\\s*\\s*" // line + + "\\s*(\\d{2}\\.\\d{2}\\.\\d{4})\\s*\\s*" // date + + "([^<]*)" // message + , Pattern.DOTALL); + private static final Pattern P_DEPARTURES_LIVE_ERRORS = Pattern.compile("(Haltestelle:)|(Wartungsgründen)|(http-equiv=\"refresh\")", + Pattern.CASE_INSENSITIVE); + public QueryDeparturesResult queryDepartures(final String stationId, final int maxDepartures, final boolean equivs) throws IOException { final QueryDeparturesResult result = new QueryDeparturesResult(); @@ -654,17 +669,35 @@ public final class BvgProvider extends AbstractHafasProvider } // parse page - final Matcher mHead = P_DEPARTURES_HEAD.matcher(page); + final Matcher mHead = P_DEPARTURES_LIVE_HEAD.matcher(page); if (mHead.matches()) { final String location = ParserUtils.resolveEntities(mHead.group(1)); final Calendar currentTime = new GregorianCalendar(timeZone()); currentTime.clear(); parseDateTime(currentTime, mHead.group(2)); + + final Map messages = new HashMap(); + + final Matcher mMsgsCoarse = P_DEPARTURES_LIVE_MSGS_COARSE.matcher(page); + while (mMsgsCoarse.find()) + { + final Matcher mMsgsFine = P_DEPARTURES_LIVE_MSGS_FINE.matcher(mMsgsCoarse.group(1)); + if (mMsgsFine.matches()) + { + final String line = normalizeLine(ParserUtils.resolveEntities(mMsgsFine.group(1))); + final String message = ParserUtils.resolveEntities(mMsgsFine.group(3)); + messages.put(line, message); + } + else + { + throw new IllegalArgumentException("cannot parse '" + mMsgsCoarse.group(1) + "' on " + uri); + } + } + final List departures = new ArrayList(8); - // choose matcher - final Matcher mDepCoarse = P_DEPARTURES_COARSE.matcher(page); + final Matcher mDepCoarse = P_DEPARTURES_LIVE_COARSE.matcher(page); while (mDepCoarse.find()) { final Matcher mDepFine = P_DEPARTURES_LIVE_FINE.matcher(mDepCoarse.group(1)); @@ -695,7 +728,7 @@ public final class BvgProvider extends AbstractHafasProvider 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); + destinationId, destination, messages.get(line)); if (!departures.contains(dep)) departures.add(dep); } @@ -732,7 +765,7 @@ public final class BvgProvider extends AbstractHafasProvider } // parse page - final Matcher mHead = P_DEPARTURES_HEAD.matcher(page); + final Matcher mHead = P_DEPARTURES_PLAN_HEAD.matcher(page); if (mHead.matches()) { final String location = ParserUtils.resolveEntities(mHead.group(1)); @@ -741,8 +774,7 @@ public final class BvgProvider extends AbstractHafasProvider ParserUtils.parseGermanDate(currentTime, mHead.group(2)); final List departures = new ArrayList(8); - // choose matcher - final Matcher mDepCoarse = P_DEPARTURES_COARSE.matcher(page); + final Matcher mDepCoarse = P_DEPARTURES_PLAN_COARSE.matcher(page); while (mDepCoarse.find()) { final Matcher mDepFine = P_DEPARTURES_PLAN_FINE.matcher(mDepCoarse.group(1));