Zürich autocomplete

git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@594 0924bc21-9374-b0fa-ee44-9ff1593b38f0
This commit is contained in:
andreas.schildbach@gmail.com 2011-05-03 22:39:24 +00:00
parent c502a77008
commit adcb971057
2 changed files with 90 additions and 10 deletions

View file

@ -269,8 +269,90 @@ public abstract class AbstractHafasProvider implements NetworkProvider
} }
} }
private static final Pattern P_XML_QUERY_DEPARTURES_COARSE = Pattern.compile("\\G<Journey ([^>]*?)(?:/>|><HIMMessage ([^>]*?)/></Journey>)(?:\n|\\z)", private static final Pattern P_XML_MLC_REQ_ID = Pattern.compile(".*?@L=(\\d+)@.*?");
Pattern.DOTALL); private static final Pattern P_XML_MLC_REQ_LONLAT = Pattern.compile(".*?@X=(\\d+)@Y=(\\d+)@.*?");
protected final List<Location> xmlMLcReq(final CharSequence constraint) throws IOException
{
final String request = "<MLcReq><MLc n=\"" + constraint + "?\" t=\"ALLTYPE\" /></MLcReq>";
// ParserUtils.printXml(ParserUtils.scrape(apiUri, true, wrap(request), null, false));
InputStream is = null;
try
{
is = ParserUtils.scrapeInputStream(apiUri, wrap(request), 3);
final XmlPullParserFactory factory = XmlPullParserFactory.newInstance(System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null);
final XmlPullParser pp = factory.newPullParser();
pp.setInput(is, DEFAULT_ENCODING);
final List<Location> results = new ArrayList<Location>();
assertResC(pp);
XmlPullUtil.enter(pp, "ResC");
XmlPullUtil.enter(pp, "MLcRes");
while (XmlPullUtil.test(pp, "MLc"))
{
final String t = XmlPullUtil.attr(pp, "t");
final LocationType type;
if ("ST".equals(t))
type = LocationType.STATION;
else if ("POI".equals(t))
type = LocationType.POI;
else if ("ADR".equals(t))
type = LocationType.ADDRESS;
else
throw new IllegalStateException("cannot handle: '" + t + "'");
final int id;
final String i = pp.getAttributeValue(null, "i");
if (i != null)
{
final Matcher iMatcherId = P_XML_MLC_REQ_ID.matcher(i);
if (!iMatcherId.matches())
throw new IllegalStateException("cannot parse id: '" + i + "'");
id = Integer.parseInt(iMatcherId.group(1));
}
else
{
id = 0;
}
final String name = XmlPullUtil.attr(pp, "n");
final String r = pp.getAttributeValue(null, "r");
final Matcher iMatcherLonLat = P_XML_MLC_REQ_LONLAT.matcher(i != null ? i : r);
if (!iMatcherLonLat.matches())
throw new IllegalStateException("cannot parse lon/lat: '" + i + "'");
final int lon = Integer.parseInt(iMatcherLonLat.group(1));
final int lat = Integer.parseInt(iMatcherLonLat.group(2));
results.add(new Location(type, id, lat, lon, null, name));
XmlPullUtil.next(pp);
}
XmlPullUtil.exit(pp, "MLcRes");
XmlPullUtil.exit(pp, "ResC");
return results;
}
catch (final XmlPullParserException x)
{
throw new RuntimeException(x);
}
finally
{
if (is != null)
is.close();
}
}
private static final Pattern P_XML_QUERY_DEPARTURES_COARSE = Pattern.compile(
"\\G<Journey ([^>]*?)(?:/>|><HIMMessage ([^>]*?)/></Journey>)(?:\n|\\z)", Pattern.DOTALL);
private static final Pattern P_XML_QUERY_DEPARTURES_FINE = Pattern.compile("" // private static final Pattern P_XML_QUERY_DEPARTURES_FINE = Pattern.compile("" //
+ "fpTime\\s*=\"(\\d{1,2}:\\d{2})\"\\s*" // time + "fpTime\\s*=\"(\\d{1,2}:\\d{2})\"\\s*" // time
+ "fpDate\\s*=\"(\\d{2}\\.\\d{2}\\.\\d{2}|\\d{4}-\\d{2}-\\d{2})\"\\s*" // date + "fpDate\\s*=\"(\\d{2}\\.\\d{2}\\.\\d{2}|\\d{4}-\\d{2}-\\d{2})\"\\s*" // date

View file

@ -37,7 +37,7 @@ public class ZvvProvider extends AbstractHafasProvider
public ZvvProvider() public ZvvProvider()
{ {
super(null, null); super(API_BASE + "query.exe/dn", null);
} }
public NetworkId id() public NetworkId id()
@ -54,16 +54,10 @@ public class ZvvProvider extends AbstractHafasProvider
return false; return false;
} }
private static final String AUTOCOMPLETE_URI = API_BASE
+ "ajax-getstop.exe/dny?start=1&tpl=suggest2json&REQ0JourneyStopsS0A=255&REQ0JourneyStopsS0B=5&REQ0JourneyStopsB=12&getstop=1&noSession=yes&REQ0JourneyStopsS0G=%s?&js=true&";
private static final String ENCODING = "ISO-8859-1";
@Override @Override
public List<Location> autocompleteStations(final CharSequence constraint) throws IOException public List<Location> autocompleteStations(final CharSequence constraint) throws IOException
{ {
final String uri = String.format(AUTOCOMPLETE_URI, ParserUtils.urlEncode(constraint.toString(), ENCODING)); return xmlMLcReq(constraint);
return ajaxGetStops(uri);
} }
@Override @Override
@ -127,6 +121,8 @@ public class ZvvProvider extends AbstractHafasProvider
return 'R'; return 'R';
if ("EXT".equals(ucType)) if ("EXT".equals(ucType))
return 'R'; return 'R';
if ("ATZ".equals(ucType)) // Autotunnelzug
return 'R';
if ("S-BAHN".equals(ucType)) if ("S-BAHN".equals(ucType))
return 'S'; return 'S';
@ -162,6 +158,8 @@ public class ZvvProvider extends AbstractHafasProvider
return 'C'; return 'C';
if ("LB".equals(ucType)) // Luftseilbahn if ("LB".equals(ucType)) // Luftseilbahn
return 'C'; return 'C';
if ("SL".equals(ucType)) // Sessel-Lift
return 'C';
if ("UNB".equals(ucType)) if ("UNB".equals(ucType))
return '?'; return '?';