2 |
|
|
3 |
import java.io.StringReader; |
import java.io.StringReader; |
4 |
import java.util.ArrayList; |
import java.util.ArrayList; |
5 |
|
import java.util.Collections; |
6 |
|
import java.util.HashMap; |
7 |
import java.util.List; |
import java.util.List; |
8 |
|
|
9 |
import javax.xml.parsers.SAXParser; |
import javax.xml.parsers.SAXParser; |
18 |
import android.util.Log; |
import android.util.Log; |
19 |
import dk.thoerup.traininfo.DepartureBean; |
import dk.thoerup.traininfo.DepartureBean; |
20 |
import dk.thoerup.traininfo.util.DownloadUtil; |
import dk.thoerup.traininfo.util.DownloadUtil; |
21 |
|
import dk.thoerup.traininfo.util.XmlUtil; |
22 |
|
|
23 |
public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider { |
public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider { |
24 |
|
|
25 |
ArrayList<DepartureBean> departures = new ArrayList<DepartureBean>(); |
final static long CACHE_TIMEOUT = 60*1000; |
26 |
|
|
27 |
|
class CacheEntry { |
28 |
|
public long timestamp; |
29 |
|
public List<DepartureBean> departures; |
30 |
|
} |
31 |
|
|
32 |
|
HashMap<Integer, CacheEntry> departureCache = new HashMap<Integer,CacheEntry>(); |
33 |
|
ArrayList<DepartureBean> departures; |
34 |
|
|
35 |
|
|
36 |
DepartureBean tempDeparture; |
DepartureBean tempDeparture; |
37 |
StringBuilder builder = new StringBuilder(512); |
StringBuilder builder = new StringBuilder(512); |
38 |
|
|
39 |
@Override |
@Override |
40 |
public void lookupDepartures(String stationCode) { |
public boolean lookupDepartures(int stationID) { |
41 |
departures.clear(); |
CacheEntry entry = departureCache.get(stationID); |
42 |
|
boolean success; |
43 |
|
|
44 |
|
long now = android.os.SystemClock.elapsedRealtime(); |
45 |
|
if (entry == null || (entry.timestamp+CACHE_TIMEOUT) < now) { |
46 |
|
|
47 |
|
success = lookupDeparturesWorker(stationID); |
48 |
|
|
49 |
|
if (success) { |
50 |
|
entry = new CacheEntry(); |
51 |
|
entry.timestamp = android.os.SystemClock.elapsedRealtime(); |
52 |
|
entry.departures = departures; |
53 |
|
|
54 |
|
departureCache.put(stationID, entry); |
55 |
|
} |
56 |
|
} else { |
57 |
|
Log.i("XmlDepartureProvider", "cache hit !!!"); |
58 |
|
success = true; |
59 |
|
} |
60 |
|
|
61 |
|
return success; |
62 |
|
} |
63 |
|
|
64 |
|
private boolean lookupDeparturesWorker(int stationID) { |
65 |
|
boolean success = false; |
66 |
|
departures = new ArrayList<DepartureBean>(); |
67 |
try |
try |
68 |
{ |
{ |
69 |
String url = "http://t-hoerup.dk/tog/xml_display.php?stationcode="+stationCode; |
|
70 |
|
String url = XmlUtil.SERVICE_BASE + "/TrainInfoService/DepartureServlet?format=xml&station=" + stationID; |
71 |
Log.i("xmlurl",url); |
Log.i("xmlurl",url); |
72 |
String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1"); |
String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1"); |
73 |
|
|
79 |
|
|
80 |
xr.setContentHandler(this); |
xr.setContentHandler(this); |
81 |
xr.setErrorHandler(this); |
xr.setErrorHandler(this); |
|
xr.setDTDHandler(this); |
|
82 |
xr.parse(source); |
xr.parse(source); |
83 |
|
success = true; |
84 |
|
|
85 |
} catch (Exception e) { |
} catch (Exception e) { |
86 |
Log.e("XmlDepartureProvider", "looupFunction", e); |
Log.e("XmlDepartureProvider", "looupFunction", e); |
87 |
} |
} |
88 |
|
return success; |
89 |
} |
} |
90 |
|
|
91 |
@Override |
@Override |
92 |
public List<DepartureBean> getDepartures() { |
public List<DepartureBean> getDepartures(int station) { |
93 |
return departures; |
CacheEntry entry = departureCache.get(station); |
94 |
|
|
95 |
|
if (entry != null) { |
96 |
|
return entry.departures; |
97 |
|
} else { |
98 |
|
return new ArrayList<DepartureBean>(); |
99 |
|
} |
100 |
|
|
101 |
} |
} |
102 |
|
|
103 |
// this can be called several times fore the same text-node if there are many chardata / lines |
// this can be called several times fore the same text-node if there are many chardata / lines |