--- android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.java 2009/09/11 12:24:53 319 +++ android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.java 2010/08/02 23:01:47 1004 @@ -1,46 +1,96 @@ package dk.thoerup.traininfo.provider; -import java.io.ByteArrayInputStream; -import java.io.IOException; + +import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; import android.location.Location; import android.util.Log; import dk.thoerup.traininfo.StationBean; +import dk.thoerup.traininfo.util.AndroidTimeoutCache; import dk.thoerup.traininfo.util.DownloadUtil; +import dk.thoerup.traininfo.util.XmlUtil; public class XmlStationProvider implements StationProvider { + + final static int CACHE_TIMEOUT = 300*1000; List stations = new ArrayList(); - + AndroidTimeoutCache> stationCache = new AndroidTimeoutCache>(CACHE_TIMEOUT); @Override public List getStations() { return stations; } + @Override public boolean lookupStations(Location location) { - boolean success = false; - String url = "http://app.t-hoerup.dk/TrainInfoService/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude(); + String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + (float)location.getLatitude() + "&longitude=" + (float)location.getLongitude(); Log.i("url", url); + return lookupStationsWorker(url); + } + + @Override + public boolean lookupStationsByName(String name) { + + // String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name); + String url = ""; + + try { + url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + URLEncoder.encode(name, "ISO8859-1"); + } catch (Exception e) { + Log.e("lookupStations", "Encoding failed", e); + } + + Log.i("url", url); + return lookupStationsWorker(url); + } + + + @Override + public boolean lookupStationsByIds(String ids) { + String url = ""; + url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids; + + Log.i("url", url); + return lookupStationsWorker(url); + } + + public boolean lookupStationsWorker(String url) { + boolean result; + List tmpStations = stationCache.get(url); + + if (tmpStations != null) { + Log.i("lookupStations", "cache hit " + url); + result = true; + } else { + result = fetchStations(url); + + if (result == true) { + stationCache.put(url, stations); + } + } + + return result; + } + + + public boolean fetchStations(String url) { + boolean success = false; + try { stations.clear(); String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); - Document doc = parseXML(xml); + Document doc = XmlUtil.parseXML(xml); Node rootNode = doc.getDocumentElement(); // stations NodeList stationList = rootNode.getChildNodes(); @@ -75,11 +125,20 @@ if (nodeName.equals("longitude")) station.setLongitude( Double.parseDouble(content) ); - if (nodeName.equals("stationcode")) - station.setCode(content); - if (nodeName.equals("calcdist")) station.setDistance( (int) Double.parseDouble(content) ); + + if (nodeName.equals("address")) + station.setAddress( content ); + + if (nodeName.equals("regional")) + station.setRegional( Boolean.parseBoolean(content) ); + + if (nodeName.equals("strain")) + station.setSTrain( Boolean.parseBoolean(content) ); + + if (nodeName.equals("metro")) + station.setMetro( Boolean.parseBoolean(content) ); } stations.add(station); @@ -92,10 +151,4 @@ } return success; } - - private Document parseXML(String str) throws SAXException, IOException, ParserConfigurationException - { - DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - return builder.parse( new ByteArrayInputStream(str.getBytes()) ); - } }