/[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 1160 by torben, Mon Oct 4 08:42:12 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo.provider;  package dk.thoerup.traininfo.provider;
2    
3  import java.io.StringReader;  
4  import java.util.ArrayList;  
5  import java.util.Collections;  import org.simpleframework.xml.Serializer;
6  import java.util.HashMap;  import org.simpleframework.xml.core.Persister;
7  import java.util.List;  
   
 import javax.xml.parsers.SAXParser;  
 import javax.xml.parsers.SAXParserFactory;  
   
 import org.xml.sax.Attributes;  
 import org.xml.sax.InputSource;  
 import org.xml.sax.SAXException;  
 import org.xml.sax.XMLReader;  
 import org.xml.sax.helpers.DefaultHandler;  
8    
9  import android.util.Log;  import android.util.Log;
10  import dk.thoerup.traininfo.DepartureBean;  import dk.thoerup.android.traininfo.common.DepartureBean;
11    import dk.thoerup.traininfo.util.AndroidTimeoutCache;
12  import dk.thoerup.traininfo.util.DownloadUtil;  import dk.thoerup.traininfo.util.DownloadUtil;
13    import dk.thoerup.traininfo.util.XmlUtil;
14    
15  public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {  public class XmlDepartureProvider implements DepartureProvider {
16    
17          final static long CACHE_TIMEOUT = 60*1000;          final static int CACHE_TIMEOUT = 60*1000;
18                    
19                    
20          class CacheEntry {          AndroidTimeoutCache<String,DepartureBean> departureCache = new AndroidTimeoutCache<String,DepartureBean>(CACHE_TIMEOUT);
                 public long timestamp;  
                 public List<DepartureBean> departures;  
         }  
21                    
         HashMap<Integer, CacheEntry> departureCache = new HashMap<Integer,CacheEntry>();          
         ArrayList<DepartureBean> departures;  
22                                    
23                    
         DepartureBean tempDeparture;  
         StringBuilder builder = new StringBuilder(512);  
           
24          @Override          @Override
25          public boolean lookupDepartures(int stationID) {          public DepartureBean lookupDepartures(int stationID, boolean arrival) {        
26                  CacheEntry entry = departureCache.get(stationID);                  
27                  boolean success;                  String key = "" + stationID + ":" + arrival;
28                    
29                    DepartureBean departures = departureCache.get(key);
30                    
31                  long now = android.os.SystemClock.elapsedRealtime();                  if (departures == null) {                      
32                  if (entry == null || (entry.timestamp+CACHE_TIMEOUT) < now) {                          departures = lookupDeparturesWorker(stationID, arrival);
33                                                    
34                          success = lookupDeparturesWorker(stationID);                          if (departures != null) {                      
35                                                            departureCache.put(key, departures);
                         if (success) {  
                                 entry = new CacheEntry();  
                                 entry.timestamp = android.os.SystemClock.elapsedRealtime();  
                                 entry.departures = departures;  
                           
                                 departureCache.put(stationID, entry);  
36                          }                          }
37                            
38                  } else {                  } else {
39                          Log.i("XmlDepartureProvider", "cache hit !!!");                          Log.i("XmlDepartureProvider", "cache hit !!!");
                         success = true;  
40                  }                                }              
41                                    
42                  return success;                  return departures;
43          }          }
44                    
45          private boolean lookupDeparturesWorker(int stationID) {          private DepartureBean lookupDeparturesWorker(int stationID, boolean arrival) {
46                  boolean success = false;  
                 departures = new ArrayList<DepartureBean>();  
47                  try                  try
48                  {                        {      
49                          //String url = "http://t-hoerup.dk/tog/xml_display.php?stationcode="+stationCode;                          int iArrival = arrival ? 1 : 0;
50                          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;
51                          Log.i("xmlurl",url);                          Log.i("xmlurl",url);
52                          String doc =  DownloadUtil.getContentString(url, 45000, "ISO-8859-1");                          String doc =  DownloadUtil.getContentString(url, 30000, "ISO-8859-1");
53    
54                            Serializer serializer = new Persister();
55    
56                            DepartureBean departures = serializer.read(DepartureBean.class, doc);
57                            
58                                                    
59                          InputSource source = new InputSource( new StringReader(doc));                          return departures;
60                                                    
                         SAXParserFactory spf = SAXParserFactory.newInstance();  
             SAXParser sp = spf.newSAXParser();  
             XMLReader xr = sp.getXMLReader();  
   
                         xr.setContentHandler(this);  
                         xr.setErrorHandler(this);  
                         xr.parse(source);  
                         success = true;  
61                                                    
62                  } catch (Exception e) {                  } catch (Exception e) {
63                          Log.e("XmlDepartureProvider", "looupFunction", e);                          Log.e("XmlDepartureProvider", "looupFunction", e);
64                  }                          return null;
65                  return success;                  }              
         }  
           
         @Override  
         public List<DepartureBean> getDepartures(int station) {  
                 CacheEntry entry = departureCache.get(station);  
                   
                 if (entry != null) {                      
                         return entry.departures;  
                 } else {  
                         return new ArrayList<DepartureBean>();  
                 }  
                   
         }  
           
         // this can be called several times fore the same text-node if there are many chardata / lines  
         @Override  
         public void characters (char ch[], int start, int length)  
         {                
                 for (int i= start; i<start+length; i++)  
                         builder.append(ch[i]);    
66          }          }
67            
68          @Override          @Override
69          public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException          public void purgeOldEntries() {
70          {                  departureCache.purgeOldEntries();              
                 if (name.equalsIgnoreCase("train"))  
                         tempDeparture = new DepartureBean();  
                   
                 builder.setLength(0); //reset StringBuilder  
71          }          }
72                    
         @Override  
         public void endElement (String uri, String name, String qName) throws SAXException  
         {  
                 if (name.equals("train")) {  
                         departures.add( tempDeparture );  
                 } else if (name.equals("time")) {  
                         tempDeparture.setTime(builder.toString().trim());  
                 } else if (name.equals("updated")) {  
                         tempDeparture.setLastUpdate(builder.toString().trim());  
                 } else if (name.equals("trainnumber")) {  
                         tempDeparture.setTrainNumber(builder.toString().trim());  
                 } else if (name.equals("destination")) {  
                         tempDeparture.setDestination(builder.toString().trim());  
                 } else if (name.equals("origin")) {  
                         tempDeparture.setOrigin(builder.toString().trim());  
                 } else if (name.equals("location")) {  
                         tempDeparture.setLocation(builder.toString().trim());  
                 } else if (name.equals("status")) {  
                         tempDeparture.setStatus(builder.toString().trim());  
                 } else if (name.equals("note")) {  
                         tempDeparture.setNote(builder.toString().trim());  
                 }  
         }  
73  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20