1 |
torben |
352 |
package dk.thoerup.traininfo.provider;
|
2 |
|
|
|
3 |
|
|
import java.util.ArrayList;
|
4 |
|
|
import java.util.List;
|
5 |
|
|
|
6 |
|
|
import org.w3c.dom.Document;
|
7 |
torben |
391 |
|
8 |
torben |
352 |
import org.w3c.dom.Node;
|
9 |
|
|
import org.w3c.dom.NodeList;
|
10 |
|
|
|
11 |
|
|
import android.util.Log;
|
12 |
|
|
import dk.thoerup.traininfo.TimetableBean;
|
13 |
torben |
391 |
import dk.thoerup.traininfo.util.AndroidTimeoutCache;
|
14 |
torben |
352 |
import dk.thoerup.traininfo.util.DownloadUtil;
|
15 |
|
|
import dk.thoerup.traininfo.util.XmlUtil;
|
16 |
|
|
|
17 |
|
|
public class XmlTimetableProvider implements TimetableProvider {
|
18 |
|
|
|
19 |
torben |
391 |
final static int CACHE_TIMEOUT = 60*1000;
|
20 |
|
|
|
21 |
|
|
List<TimetableBean> timetables;
|
22 |
torben |
352 |
|
23 |
torben |
391 |
AndroidTimeoutCache<String,List<TimetableBean>> departureCache = new AndroidTimeoutCache<String,List<TimetableBean>>(CACHE_TIMEOUT);
|
24 |
|
|
|
25 |
torben |
352 |
@Override
|
26 |
torben |
391 |
public List<TimetableBean> getTimetable(String trainID) {
|
27 |
|
|
List<TimetableBean> list = departureCache.get(trainID);
|
28 |
|
|
|
29 |
|
|
if (list == null) {
|
30 |
|
|
list = new ArrayList<TimetableBean>();
|
31 |
|
|
}
|
32 |
|
|
|
33 |
|
|
return list;
|
34 |
torben |
352 |
}
|
35 |
|
|
|
36 |
|
|
@Override
|
37 |
|
|
public boolean lookupTimetable(String trainID) {
|
38 |
torben |
391 |
boolean success;
|
39 |
|
|
|
40 |
|
|
timetables = departureCache.get(trainID);
|
41 |
|
|
|
42 |
|
|
if (timetables == null) {
|
43 |
|
|
success = lookupTimetableWorker(trainID);
|
44 |
|
|
|
45 |
|
|
if (success) {
|
46 |
|
|
departureCache.put(trainID, timetables);
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
} else {
|
50 |
|
|
Log.i("XmlTimetableProvider", "cache hit !!!");
|
51 |
|
|
success = true;
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
return success;
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
|
58 |
|
|
public boolean lookupTimetableWorker(String trainID) {
|
59 |
torben |
352 |
boolean success = false;
|
60 |
torben |
365 |
String url = XmlUtil.SERVICE_BASE + "/TimetableServlet?train=" + trainID.replace(" ", "%20") ;
|
61 |
torben |
352 |
Log.i("url", url);
|
62 |
|
|
try {
|
63 |
torben |
391 |
timetables = new ArrayList<TimetableBean>();
|
64 |
torben |
352 |
|
65 |
|
|
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
|
66 |
|
|
|
67 |
|
|
|
68 |
|
|
Document doc = XmlUtil.parseXML(xml);
|
69 |
|
|
Node rootNode = doc.getDocumentElement(); // stations
|
70 |
|
|
NodeList stationList = rootNode.getChildNodes();
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
for (int i=0; i<stationList.getLength(); i++) {
|
74 |
|
|
Node entryNode = stationList.item(i);
|
75 |
|
|
|
76 |
|
|
if (! entryNode.getNodeName().equals("entry"))
|
77 |
|
|
continue;
|
78 |
|
|
|
79 |
|
|
TimetableBean timetable = new TimetableBean();
|
80 |
|
|
|
81 |
torben |
365 |
NodeList entries = entryNode.getChildNodes();
|
82 |
|
|
|
83 |
|
|
if (entryNode.hasAttributes() && entryNode.getAttributes().getNamedItem("current") != null) {
|
84 |
|
|
timetable.setCurrent( Boolean.parseBoolean(entryNode.getAttributes().getNamedItem("current").getNodeValue()));
|
85 |
|
|
} else {
|
86 |
|
|
timetable.setCurrent(false);
|
87 |
|
|
}
|
88 |
|
|
|
89 |
torben |
352 |
for (int j=0; j<entries.getLength(); j++) {
|
90 |
|
|
Node current = entries.item(j);
|
91 |
|
|
|
92 |
|
|
String content = null;
|
93 |
|
|
if (current.getFirstChild() != null)
|
94 |
|
|
content = current.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value
|
95 |
|
|
|
96 |
|
|
String nodeName = current.getNodeName();
|
97 |
|
|
|
98 |
|
|
if (nodeName.equals("station"))
|
99 |
|
|
timetable.setStation( content );
|
100 |
|
|
|
101 |
|
|
if (nodeName.equals("arrival"))
|
102 |
|
|
timetable.setArrival( content );
|
103 |
|
|
|
104 |
|
|
if (nodeName.equals("departure"))
|
105 |
|
|
timetable.setDeparture( content );
|
106 |
|
|
|
107 |
torben |
365 |
/*if (nodeName.equals("current"))
|
108 |
|
|
timetable.setCurrent( Boolean.parseBoolean(content) );*/
|
109 |
torben |
352 |
|
110 |
|
|
}
|
111 |
|
|
timetables.add(timetable);
|
112 |
|
|
}
|
113 |
|
|
success = true;
|
114 |
|
|
|
115 |
|
|
|
116 |
|
|
} catch (Exception e) {
|
117 |
|
|
Log.e("XmlStationProvider", "lookupStations: ", e);
|
118 |
|
|
}
|
119 |
|
|
return success;
|
120 |
|
|
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
}
|