/[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 981 by torben, Sat Jul 10 16:03:10 2010 UTC revision 1066 by torben, Thu Sep 16 15:32:42 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo.provider;  package dk.thoerup.traininfo.provider;
2    
 import java.io.StringReader;  
3    
 import javax.xml.parsers.SAXParser;  
 import javax.xml.parsers.SAXParserFactory;  
4    
5  import org.xml.sax.Attributes;  import org.simpleframework.xml.Serializer;
6  import org.xml.sax.InputSource;  import org.simpleframework.xml.core.Persister;
7  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.DepartureEntry;  import dk.thoerup.android.traininfo.common.DepartureEntry;
12  import dk.thoerup.traininfo.util.AndroidTimeoutCache;  import dk.thoerup.traininfo.util.AndroidTimeoutCache;
13  import dk.thoerup.traininfo.util.DownloadUtil;  import dk.thoerup.traininfo.util.DownloadUtil;
14  import dk.thoerup.traininfo.util.XmlUtil;  import dk.thoerup.traininfo.util.XmlUtil;
15    
16  public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {  public class XmlDepartureProvider implements DepartureProvider {
17    
18          final static int CACHE_TIMEOUT = 60*1000;          final static int CACHE_TIMEOUT = 60*1000;
19                                    
20                    
21          AndroidTimeoutCache<String,DepartureBean> departureCache = new AndroidTimeoutCache<String,DepartureBean>(CACHE_TIMEOUT);          AndroidTimeoutCache<String,DepartureBean> departureCache = new AndroidTimeoutCache<String,DepartureBean>(CACHE_TIMEOUT);
22                    
         DepartureBean departures;  
23                                    
24                    
25          DepartureEntry tempDeparture;          DepartureEntry tempDeparture;
26          StringBuilder builder = new StringBuilder(512);          StringBuilder builder = new StringBuilder(512);
27                    
28    
29            
30          @Override          @Override
31          public boolean lookupDepartures(int stationID, boolean arrival) {                        public DepartureBean lookupDepartures(int stationID, boolean arrival) {        
                 boolean success;  
32                                    
33                  String key = "" + stationID + ":" + arrival;                  String key = "" + stationID + ":" + arrival;
34                                    
35                  departures = departureCache.get(key);                  DepartureBean departures = departureCache.get(key);
36                    
37                  if (departures == null) {                                        if (departures == null) {                      
38                          success = lookupDeparturesWorker(stationID, arrival);                          departures = lookupDeparturesWorker(stationID, arrival);
39                                                    
40                          if (success) {                                            if (departures != null) {                      
41                                  departureCache.put(key, departures);                                  departureCache.put(key, departures);
42                          }                          }
43                                                    
44                  } else {                  } else {
45                          Log.i("XmlDepartureProvider", "cache hit !!!");                          Log.i("XmlDepartureProvider", "cache hit !!!");
                         success = true;  
46                  }                                }              
47                                    
48                  return success;                  return departures;
49          }          }
50                    
51          private boolean lookupDeparturesWorker(int stationID, boolean arrival) {          private DepartureBean lookupDeparturesWorker(int stationID, boolean arrival) {
52                  boolean success = false;  
                 departures = new DepartureBean();  
53                  try                  try
54                  {                        {      
55                          int iArrival = arrival ? 1 : 0;                          int iArrival = arrival ? 1 : 0;
56                          String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID + "&arrival=" + iArrival;                          String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID + "&arrival=" + iArrival;
57                          Log.i("xmlurl",url);                          Log.i("xmlurl",url);
58                          String doc =  DownloadUtil.getContentString(url, 45000, "ISO-8859-1");                          String doc =  DownloadUtil.getContentString(url, 30000, "ISO-8859-1");
59    
60                            Serializer serializer = new Persister();
61    
62                            DepartureBean departures = serializer.read(DepartureBean.class, doc);
63                                                    
                         InputSource source = new InputSource( new StringReader(doc));  
64                                                    
65                          SAXParserFactory spf = SAXParserFactory.newInstance();                          return departures;
66              SAXParser sp = spf.newSAXParser();                          
             XMLReader xr = sp.getXMLReader();  
   
                         xr.setContentHandler(this);  
                         xr.setErrorHandler(this);  
                         xr.parse(source);  
                         success = true;  
67                                                    
68                  } catch (Exception e) {                  } catch (Exception e) {
69                          Log.e("XmlDepartureProvider", "looupFunction", e);                          Log.e("XmlDepartureProvider", "looupFunction", e);
70                  }                          return null;
71                  return success;                  }              
         }  
           
         @Override  
         public DepartureBean getDepartures(int station,boolean arrival) {  
                   
                 String key = "" + station + ":" + arrival;  
                   
                 DepartureBean bean = departureCache.get(key);  
                   
                 if (bean == null) {                      
                         bean = new DepartureBean();  
                 }  
                   
                 return bean;  
         }  
           
         // 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]);    
         }  
           
         @Override  
         public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException  
         {  
                 if (name.equalsIgnoreCase("train"))  
                         tempDeparture = new DepartureEntry();  
                   
                 builder.setLength(0); //reset StringBuilder  
72          }          }
73                    
         @Override  
         public void endElement (String uri, String name, String qName) throws SAXException  
         {  
                 if (name.equals("train")) {                      
                         departures.entries.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());  
                 } else if (name.equals("type")) {  
                         tempDeparture.setType(builder.toString().trim());  
                 } else if (name.equals("notification")) {  
                         departures.notifications.add( builder.toString().trim() );  
                 }  
         }  
74  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20