--- android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlDepartureProvider.java 2009/09/29 19:08:39 358 +++ android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlDepartureProvider.java 2010/08/03 06:12:10 1007 @@ -1,10 +1,6 @@ package dk.thoerup.traininfo.provider; import java.io.StringReader; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; @@ -17,57 +13,52 @@ import android.util.Log; import dk.thoerup.traininfo.DepartureBean; +import dk.thoerup.traininfo.DepartureEntry; +import dk.thoerup.traininfo.util.AndroidTimeoutCache; import dk.thoerup.traininfo.util.DownloadUtil; import dk.thoerup.traininfo.util.XmlUtil; -public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider { +public class XmlDepartureProvider implements DepartureProvider { - final static long CACHE_TIMEOUT = 60*1000; + final static int CACHE_TIMEOUT = 60*1000; + - class CacheEntry { - public long timestamp; - public List departures; - } + AndroidTimeoutCache departureCache = new AndroidTimeoutCache(CACHE_TIMEOUT); - HashMap departureCache = new HashMap(); - ArrayList departures; - DepartureBean tempDeparture; + DepartureEntry tempDeparture; StringBuilder builder = new StringBuilder(512); + + @Override - public boolean lookupDepartures(int stationID) { - CacheEntry entry = departureCache.get(stationID); - boolean success; + public DepartureBean lookupDepartures(int stationID, boolean arrival) { + + String key = "" + stationID + ":" + arrival; + + DepartureBean departures = departureCache.get(key); - long now = android.os.SystemClock.elapsedRealtime(); - if (entry == null || (entry.timestamp+CACHE_TIMEOUT) < now) { - - success = lookupDeparturesWorker(stationID); + if (departures == null) { + departures = lookupDeparturesWorker(stationID, arrival); - if (success) { - entry = new CacheEntry(); - entry.timestamp = android.os.SystemClock.elapsedRealtime(); - entry.departures = departures; - - departureCache.put(stationID, entry); + if (departures != null) { + departureCache.put(key, departures); } + } else { Log.i("XmlDepartureProvider", "cache hit !!!"); - success = true; } - return success; + return departures; } - private boolean lookupDeparturesWorker(int stationID) { - boolean success = false; - departures = new ArrayList(); + private DepartureBean lookupDeparturesWorker(int stationID, boolean arrival) { + try { - - String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID; + int iArrival = arrival ? 1 : 0; + String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID + "&arrival=" + iArrival; Log.i("xmlurl",url); String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1"); @@ -77,67 +68,71 @@ SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); - xr.setContentHandler(this); - xr.setErrorHandler(this); + DepartureParser departureParser = new DepartureParser(); + xr.setContentHandler(departureParser); + xr.setErrorHandler(departureParser); xr.parse(source); - success = true; + + return departureParser.getDepartures(); } catch (Exception e) { Log.e("XmlDepartureProvider", "looupFunction", e); - } - return success; + return null; + } } - @Override - public List getDepartures(int station) { - CacheEntry entry = departureCache.get(station); + + class DepartureParser extends DefaultHandler { - if (entry != null) { - return entry.departures; - } else { - return new ArrayList(); + private DepartureBean departures = new DepartureBean(); + + public DepartureBean getDepartures() { + return departures; } - - } - - // this can be called several times fore the same text-node if there are many chardata / lines - @Override - public void characters (char ch[], int start, int length) - { - for (int i= start; i