/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlTimetableProvider.java
ViewVC logotype

Contents of /android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlTimetableProvider.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1007 - (show annotations) (download)
Tue Aug 3 06:12:10 2010 UTC (13 years, 9 months ago) by torben
File size: 3516 byte(s)
switch the last 2 xml providers away from 2-step lookup
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 AndroidTimeoutCache<String,List<TimetableBean>> departureCache = new AndroidTimeoutCache<String,List<TimetableBean>>(CACHE_TIMEOUT);
22
23 @Override
24 public List<TimetableBean> lookupTimetable(String type, String trainID) {
25
26 String trainNumber = extractTrainNumber(trainID);
27
28 String key = type + "-" + trainID;
29 List<TimetableBean> timetables = departureCache.get(key);
30
31 if (timetables == null) {
32 timetables = lookupTimetableWorker(type, trainNumber);
33
34 if (timetables != null) {
35 departureCache.put(key, timetables);
36 }
37
38 } else {
39 Log.i("XmlTimetableProvider", "cache hit !!!");
40 }
41
42 return timetables;
43 }
44
45
46 public List<TimetableBean> lookupTimetableWorker(String type, String trainNumber) {
47
48 String url = XmlUtil.SERVICE_BASE + "/TimetableServlet?train=" + trainNumber + "&type=" + type;
49 Log.i("url", url);
50 try {
51 List<TimetableBean> timetables = new ArrayList<TimetableBean>();
52
53 String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
54
55
56 Document doc = XmlUtil.parseXML(xml);
57 Node rootNode = doc.getDocumentElement(); // stations
58 NodeList stationList = rootNode.getChildNodes();
59
60
61 for (int i=0; i<stationList.getLength(); i++) {
62 Node entryNode = stationList.item(i);
63
64 if (! entryNode.getNodeName().equals("entry"))
65 continue;
66
67 TimetableBean timetable = new TimetableBean();
68
69 NodeList entries = entryNode.getChildNodes();
70
71 if (entryNode.hasAttributes() && entryNode.getAttributes().getNamedItem("current") != null) {
72 timetable.setCurrent( Boolean.parseBoolean(entryNode.getAttributes().getNamedItem("current").getNodeValue()));
73 } else {
74 timetable.setCurrent(false);
75 }
76
77 for (int j=0; j<entries.getLength(); j++) {
78 Node current = entries.item(j);
79
80 String content = null;
81 if (current.getFirstChild() != null)
82 content = current.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value
83
84 String nodeName = current.getNodeName();
85
86 if (nodeName.equals("station"))
87 timetable.setStation( content );
88
89 if (nodeName.equals("arrival"))
90 timetable.setArrival( content );
91
92 if (nodeName.equals("departure"))
93 timetable.setDeparture( content );
94
95 if (nodeName.equals("stationid"))
96 timetable.setStationId( Integer.parseInt(content));
97
98 /*if (nodeName.equals("current"))
99 timetable.setCurrent( Boolean.parseBoolean(content) );*/
100
101 }
102 timetables.add(timetable);
103 }
104 return timetables;
105
106
107 } catch (Exception e) {
108 Log.e("XmlStationProvider", "lookupStations: ", e);
109 }
110
111 return null;
112 }
113
114 private String extractTrainNumber (String trainID) {
115
116 String parts[] = trainID.split(" ");
117 if (parts.length == 2) {
118 return parts[1];
119 } else {
120 return parts[0];
121 }
122 }
123
124 }

  ViewVC Help
Powered by ViewVC 1.1.20