package dk.thoerup.traininfo.provider; import java.io.StringReader; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; 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 { final static int CACHE_TIMEOUT = 60*1000; AndroidTimeoutCache departureCache = new AndroidTimeoutCache(CACHE_TIMEOUT); DepartureBean departures; DepartureEntry tempDeparture; StringBuilder builder = new StringBuilder(512); @Override public boolean lookupDepartures(int stationID, boolean arrival) { boolean success; String key = "" + stationID + ":" + arrival; departures = departureCache.get(key); if (departures == null) { success = lookupDeparturesWorker(stationID, arrival); if (success) { departureCache.put(key, departures); } } else { Log.i("XmlDepartureProvider", "cache hit !!!"); success = true; } return success; } private boolean lookupDeparturesWorker(int stationID, boolean arrival) { boolean success = false; departures = new DepartureBean(); try { 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"); InputSource source = new InputSource( new StringReader(doc)); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); xr.setContentHandler(this); xr.setErrorHandler(this); xr.parse(source); success = true; } catch (Exception e) { Log.e("XmlDepartureProvider", "looupFunction", e); } return success; } @Override public DepartureBean getDepartures(int station,boolean arrival) { String key = "" + station + ":" + arrival; DepartureBean bean = departureCache.get(key); if (bean == null) { bean = new DepartureBean(); } return bean; } // 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