Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/backend/FiveTStopsFetcher.java
Show All 33 Lines | |||||
* Once was asynchronous BusStop[] fetcher from a query, code mostly taken from | * Once was asynchronous BusStop[] fetcher from a query, code mostly taken from | ||||
* AsyncWgetBusStopSuggestions (by Valerio Bozzolan) | * AsyncWgetBusStopSuggestions (by Valerio Bozzolan) | ||||
* | * | ||||
* @see FiveTScraperFetcher | * @see FiveTScraperFetcher | ||||
*/ | */ | ||||
public class FiveTStopsFetcher implements StopsFinderByName { | public class FiveTStopsFetcher implements StopsFinderByName { | ||||
@Override | @Override | ||||
public List<Stop> FindByName(String name, AtomicReference<result> res) { | public List<Stop> FindByName(String name, AtomicReference<Result> res) { | ||||
// API apparently limited to 20 results | // API apparently limited to 20 results | ||||
ArrayList<Stop> busStops = new ArrayList<>(20); | ArrayList<Stop> busStops = new ArrayList<>(20); | ||||
String stopID; | String stopID; | ||||
String stopName; | String stopName; | ||||
String stopLocation; | String stopLocation; | ||||
//Stop busStop; | //Stop busStop; | ||||
if(name.length() < 3) { | if(name.length() < 3) { | ||||
res.set(result.QUERY_TOO_SHORT); | res.set(Result.QUERY_TOO_SHORT); | ||||
return busStops; | return busStops; | ||||
} | } | ||||
String responseInDOMFormatBecause5THaveAbsolutelyNoIdeaWhatJSONWas; | String responseInDOMFormatBecause5THaveAbsolutelyNoIdeaWhatJSONWas; | ||||
URL u; | URL u; | ||||
try { | try { | ||||
u = new URL("http://www.5t.torino.it/5t/trasporto/stop-lookup.jsp?action=search&stopShortName=" + URLEncoder.encode(name, "utf-8")); | u = new URL("http://www.5t.torino.it/5t/trasporto/stop-lookup.jsp?action=search&stopShortName=" + URLEncoder.encode(name, "utf-8")); | ||||
} catch(Exception e) { | } catch(Exception e) { | ||||
res.set(Fetcher.result.PARSER_ERROR); | res.set(Result.PARSER_ERROR); | ||||
return busStops; | return busStops; | ||||
} | } | ||||
responseInDOMFormatBecause5THaveAbsolutelyNoIdeaWhatJSONWas = networkTools.getDOM(u, res); | responseInDOMFormatBecause5THaveAbsolutelyNoIdeaWhatJSONWas = networkTools.getDOM(u, res); | ||||
if (responseInDOMFormatBecause5THaveAbsolutelyNoIdeaWhatJSONWas == null) { | if (responseInDOMFormatBecause5THaveAbsolutelyNoIdeaWhatJSONWas == null) { | ||||
// result already set in getDOM() | // result already set in getDOM() | ||||
return busStops; | return busStops; | ||||
} | } | ||||
Show All 32 Lines | public List<Stop> FindByName(String name, AtomicReference<Result> res) { | ||||
if(stopLocation == null || stopLocation.length() == 0) { | if(stopLocation == null || stopLocation.length() == 0) { | ||||
stopLocation = db.getLocationFromID(stopID); | stopLocation = db.getLocationFromID(stopID); | ||||
}*/ | }*/ | ||||
busStops.add(new Stop(stopName, stopID, stopLocation, null, null)); | busStops.add(new Stop(stopName, stopID, stopLocation, null, null)); | ||||
} | } | ||||
if(busStops.size() == 0) { | if(busStops.size() == 0) { | ||||
res.set(result.EMPTY_RESULT_SET); | res.set(Result.EMPTY_RESULT_SET); | ||||
} else { | } else { | ||||
res.set(result.OK); | res.set(Result.OK); | ||||
} | } | ||||
Collections.sort(busStops); | Collections.sort(busStops); | ||||
// TODO: remove duplicates? (see GTTStopsFetcher) | // TODO: remove duplicates? (see GTTStopsFetcher) | ||||
return busStops; | return busStops; | ||||
} | } | ||||
} | } |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register