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