1 |
package dk.thoerup.traininfo.provider; |
package dk.thoerup.traininfo.provider; |
2 |
|
|
|
import java.io.BufferedReader; |
|
|
import java.io.IOException; |
|
|
import java.io.InputStream; |
|
|
import java.io.InputStreamReader; |
|
3 |
import java.io.StringReader; |
import java.io.StringReader; |
|
import java.net.URL; |
|
|
import java.net.URLConnection; |
|
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; |
17 |
|
|
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; |
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 station) { |
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 doc = getUrlContents("http://t-hoerup.dk/tog/xml_display.php?stationname="+station); |
|
70 |
|
String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID; |
71 |
|
Log.i("xmlurl",url); |
72 |
|
String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1"); |
73 |
|
|
74 |
InputSource source = new InputSource( new StringReader(doc)); |
InputSource source = new InputSource( new StringReader(doc)); |
75 |
|
|
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 |
private String getUrlContents(String uri) throws IOException |
return entry.departures; |
97 |
{ |
} else { |
98 |
URL url = new URL(uri); |
return new ArrayList<DepartureBean>(); |
|
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"); |
|
99 |
} |
} |
100 |
|
|
|
return sbuilder.toString(); |
|
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 |
123 |
if (name.equals("train")) { |
if (name.equals("train")) { |
124 |
departures.add( tempDeparture ); |
departures.add( tempDeparture ); |
125 |
} else if (name.equals("time")) { |
} else if (name.equals("time")) { |
126 |
tempDeparture.setTime(builder.toString()); |
tempDeparture.setTime(builder.toString().trim()); |
127 |
} else if (name.equals("updated")) { |
} else if (name.equals("updated")) { |
128 |
tempDeparture.setLastUpdate(builder.toString()); |
tempDeparture.setLastUpdate(builder.toString().trim()); |
129 |
} else if (name.equals("trainnumber")) { |
} else if (name.equals("trainnumber")) { |
130 |
tempDeparture.setTrainNumber(builder.toString()); |
tempDeparture.setTrainNumber(builder.toString().trim()); |
131 |
} else if (name.equals("destination")) { |
} else if (name.equals("destination")) { |
132 |
tempDeparture.setDestination(builder.toString()); |
tempDeparture.setDestination(builder.toString().trim()); |
133 |
} else if (name.equals("origin")) { |
} else if (name.equals("origin")) { |
134 |
tempDeparture.setOrigin(builder.toString()); |
tempDeparture.setOrigin(builder.toString().trim()); |
135 |
} else if (name.equals("location")) { |
} else if (name.equals("location")) { |
136 |
tempDeparture.setLocation(builder.toString()); |
tempDeparture.setLocation(builder.toString().trim()); |
137 |
} else if (name.equals("status")) { |
} else if (name.equals("status")) { |
138 |
tempDeparture.setStatus(builder.toString()); |
tempDeparture.setStatus(builder.toString().trim()); |
139 |
} else if (name.equals("note")) { |
} else if (name.equals("note")) { |
140 |
tempDeparture.setNote(builder.toString()); |
tempDeparture.setNote(builder.toString().trim()); |
141 |
} |
} |
142 |
} |
} |
143 |
} |
} |