/[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 246 by torben, Sun Aug 9 19:49:29 2009 UTC revision 320 by torben, Sat Sep 12 12:33:24 2009 UTC
# Line 2  package dk.thoerup.traininfo.provider; Line 2  package dk.thoerup.traininfo.provider;
2    
3  import java.io.StringReader;  import java.io.StringReader;
4  import java.util.ArrayList;  import java.util.ArrayList;
5    import java.util.Collections;
6    import java.util.HashMap;
7  import java.util.List;  import java.util.List;
8    
9  import javax.xml.parsers.SAXParser;  import javax.xml.parsers.SAXParser;
# Line 19  import dk.thoerup.traininfo.util.Downloa Line 21  import dk.thoerup.traininfo.util.Downloa
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 long CACHE_TIMEOUT = 60*1000;
25                    
26            class CacheEntry {
27                    public long timestamp;
28                    public List<DepartureBean> departures;
29            }
30            
31            HashMap<Integer, CacheEntry> departureCache = new HashMap<Integer,CacheEntry>();        
32            ArrayList<DepartureBean> departures;
33                    
34                    
35          DepartureBean tempDeparture;          DepartureBean tempDeparture;
36          StringBuilder builder = new StringBuilder(512);          StringBuilder builder = new StringBuilder(512);
37                    
38          @Override          @Override
39          public void lookupDepartures(String station) {          public boolean lookupDepartures(int stationID) {
40                  departures.clear();                  CacheEntry entry = departureCache.get(stationID);
41                    boolean success;
42            
43                    long now = android.os.SystemClock.elapsedRealtime();
44                    if (entry == null || (entry.timestamp+CACHE_TIMEOUT) < now) {
45                            
46                            success = lookupDeparturesWorker(stationID);
47                            
48                            if (success) {
49                                    entry = new CacheEntry();
50                                    entry.timestamp = android.os.SystemClock.elapsedRealtime();
51                                    entry.departures = departures;
52                            
53                                    departureCache.put(stationID, entry);
54                            }
55                    } else {
56                            Log.i("XmlDepartureProvider", "cache hit !!!");
57                            success = true;
58                    }              
59                    
60                    return success;
61            }
62            
63            private boolean lookupDeparturesWorker(int stationID) {
64                    boolean success = false;
65                    departures = new ArrayList<DepartureBean>();
66                  try                  try
67                  {       String url = "http://t-hoerup.dk/tog/xml_display.php?stationname="+station;                  {      
68                          String doc =  DownloadUtil.getContent(url, 45000, "ISO-8859-1");                          //String url = "http://t-hoerup.dk/tog/xml_display.php?stationcode="+stationCode;
69                            String url = "http://app.t-hoerup.dk/TrainInfoService/DepartureServlet?format=xml&station=" + stationID;
70                            Log.i("xmlurl",url);
71                            String doc =  DownloadUtil.getContentString(url, 45000, "ISO-8859-1");
72                                                    
73                          InputSource source = new InputSource( new StringReader(doc));                          InputSource source = new InputSource( new StringReader(doc));
74                                                    
# Line 40  public class XmlDepartureProvider extend Line 78  public class XmlDepartureProvider extend
78    
79                          xr.setContentHandler(this);                          xr.setContentHandler(this);
80                          xr.setErrorHandler(this);                          xr.setErrorHandler(this);
                         xr.setDTDHandler(this);  
81                          xr.parse(source);                          xr.parse(source);
82                            success = true;
83                            
84                  } catch (Exception e) {                  } catch (Exception e) {
85                          Log.e("XmlDepartureProvider", "looupFunction", e);                          Log.e("XmlDepartureProvider", "looupFunction", e);
86                  }                  }
87                    return success;
88          }          }
89                    
90          @Override          @Override
91          public List<DepartureBean> getDepartures() {          public List<DepartureBean> getDepartures(int station) {
92                  return departures;                  CacheEntry entry = departureCache.get(station);
93                    
94                    if (entry != null) {                    
95                            return entry.departures;
96                    } else {
97                            return new ArrayList<DepartureBean>();
98                    }
99                    
100          }          }
101                    
102          // 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

Legend:
Removed from v.246  
changed lines
  Added in v.320

  ViewVC Help
Powered by ViewVC 1.1.20