/[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 835 by torben, Fri Jun 11 14:05:24 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo.provider;  package dk.thoerup.traininfo.provider;
2    
 import java.io.BufferedReader;  
 import java.io.IOException;  
 import java.io.InputStream;  
 import java.io.InputStreamReader;  
3  import java.io.StringReader;  import java.io.StringReader;
 import java.net.URL;  
 import java.net.URLConnection;  
4  import java.util.ArrayList;  import java.util.ArrayList;
5  import java.util.List;  import java.util.List;
6    
# Line 21  import org.xml.sax.helpers.DefaultHandle Line 15  import org.xml.sax.helpers.DefaultHandle
15    
16  import android.util.Log;  import android.util.Log;
17  import dk.thoerup.traininfo.DepartureBean;  import dk.thoerup.traininfo.DepartureBean;
18    import dk.thoerup.traininfo.util.AndroidTimeoutCache;
19    import dk.thoerup.traininfo.util.DownloadUtil;
20    import dk.thoerup.traininfo.util.XmlUtil;
21    
22  public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {  public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {
23    
24          ArrayList<DepartureBean> departures = new ArrayList<DepartureBean>();          final static int CACHE_TIMEOUT = 60*1000;
25                    
26            
27            AndroidTimeoutCache<String,List<DepartureBean>> departureCache = new AndroidTimeoutCache<String,List<DepartureBean>>(CACHE_TIMEOUT);
28                    
29            List<DepartureBean> departures;
30                    
31                    
32          DepartureBean tempDeparture;          DepartureBean tempDeparture;
33          StringBuilder builder = new StringBuilder(512);          StringBuilder builder = new StringBuilder(512);
34                    
35          @Override          @Override
36          public void lookupDepartures(String station) {          public boolean lookupDepartures(int stationID, boolean arrival) {              
37                  departures.clear();                  boolean success;
38                    
39                    String key = "" + stationID + ":" + arrival;
40                    
41                    departures = departureCache.get(key);
42            
43                    if (departures == null) {                      
44                            success = lookupDeparturesWorker(stationID, arrival);
45                            
46                            if (success) {                  
47                                    departureCache.put(key, departures);
48                            }
49                            
50                    } else {
51                            Log.i("XmlDepartureProvider", "cache hit !!!");
52                            success = true;
53                    }              
54                    
55                    return success;
56            }
57            
58            private boolean lookupDeparturesWorker(int stationID, boolean arrival) {
59                    boolean success = false;
60                    departures = new ArrayList<DepartureBean>();
61                  try                  try
62                  {                        {      
63                          String doc = getUrlContents("http://t-hoerup.dk/tog/xml_display.php?stationname="+station);                          int iArrival = arrival ? 1 : 0;
64                            String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID + "&arrival=" + iArrival;
65                            Log.i("xmlurl",url);
66                            String doc =  DownloadUtil.getContentString(url, 45000, "ISO-8859-1");
67                                                    
68                          InputSource source = new InputSource( new StringReader(doc));                          InputSource source = new InputSource( new StringReader(doc));
69                                                    
# Line 45  public class XmlDepartureProvider extend Line 73  public class XmlDepartureProvider extend
73    
74                          xr.setContentHandler(this);                          xr.setContentHandler(this);
75                          xr.setErrorHandler(this);                          xr.setErrorHandler(this);
                         xr.setDTDHandler(this);  
76                          xr.parse(source);                          xr.parse(source);
77                            success = true;
78                            
79                  } catch (Exception e) {                  } catch (Exception e) {
80                          Log.e("XmlDepartureProvider", "looupFunction", e);                          Log.e("XmlDepartureProvider", "looupFunction", e);
81                  }                  }
82                    return success;
83          }          }
84                    
85          @Override          @Override
86          public List<DepartureBean> getDepartures() {          public List<DepartureBean> getDepartures(int station,boolean arrival) {
87                  return departures;                  
88          }                  String key = "" + station + ":" + arrival;
89                    
90          private String getUrlContents(String uri) throws IOException                  List<DepartureBean> list = departureCache.get(key);
91          {                  
92                  URL url = new URL(uri);                          if (list == null) {                    
93                  URLConnection conn = url.openConnection();                          list = new ArrayList<DepartureBean>();
94                  conn.setConnectTimeout(5000);                  }
95                  InputStream stream = conn.getInputStream();                  
96                    return list;
                 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");  
                 }  
   
                 return sbuilder.toString();  
97          }          }
98                    
99          // 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 100  public class XmlDepartureProvider extend Line 119  public class XmlDepartureProvider extend
119                  if (name.equals("train")) {                  if (name.equals("train")) {
120                          departures.add( tempDeparture );                          departures.add( tempDeparture );
121                  } else if (name.equals("time")) {                  } else if (name.equals("time")) {
122                          tempDeparture.setTime(builder.toString());                          tempDeparture.setTime(builder.toString().trim());
123                  } else if (name.equals("updated")) {                  } else if (name.equals("updated")) {
124                          tempDeparture.setLastUpdate(builder.toString());                          tempDeparture.setLastUpdate(builder.toString().trim());
125                  } else if (name.equals("trainnumber")) {                  } else if (name.equals("trainnumber")) {
126                          tempDeparture.setTrainNumber(builder.toString());                          tempDeparture.setTrainNumber(builder.toString().trim());
127                  } else if (name.equals("destination")) {                  } else if (name.equals("destination")) {
128                          tempDeparture.setDestination(builder.toString());                          tempDeparture.setDestination(builder.toString().trim());
129                  } else if (name.equals("origin")) {                  } else if (name.equals("origin")) {
130                          tempDeparture.setOrigin(builder.toString());                          tempDeparture.setOrigin(builder.toString().trim());
131                  } else if (name.equals("location")) {                  } else if (name.equals("location")) {
132                          tempDeparture.setLocation(builder.toString());                          tempDeparture.setLocation(builder.toString().trim());
133                  } else if (name.equals("status")) {                  } else if (name.equals("status")) {
134                          tempDeparture.setStatus(builder.toString());                          tempDeparture.setStatus(builder.toString().trim());
135                  } else if (name.equals("note")) {                  } else if (name.equals("note")) {
136                          tempDeparture.setNote(builder.toString());                          tempDeparture.setNote(builder.toString().trim());
137                  }                  } else if (name.equals("type")) {
138                            tempDeparture.setType(builder.toString().trim());
139                    }
140          }          }
141  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20