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.List; 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; public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider { ArrayList departures = new ArrayList(); DepartureBean tempDeparture; StringBuilder builder = new StringBuilder(512); @Override public void lookupDepartures(String station) { departures.clear(); try { String doc = getUrlContents("http://t-hoerup.dk/tog/xml_display.php?stationname="+station); 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.setDTDHandler(this); xr.parse(source); } catch (Exception e) { Log.e("XmlDepartureProvider", "looupFunction", e); } } @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"); } return sbuilder.toString(); } // 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