/[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 358 by torben, Tue Sep 29 19:08:39 2009 UTC revision 1024 by torben, Tue Aug 31 05:44:45 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;  import dk.thoerup.traininfo.util.XmlUtil;
20    
21  public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {  public class XmlDepartureProvider 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                    
         HashMap<Integer, CacheEntry> departureCache = new HashMap<Integer,CacheEntry>();          
         ArrayList<DepartureBean> departures;  
28                                    
29                    
30          DepartureBean tempDeparture;          DepartureEntry tempDeparture;
31          StringBuilder builder = new StringBuilder(512);          StringBuilder builder = new StringBuilder(512);
32                    
33    
34            
35          @Override          @Override
36          public boolean lookupDepartures(int stationID) {          public DepartureBean lookupDepartures(int stationID, boolean arrival) {        
37                  CacheEntry entry = departureCache.get(stationID);                  
38                  boolean success;                  String key = "" + stationID + ":" + arrival;
39                    
40                    DepartureBean departures = departureCache.get(key);
41                    
42                  long now = android.os.SystemClock.elapsedRealtime();                  if (departures == null) {                      
43                  if (entry == null || (entry.timestamp+CACHE_TIMEOUT) < now) {                          departures = lookupDeparturesWorker(stationID, arrival);
                           
                         success = lookupDeparturesWorker(stationID);  
44                                                    
45                          if (success) {                          if (departures != null) {                      
46                                  entry = new CacheEntry();                                  departureCache.put(key, departures);
                                 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 !!!");
                         success = true;  
51                  }                                }              
52                                    
53                  return success;                  return departures;
54          }          }
55                    
56          private boolean lookupDeparturesWorker(int stationID) {          private DepartureBean lookupDeparturesWorker(int stationID, boolean arrival) {
57                  boolean success = false;  
                 departures = new ArrayList<DepartureBean>();  
58                  try                  try
59                  {                        {      
60                            int iArrival = arrival ? 1 : 0;
61                          String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID;                          String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID + "&arrival=" + iArrival;
62                          Log.i("xmlurl",url);                          Log.i("xmlurl",url);
63                          String doc =  DownloadUtil.getContentString(url, 45000, "ISO-8859-1");                          String doc =  DownloadUtil.getContentString(url, 45000, "ISO-8859-1");
64                                                    
# Line 77  public class XmlDepartureProvider extend Line 68  public class XmlDepartureProvider extend
68              SAXParser sp = spf.newSAXParser();              SAXParser sp = spf.newSAXParser();
69              XMLReader xr = sp.getXMLReader();              XMLReader xr = sp.getXMLReader();
70    
71                          xr.setContentHandler(this);              DepartureParser departureParser = new DepartureParser();
72                          xr.setErrorHandler(this);                          xr.setContentHandler(departureParser);
73                            xr.setErrorHandler(departureParser);
74                          xr.parse(source);                          xr.parse(source);
75                          success = true;                          
76                            return departureParser.getDepartures();
77                                                    
78                  } catch (Exception e) {                  } catch (Exception e) {
79                          Log.e("XmlDepartureProvider", "looupFunction", e);                          Log.e("XmlDepartureProvider", "looupFunction", e);
80                  }                          return null;
81                  return success;                  }              
82          }          }
83                    
84          @Override  
85          public List<DepartureBean> getDepartures(int station) {          class DepartureParser extends DefaultHandler {
                 CacheEntry entry = departureCache.get(station);  
86                                    
87                  if (entry != null) {                                      private DepartureBean departures = new DepartureBean();
88                          return entry.departures;  
89                  } else {                  public DepartureBean getDepartures() {
90                          return new ArrayList<DepartureBean>();                          return departures;
91                  }                  }
                   
         }  
           
         // 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]);    
         }  
92                    
93          @Override                  // this can be called several times fore the same text-node if there are many chardata / lines
94          public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException                  @Override
95          {                  public void characters (char ch[], int start, int length)
96                  if (name.equalsIgnoreCase("train"))                  {              
97                          tempDeparture = new DepartureBean();                          builder.append(ch, start, length);
98                    }
99                                    
100                  builder.setLength(0); //reset StringBuilder                  @Override
101          }                  public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException
102                            {
103          @Override                          if (name.equalsIgnoreCase("train"))
104          public void endElement (String uri, String name, String qName) throws SAXException                                  tempDeparture = new DepartureEntry();
105          {                          
106                  if (name.equals("train")) {                          builder.setLength(0); //reset StringBuilder
107                          departures.add( tempDeparture );                  }
108                  } else if (name.equals("time")) {                  
109                          tempDeparture.setTime(builder.toString().trim());                  @Override
110                  } else if (name.equals("updated")) {                  public void endElement (String uri, String name, String qName) throws SAXException
111                          tempDeparture.setLastUpdate(builder.toString().trim());                  {
112                  } else if (name.equals("trainnumber")) {                          if (name.equals("train")) {                    
113                          tempDeparture.setTrainNumber(builder.toString().trim());                                  departures.entries.add( tempDeparture );
114                  } else if (name.equals("destination")) {                          } else if (name.equals("time")) {
115                          tempDeparture.setDestination(builder.toString().trim());                                  tempDeparture.setTime(builder.toString().trim());
116                  } else if (name.equals("origin")) {                          } else if (name.equals("updated")) {
117                          tempDeparture.setOrigin(builder.toString().trim());                                  tempDeparture.setLastUpdate(builder.toString().trim());
118                  } else if (name.equals("location")) {                          } else if (name.equals("trainnumber")) {
119                          tempDeparture.setLocation(builder.toString().trim());                                  tempDeparture.setTrainNumber(builder.toString().trim());
120                  } else if (name.equals("status")) {                          } else if (name.equals("destination")) {
121                          tempDeparture.setStatus(builder.toString().trim());                                  tempDeparture.setDestination(builder.toString().trim());
122                  } else if (name.equals("note")) {                          } else if (name.equals("origin")) {
123                          tempDeparture.setNote(builder.toString().trim());                                  tempDeparture.setOrigin(builder.toString().trim());
124                  }                          } else if (name.equals("location")) {
125                                    tempDeparture.setLocation(builder.toString().trim());
126                            } else if (name.equals("status")) {
127                                    tempDeparture.setStatus(builder.toString().trim());
128                            } else if (name.equals("note")) {
129                                    tempDeparture.setNote(builder.toString().trim());
130                            } else if (name.equals("type")) {
131                                    tempDeparture.setType(builder.toString().trim());
132                            } else if (name.equals("notification")) {
133                                    departures.notifications.add( builder.toString().trim() );
134                            }
135                    }
136          }          }
137  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20