From c5bdc06763be1b74a21ff859f2de9a39b0cdd106 Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Wed, 20 Jun 2012 19:25:39 +0200 Subject: [PATCH] support Ireland and Dublin --- .../de/schildbach/pte/EireannProvider.java | 179 ++++++++++++++++++ enabler/src/de/schildbach/pte/NetworkId.java | 3 + .../de/schildbach/pte/util/ParserUtils.java | 26 ++- .../pte/live/EireannProviderLiveTest.java | 94 +++++++++ 4 files changed, 296 insertions(+), 6 deletions(-) create mode 100644 enabler/src/de/schildbach/pte/EireannProvider.java create mode 100644 enabler/test/de/schildbach/pte/live/EireannProviderLiveTest.java diff --git a/enabler/src/de/schildbach/pte/EireannProvider.java b/enabler/src/de/schildbach/pte/EireannProvider.java new file mode 100644 index 00000000..a9a57de9 --- /dev/null +++ b/enabler/src/de/schildbach/pte/EireannProvider.java @@ -0,0 +1,179 @@ +/* + * Copyright 2012 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; + +import java.io.IOException; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import de.schildbach.pte.dto.Line; +import de.schildbach.pte.dto.Location; +import de.schildbach.pte.dto.LocationType; +import de.schildbach.pte.dto.NearbyStationsResult; +import de.schildbach.pte.dto.QueryDeparturesResult; +import de.schildbach.pte.util.ParserUtils; + +/** + * Ireland, Dublin + * + * @author Andreas Schildbach + */ +public class EireannProvider extends AbstractHafasProvider +{ + public static final NetworkId NETWORK_ID = NetworkId.EIREANN; + private static final String API_BASE = "http://buseireann.fahrinfo.ivu.de/Fahrinfo/bin/"; + + public EireannProvider() + { + super(API_BASE + "query.bin/en", 4, null); + } + + public NetworkId id() + { + return NETWORK_ID; + } + + public boolean hasCapabilities(final Capability... capabilities) + { + for (final Capability capability : capabilities) + if (capability == Capability.AUTOCOMPLETE_ONE_LINE || capability == Capability.DEPARTURES || capability == Capability.CONNECTIONS) + return true; + + return false; + } + + @Override + protected void setProductBits(final StringBuilder productBits, final char product) + { + if (product == 'I') + { + } + else if (product == 'R') + { + } + else if (product == 'S') + { + } + else if (product == 'U') + { + } + else if (product == 'T') + { + } + else if (product == 'B') + { + productBits.setCharAt(3, '1'); + } + else if (product == 'P') + { + } + else if (product == 'F') + { + } + else if (product == 'C') + { + } + else + { + throw new IllegalArgumentException("cannot handle: " + product); + } + } + + public NearbyStationsResult queryNearbyStations(final Location location, final int maxDistance, final int maxStations) throws IOException + { + final StringBuilder uri = new StringBuilder(API_BASE); + + if (location.hasLocation()) + { + uri.append("query.bin/eny"); + uri.append("?performLocating=2&tpl=stop2json"); + uri.append("&look_maxno=").append(maxStations != 0 ? maxStations : 200); + uri.append("&look_maxdist=").append(maxDistance != 0 ? maxDistance : 5000); + uri.append("&look_stopclass=").append(allProductsInt()); + uri.append("&look_x=").append(location.lon); + uri.append("&look_y=").append(location.lat); + + return jsonNearbyStations(uri.toString()); + } + else if (location.type == LocationType.STATION && location.hasId()) + { + uri.append("stboard.bin/en"); + uri.append("?productsFilter=").append(allProductsString()); + uri.append("&boardType=dep"); + uri.append("&input=").append(location.id); + uri.append("&sTI=1&start=yes&hcount=0"); + uri.append("&L=vs_java3"); + + return xmlNearbyStations(uri.toString()); + } + else + { + throw new IllegalArgumentException("cannot handle: " + location.toDebugString()); + } + } + + public QueryDeparturesResult queryDepartures(final int stationId, final int maxDepartures, final boolean equivs) throws IOException + { + final StringBuilder uri = new StringBuilder(); + uri.append(API_BASE).append("stboard.bin/en"); + uri.append("?productsFilter=").append(allProductsString()); + uri.append("&boardType=dep"); + uri.append("&disableEquivs=yes"); // don't use nearby stations + 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(), stationId); + } + + private static final String AUTOCOMPLETE_URI = API_BASE + "ajax-getstop.bin/en?getstop=1&REQ0JourneyStopsS0A=255&S=%s?&js=true&"; + + public List autocompleteStations(final CharSequence constraint) throws IOException + { + final String uri = String.format(AUTOCOMPLETE_URI, ParserUtils.urlEncode(constraint.toString(), ISO_8859_1)); + + return jsonGetStops(uri); + } + + private static final Pattern P_NORMALIZE_LINE = Pattern.compile("([^#]+)#"); + + @Override + protected Line parseLineAndType(final String lineAndType) + { + final Matcher mLine = P_NORMALIZE_LINE.matcher(lineAndType); + if (mLine.matches()) + return newLine('B' + mLine.group(1)); + + return super.parseLineAndType(lineAndType); + } + + @Override + protected char normalizeType(final String type) + { + final String ucType = type.toUpperCase(); + + if ("COA".equals(ucType)) + return 'B'; + if ("CIT".equals(ucType)) + return 'B'; + + return 0; + } +} diff --git a/enabler/src/de/schildbach/pte/NetworkId.java b/enabler/src/de/schildbach/pte/NetworkId.java index 8a28c1f6..e28e5e73 100644 --- a/enabler/src/de/schildbach/pte/NetworkId.java +++ b/enabler/src/de/schildbach/pte/NetworkId.java @@ -55,6 +55,9 @@ public enum NetworkId // United Kingdom TFL, TLEM, TLEA, TLSE, TLSW, + // Ireland + EIREANN, + // Slovenia MARIBOR, diff --git a/enabler/src/de/schildbach/pte/util/ParserUtils.java b/enabler/src/de/schildbach/pte/util/ParserUtils.java index d50d6e1f..b96c31df 100644 --- a/enabler/src/de/schildbach/pte/util/ParserUtils.java +++ b/enabler/src/de/schildbach/pte/util/ParserUtils.java @@ -316,16 +316,30 @@ public final class ParserUtils } private static final Pattern P_ISO_DATE = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})"); + private static final Pattern P_ISO_DATE_REVERSE = Pattern.compile("(\\d{2})-(\\d{2})-(\\d{4})"); 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 + "'"); + final Matcher mIso = P_ISO_DATE.matcher(str); + if (mIso.matches()) + { + calendar.set(Calendar.YEAR, Integer.parseInt(mIso.group(1))); + calendar.set(Calendar.MONTH, Integer.parseInt(mIso.group(2)) - 1); + calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(mIso.group(3))); + return; + } + + final Matcher mIsoReverse = P_ISO_DATE_REVERSE.matcher(str); + if (mIsoReverse.matches()) + { + calendar.set(Calendar.YEAR, Integer.parseInt(mIsoReverse.group(3))); + calendar.set(Calendar.MONTH, Integer.parseInt(mIsoReverse.group(2)) - 1); + calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(mIsoReverse.group(1))); + return; + } + + throw new RuntimeException("cannot parse: '" + str + "'"); - calendar.set(Calendar.YEAR, Integer.parseInt(m.group(1))); - 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})"); diff --git a/enabler/test/de/schildbach/pte/live/EireannProviderLiveTest.java b/enabler/test/de/schildbach/pte/live/EireannProviderLiveTest.java new file mode 100644 index 00000000..dc275f54 --- /dev/null +++ b/enabler/test/de/schildbach/pte/live/EireannProviderLiveTest.java @@ -0,0 +1,94 @@ +/* + * Copyright 2010-2012 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.Date; +import java.util.List; + +import org.junit.Test; + +import de.schildbach.pte.EireannProvider; +import de.schildbach.pte.NetworkProvider.Accessibility; +import de.schildbach.pte.NetworkProvider.WalkSpeed; +import de.schildbach.pte.dto.Location; +import de.schildbach.pte.dto.LocationType; +import de.schildbach.pte.dto.NearbyStationsResult; +import de.schildbach.pte.dto.QueryConnectionsResult; +import de.schildbach.pte.dto.QueryDeparturesResult; + +/** + * @author Andreas Schildbach + */ +public class EireannProviderLiveTest extends AbstractProviderLiveTest +{ + public EireannProviderLiveTest() + { + super(new EireannProvider()); + } + + @Test + public void nearbyStations() throws Exception + { + final NearbyStationsResult result = provider.queryNearbyStations(new Location(LocationType.STATION, 8013500), 0, 0); + + System.out.println(result.status + " " + result.stations.size() + " " + result.stations); + } + + @Test + public void nearbyStationsByCoordinate() throws Exception + { + final NearbyStationsResult result = provider.queryNearbyStations(new Location(LocationType.ADDRESS, 53343993, -6267371), 0, 0); + + print(result); + } + + @Test + public void queryDepartures() throws Exception + { + final QueryDeparturesResult result = provider.queryDepartures(8013500, 0, false); + + print(result); + } + + @Test + public void autoComplete() throws Exception + { + final List autocompletes = provider.autocompleteStations("Dublin"); + + print(autocompletes); + } + + @Test + public void autoCompleteAddress() throws Exception + { + final List autocompletes = provider.autocompleteStations("Dorfstrasse 10, Dällikon, Schweiz"); + + print(autocompletes); + } + + @Test + public void shortConnection() throws Exception + { + final QueryConnectionsResult result = queryConnections(new Location(LocationType.STATION, 8052281, null, "Dublin Rd (GMIT)"), null, + new Location(LocationType.STATION, 8013100, null, "Dublin Airport (Atrium Road)"), new Date(), true, ALL_PRODUCTS, WalkSpeed.NORMAL, + Accessibility.NEUTRAL); + System.out.println(result); + final QueryConnectionsResult laterResult = queryMoreConnections(result.context, true); + System.out.println(laterResult); + } +}