/[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 237 by torben, Sat Aug 8 19:02:20 2009 UTC revision 1079 by torben, Mon Sep 20 06:41:59 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo.provider;  package dk.thoerup.traininfo.provider;
2    
3  import java.io.BufferedReader;  
4  import java.io.IOException;  
5  import java.io.InputStream;  import org.simpleframework.xml.Serializer;
6  import java.io.InputStreamReader;  import org.simpleframework.xml.core.Persister;
7  import java.io.StringReader;  
 import java.net.URL;  
 import java.net.URLConnection;  
 import java.util.ArrayList;  
 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;
13    import dk.thoerup.traininfo.util.XmlUtil;
14    
15  public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {  public class XmlDepartureProvider implements DepartureProvider {
16    
17          ArrayList<DepartureBean> departures = new ArrayList<DepartureBean>();          final static int CACHE_TIMEOUT = 60*1000;
18                    
19                    
20            AndroidTimeoutCache<String,DepartureBean> departureCache = new AndroidTimeoutCache<String,DepartureBean>(CACHE_TIMEOUT);
21                    
22          DepartureBean tempDeparture;                  
         StringBuilder builder = new StringBuilder(512);  
23                    
24          @Override          @Override
25          public void lookupDepartures(String station) {          public DepartureBean lookupDepartures(int stationID, boolean arrival) {        
26                  departures.clear();                  
27                  try                  String key = "" + stationID + ":" + arrival;
28                  {                        
29                          String doc = getUrlContents("http://t-hoerup.dk/tog/xml_display.php?stationname="+station);                  DepartureBean departures = departureCache.get(key);
30            
31                    if (departures == null) {                      
32                            departures = lookupDeparturesWorker(stationID, arrival);
33                                                    
34                          InputSource source = new InputSource( new StringReader(doc));                          if (departures != null) {                      
35                                    departureCache.put(key, departures);
36                            }
37                                                    
38                          SAXParserFactory spf = SAXParserFactory.newInstance();                  } else {
39              SAXParser sp = spf.newSAXParser();                          Log.i("XmlDepartureProvider", "cache hit !!!");
40              XMLReader xr = sp.getXMLReader();                  }              
41                    
                         xr.setContentHandler(this);  
                         xr.setErrorHandler(this);  
                         xr.setDTDHandler(this);  
                         xr.parse(source);  
                 } catch (Exception e) {  
                         Log.e("XmlDepartureProvider", "looupFunction", e);  
                 }  
         }  
           
         @Override  
         public List<DepartureBean> getDepartures() {  
42                  return departures;                  return departures;
43          }          }
44            
45            private DepartureBean lookupDeparturesWorker(int stationID, boolean arrival) {
46    
47          private String getUrlContents(String uri) throws IOException                  try
48          {                  {      
49                  URL url = new URL(uri);                                  int iArrival = arrival ? 1 : 0;
50                  URLConnection conn = url.openConnection();                          String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID + "&arrival=" + iArrival;
51                  conn.setConnectTimeout(5000);                          Log.i("xmlurl",url);
52                  InputStream stream = conn.getInputStream();                          String doc =  DownloadUtil.getContentString(url, 30000, "ISO-8859-1");
   
                 BufferedReader in = new BufferedReader(new InputStreamReader(stream, "ISO-8859-1"),8192);  
   
                 StringBuilder sbuilder = new StringBuilder();  
   
                 String line;  
                 while ( (line = in.readLine()) != null) {  
                         sbuilder.append(line);  
                         sbuilder.append("\r\n");  
                 }  
53    
54                  return sbuilder.toString();                          Serializer serializer = new Persister();
55          }  
56                                    DepartureBean departures = serializer.read(DepartureBean.class, doc);
57          // this can be called several times fore the same text-node if there are many chardata / lines                          
58          @Override                          
59          public void characters (char ch[], int start, int length)                          return departures;
60          {                                        
61                  for (int i= start; i<start+length; i++)                          
62                          builder.append(ch[i]);                    } catch (Exception e) {
63          }                          Log.e("XmlDepartureProvider", "looupFunction", e);
64                                    return null;
65          @Override                  }              
         public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException  
         {  
                 if (name.equalsIgnoreCase("train"))  
                         tempDeparture = new DepartureBean();  
                   
                 builder.setLength(0); //reset StringBuilder  
66          }          }
67                    
         @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());  
                 } else if (name.equals("updated")) {  
                         tempDeparture.setLastUpdate(builder.toString());  
                 } else if (name.equals("trainnumber")) {  
                         tempDeparture.setTrainNumber(builder.toString());  
                 } else if (name.equals("destination")) {  
                         tempDeparture.setDestination(builder.toString());  
                 } else if (name.equals("origin")) {  
                         tempDeparture.setOrigin(builder.toString());  
                 } else if (name.equals("location")) {  
                         tempDeparture.setLocation(builder.toString());  
                 } else if (name.equals("status")) {  
                         tempDeparture.setStatus(builder.toString());  
                 } else if (name.equals("note")) {  
                         tempDeparture.setNote(builder.toString());  
                 }  
         }  
68  }  }

Legend:
Removed from v.237  
changed lines
  Added in v.1079

  ViewVC Help
Powered by ViewVC 1.1.20