auto-complete addresses for Austria

git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@200 0924bc21-9374-b0fa-ee44-9ff1593b38f0
This commit is contained in:
andreas.schildbach 2010-09-25 19:37:37 +00:00
parent 72d27105bd
commit 8f85e8ce86
9 changed files with 90 additions and 28 deletions

View file

@ -16,16 +16,20 @@
*/
package de.schildbach.pte;
import de.schildbach.pte.NetworkProvider.LocationType;
/**
* @author Andreas Schildbach
*/
public final class Autocomplete
{
public final LocationType locationType;
public final int locationId;
public final String location;
public Autocomplete(final int locationId, final String location)
public Autocomplete(final LocationType locationType, final int locationId, final String location)
{
this.locationType = locationType;
this.locationId = locationId;
this.location = location;
}

View file

@ -64,13 +64,13 @@ public final class BahnProvider implements NetworkProvider
final Matcher mSingle = P_SINGLE_NAME.matcher(page);
if (mSingle.matches())
{
results.add(new Autocomplete(Integer.parseInt(mSingle.group(2)), ParserUtils.resolveEntities(mSingle.group(1))));
results.add(new Autocomplete(LocationType.STATION, Integer.parseInt(mSingle.group(2)), ParserUtils.resolveEntities(mSingle.group(1))));
}
else
{
final Matcher mMulti = P_MULTI_NAME.matcher(page);
while (mMulti.find())
results.add(new Autocomplete(Integer.parseInt(mMulti.group(1)), ParserUtils.resolveEntities(mMulti.group(2))));
results.add(new Autocomplete(LocationType.STATION, Integer.parseInt(mMulti.group(1)), ParserUtils.resolveEntities(mMulti.group(2))));
}
return results;

View file

@ -72,12 +72,12 @@ public class LinzProvider implements NetworkProvider
// final double sLon = latLonToDouble(Integer.parseInt(mAutocomplete.group(2)));
// final double sLat = latLonToDouble(Integer.parseInt(mAutocomplete.group(3)));
final String sName = m.group(4).trim();
results.add(new Autocomplete(sId, sName));
results.add(new Autocomplete(LocationType.STATION, sId, sName));
}
else if (m.group(5) != null)
{
final String sName = m.group(5).trim();
results.add(new Autocomplete(0, sName));
results.add(new Autocomplete(LocationType.ANY, 0, sName));
}
}

View file

@ -78,7 +78,7 @@ public class MvvProvider implements NetworkProvider
if (type.equals("stop"))
{
final Autocomplete result = new Autocomplete(locationId, city + ", " + location);
final Autocomplete result = new Autocomplete(LocationType.STATION, locationId, city + ", " + location);
results.add(result);
}
}

View file

