/[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 358 by torben, Tue Sep 29 19:08:39 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 16  import org.xml.sax.helpers.DefaultHandle Line 18  import org.xml.sax.helpers.DefaultHandle
18  import android.util.Log;  import android.util.Log;
19  import dk.thoerup.traininfo.DepartureBean;  import dk.thoerup.traininfo.DepartureBean;
20  import dk.thoerup.traininfo.util.DownloadUtil;  import dk.thoerup.traininfo.util.DownloadUtil;
21    import dk.thoerup.traininfo.util.XmlUtil;
22    
23  public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {  public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {
24    
25          ArrayList<DepartureBean> departures = new ArrayList<DepartureBean>();          final static long CACHE_TIMEOUT = 60*1000;
26                    
27            class CacheEntry {
28                    public long timestamp;
29                    public List<DepartureBean> departures;
30            }
31            
32            HashMap<Integer, CacheEntry> departureCache = new HashMap<Integer,CacheEntry>();        
33            ArrayList<DepartureBean> departures;
34                    
35                    
36          DepartureBean tempDeparture;          DepartureBean tempDeparture;
37          StringBuilder builder = new StringBuilder(512);          StringBuilder builder = new StringBuilder(512);
38                    
39          @Override          @Override
40          public void lookupDepartures(String station) {          public boolean lookupDepartures(int stationID) {
41                  departures.clear();                  CacheEntry entry = departureCache.get(stationID);
42                    boolean success;
43            
44                    long now = android.os.SystemClock.elapsedRealtime();
45                    if (entry == null || (entry.timestamp+CACHE_TIMEOUT) < now) {
46                            
47                            success = lookupDeparturesWorker(stationID);
48                            
49                            if (success) {
50                                    entry = new CacheEntry();
51                                    entry.timestamp = android.os.SystemClock.elapsedRealtime();
52                                    entry.departures = departures;
53                            
54                                    departureCache.put(stationID, entry);
55                            }
56                    } else {
57                            Log.i("XmlDepartureProvider", "cache hit !!!");
58                            success = true;
59                    }              
60                    
61                    return success;
62            }
63            
64            private boolean lookupDeparturesWorker(int stationID) {
65                    boolean success = false;
66                    departures = new ArrayList<DepartureBean>();
67                  try                  try
68                  {       String url = "http://t-hoerup.dk/tog/xml_display.php?stationname="+station;                  {      
69                          String doc =  DownloadUtil.getContent(url, 30000, "ISO-8859-1");  
70                            String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID;
71                            Log.i("xmlurl",url);
72                            String doc =  DownloadUtil.getContentString(url, 45000, "ISO-8859-1");
73                                                    
74                          InputSource source = new InputSource( new StringReader(doc));                          InputSource source = new InputSource( new StringReader(doc));
75                                                    
# Line 40  public class XmlDepartureProvider extend Line 79  public class XmlDepartureProvider extend
79    
80                          xr.setContentHandler(this);                          xr.setContentHandler(this);
81                          xr.setErrorHandler(this);                          xr.setErrorHandler(this);
                         xr.setDTDHandler(this);  
82                          xr.parse(source);                          xr.parse(source);
83                            success = true;
84                            
85                  } catch (Exception e) {                  } catch (Exception e) {
86                          Log.e("XmlDepartureProvider", "looupFunction", e);                          Log.e("XmlDepartureProvider", "looupFunction", e);
87                  }                  }
88                    return success;
89          }          }
90                    
91          @Override          @Override
92          public List<DepartureBean> getDepartures() {          public List<DepartureBean> getDepartures(int station) {
93                  return departures;                  CacheEntry entry = departureCache.get(station);
94                    
95                    if (entry != null) {                    
96                            return entry.departures;
97                    } else {
98                            return new ArrayList<DepartureBean>();
99                    }
100                    
101          }          }
102                    
103          // 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 123  public class XmlDepartureProvider extend
123                  if (name.equals("train")) {                  if (name.equals("train")) {
124                          departures.add( tempDeparture );                          departures.add( tempDeparture );
125                  } else if (name.equals("time")) {                  } else if (name.equals("time")) {
126                          tempDeparture.setTime(builder.toString());                          tempDeparture.setTime(builder.toString().trim());
127                  } else if (name.equals("updated")) {                  } else if (name.equals("updated")) {
128                          tempDeparture.setLastUpdate(builder.toString());                          tempDeparture.setLastUpdate(builder.toString().trim());
129                  } else if (name.equals("trainnumber")) {                  } else if (name.equals("trainnumber")) {
130                          tempDeparture.setTrainNumber(builder.toString());                          tempDeparture.setTrainNumber(builder.toString().trim());
131                  } else if (name.equals("destination")) {                  } else if (name.equals("destination")) {
132                          tempDeparture.setDestination(builder.toString());                          tempDeparture.setDestination(builder.toString().trim());
133                  } else if (name.equals("origin")) {                  } else if (name.equals("origin")) {
134                          tempDeparture.setOrigin(builder.toString());                          tempDeparture.setOrigin(builder.toString().trim());
135                  } else if (name.equals("location")) {                  } else if (name.equals("location")) {
136                          tempDeparture.setLocation(builder.toString());                          tempDeparture.setLocation(builder.toString().trim());
137                  } else if (name.equals("status")) {                  } else if (name.equals("status")) {
138                          tempDeparture.setStatus(builder.toString());                          tempDeparture.setStatus(builder.toString().trim());
139                  } else if (name.equals("note")) {                  } else if (name.equals("note")) {
140                          tempDeparture.setNote(builder.toString());                          tempDeparture.setNote(builder.toString().trim());
141                  }                  }
142          }          }
143  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20