From bcd07f256f9b537b989a475f879115ef0cebe3b2 Mon Sep 17 00:00:00 2001 From: "andreas.schildbach@gmail.com" Date: Wed, 4 May 2011 08:35:58 +0000 Subject: [PATCH] Luxembourg git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@596 0924bc21-9374-b0fa-ee44-9ff1593b38f0 --- src/de/schildbach/pte/LuProvider.java | 132 ++++++++++++++++++ src/de/schildbach/pte/NetworkId.java | 3 + .../pte/live/LuProviderLiveTest.java | 82 +++++++++++ 3 files changed, 217 insertions(+) create mode 100644 src/de/schildbach/pte/LuProvider.java create mode 100644 test/de/schildbach/pte/live/LuProviderLiveTest.java diff --git a/src/de/schildbach/pte/LuProvider.java b/src/de/schildbach/pte/LuProvider.java new file mode 100644 index 00000000..f34357df --- /dev/null +++ b/src/de/schildbach/pte/LuProvider.java @@ -0,0 +1,132 @@ +/* + * 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; + +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 LuProvider extends AbstractHafasProvider +{ + public static final NetworkId NETWORK_ID = NetworkId.LU; + private static final String API_BASE = "http://mobiliteitszentral.hafas.de/hafas/"; + + public LuProvider() + { + super(API_BASE + "query.exe/dn", null); + } + + public NetworkId id() + { + return NETWORK_ID; + } + + public boolean hasCapabilities(Capability... capabilities) + { + for (final Capability capability : capabilities) + if (capability == Capability.DEPARTURES || capability == Capability.CONNECTIONS) + return true; + + return false; + } + + @Override + public List autocompleteStations(final CharSequence constraint) throws IOException + { + return xmlMLcReq(constraint); + } + + @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=11111111111111"); + uri.append("&boardType=dep"); + uri.append("&input=").append(ParserUtils.urlEncode(stationId)); + uri.append("&sTI=1&start=yes&hcount=0&L=vs_java3"); + + 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).trim(); + + 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); + } + + @Override + protected char normalizeType(final String type) + { + final String ucType = type.toUpperCase(); + + if ("CRE".equals(ucType)) + return 'R'; + + 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=11111111111"); + 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/NetworkId.java b/src/de/schildbach/pte/NetworkId.java index e69753cb..b4755fe8 100644 --- a/src/de/schildbach/pte/NetworkId.java +++ b/src/de/schildbach/pte/NetworkId.java @@ -40,6 +40,9 @@ public enum NetworkId // Denmark DSB, + // Luxembourg + LU, + // United Kingdom TFL, TLEM, TLEA, TLSE, TLSW, diff --git a/test/de/schildbach/pte/live/LuProviderLiveTest.java b/test/de/schildbach/pte/live/LuProviderLiveTest.java new file mode 100644 index 00000000..73bca764 --- /dev/null +++ b/test/de/schildbach/pte/live/LuProviderLiveTest.java @@ -0,0 +1,82 @@ +/* + * 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.Date; +import java.util.List; + +import org.junit.Test; + +import de.schildbach.pte.LuProvider; +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 LuProviderLiveTest +{ + private final LuProvider provider = new LuProvider(); + private static final String ALL_PRODUCTS = "IRSUTBFC"; + + @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("9865836", 0, 0, 0, 0); + + System.out.println(result.stations.size() + " " + result.stations); + } + + @Test + public void queryDepartures() throws Exception + { + final QueryDeparturesResult result = provider.queryDepartures("9865836", 0, false); + + System.out.println(result.stationDepartures); + } + + @Test + public void shortConnection() throws Exception + { + final QueryConnectionsResult result = provider.queryConnections(new Location(LocationType.STATION, 9409001, null, "Echternach, Bel Air"), + null, new Location(LocationType.STATION, 9440001, null, "Echternach, Gare"), new Date(), true, ALL_PRODUCTS, WalkSpeed.NORMAL); + System.out.println(result); + final QueryConnectionsResult moreResult = provider.queryMoreConnections(result.context); + System.out.println(moreResult); + } +}