/[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 320 by torben, Sat Sep 12 12:33:24 2009 UTC revision 981 by torben, Sat Jul 10 16:03:10 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo.provider;  package dk.thoerup.traininfo.provider;
2    
3  import java.io.StringReader;  import java.io.StringReader;
 import java.util.ArrayList;  
 import java.util.Collections;  
 import java.util.HashMap;  
 import java.util.List;  
4    
5  import javax.xml.parsers.SAXParser;  import javax.xml.parsers.SAXParser;
6  import javax.xml.parsers.SAXParserFactory;  import javax.xml.parsers.SAXParserFactory;
# Line 17  import org.xml.sax.helpers.DefaultHandle Line 13  import org.xml.sax.helpers.DefaultHandle
13    
14  import android.util.Log;  import android.util.Log;
15  import dk.thoerup.traininfo.DepartureBean;  import dk.thoerup.traininfo.DepartureBean;
16    import dk.thoerup.traininfo.DepartureEntry;
17    import dk.thoerup.traininfo.util.AndroidTimeoutCache;
18  import dk.thoerup.traininfo.util.DownloadUtil;  import dk.thoerup.traininfo.util.DownloadUtil;
19    import dk.thoerup.traininfo.util.XmlUtil;
20    
21  public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {  public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {
22    
23          final static long CACHE_TIMEOUT = 60*1000;          final static int CACHE_TIMEOUT = 60*1000;
24                    
25                    
26          class CacheEntry {          AndroidTimeoutCache<String,DepartureBean> departureCache = new AndroidTimeoutCache<String,DepartureBean>(CACHE_TIMEOUT);
                 public long timestamp;  
                 public List<DepartureBean> departures;  
         }  
27                    
28          HashMap<Integer, CacheEntry> departureCache = new HashMap<Integer,CacheEntry>();                  DepartureBean departures;
         ArrayList<DepartureBean> departures;  
29                                    
30                    
31          DepartureBean tempDeparture;          DepartureEntry tempDeparture;
32          StringBuilder builder = new StringBuilder(512);          StringBuilder builder = new StringBuilder(512);
33                    
34          @Override          @Override
35          public boolean lookupDepartures(int stationID) {          public boolean lookupDepartures(int stationID, boolean arrival) {              
                 CacheEntry entry = departureCache.get(stationID);  
36                  boolean success;                  boolean success;
37                    
38                    String key = "" + stationID + ":" + arrival;
39                    
40                    departures = departureCache.get(key);
41                    
42                  long now = android.os.SystemClock.elapsedRealtime();                  if (departures == null) {                      
43                  if (entry == null || (entry.timestamp+CACHE_TIMEOUT) < now) {                          success = lookupDeparturesWorker(stationID, arrival);
44                                                    
45                          success = lookupDeparturesWorker(stationID);                          if (success) {                  
46                                                            departureCache.put(key, departures);
                         if (success) {  
                                 entry = new CacheEntry();  
                                 entry.timestamp = android.os.SystemClock.elapsedRealtime();  
                                 entry.departures = departures;  
                           
                                 departureCache.put(stationID, entry);  
47                          }                          }
48                            
49                  } else {                  } else {
50                          Log.i("XmlDepartureProvider", "cache hit !!!");                          Log.i("XmlDepartureProvider", "cache hit !!!");
51                          success = true;                          success = true;
# Line 60  public class XmlDepartureProvider extend Line 54  public class XmlDepartureProvider extend
54                  return success;                  return success;
55          }          }
56                    
57          private boolean lookupDeparturesWorker(int stationID) {          private boolean lookupDeparturesWorker(int stationID, boolean arrival) {
58                  boolean success = false;                  boolean success = false;
59                  departures = new ArrayList<DepartureBean>();                  departures = new DepartureBean();
60                  try                  try
61                  {                        {      
62                          //String url = "http://t-hoerup.dk/tog/xml_display.php?stationcode="+stationCode;                          int iArrival = arrival ? 1 : 0;
63                          String url = "http://app.t-hoerup.dk/TrainInfoService/DepartureServlet?format=xml&station=" + stationID;                          String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID + "&arrival=" + iArrival;
64                          Log.i("xmlurl",url);                          Log.i("xmlurl",url);
65                          String doc =  DownloadUtil.getContentString(url, 45000, "ISO-8859-1");                          String doc =  DownloadUtil.getContentString(url, 45000, "ISO-8859-1");
66                                                    
# Line 88  public class XmlDepartureProvider extend Line 82  public class XmlDepartureProvider extend
82          }          }
83                    
84          @Override          @Override
85          public List<DepartureBean> getDepartures(int station) {          public DepartureBean getDepartures(int station,boolean arrival) {
                 CacheEntry entry = departureCache.get(station);  
86                                    
87                  if (entry != null) {                                      String key = "" + station + ":" + arrival;
                         return entry.departures;  
                 } else {  
                         return new ArrayList<DepartureBean>();  
                 }  
88                                    
89                    DepartureBean bean = departureCache.get(key);
90                    
91                    if (bean == null) {                    
92                            bean = new DepartureBean();
93                    }
94                    
95                    return bean;
96          }          }
97                    
98          // 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 111  public class XmlDepartureProvider extend Line 107  public class XmlDepartureProvider extend
107          public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException          public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException
108          {          {
109                  if (name.equalsIgnoreCase("train"))                  if (name.equalsIgnoreCase("train"))
110                          tempDeparture = new DepartureBean();                          tempDeparture = new DepartureEntry();
111                                    
112                  builder.setLength(0); //reset StringBuilder                  builder.setLength(0); //reset StringBuilder
113          }          }
# Line 119  public class XmlDepartureProvider extend Line 115  public class XmlDepartureProvider extend
115          @Override          @Override
116          public void endElement (String uri, String name, String qName) throws SAXException          public void endElement (String uri, String name, String qName) throws SAXException
117          {          {
118                  if (name.equals("train")) {                  if (name.equals("train")) {                    
119                          departures.add( tempDeparture );                          departures.entries.add( tempDeparture );
120                  } else if (name.equals("time")) {                  } else if (name.equals("time")) {
121                          tempDeparture.setTime(builder.toString().trim());                          tempDeparture.setTime(builder.toString().trim());
122                  } else if (name.equals("updated")) {                  } else if (name.equals("updated")) {
# Line 137  public class XmlDepartureProvider extend Line 133  public class XmlDepartureProvider extend
133                          tempDeparture.setStatus(builder.toString().trim());                          tempDeparture.setStatus(builder.toString().trim());
134                  } else if (name.equals("note")) {                  } else if (name.equals("note")) {
135                          tempDeparture.setNote(builder.toString().trim());                          tempDeparture.setNote(builder.toString().trim());
136                  }                  } else if (name.equals("type")) {
137                            tempDeparture.setType(builder.toString().trim());
138                    } else if (name.equals("notification")) {
139                            departures.notifications.add( builder.toString().trim() );
140                    }
141          }          }
142  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20