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