diff --git a/src/de/schildbach/pte/AbstractHafasProvider.java b/src/de/schildbach/pte/AbstractHafasProvider.java new file mode 100644 index 00000000..134b9a1c --- /dev/null +++ b/src/de/schildbach/pte/AbstractHafasProvider.java @@ -0,0 +1,72 @@ +/* + * Copyright 2010 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.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @author Andreas Schildbach + */ +public abstract class AbstractHafasProvider implements NetworkProvider +{ + private final static Pattern P_NEARBY_COARSE = Pattern.compile("(.*?)", Pattern.DOTALL); + private final static Pattern P_NEARBY_FINE = Pattern.compile(".*?&REQMapRoute0\\.Location0\\.X=(-?\\d+)&REQMapRoute0\\.Location0\\.Y=(-?\\d+)" + + "&.*?[\\?&]input=(\\d+)&[^\"]*\">([^<]*)<.*?", Pattern.DOTALL); + + protected abstract String nearbyStationUri(String stationId); + + public List nearbyStations(final String stationId, final int lat, final int lon, final int maxDistance, final int maxStations) + throws IOException + { + if (stationId == null) + throw new IllegalArgumentException("stationId must be given"); + + final List stations = new ArrayList(); + + final String uri = nearbyStationUri(stationId); + final CharSequence page = ParserUtils.scrape(uri); + + final Matcher mCoarse = P_NEARBY_COARSE.matcher(page); + while (mCoarse.find()) + { + final Matcher mFine = P_NEARBY_FINE.matcher(mCoarse.group(2)); + if (mFine.matches()) + { + final int parsedLon = Integer.parseInt(mFine.group(1)); + final int parsedLat = Integer.parseInt(mFine.group(2)); + final int parsedId = Integer.parseInt(mFine.group(3)); + final String parsedName = ParserUtils.resolveEntities(mFine.group(4)); + + stations.add(new Station(parsedId, parsedName, parsedLat, parsedLon, 0, null, null)); + } + else + { + throw new IllegalArgumentException("cannot parse '" + mCoarse.group(2) + "' on " + uri); + } + } + + if (maxStations == 0 || maxStations >= stations.size()) + return stations; + else + return stations.subList(0, maxStations); + } +} diff --git a/src/de/schildbach/pte/OebbProvider.java b/src/de/schildbach/pte/OebbProvider.java index 2f2d41ee..674490a6 100644 --- a/src/de/schildbach/pte/OebbProvider.java +++ b/src/de/schildbach/pte/OebbProvider.java @@ -34,7 +34,7 @@ import org.json.JSONObject; import de.schildbach.pte.QueryDeparturesResult.Status; -public class OebbProvider implements NetworkProvider +public class OebbProvider extends AbstractHafasProvider { public static final String NETWORK_ID = "fahrplan.oebb.at"; public static final String API_BASE = "http://fahrplan.oebb.at/bin/"; @@ -121,43 +121,12 @@ public class OebbProvider implements NetworkProvider } } - private final String NEARBY_URI = API_BASE + "stboard.exe/dn?distance=50&near=Suchen&input=%d"; - private final static Pattern P_NEARBY_COARSE = Pattern.compile("(.*?)", Pattern.DOTALL); - private final static Pattern P_NEARBY_FINE = Pattern.compile(".*?stboard\\.exe/.*?&input=.*?%23(\\d+)&.*?>(.*?).*?", Pattern.DOTALL); + private final String NEARBY_URI = API_BASE + "stboard.exe/dn?distance=50&near=Suchen&input=%s"; - public List nearbyStations(final String stationId, final int lat, final int lon, final int maxDistance, final int maxStations) - throws IOException + @Override + protected String nearbyStationUri(final String stationId) { - if (stationId == null) - throw new IllegalArgumentException("stationId must be given"); - - final List stations = new ArrayList(); - - final String uri = String.format(NEARBY_URI, stationId); - final CharSequence page = ParserUtils.scrape(uri); - - final Matcher mCoarse = P_NEARBY_COARSE.matcher(page); - while (mCoarse.find()) - { - final Matcher mFine = P_NEARBY_FINE.matcher(mCoarse.group(1)); - if (mFine.matches()) - { - final int parsedId = Integer.parseInt(mFine.group(1)); - final String parsedName = ParserUtils.resolveEntities(mFine.group(2)); - - final Station station = new Station(parsedId, parsedName, 0, 0, 0, null, null); - stations.add(station); - } - else - { - throw new IllegalArgumentException("cannot parse '" + mCoarse.group(1) + "' on " + uri); - } - } - - if (maxStations == 0 || maxStations >= stations.size()) - return stations; - else - return stations.subList(0, maxStations); + return String.format(NEARBY_URI, stationId); } public StationLocationResult stationLocation(final String stationId) throws IOException diff --git a/src/de/schildbach/pte/RmvProvider.java b/src/de/schildbach/pte/RmvProvider.java index 189015a7..1f7cea30 100644 --- a/src/de/schildbach/pte/RmvProvider.java +++ b/src/de/schildbach/pte/RmvProvider.java @@ -35,7 +35,7 @@ import de.schildbach.pte.QueryDeparturesResult.Status; /** * @author Andreas Schildbach */ -public class RmvProvider implements NetworkProvider +public class RmvProvider extends AbstractHafasProvider { public static final String NETWORK_ID = "mobil.rmv.de"; public static final String NETWORK_ID_ALT = "www.rmv.de"; @@ -81,12 +81,20 @@ public class RmvProvider implements NetworkProvider private final static Pattern P_NEARBY_STATIONS = Pattern.compile("\\n" + "(.+?)\\s*\\((\\d+) m/[A-Z]+\\)\\n", Pattern.DOTALL); - private final String NEARBY_URI = "http://www.rmv.de/auskunft/bin/jp/stboard.exe/dn?L=vs_rmv&distance=50&near&input=%d"; + private final String NEARBY_URI = "http://www.rmv.de/auskunft/bin/jp/stboard.exe/dn?L=vs_rmv&distance=50&near&input=%s"; + + @Override + protected String nearbyStationUri(final String stationId) + { + return String.format(NEARBY_URI, stationId); + } + private final static Pattern P_NEARBY_COARSE = Pattern.compile("(.*?)", Pattern.DOTALL); private final static Pattern P_NEARBY_FINE = Pattern.compile(".*?auskunft/bin/jp/stboard\\.exe/dn\\?L=vs_rmv&input=(\\d+).*?" + "&REQMapRoute0\\.Location0\\.X=(-?\\d+)&REQMapRoute0\\.Location0\\.Y=(-?\\d+)" // + "&REQMapRoute0\\.Location0\\.Name=(.*?)\">.*?", Pattern.DOTALL); + @Override public List nearbyStations(final String stationId, final int lat, final int lon, final int maxDistance, final int maxStations) throws IOException { @@ -110,8 +118,7 @@ public class RmvProvider implements NetworkProvider } else if (stationId != null) { - final String uri = String.format(NEARBY_URI, stationId); - + final String uri = nearbyStationUri(stationId); final CharSequence page = ParserUtils.scrape(uri); final Matcher mCoarse = P_NEARBY_COARSE.matcher(page); diff --git a/src/de/schildbach/pte/SbbProvider.java b/src/de/schildbach/pte/SbbProvider.java index 50a3d4d1..ad554ffa 100644 --- a/src/de/schildbach/pte/SbbProvider.java +++ b/src/de/schildbach/pte/SbbProvider.java @@ -35,7 +35,7 @@ import de.schildbach.pte.QueryDeparturesResult.Status; /** * @author Andreas Schildbach */ -public class SbbProvider implements NetworkProvider +public class SbbProvider extends AbstractHafasProvider { public static final String NETWORK_ID = "fahrplan.sbb.ch"; @@ -78,46 +78,12 @@ public class SbbProvider implements NetworkProvider return results; } - private final static String NEARBY_URI = "http://fahrplan.sbb.ch/bin/bhftafel.exe/dn?input=%d&distance=50&near=Anzeigen"; - private final static Pattern P_NEARBY_COARSE = Pattern.compile("(.*?)", Pattern.DOTALL); - private final static Pattern P_NEARBY_FINE = Pattern.compile(".*?&REQMapRoute0\\.Location0\\.X=(-?\\d+)&REQMapRoute0\\.Location0\\.Y=(-?\\d+)" - + "&REQMapRoute0\\.Location0\\.Name=(.*?)&sturl=.*?dn\\?input=(\\d+).*?", Pattern.DOTALL); + private final static String NEARBY_URI = "http://fahrplan.sbb.ch/bin/bhftafel.exe/dn?input=%s&distance=50&near=Anzeigen"; - public List nearbyStations(final String stationId, final int lat, final int lon, final int maxDistance, final int maxStations) - throws IOException + @Override + protected String nearbyStationUri(final String stationId) { - if (stationId == null) - throw new IllegalArgumentException("stationId must be given"); - - final List stations = new ArrayList(); - - final String uri = String.format(NEARBY_URI, stationId); - final CharSequence page = ParserUtils.scrape(uri); - - final Matcher mCoarse = P_NEARBY_COARSE.matcher(page); - while (mCoarse.find()) - { - final Matcher mFine = P_NEARBY_FINE.matcher(mCoarse.group(1)); - if (mFine.matches()) - { - final int parsedLon = Integer.parseInt(mFine.group(1)); - final int parsedLat = Integer.parseInt(mFine.group(2)); - final String parsedName = ParserUtils.resolveEntities(mFine.group(3)); - final int parsedId = Integer.parseInt(mFine.group(4)); - - final Station station = new Station(parsedId, parsedName, parsedLat, parsedLon, 0, null, null); - stations.add(station); - } - else - { - throw new IllegalArgumentException("cannot parse '" + mCoarse.group(1) + "' on " + uri); - } - } - - if (maxStations == 0 || maxStations >= stations.size()) - return stations; - else - return stations.subList(0, maxStations); + return String.format(NEARBY_URI, stationId); } public StationLocationResult stationLocation(final String stationId) throws IOException diff --git a/src/de/schildbach/pte/SncbProvider.java b/src/de/schildbach/pte/SncbProvider.java index a9bd1e42..f5a69405 100644 --- a/src/de/schildbach/pte/SncbProvider.java +++ b/src/de/schildbach/pte/SncbProvider.java @@ -13,7 +13,7 @@ import java.util.regex.Pattern; import de.schildbach.pte.QueryDeparturesResult.Status; -public class SncbProvider implements NetworkProvider +public class SncbProvider extends AbstractHafasProvider { public static final String NETWORK_ID = "hari.b-rail.be"; @@ -33,46 +33,12 @@ public class SncbProvider implements NetworkProvider throw new UnsupportedOperationException(); } - private final String NEARBY_URI = "http://hari.b-rail.be/HAFAS/bin/stboard.exe/en?input=%d&distance=50&near=Anzeigen"; - private final static Pattern P_NEARBY_COARSE = Pattern.compile("(.*?)", Pattern.DOTALL); - private final static Pattern P_NEARBY_FINE = Pattern.compile(".*?&REQMapRoute0\\.Location0\\.X=(-?\\d+)&REQMapRoute0\\.Location0\\.Y=(-?\\d+)" - + "&REQMapRoute0\\.Location0\\.Name=(.*?)\">.*?en\\?input=(\\d+).*?", Pattern.DOTALL); + private final String NEARBY_URI = "http://hari.b-rail.be/HAFAS/bin/stboard.exe/en?input=%s&distance=50&near=Anzeigen"; - public List nearbyStations(final String stationId, final int lat, final int lon, final int maxDistance, final int maxStations) - throws IOException + @Override + protected String nearbyStationUri(final String stationId) { - if (stationId == null) - throw new IllegalArgumentException("stationId must be given"); - - final List stations = new ArrayList(); - - final String uri = String.format(NEARBY_URI, stationId); - final CharSequence page = ParserUtils.scrape(uri); - - final Matcher mCoarse = P_NEARBY_COARSE.matcher(page); - while (mCoarse.find()) - { - final Matcher mFine = P_NEARBY_FINE.matcher(mCoarse.group(1)); - if (mFine.matches()) - { - final int parsedLon = Integer.parseInt(mFine.group(1)); - final int parsedLat = Integer.parseInt(mFine.group(2)); - final String parsedName = ParserUtils.resolveEntities(mFine.group(3)); - final int parsedId = Integer.parseInt(mFine.group(4)); - - final Station station = new Station(parsedId, parsedName, parsedLat, parsedLon, 0, null, null); - stations.add(station); - } - else - { - throw new IllegalArgumentException("cannot parse '" + mCoarse.group(1) + "' on " + uri); - } - } - - if (maxStations == 0 || maxStations >= stations.size()) - return stations; - else - return stations.subList(0, maxStations); + return String.format(NEARBY_URI, stationId); } public StationLocationResult stationLocation(final String stationId) throws IOException diff --git a/test/de/schildbach/pte/live/GvhProviderLiveTest.java b/test/de/schildbach/pte/live/GvhProviderLiveTest.java index 4d77eb21..8d224dcb 100644 --- a/test/de/schildbach/pte/live/GvhProviderLiveTest.java +++ b/test/de/schildbach/pte/live/GvhProviderLiveTest.java @@ -40,7 +40,7 @@ public class GvhProviderLiveTest } @Test - public void nearby() throws Exception + public void nearbyStation() throws Exception { final List results = provider.nearbyStations("25000031", 0, 0, 0, 0); diff --git a/test/de/schildbach/pte/live/OebbProviderLiveTest.java b/test/de/schildbach/pte/live/OebbProviderLiveTest.java index 27835137..6e0d6b83 100644 --- a/test/de/schildbach/pte/live/OebbProviderLiveTest.java +++ b/test/de/schildbach/pte/live/OebbProviderLiveTest.java @@ -18,11 +18,13 @@ package de.schildbach.pte.live; import java.util.Date; +import java.util.List; import org.junit.Test; import de.schildbach.pte.OebbProvider; import de.schildbach.pte.QueryConnectionsResult; +import de.schildbach.pte.Station; import de.schildbach.pte.NetworkProvider.LocationType; import de.schildbach.pte.NetworkProvider.WalkSpeed; @@ -33,6 +35,14 @@ public class OebbProviderLiveTest { private OebbProvider provider = new OebbProvider(); + @Test + public void nearbyStation() throws Exception + { + final List results = provider.nearbyStations("902006", 0, 0, 0, 0); + + System.out.println(results.size() + " " + results); + } + @Test public void shortConnection() throws Exception { diff --git a/test/de/schildbach/pte/live/RmvProviderLiveTest.java b/test/de/schildbach/pte/live/RmvProviderLiveTest.java new file mode 100644 index 00000000..86380a94 --- /dev/null +++ b/test/de/schildbach/pte/live/RmvProviderLiveTest.java @@ -0,0 +1,40 @@ +/* + * Copyright 2010 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.RmvProvider; +import de.schildbach.pte.Station; + +/** + * @author Andreas Schildbach + */ +public class RmvProviderLiveTest +{ + private final RmvProvider provider = new RmvProvider(); + + @Test + public void nearbyStation() throws Exception + { + final List results = provider.nearbyStations("3000001", 0, 0, 0, 0); + + System.out.println(results.size() + " " + results); + } +} diff --git a/test/de/schildbach/pte/live/SbbProviderLiveTest.java b/test/de/schildbach/pte/live/SbbProviderLiveTest.java index 19e56c99..891b4ceb 100644 --- a/test/de/schildbach/pte/live/SbbProviderLiveTest.java +++ b/test/de/schildbach/pte/live/SbbProviderLiveTest.java @@ -25,6 +25,7 @@ import org.junit.Test; import de.schildbach.pte.Autocomplete; import de.schildbach.pte.QueryConnectionsResult; import de.schildbach.pte.SbbProvider; +import de.schildbach.pte.Station; import de.schildbach.pte.NetworkProvider.LocationType; import de.schildbach.pte.NetworkProvider.WalkSpeed; @@ -35,6 +36,14 @@ public class SbbProviderLiveTest { private SbbProvider provider = new SbbProvider(); + @Test + public void nearbyStation() throws Exception + { + final List results = provider.nearbyStations("8500010", 0, 0, 0, 0); + + System.out.println(results.size() + " " + results); + } + @Test public void shortConnection() throws Exception { diff --git a/test/de/schildbach/pte/live/SncbProviderLiveTest.java b/test/de/schildbach/pte/live/SncbProviderLiveTest.java new file mode 100644 index 00000000..66b37adb --- /dev/null +++ b/test/de/schildbach/pte/live/SncbProviderLiveTest.java @@ -0,0 +1,40 @@ +/* + * Copyright 2010 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.SncbProvider; +import de.schildbach.pte.Station; + +/** + * @author Andreas Schildbach + */ +public class SncbProviderLiveTest +{ + private final SncbProvider provider = new SncbProvider(); + + @Test + public void nearbyStation() throws Exception + { + final List results = provider.nearbyStations("100080", 0, 0, 0, 0); + + System.out.println(results.size() + " " + results); + } +}