From be7a17398938933e9731dbee37e6bb28b7b8f69a Mon Sep 17 00:00:00 2001 From: "andreas.schildbach@gmail.com" Date: Mon, 9 May 2011 09:12:26 +0000 Subject: [PATCH] Sweden git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@633 0924bc21-9374-b0fa-ee44-9ff1593b38f0 --- .../schildbach/pte/AbstractHafasProvider.java | 39 +++-- src/de/schildbach/pte/NetworkId.java | 3 + src/de/schildbach/pte/SeProvider.java | 162 ++++++++++++++++++ .../pte/live/SeProviderLiveTest.java | 95 ++++++++++ 4 files changed, 283 insertions(+), 16 deletions(-) create mode 100644 src/de/schildbach/pte/SeProvider.java create mode 100644 test/de/schildbach/pte/live/SeProviderLiveTest.java diff --git a/src/de/schildbach/pte/AbstractHafasProvider.java b/src/de/schildbach/pte/AbstractHafasProvider.java index 2f7405ad..99718892 100644 --- a/src/de/schildbach/pte/AbstractHafasProvider.java +++ b/src/de/schildbach/pte/AbstractHafasProvider.java @@ -59,7 +59,7 @@ import de.schildbach.pte.util.XmlPullUtil; public abstract class AbstractHafasProvider implements NetworkProvider { private static final String DEFAULT_ENCODING = "ISO-8859-1"; - private static final String prod = "hafas"; + private static final String PROD = "hafas"; private final String apiUri; private final int numProductBits; @@ -104,6 +104,11 @@ public abstract class AbstractHafasProvider implements NetworkProvider return (1 << numProductBits) - 1; } + protected char intToProduct(final int value) + { + return 0; + } + protected String[] splitNameAndPlace(final String name) { return new String[] { null, name }; @@ -112,7 +117,7 @@ public abstract class AbstractHafasProvider implements NetworkProvider private final String wrap(final String request) { return "" // - + "" // + + "" // + request // + ""; } @@ -400,9 +405,9 @@ public abstract class AbstractHafasProvider implements NetworkProvider + "(?:newpl\\s*=\"([^\"]*)\"\\s*)?" // + "(?:platform\\s*=\"([^\"]*)\"\\s*)?" // position + "targetLoc\\s*=\"([^\"]*)\"\\s*" // destination - + "(?:hafasname\\s*=\"[^\"]*\"\\s*)?" // (???) + + "(?:hafasname\\s*=\"([^\"]*)\"\\s*)?" // line + "prod\\s*=\"([^\"]*)\"\\s*" // line - + "(?:class\\s*=\"[^\"]*\"\\s*)?" // (???) + + "(?:class\\s*=\"([^\"]*)\"\\s*)?" // class + "(?:dir\\s*=\"[^\"]*\"\\s*)?" // (destination) + "(?:capacity\\s*=\"[^\"]*\"\\s*)?" // (???) + "(?:depStation\\s*=\"(.*?)\"\\s*)?" // @@ -446,7 +451,7 @@ public abstract class AbstractHafasProvider implements NetworkProvider final Matcher mFine = P_XML_QUERY_DEPARTURES_FINE.matcher(mCoarse.group(1)); if (mFine.matches()) { - if (mFine.group(8) == null) + if (mFine.group(10) == null) { final Calendar plannedTime = new GregorianCalendar(timeZone()); plannedTime.clear(); @@ -475,13 +480,22 @@ public abstract class AbstractHafasProvider implements NetworkProvider final String destination = ParserUtils.resolveEntities(mFine.group(6)).trim(); - final String line = normalizeLine(ParserUtils.resolveEntities(mFine.group(7))); + final String hafasName = ParserUtils.resolveEntities(mFine.group(7)); + + String prod = ParserUtils.resolveEntities(mFine.group(8)); + final Matcher m = P_NORMALIZE_LINE.matcher(prod); + if (m.matches()) + prod = m.group(1) + m.group(2); + + final char product = mFine.group(9) != null ? intToProduct(Integer.parseInt(mFine.group(9))) : 0; + + final String line = product != 0 ? product + prod : normalizeLine(prod); final String message; - if (mFine.group(9) != null) + if (mFine.group(11) != null) { - final String m = ParserUtils.resolveEntities(mFine.group(9)).trim(); - message = m.length() > 0 ? m : null; + final String msg = ParserUtils.resolveEntities(mFine.group(11)).trim(); + message = msg.length() > 0 ? msg : null; } else { @@ -934,13 +948,6 @@ public abstract class AbstractHafasProvider implements NetworkProvider throw new IllegalArgumentException(location.type.toString()); } - private final static Pattern P_WHITESPACE = Pattern.compile("\\s+"); - - private final String normalizeWhitespace(final String str) - { - return P_WHITESPACE.matcher(str).replaceAll(""); - } - public GetConnectionDetailsResult getConnectionDetails(String connectionUri) throws IOException { throw new UnsupportedOperationException(); diff --git a/src/de/schildbach/pte/NetworkId.java b/src/de/schildbach/pte/NetworkId.java index de77b2ff..b8857809 100644 --- a/src/de/schildbach/pte/NetworkId.java +++ b/src/de/schildbach/pte/NetworkId.java @@ -40,6 +40,9 @@ public enum NetworkId // Denmark DSB, + // Sweden + SE, + // Luxembourg LU, diff --git a/src/de/schildbach/pte/SeProvider.java b/src/de/schildbach/pte/SeProvider.java new file mode 100644 index 00000000..03dc5c7c --- /dev/null +++ b/src/de/schildbach/pte/SeProvider.java @@ -0,0 +1,162 @@ +/* + * 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.LocationType; +import de.schildbach.pte.dto.NearbyStationsResult; +import de.schildbach.pte.dto.QueryDeparturesResult; +import de.schildbach.pte.util.ParserUtils; + +/** + * @author Andreas Schildbach + */ +public class SeProvider extends AbstractHafasProvider +{ + public static final NetworkId NETWORK_ID = NetworkId.SE; + private static final String API_BASE = "http://reseplanerare.resrobot.se/bin/"; + + public SeProvider() + { + super(API_BASE + "query.exe/sn", 14, null, "UTF-8", 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) + return true; + + return false; + } + + @Override + protected char intToProduct(final int value) + { + if (value == 1) // Flyg + return 'I'; + if (value == 2) // X2000 + return 'I'; + if (value == 4) + return 'R'; + if (value == 8) // Expressbus + return 'B'; + if (value == 16) + return 'R'; + if (value == 32) // Tunnelbana + return 'U'; + if (value == 64) // Spårvagn + return 'T'; + if (value == 128) + return 'B'; + if (value == 256) + return 'F'; + if (value == 512) // Länstaxi + return 'F'; + if (value == 1024) // Future + return 'R'; + + throw new IllegalArgumentException("cannot handle: " + value); + } + + private static final Pattern P_SPLIT_NAME_KN = Pattern.compile("(.*?) \\((.*?) kn\\)"); + + @Override + protected String[] splitNameAndPlace(final String name) + { + final Matcher m = P_SPLIT_NAME_KN.matcher(name); + if (m.matches()) + return new String[] { m.group(2), m.group(1) }; + + return super.splitNameAndPlace(name); + } + + private static final String AUTOCOMPLETE_URI = API_BASE + + "ajax-getstop.exe/sny?start=1&tpl=suggest2json&REQ0JourneyStopsS0A=7&getstop=1&noSession=yes&REQ0JourneyStopsB=12&REQ0JourneyStopsS0G=&S=%s"; + private static final String ENCODING = "ISO-8859-1"; + + public List autocompleteStations(final CharSequence constraint) throws IOException + { + final String uri = String.format(AUTOCOMPLETE_URI, ParserUtils.urlEncode(constraint.toString(), ENCODING)); + + return jsonGetStops(uri); + } + + 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.exe/sny"); + uri.append("?performLocating=2&tpl=stop2json"); + uri.append("&look_maxno=").append(maxStations != 0 ? maxStations : 150); + 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.exe/sn"); + uri.append("?productsFilter=").append(allProductsString()); + uri.append("&boardType=dep"); + uri.append("&input=").append(location.id); + uri.append("&sTI=1&start=yes&hcount=0&L=vs_java3"); + + return xmlNearbyStations(uri.toString()); + } + else + { + throw new IllegalArgumentException("cannot handle: " + location.toDebugString()); + } + } + + @Override + protected char normalizeType(final String type) + { + throw new UnsupportedOperationException(); + } + + 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.exe/sn"); + uri.append("?productsFilter=").append(allProductsString()); + uri.append("&boardType=dep"); + uri.append("&disableEquivs=").append(equivs ? "no" : "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); + } +} diff --git a/test/de/schildbach/pte/live/SeProviderLiveTest.java b/test/de/schildbach/pte/live/SeProviderLiveTest.java new file mode 100644 index 00000000..4625b97a --- /dev/null +++ b/test/de/schildbach/pte/live/SeProviderLiveTest.java @@ -0,0 +1,95 @@ +/* + * 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.SeProvider; +import de.schildbach.pte.NetworkProvider.WalkSpeed; +import de.schildbach.pte.dto.Departure; +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; +import de.schildbach.pte.dto.StationDepartures; + +/** + * @author Andreas Schildbach + */ +public class SeProviderLiveTest +{ + private final SeProvider provider = new SeProvider(); + private static final String ALL_PRODUCTS = "IRSUTBFC"; + + @Test + public void nearbyStations() throws Exception + { + final NearbyStationsResult result = provider.queryNearbyStations(new Location(LocationType.STATION, 7414867), 0, 0); + + System.out.println(result.stations.size() + " " + result.stations); + } + + @Test + public void nearbyStationsByCoordinate() throws Exception + { + final NearbyStationsResult result = provider.queryNearbyStations(new Location(LocationType.ADDRESS, 57709311, 11988459), 0, 0); + + System.out.println(result.stations.size() + " " + result.stations); + } + + @Test + public void queryDepartures() throws Exception + { + final QueryDeparturesResult result = provider.queryDepartures(7414867, 0, false); + + System.out.println(result.stationDepartures); + for (StationDepartures d : result.stationDepartures) + for (Departure e : d.departures) + System.out.println(e); + } + + @Test + public void autocomplete() throws Exception + { + final List autocompletes = provider.autocompleteStations("Luleå Airport"); + + 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 shortConnection() throws Exception + { + final QueryConnectionsResult result = provider.queryConnections(new Location(LocationType.STATION, 7414867, null, "Luleå Airport"), null, + new Location(LocationType.STATION, 7498000, null, "STOCKHOLM"), new Date(), true, ALL_PRODUCTS, WalkSpeed.NORMAL); + System.out.println(result); + final QueryConnectionsResult moreResult = provider.queryMoreConnections(result.context); + System.out.println(moreResult); + } +}