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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 357 by torben, Tue Sep 29 19:06:34 2009 UTC revision 1007 by torben, Tue Aug 3 06:12:10 2010 UTC
# Line 4  import java.util.ArrayList; Line 4  import java.util.ArrayList;
4  import java.util.List;  import java.util.List;
5    
6  import org.w3c.dom.Document;  import org.w3c.dom.Document;
7    
8  import org.w3c.dom.Node;  import org.w3c.dom.Node;
9  import org.w3c.dom.NodeList;  import org.w3c.dom.NodeList;
10    
11  import android.util.Log;  import android.util.Log;
12  import dk.thoerup.traininfo.TimetableBean;  import dk.thoerup.traininfo.TimetableBean;
13    import dk.thoerup.traininfo.util.AndroidTimeoutCache;
14  import dk.thoerup.traininfo.util.DownloadUtil;  import dk.thoerup.traininfo.util.DownloadUtil;
15  import dk.thoerup.traininfo.util.XmlUtil;  import dk.thoerup.traininfo.util.XmlUtil;
16    
17  public class XmlTimetableProvider implements TimetableProvider {  public class XmlTimetableProvider implements TimetableProvider {
18                    
19          List<TimetableBean> timetables = new ArrayList<TimetableBean>();          final static int CACHE_TIMEOUT = 60*1000;
20    
21            AndroidTimeoutCache<String,List<TimetableBean>> departureCache = new AndroidTimeoutCache<String,List<TimetableBean>>(CACHE_TIMEOUT);    
22    
23          @Override          @Override
24          public List<TimetableBean> getTimetable() {          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;                  return timetables;
43          }          }
44            
45    
46          @Override          public List<TimetableBean> lookupTimetableWorker(String type, String trainNumber) {
47          public boolean lookupTimetable(String trainID) {  
48                  boolean success = false;                  String url = XmlUtil.SERVICE_BASE + "/TimetableServlet?train=" + trainNumber + "&type=" + type;
                 String url = XmlUtil.SERVICE_BASE + "/TimetableServlet?train=" + trainID ;  
49                  Log.i("url", url);                  Log.i("url", url);
50                  try {                  try {
51                          timetables.clear();                          List<TimetableBean> timetables = new ArrayList<TimetableBean>();
52                                                    
53                          String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");                          String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
54                                                    
# Line 45  public class XmlTimetableProvider implem Line 66  public class XmlTimetableProvider implem
66                    
67                                  TimetableBean timetable = new TimetableBean();                                  TimetableBean timetable = new TimetableBean();
68                                                                    
69                                  NodeList entries = entryNode.getChildNodes();                                  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++) {                                      for (int j=0; j<entries.getLength(); j++) {    
78                                          Node current = entries.item(j);                                          Node current = entries.item(j);
79                                                                                                                                                                    
# Line 64  public class XmlTimetableProvider implem Line 92  public class XmlTimetableProvider implem
92                                          if (nodeName.equals("departure"))                                          if (nodeName.equals("departure"))
93                                                  timetable.setDeparture( content );                                                  timetable.setDeparture( content );
94                                                                                    
95                                          if (nodeName.equals("current"))                                          if (nodeName.equals("stationid"))
96                                                  timetable.setCurrent( Boolean.parseBoolean(content) );                                                  timetable.setStationId( Integer.parseInt(content));
97                                            
98                                            /*if (nodeName.equals("current"))
99                                                    timetable.setCurrent( Boolean.parseBoolean(content) );*/
100    
101                                  }                                  }
102                                  timetables.add(timetable);                                                                timetables.add(timetable);                              
103                          }                          }
104                          success = true;                          return timetables;
105                                                    
106                                                    
107                  } catch (Exception e) {                  } catch (Exception e) {
108                          Log.e("XmlStationProvider", "lookupStations: ", e);                          Log.e("XmlStationProvider", "lookupStations: ", e);
109                  }                  }
                 return success;  
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  }  }

Legend:
Removed from v.357  
changed lines
  Added in v.1007

  ViewVC Help
Powered by ViewVC 1.1.20