@ -20,6 +20,7 @@ import de.schildbach.pte.QueryDeparturesResult.Status;
public class OebbProvider implements NetworkProvider
{
public static final String NETWORK_ID = "fahrplan.oebb.at";
public static final String API_BASE = "http://fahrplan.oebb.at/bin/";
public boolean hasCapabilities(final Capability... capabilities)
{
@ -30,30 +31,77 @@ public class OebbProvider implements NetworkProvider
return false;
}
private static final String NAME_URL = "http://fahrplan.oebb.at/bin/stboard.exe/dn?input=";
private static final Pattern P_SINGLE_NAME = Pattern
.compile(".*?<input type=\"hidden\" name=\"input\" value=\"(.+?)#(\\d+)\">.*", Pattern.DOTALL);
private static final Pattern P_MULTI_NAME = Pattern.compile("<option value=\".+?#(\\d+)\">(.+?)</option>", Pattern.DOTALL);
private static final String AUTOCOMPLETE_URI = API_BASE
+ "ajax-getstop.exe/dny?start=1&tpl=suggest2json&REQ0JourneyStopsS0A=255&REQ0JourneyStopsB=12&S=%s?&js=true&";
private static final String ENCODING = "ISO-8859-1";
private static final Pattern P_AUTOCOMPLETE_JSON = Pattern.compile("SLs\\.sls=(.*?);SLs\\.showSuggestion\\(\\);", Pattern.DOTALL);
private static final Pattern P_AUTOCOMPLETE_ID = Pattern.compile(".*?@L=(\\d+)@.*?");
public List<Autocomplete> autocompleteStations(final CharSequence constraint) throws IOException
{
final CharSequence page = ParserUtils.scrape(NAME_URL + ParserUtils.urlEncode(constraint.toString()));
final String uri = String.format(AUTOCOMPLETE_URI, ParserUtils.urlEncode(constraint.toString(), ENCODING));
final CharSequence page = ParserUtils.scrape(uri);
final List<Autocomplete> results = new ArrayList<Autocomplete>();
final Matcher mSingle = P_SINGLE_NAME.matcher(page);
if (mSingle.matches())
final Matcher mJson = P_AUTOCOMPLETE_JSON.matcher(page);
if (mJson.matches())
{
results.add(new Autocomplete(Integer.parseInt(mSingle.group(2)), ParserUtils.resolveEntities(mSingle.group(1))));
final String json = mJson.group(1);
final List<Autocomplete> results = new ArrayList<Autocomplete>();
try
{
final JSONObject head = new JSONObject(json);
final JSONArray aSuggestions = head.getJSONArray("suggestions");
for (int i = 0; i < aSuggestions.length(); i++)
{
final JSONObject suggestion = aSuggestions.optJSONObject(i);
if (suggestion != null)
{
final int type = suggestion.getInt("type");
final String value = suggestion.getString("value");
if (type == 1) // station
{
final String id = suggestion.getString("id");
final Matcher m = P_AUTOCOMPLETE_ID.matcher(id);
if (m.matches())
{
final int localId = Integer.parseInt(m.group(1));
results.add(new Autocomplete(LocationType.STATION, localId, value));
}
else
{
throw new IllegalStateException("id does not match: " + id);
}
}
else if (type == 2) // address
{
results.add(new Autocomplete(LocationType.ADDRESS, 0, value));
}
else if (type == 4) // poi
{
results.add(new Autocomplete(LocationType.ANY, 0, value));
}
else
{
throw new IllegalStateException("unknown type " + type + " on " + uri);
}
}
}
return results;
}
catch (final JSONException x)
{
x.printStackTrace();
throw new RuntimeException("cannot parse: '" + json + "' on " + uri, x);
}
}
else
{
final Matcher mMulti = P_MULTI_NAME.matcher(page);
while (mMulti.find())
results.add(new Autocomplete(Integer.parseInt(mMulti.group(1)), ParserUtils.resolveEntities(mMulti.group(2))));
throw new RuntimeException("cannot parse: '" + page + "' on " + uri);
}
return results;
}
private final String NEARBY_URI = "http://fahrplan.oebb.at/bin/stboard.exe/dn?distance=50&near=Suchen&input=%d";

View file

@ -66,13 +66,13 @@ public class RmvProvider implements NetworkProvider
final Matcher mSingle = P_SINGLE_NAME.matcher(page);
if (mSingle.matches())
{
results.add(new Autocomplete(Integer.parseInt(mSingle.group(2)), ParserUtils.resolveEntities(mSingle.group(1))));
results.add(new Autocomplete(LocationType.STATION, Integer.parseInt(mSingle.group(2)), ParserUtils.resolveEntities(mSingle.group(1))));
}
else
{
final Matcher mMulti = P_MULTI_NAME.matcher(page);
while (mMulti.find())
results.add(new Autocomplete(Integer.parseInt(mMulti.group(1)), ParserUtils.resolveEntities(mMulti.group(2))));
results.add(new Autocomplete(LocationType.STATION, Integer.parseInt(mMulti.group(1)), ParserUtils.resolveEntities(mMulti.group(2))));
}
return results;

View file

@ -66,13 +66,13 @@ public class SbbProvider implements NetworkProvider
final Matcher mSingle = P_SINGLE_NAME.matcher(page);
if (mSingle.matches())
{
results.add(new Autocomplete(Integer.parseInt(mSingle.group(2)), ParserUtils.resolveEntities(mSingle.group(1))));
results.add(new Autocomplete(LocationType.STATION, Integer.parseInt(mSingle.group(2)), ParserUtils.resolveEntities(mSingle.group(1))));
}
else
{
final Matcher mMulti = P_MULTI_NAME.matcher(page);
while (mMulti.find())
results.add(new Autocomplete(Integer.parseInt(mMulti.group(1)), ParserUtils.resolveEntities(mMulti.group(2))));
results.add(new Autocomplete(LocationType.STATION, Integer.parseInt(mMulti.group(1)), ParserUtils.resolveEntities(mMulti.group(2))));
}
return results;

View file

@ -73,7 +73,7 @@ public final class VbbProvider implements NetworkProvider
final Matcher mSingle = P_SINGLE_MASTID.matcher(page);
if (mSingle.matches())
{
results.add(new Autocomplete(0 /* TODO */, ParserUtils.resolveEntities(mSingle.group(1))));
results.add(new Autocomplete(LocationType.ANY, 0 /* TODO */, ParserUtils.resolveEntities(mSingle.group(1))));
}
}
else
@ -83,13 +83,14 @@ public final class VbbProvider implements NetworkProvider
final Matcher mSingle = P_SINGLE_NAME.matcher(page);
if (mSingle.matches())
{
results.add(new Autocomplete(0 /* TODO */, ParserUtils.resolveEntities(mSingle.group(1))));
results.add(new Autocomplete(LocationType.ANY, 0 /* TODO */, ParserUtils.resolveEntities(mSingle.group(1))));
}
else
{
final Matcher mMulti = P_MULTI_NAME.matcher(page);
while (mMulti.find())
results.add(new Autocomplete(Integer.parseInt(mMulti.group(1)), ParserUtils.resolveEntities(mMulti.group(2))));
results.add(new Autocomplete(LocationType.STATION, Integer.parseInt(mMulti.group(1)), ParserUtils
.resolveEntities(mMulti.group(2))));
}
}

View file

@ -18,9 +18,11 @@
package de.schildbach.pte.live;
import java.util.Date;
import java.util.List;
import org.junit.Test;
import de.schildbach.pte.Autocomplete;
import de.schildbach.pte.QueryConnectionsResult;
import de.schildbach.pte.SbbProvider;
import de.schildbach.pte.NetworkProvider.LocationType;
@ -62,4 +64,11 @@ public class SbbProviderLiveTest
final QueryConnectionsResult moreResult = provider.queryMoreConnections(result.linkLater);
System.out.println(moreResult);
}
@Test
public void autoComplete() throws Exception
{
final List<Autocomplete> result = provider.autocompleteStations("haupt");
System.out.println(result);
}
}