/[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 840 - (show annotations) (download)
Fri Jun 11 18:46:46 2010 UTC (13 years, 11 months ago) by torben
File size: 3823 byte(s)
Enable forward navigation from timetable list to a new departure list
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("stationid"))
112 timetable.setStationId( Integer.parseInt(content));
113
114 /*if (nodeName.equals("current"))
115 timetable.setCurrent( Boolean.parseBoolean(content) );*/
116
117 }
118 timetables.add(timetable);
119 }
120 success = true;
121
122
123 } catch (Exception e) {
124 Log.e("XmlStationProvider", "lookupStations: ", e);
125 }
126
127 return success;
128
129 }
130
131 private String extractTrainNumber (String trainID) {
132
133 String parts[] = trainID.split(" ");
134 if (parts.length == 2) {
135 return parts[1];
136 } else {
137 return parts[0];
138 }
139 }
140
141 }

  ViewVC Help
Powered by ViewVC 1.1.20