From 7ddac5813d02c23cfc17883303e7f2eb72c54db0 Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Mon, 7 Jul 2014 12:43:03 +0200 Subject: [PATCH] Migrate Copenhagen to auto-complete and query for nearby stations via getstops endpoint. --- .../schildbach/pte/AbstractHafasProvider.java | 67 ------------------- .../src/de/schildbach/pte/DsbProvider.java | 28 ++------ 2 files changed, 4 insertions(+), 91 deletions(-) diff --git a/enabler/src/de/schildbach/pte/AbstractHafasProvider.java b/enabler/src/de/schildbach/pte/AbstractHafasProvider.java index 05a6459d..43bd8d49 100644 --- a/enabler/src/de/schildbach/pte/AbstractHafasProvider.java +++ b/enabler/src/de/schildbach/pte/AbstractHafasProvider.java @@ -417,73 +417,6 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider } } - protected final List xmlLocationList(final String uri) throws IOException - { - Reader reader = null; - - try - { - reader = new InputStreamReader(ParserUtils.scrapeInputStream(uri), UTF_8); - - final XmlPullParserFactory factory = XmlPullParserFactory.newInstance(System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null); - final XmlPullParser pp = factory.newPullParser(); - pp.setInput(reader); - - final List results = new ArrayList(); - - pp.require(XmlPullParser.START_DOCUMENT, null, null); - pp.next(); - - XmlPullUtil.enter(pp, "LocationList"); - - while (XmlPullUtil.test(pp, "StopLocation") || XmlPullUtil.test(pp, "CoordLocation")) - { - final String name = ParserUtils.resolveEntities(XmlPullUtil.attr(pp, "name")); - final int lon = XmlPullUtil.intAttr(pp, "x"); - final int lat = XmlPullUtil.intAttr(pp, "y"); - - if (XmlPullUtil.test(pp, "StopLocation")) - { - final String id = XmlPullUtil.attr(pp, "id"); - final String[] placeAndName = splitPlaceAndName(name); - results.add(new Location(LocationType.STATION, id, lat, lon, placeAndName[0], placeAndName[1])); - } - else - { - final String type = XmlPullUtil.attr(pp, "type"); - if ("POI".equals(type)) - results.add(new Location(LocationType.POI, null, lat, lon, null, name)); - else if ("ADR".equals(type)) - results.add(new Location(LocationType.ADDRESS, null, lat, lon, null, name)); - else - throw new IllegalStateException("unknown type " + type + " on " + uri); - } - - if (pp.isEmptyElementTag()) - { - XmlPullUtil.next(pp); - } - else - { - XmlPullUtil.enter(pp); - XmlPullUtil.exit(pp); - } - } - XmlPullUtil.exit(pp, "LocationList"); - - return results; - } - catch (final XmlPullParserException x) - { - throw new RuntimeException(x); - } - finally - { - if (reader != null) - reader.close(); - } - } - private static final Pattern P_XML_MLC_REQ_ID = Pattern.compile(".*?@L=0*(\\d+)@.*?"); private static final Pattern P_XML_MLC_REQ_LONLAT = Pattern.compile(".*?@X=(-?\\d+)@Y=(-?\\d+)@.*?"); diff --git a/enabler/src/de/schildbach/pte/DsbProvider.java b/enabler/src/de/schildbach/pte/DsbProvider.java index 741edca8..249a895a 100644 --- a/enabler/src/de/schildbach/pte/DsbProvider.java +++ b/enabler/src/de/schildbach/pte/DsbProvider.java @@ -19,15 +19,12 @@ package de.schildbach.pte; import java.io.IOException; import java.util.Collection; -import java.util.List; -import java.util.Locale; import de.schildbach.pte.dto.Location; import de.schildbach.pte.dto.LocationType; import de.schildbach.pte.dto.NearbyStationsResult; import de.schildbach.pte.dto.Product; import de.schildbach.pte.dto.QueryDeparturesResult; -import de.schildbach.pte.util.ParserUtils; /** * @author Andreas Schildbach @@ -42,7 +39,7 @@ public class DsbProvider extends AbstractHafasProvider public DsbProvider() { - super(API_BASE + "stboard.exe/mn", null, API_BASE + "query.exe/dn", 11); + super(API_BASE + "stboard.exe/mn", API_BASE + "ajax-getstop.exe/mn", API_BASE + "query.exe/dn", 11); } public NetworkId id() @@ -135,21 +132,14 @@ public class DsbProvider extends AbstractHafasProvider } } - private static final String NEARBY_STATIONS_BY_COORDINATE_URI = "http://xmlopen.rejseplanen.dk/bin/rest.exe/stopsNearby?coordX=%d&coordY=%d"; - public NearbyStationsResult queryNearbyStations(final Location location, final int maxDistance, final int maxStations) throws IOException { if (location.hasLocation()) { - final StringBuilder uri = new StringBuilder(String.format(Locale.ENGLISH, NEARBY_STATIONS_BY_COORDINATE_URI, location.lon, location.lat)); - if (maxStations != 0) - uri.append("&maxNumber=").append(maxStations); - if (maxDistance != 0) - uri.append("&maxRadius=").append(maxDistance); + final StringBuilder uri = new StringBuilder(queryEndpoint); + uri.append(jsonNearbyStationsParameters(location, maxDistance, maxStations)); - final List locations = xmlLocationList(uri.toString()); - - return new NearbyStationsResult(null, locations); + return jsonNearbyStations(uri.toString()); } else if (location.type == LocationType.STATION && location.hasId()) { @@ -172,16 +162,6 @@ public class DsbProvider extends AbstractHafasProvider return xmlQueryDepartures(uri.toString(), stationId); } - private static final String AUTOCOMPLETE_URI = "http://xmlopen.rejseplanen.dk/bin/rest.exe/location.name?input=%s"; - - @Override - public List autocompleteStations(final CharSequence constraint) throws IOException - { - final String uri = String.format(Locale.ENGLISH, AUTOCOMPLETE_URI, ParserUtils.urlEncode(constraint.toString(), ISO_8859_1)); - - return xmlLocationList(uri); - } - @Override public Collection defaultProducts() {