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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 699 - (hide annotations) (download)
Mon May 3 11:19:18 2010 UTC (14 years ago) by torben
File size: 3717 byte(s)
Switch to using type ("S2" vs "FJRN") for differentiating between regional trains and s-tog (corresponding change R697 on TrainInfoService)
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 torben 391
8 torben 352 import org.w3c.dom.Node;
9     import org.w3c.dom.NodeList;
10    
11     import android.util.Log;
12     import dk.thoerup.traininfo.TimetableBean;
13 torben 391 import dk.thoerup.traininfo.util.AndroidTimeoutCache;
14 torben 352 import dk.thoerup.traininfo.util.DownloadUtil;
15     import dk.thoerup.traininfo.util.XmlUtil;
16    
17     public class XmlTimetableProvider implements TimetableProvider {
18    
19 torben 391 final static int CACHE_TIMEOUT = 60*1000;
20    
21     List<TimetableBean> timetables;
22 torben 352
23 torben 391 AndroidTimeoutCache<String,List<TimetableBean>> departureCache = new AndroidTimeoutCache<String,List<TimetableBean>>(CACHE_TIMEOUT);
24    
25 torben 352 @Override
26 torben 699 public List<TimetableBean> getTimetable(String type, String trainID) {
27     String key = type + "-" + trainID;
28     List<TimetableBean> list = departureCache.get(key);
29 torben 391
30     if (list == null) {
31     list = new ArrayList<TimetableBean>();
32     }
33    
34     return list;
35 torben 352 }
36    
37     @Override
38 torben 699 public boolean lookupTimetable(String type, String trainID) {
39 torben 391 boolean success;
40    
41 torben 699 String trainNumber = extractTrainNumber(trainID);
42    
43     String key = type + "-" + trainID;
44     timetables = departureCache.get(key);
45 torben 391
46     if (timetables == null) {
47 torben 699 success = lookupTimetableWorker(type, trainNumber);
48 torben 391
49     if (success) {
50 torben 699 departureCache.put(key, timetables);
51 torben 391 }
52    
53     } else {
54     Log.i("XmlTimetableProvider", "cache hit !!!");
55     success = true;
56     }
57    
58     return success;
59     }
60    
61    
62 torben 699 public boolean lookupTimetableWorker(String type, String trainNumber) {
63 torben 352 boolean success = false;
64 torben 699 String url = XmlUtil.SERVICE_BASE + "/TimetableServlet?train=" + trainNumber + "&type=" + type;
65 torben 352 Log.i("url", url);
66     try {
67 torben 391 timetables = new ArrayList<TimetableBean>();
68 torben 352
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 torben 365 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 torben 352 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 torben 365 /*if (nodeName.equals("current"))
112     timetable.setCurrent( Boolean.parseBoolean(content) );*/
113 torben 352
114     }
115     timetables.add(timetable);
116     }
117     success = true;
118    
119    
120     } catch (Exception e) {
121     Log.e("XmlStationProvider", "lookupStations: ", e);
122     }
123 torben 699
124 torben 352 return success;
125    
126     }
127 torben 699
128     private String extractTrainNumber (String trainID) {
129 torben 352
130 torben 699 String parts[] = trainID.split(" ");
131     if (parts.length == 2) {
132     return parts[1];
133     } else {
134     return parts[0];
135     }
136     }
137    
138 torben 352 }

  ViewVC Help
Powered by ViewVC 1.1.20