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 |
|
|
import org.w3c.dom.Node;
|
8 |
|
|
import org.w3c.dom.NodeList;
|
9 |
|
|
|
10 |
|
|
import android.util.Log;
|
11 |
|
|
import dk.thoerup.traininfo.TimetableBean;
|
12 |
|
|
import dk.thoerup.traininfo.util.DownloadUtil;
|
13 |
|
|
import dk.thoerup.traininfo.util.XmlUtil;
|
14 |
|
|
|
15 |
|
|
public class XmlTimetableProvider implements TimetableProvider {
|
16 |
|
|
|
17 |
|
|
List<TimetableBean> timetables = new ArrayList<TimetableBean>();
|
18 |
|
|
|
19 |
|
|
@Override
|
20 |
|
|
public List<TimetableBean> getTimetable() {
|
21 |
|
|
return timetables;
|
22 |
|
|
}
|
23 |
|
|
|
24 |
|
|
@Override
|
25 |
|
|
public boolean lookupTimetable(String trainID) {
|
26 |
|
|
boolean success = false;
|
27 |
torben |
357 |
String url = XmlUtil.SERVICE_BASE + "/TimetableServlet?train=" + trainID ;
|
28 |
torben |
352 |
Log.i("url", url);
|
29 |
|
|
try {
|
30 |
|
|
timetables.clear();
|
31 |
|
|
|
32 |
|
|
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
|
33 |
|
|
|
34 |
|
|
|
35 |
|
|
Document doc = XmlUtil.parseXML(xml);
|
36 |
|
|
Node rootNode = doc.getDocumentElement(); // stations
|
37 |
|
|
NodeList stationList = rootNode.getChildNodes();
|
38 |
|
|
|
39 |
|
|
|
40 |
|
|
for (int i=0; i<stationList.getLength(); i++) {
|
41 |
|
|
Node entryNode = stationList.item(i);
|
42 |
|
|
|
43 |
|
|
if (! entryNode.getNodeName().equals("entry"))
|
44 |
|
|
continue;
|
45 |
|
|
|
46 |
|
|
TimetableBean timetable = new TimetableBean();
|
47 |
|
|
|
48 |
|
|
NodeList entries = entryNode.getChildNodes();
|
49 |
|
|
for (int j=0; j<entries.getLength(); j++) {
|
50 |
|
|
Node current = entries.item(j);
|
51 |
|
|
|
52 |
|
|
String content = null;
|
53 |
|
|
if (current.getFirstChild() != null)
|
54 |
|
|
content = current.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value
|
55 |
|
|
|
56 |
|
|
String nodeName = current.getNodeName();
|
57 |
|
|
|
58 |
|
|
if (nodeName.equals("station"))
|
59 |
|
|
timetable.setStation( content );
|
60 |
|
|
|
61 |
|
|
if (nodeName.equals("arrival"))
|
62 |
|
|
timetable.setArrival( content );
|
63 |
|
|
|
64 |
|
|
if (nodeName.equals("departure"))
|
65 |
|
|
timetable.setDeparture( content );
|
66 |
|
|
|
67 |
|
|
if (nodeName.equals("current"))
|
68 |
|
|
timetable.setCurrent( Boolean.parseBoolean(content) );
|
69 |
|
|
|
70 |
|
|
}
|
71 |
|
|
timetables.add(timetable);
|
72 |
|
|
}
|
73 |
|
|
success = true;
|
74 |
|
|
|
75 |
|
|
|
76 |
|
|
} catch (Exception e) {
|
77 |
|
|
Log.e("XmlStationProvider", "lookupStations: ", e);
|
78 |
|
|
}
|
79 |
|
|
return success;
|
80 |
|
|
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
}
|