--- android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlDepartureProvider.java 2009/08/08 19:02:20 237 +++ android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlDepartureProvider.java 2009/09/11 12:24:53 319 @@ -1,13 +1,9 @@ package dk.thoerup.traininfo.provider; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.io.StringReader; -import java.net.URL; -import java.net.URLConnection; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.List; import javax.xml.parsers.SAXParser; @@ -21,21 +17,58 @@ import android.util.Log; import dk.thoerup.traininfo.DepartureBean; +import dk.thoerup.traininfo.util.DownloadUtil; public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider { - ArrayList departures = new ArrayList(); + final static long CACHE_TIMEOUT = 60*1000; + class CacheEntry { + public long timestamp; + public List departures; + } + + HashMap departureCache = new HashMap(); + ArrayList departures; + DepartureBean tempDeparture; StringBuilder builder = new StringBuilder(512); @Override - public void lookupDepartures(String station) { - departures.clear(); + public boolean lookupDepartures(int stationID) { + CacheEntry entry = departureCache.get(stationID); + boolean success; + + long now = android.os.SystemClock.elapsedRealtime(); + if (entry == null || (entry.timestamp+CACHE_TIMEOUT) < now) { + + success = lookupDeparturesWorker(stationID); + + if (success) { + entry = new CacheEntry(); + entry.timestamp = android.os.SystemClock.elapsedRealtime(); + entry.departures = departures; + + departureCache.put(stationID, entry); + } + } else { + Log.i("XmlDepartureProvider", "cache hit !!!"); + success = true; + } + + return success; + } + + private boolean lookupDeparturesWorker(int stationID) { + boolean success = false; + departures = new ArrayList(); try { - String doc = getUrlContents("http://t-hoerup.dk/tog/xml_display.php?stationname="+station); + //String url = "http://t-hoerup.dk/tog/xml_display.php?stationcode="+stationCode; + String url = "http://app.t-hoerup.dk/TrainInfoService/DepartureServlet?format=xml&station=" + stationID; + Log.i("xmlurl",url); + String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1"); InputSource source = new InputSource( new StringReader(doc)); @@ -45,36 +78,25 @@ xr.setContentHandler(this); xr.setErrorHandler(this); - xr.setDTDHandler(this); xr.parse(source); + success = true; + } catch (Exception e) { Log.e("XmlDepartureProvider", "looupFunction", e); } + return success; } @Override - public List getDepartures() { - return departures; - } - - private String getUrlContents(String uri) throws IOException - { - URL url = new URL(uri); - URLConnection conn = url.openConnection(); - conn.setConnectTimeout(5000); - InputStream stream = conn.getInputStream(); - - BufferedReader in = new BufferedReader(new InputStreamReader(stream, "ISO-8859-1"),8192); - - StringBuilder sbuilder = new StringBuilder(); - - String line; - while ( (line = in.readLine()) != null) { - sbuilder.append(line); - sbuilder.append("\r\n"); + public List getDepartures(int station) { + CacheEntry entry = departureCache.get(station); + + if (entry != null) { + return Collections.unmodifiableList(entry.departures); + } else { + return new ArrayList(); } - - return sbuilder.toString(); + } // this can be called several times fore the same text-node if there are many chardata / lines @@ -100,21 +122,21 @@ if (name.equals("train")) { departures.add( tempDeparture ); } else if (name.equals("time")) { - tempDeparture.setTime(builder.toString()); + tempDeparture.setTime(builder.toString().trim()); } else if (name.equals("updated")) { - tempDeparture.setLastUpdate(builder.toString()); + tempDeparture.setLastUpdate(builder.toString().trim()); } else if (name.equals("trainnumber")) { - tempDeparture.setTrainNumber(builder.toString()); + tempDeparture.setTrainNumber(builder.toString().trim()); } else if (name.equals("destination")) { - tempDeparture.setDestination(builder.toString()); + tempDeparture.setDestination(builder.toString().trim()); } else if (name.equals("origin")) { - tempDeparture.setOrigin(builder.toString()); + tempDeparture.setOrigin(builder.toString().trim()); } else if (name.equals("location")) { - tempDeparture.setLocation(builder.toString()); + tempDeparture.setLocation(builder.toString().trim()); } else if (name.equals("status")) { - tempDeparture.setStatus(builder.toString()); + tempDeparture.setStatus(builder.toString().trim()); } else if (name.equals("note")) { - tempDeparture.setNote(builder.toString()); + tempDeparture.setNote(builder.toString().trim()); } } }