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

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

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

revision 238 by torben, Sat Aug 8 20:09:47 2009 UTC revision 391 by torben, Sat Oct 3 10:55:43 2009 UTC
# Line 15  import org.xml.sax.helpers.DefaultHandle Line 15  import org.xml.sax.helpers.DefaultHandle
15    
16  import android.util.Log;  import android.util.Log;
17  import dk.thoerup.traininfo.DepartureBean;  import dk.thoerup.traininfo.DepartureBean;
18    import dk.thoerup.traininfo.util.AndroidTimeoutCache;
19  import dk.thoerup.traininfo.util.DownloadUtil;  import dk.thoerup.traininfo.util.DownloadUtil;
20    import dk.thoerup.traininfo.util.XmlUtil;
21    
22  public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {  public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {
23    
24          ArrayList<DepartureBean> departures = new ArrayList<DepartureBean>();          final static int CACHE_TIMEOUT = 60*1000;
25                    
26            
27            AndroidTimeoutCache<Integer,List<DepartureBean>> departureCache = new AndroidTimeoutCache<Integer,List<DepartureBean>>(CACHE_TIMEOUT);
28                    
29            List<DepartureBean> departures;
30                    
31                    
32          DepartureBean tempDeparture;          DepartureBean tempDeparture;
33          StringBuilder builder = new StringBuilder(512);          StringBuilder builder = new StringBuilder(512);
34                    
35          @Override          @Override
36          public void lookupDepartures(String station) {          public boolean lookupDepartures(int stationID) {                
37                  departures.clear();                  boolean success;
38                    
39                    departures = departureCache.get(stationID);
40            
41                    if (departures == null) {                      
42                            success = lookupDeparturesWorker(stationID);
43                            
44                            if (success) {                  
45                                    departureCache.put(stationID, departures);
46                            }
47                            
48                    } else {
49                            Log.i("XmlDepartureProvider", "cache hit !!!");
50                            success = true;
51                    }              
52                    
53                    return success;
54            }
55            
56            private boolean lookupDeparturesWorker(int stationID) {
57                    boolean success = false;
58                    departures = new ArrayList<DepartureBean>();
59                  try                  try
60                  {       String url = "http://t-hoerup.dk/tog/xml_display.php?stationname="+station;                  {      
61                          String doc =  DownloadUtil.getContent(url, 30000, "ISO-8859-1");  
62                            String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID;
63                            Log.i("xmlurl",url);
64                            String doc =  DownloadUtil.getContentString(url, 45000, "ISO-8859-1");
65                                                    
66                          InputSource source = new InputSource( new StringReader(doc));                          InputSource source = new InputSource( new StringReader(doc));
67                                                    
# Line 40  public class XmlDepartureProvider extend Line 71  public class XmlDepartureProvider extend
71    
72                          xr.setContentHandler(this);                          xr.setContentHandler(this);
73                          xr.setErrorHandler(this);                          xr.setErrorHandler(this);
                         xr.setDTDHandler(this);  
74                          xr.parse(source);                          xr.parse(source);
75                            success = true;
76                            
77                  } catch (Exception e) {                  } catch (Exception e) {
78                          Log.e("XmlDepartureProvider", "looupFunction", e);                          Log.e("XmlDepartureProvider", "looupFunction", e);
79                  }                  }
80                    return success;
81          }          }
82                    
83          @Override          @Override
84          public List<DepartureBean> getDepartures() {          public List<DepartureBean> getDepartures(int station) {
85                  return departures;                  List<DepartureBean> list = departureCache.get(station);
86                    
87                    if (list == null) {                    
88                            list = new ArrayList<DepartureBean>();
89                    }
90                    
91                    return list;
92          }          }
93                    
94          // this can be called several times fore the same text-node if there are many chardata / lines          // this can be called several times fore the same text-node if there are many chardata / lines
# Line 75  public class XmlDepartureProvider extend Line 114  public class XmlDepartureProvider extend
114                  if (name.equals("train")) {                  if (name.equals("train")) {
115                          departures.add( tempDeparture );                          departures.add( tempDeparture );
116                  } else if (name.equals("time")) {                  } else if (name.equals("time")) {
117                          tempDeparture.setTime(builder.toString());                          tempDeparture.setTime(builder.toString().trim());
118                  } else if (name.equals("updated")) {                  } else if (name.equals("updated")) {
119                          tempDeparture.setLastUpdate(builder.toString());                          tempDeparture.setLastUpdate(builder.toString().trim());
120                  } else if (name.equals("trainnumber")) {                  } else if (name.equals("trainnumber")) {
121                          tempDeparture.setTrainNumber(builder.toString());                          tempDeparture.setTrainNumber(builder.toString().trim());
122                  } else if (name.equals("destination")) {                  } else if (name.equals("destination")) {
123                          tempDeparture.setDestination(builder.toString());                          tempDeparture.setDestination(builder.toString().trim());
124                  } else if (name.equals("origin")) {                  } else if (name.equals("origin")) {
125                          tempDeparture.setOrigin(builder.toString());                          tempDeparture.setOrigin(builder.toString().trim());
126                  } else if (name.equals("location")) {                  } else if (name.equals("location")) {
127                          tempDeparture.setLocation(builder.toString());                          tempDeparture.setLocation(builder.toString().trim());
128                  } else if (name.equals("status")) {                  } else if (name.equals("status")) {
129                          tempDeparture.setStatus(builder.toString());                          tempDeparture.setStatus(builder.toString().trim());
130                  } else if (name.equals("note")) {                  } else if (name.equals("note")) {
131                          tempDeparture.setNote(builder.toString());                          tempDeparture.setNote(builder.toString().trim());
132                  }                  }
133          }          }
134  }  }

Legend:
Removed from v.238  
changed lines
  Added in v.391

  ViewVC Help
Powered by ViewVC 1.1.20