diff --git a/src/de/schildbach/pte/AbstractHafasProvider.java b/src/de/schildbach/pte/AbstractHafasProvider.java index 1f41b37e..9d48769b 100644 --- a/src/de/schildbach/pte/AbstractHafasProvider.java +++ b/src/de/schildbach/pte/AbstractHafasProvider.java @@ -273,7 +273,7 @@ public abstract class AbstractHafasProvider implements NetworkProvider Pattern.DOTALL); private static final Pattern P_XML_QUERY_DEPARTURES_FINE = Pattern.compile("" // + "fpTime\\s*=\"(\\d{1,2}:\\d{2})\"\\s*" // time - + "fpDate\\s*=\"(\\d{2}\\.\\d{2}\\.\\d{2})\"\\s*" // date + + "fpDate\\s*=\"(\\d{2}\\.\\d{2}\\.\\d{2}|\\d{4}-\\d{2}-\\d{2})\"\\s*" // date + "delay\\s*=\"(?:-|k\\.A\\.?|cancel|\\+?\\s*(\\d+))\"\\s*" // delay + "(?:e_delay\\s*=\"\\d+\"\\s*)?" // (???) + "(?:newpl\\s*=\"([^\"]*)\"\\s*)?" // @@ -283,6 +283,7 @@ public abstract class AbstractHafasProvider implements NetworkProvider + "prod\\s*=\"([^\"]*)\"\\s*" // line + "(?:class\\s*=\"[^\"]*\"\\s*)?" // (???) + "(?:dir\\s*=\"[^\"]*\"\\s*)?" // (destination) + + "(?:capacity\\s*=\"[^\"]*\"\\s*)?" // (???) + "(?:depStation\\s*=\"(.*?)\"\\s*)?" // + "(?:delayReason\\s*=\"([^\"]*)\"\\s*)?" // message + "(?:is_reachable\\s*=\"([^\"]*)\"\\s*)?" // (???) @@ -329,7 +330,11 @@ public abstract class AbstractHafasProvider implements NetworkProvider final Calendar plannedTime = new GregorianCalendar(timeZone()); plannedTime.clear(); ParserUtils.parseEuropeanTime(plannedTime, mFine.group(1)); - ParserUtils.parseGermanDate(plannedTime, mFine.group(2)); + final String dateStr = mFine.group(2); + if (dateStr.length() == 8) + ParserUtils.parseGermanDate(plannedTime, dateStr); + else + ParserUtils.parseIsoDate(plannedTime, dateStr); final Calendar predictedTime; if (mFine.group(3) != null) @@ -815,13 +820,13 @@ public abstract class AbstractHafasProvider implements NetworkProvider throw new UnsupportedOperationException(); } - private static final Pattern P_XML_NEARBY_STATIONS_COARSE = Pattern.compile("\\G(?:\n|\\z)", Pattern.DOTALL); + private static final Pattern P_XML_NEARBY_STATIONS_COARSE = Pattern.compile("\\G<\\s*St\\s*(.*?)/?>(?:\n|\\z)", Pattern.DOTALL); private static final Pattern P_XML_NEARBY_STATIONS_FINE = Pattern.compile("" // + "evaId=\"(\\d+)\"\\s*" // id + "name=\"([^\"]+)\".*?" // name - + "x=\"(\\d+)\"\\s*" // x - + "y=\"(\\d+)\"\\s*" // y - ); + + "(?:x=\"(\\d+)\"\\s*)?" // x + + "(?:y=\"(\\d+)\"\\s*)?" // y + , Pattern.DOTALL); private static final Pattern P_XML_NEARBY_STATIONS_MESSAGES = Pattern.compile(". + */ + +package de.schildbach.pte; + +import java.io.IOException; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import de.schildbach.pte.dto.Location; +import de.schildbach.pte.dto.NearbyStationsResult; +import de.schildbach.pte.dto.QueryDeparturesResult; +import de.schildbach.pte.util.ParserUtils; + +/** + * @author Andreas Schildbach + */ +public class ZvvProvider extends AbstractHafasProvider +{ + public static final NetworkId NETWORK_ID = NetworkId.ZVV; + private static final String API_BASE = "http://onlinev2.fahrplan.zvv.ch/bin/"; // http://online.fahrplan.zvv.ch/bin/ + + public ZvvProvider() + { + super(null, null); + } + + public NetworkId id() + { + return NETWORK_ID; + } + + public boolean hasCapabilities(Capability... capabilities) + { + for (final Capability capability : capabilities) + if (capability == Capability.DEPARTURES) + return true; + + return false; + } + + private static final String AUTOCOMPLETE_URI = API_BASE + + "ajax-getstop.exe/dny?start=1&tpl=suggest2json&REQ0JourneyStopsS0A=255&REQ0JourneyStopsS0B=5&REQ0JourneyStopsB=12&getstop=1&noSession=yes&REQ0JourneyStopsS0G=%s?&js=true&"; + private static final String ENCODING = "ISO-8859-1"; + + @Override + public List autocompleteStations(final CharSequence constraint) throws IOException + { + final String uri = String.format(AUTOCOMPLETE_URI, ParserUtils.urlEncode(constraint.toString(), ENCODING)); + + return ajaxGetStops(uri); + } + + @Override + protected String nearbyStationUri(String stationId) + { + throw new UnsupportedOperationException(); + } + + @Override + public NearbyStationsResult nearbyStations(final String stationId, final int lat, final int lon, final int maxDistance, final int maxStations) + throws IOException + { + final StringBuilder uri = new StringBuilder(API_BASE); + uri.append("stboard.exe/dn"); + uri.append("?productsFilter=1111111111"); + uri.append("&boardType=dep"); + uri.append("&input=").append(ParserUtils.urlEncode(stationId)); + uri.append("&sTI=1&start=yes&hcount=0"); + uri.append("&L=vs_java3"); + + // &inputTripelId=A%3d1%40O%3dCopenhagen%20Airport%40X%3d12646941%40Y%3d55629753%40U%3d86%40L%3d900000011%40B%3d1 + + return xmlNearbyStations(uri.toString()); + } + + private static final Pattern P_NORMALIZE_LINE_AND_TYPE = Pattern.compile("([^#]*)#(.*)"); + + @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); + + 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); + } + + private static final Pattern P_NORMALIZE_LINE_S = Pattern.compile("S\\d+"); + + @Override + protected char normalizeType(final String type) + { + final String ucType = type.toUpperCase(); + + // E-Bus: Bus, Tram oder Zug? + // TX: Ruftaxi? + + if ("ICN".equals(ucType)) + return 'I'; + + if ("D".equals(ucType)) + return 'R'; + if ("EXT".equals(ucType)) + return 'R'; + + if ("S-BAHN".equals(ucType)) + return 'S'; + if (P_NORMALIZE_LINE_S.matcher(type).matches()) + return 'S'; + + if ("T".equals(ucType)) + return 'T'; + if ("TRM".equals(ucType)) + return 'T'; + if ("TRM-NF".equals(ucType)) // Niederflur + return 'T'; + + if ("BUS-NF".equals(ucType)) // Niederflur + return 'B'; + if ("TRO".equals(ucType)) + return 'B'; + + if ("SCHIFF".equals(ucType)) + return 'F'; + if ("D-SCHIFF".equals(ucType)) + return 'F'; + if ("FAE".equals(ucType)) + return 'F'; + + if ("BERGBAHN".equals(ucType)) + return 'C'; + if ("SEILBAHN".equals(ucType)) + return 'C'; + if ("FUN".equals(ucType)) // Standseilbahn + return 'C'; + if ("GB".equals(ucType)) // Gondelbahn + return 'C'; + if ("LB".equals(ucType)) // Luftseilbahn + return 'C'; + + if ("UNB".equals(ucType)) + return '?'; + + final char t = normalizeCommonTypes(ucType); + if (t != 0) + return t; + + return 0; + } + + public QueryDeparturesResult queryDepartures(final String stationId, final int maxDepartures, final boolean equivs) throws IOException + { + final StringBuilder uri = new StringBuilder(); + uri.append(API_BASE).append("stboard.exe/dn"); + uri.append("?productsFilter=1111111111"); + uri.append("&boardType=dep"); + uri.append("&maxJourneys=50"); // ignore maxDepartures because result contains other stations + uri.append("&start=yes"); + uri.append("&L=vs_java3"); + uri.append("&input=").append(stationId); + + return xmlQueryDepartures(uri.toString(), Integer.parseInt(stationId)); + } +} diff --git a/src/de/schildbach/pte/util/ParserUtils.java b/src/de/schildbach/pte/util/ParserUtils.java index 56770c42..28ec8125 100644 --- a/src/de/schildbach/pte/util/ParserUtils.java +++ b/src/de/schildbach/pte/util/ParserUtils.java @@ -263,6 +263,19 @@ public final class ParserUtils return builder.toString(); } + private static final Pattern P_ISO_DATE = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})"); + + public static final void parseIsoDate(final Calendar calendar, final CharSequence str) + { + final Matcher m = P_ISO_DATE.matcher(str); + if (!m.matches()) + throw new RuntimeException("cannot parse: '" + str + "'"); + + calendar.set(Calendar.YEAR, Integer.parseInt(m.group(3))); + calendar.set(Calendar.MONTH, Integer.parseInt(m.group(2)) - 1); + calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(m.group(3))); + } + private static final Pattern P_GERMAN_DATE = Pattern.compile("(\\d{2})[\\./](\\d{2})[\\./](\\d{2,4})"); public static final void parseGermanDate(final Calendar calendar, final CharSequence str) diff --git a/test/de/schildbach/pte/live/ZvvProviderLiveTest.java b/test/de/schildbach/pte/live/ZvvProviderLiveTest.java new file mode 100644 index 00000000..c0538eec --- /dev/null +++ b/test/de/schildbach/pte/live/ZvvProviderLiveTest.java @@ -0,0 +1,67 @@ +/* + * Copyright 2010, 2011 the original author or authors. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package de.schildbach.pte.live; + +import java.util.List; + +import org.junit.Test; + +import de.schildbach.pte.ZvvProvider; +import de.schildbach.pte.dto.Location; +import de.schildbach.pte.dto.NearbyStationsResult; +import de.schildbach.pte.dto.QueryDeparturesResult; + +/** + * @author Andreas Schildbach + */ +public class ZvvProviderLiveTest +{ + private final ZvvProvider provider = new ZvvProvider(); + + @Test + public void autocomplete() throws Exception + { + final List autocompletes = provider.autocompleteStations("Flughafen"); + + list(autocompletes); + } + + private void list(final List autocompletes) + { + System.out.print(autocompletes.size() + " "); + for (final Location autocomplete : autocompletes) + System.out.print(autocomplete.toDebugString() + " "); + System.out.println(); + } + + @Test + public void nearbyStation() throws Exception + { + final NearbyStationsResult result = provider.nearbyStations("183400", 0, 0, 0, 0); + + System.out.println(result.stations.size() + " " + result.stations); + } + + @Test + public void queryDepartures() throws Exception + { + final QueryDeparturesResult result = provider.queryDepartures("183400", 0, false); + + System.out.println(result.stationDepartures); + } +}