/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.java
ViewVC logotype

Diff of /android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 319 by torben, Fri Sep 11 12:24:53 2009 UTC revision 1006 by torben, Mon Aug 2 23:18:53 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo.provider;  package dk.thoerup.traininfo.provider;
2    
3  import java.io.ByteArrayInputStream;  
4  import java.io.IOException;  import java.net.URLEncoder;
5  import java.util.ArrayList;  import java.util.ArrayList;
6  import java.util.List;  import java.util.List;
7    
 import javax.xml.parsers.DocumentBuilder;  
 import javax.xml.parsers.DocumentBuilderFactory;  
 import javax.xml.parsers.ParserConfigurationException;  
   
8  import org.w3c.dom.Document;  import org.w3c.dom.Document;
9  import org.w3c.dom.Node;  import org.w3c.dom.Node;
10  import org.w3c.dom.NodeList;  import org.w3c.dom.NodeList;
 import org.xml.sax.SAXException;  
11    
12  import android.location.Location;  import android.location.Location;
13  import android.util.Log;  import android.util.Log;
14  import dk.thoerup.traininfo.StationBean;  import dk.thoerup.traininfo.StationBean;
15    import dk.thoerup.traininfo.util.AndroidTimeoutCache;
16  import dk.thoerup.traininfo.util.DownloadUtil;  import dk.thoerup.traininfo.util.DownloadUtil;
17    import dk.thoerup.traininfo.util.XmlUtil;
18    
19  public class XmlStationProvider implements StationProvider {  public class XmlStationProvider implements StationProvider {
20            
21            final static int CACHE_TIMEOUT = 300*1000;
22    
23          List<StationBean> stations = new ArrayList<StationBean>();                //List<StationBean> stations = new ArrayList<StationBean>();    
24            AndroidTimeoutCache<String, List<StationBean>> stationCache = new AndroidTimeoutCache<String, List<StationBean>>(CACHE_TIMEOUT);
25                    
26    
27    
28            @Override
29            public List<StationBean> lookupStations(Location location) {
30                    String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + (float)location.getLatitude() + "&longitude=" + (float)location.getLongitude();
31                    Log.i("url", url);
32                    return lookupStationsWorker(url);
33            }
34                    
35          @Override          @Override
36          public List<StationBean> getStations() {          public List<StationBean> lookupStationsByName(String name) {            
37                  return stations;                  
38            //      String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name);
39                    String url = "";
40                    
41                    try {
42                            url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + URLEncoder.encode(name, "ISO8859-1");    
43                    } catch (Exception e) {
44                            Log.e("lookupStations", "Encoding failed", e);
45                    }
46                    
47                    Log.i("url", url);
48                    return lookupStationsWorker(url);
49          }          }
50            
51            
52          @Override          @Override
53          public boolean lookupStations(Location location) {          public List<StationBean> lookupStationsByIds(String ids) {
54                  boolean success = false;                  String url = "";
55                  String url = "http://app.t-hoerup.dk/TrainInfoService/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude();                  url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids;
56                    
57                  Log.i("url", url);                  Log.i("url", url);
58                    return lookupStationsWorker(url);
59            }
60            
61            public List<StationBean> lookupStationsWorker(String url) {
62                    
63                    List<StationBean> tmpStations = stationCache.get(url);
64                    
65                    if (tmpStations != null) {
66                            Log.i("lookupStations", "cache hit " + url);
67                    } else {
68                            tmpStations = fetchStations(url);
69                            
70                            if (tmpStations != null) {
71                                    stationCache.put(url, tmpStations);
72                            }
73                    }
74                    
75                    return tmpStations;
76            }
77    
78    
79            public List<StationBean> fetchStations(String url) {
80    
81                  try {                  try {
82                          stations.clear();                          List<StationBean> stations = new ArrayList<StationBean>();
83                                                    
84                          String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");                          String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
85                                                    
86                                                    
87                          Document doc = parseXML(xml);                          Document doc = XmlUtil.parseXML(xml);
88                          Node rootNode = doc.getDocumentElement(); // stations                          Node rootNode = doc.getDocumentElement(); // stations
89                          NodeList stationList = rootNode.getChildNodes();                          NodeList stationList = rootNode.getChildNodes();
90                                                    
# Line 75  public class XmlStationProvider implemen Line 119  public class XmlStationProvider implemen
119                                          if (nodeName.equals("longitude"))                                          if (nodeName.equals("longitude"))
120                                                  station.setLongitude( Double.parseDouble(content) );                                                  station.setLongitude( Double.parseDouble(content) );
121                                                                                    
                                         if (nodeName.equals("stationcode"))  
                                                 station.setCode(content);  
                                           
122                                          if (nodeName.equals("calcdist"))                                          if (nodeName.equals("calcdist"))
123                                                  station.setDistance( (int) Double.parseDouble(content) );                                                  station.setDistance( (int) Double.parseDouble(content) );
124                                            
125                                            if (nodeName.equals("address"))
126                                                    station.setAddress( content );
127                                            
128                                            if (nodeName.equals("regional"))
129                                                    station.setRegional( Boolean.parseBoolean(content) );
130                                            
131                                            if (nodeName.equals("strain"))
132                                                    station.setSTrain( Boolean.parseBoolean(content) );
133                                            
134                                            if (nodeName.equals("metro"))
135                                                    station.setMetro( Boolean.parseBoolean(content) );
136    
137                                  }                                  }
138                                  stations.add(station);                                                            stations.add(station);                          
139                          }                          }
140                          success = true;                          return stations;
141                                                    
142                                                    
143                  } catch (Exception e) {                  } catch (Exception e) {
144                          Log.e("XmlStationProvider", "lookupStations: ", e);                          Log.e("XmlStationProvider", "lookupStations: ", e);
145                            return null;
146                  }                  }
                 return success;  
         }  
   
         private Document parseXML(String str) throws SAXException, IOException, ParserConfigurationException  
         {  
                 DocumentBuilder builder =  DocumentBuilderFactory.newInstance().newDocumentBuilder();  
                 return  builder.parse( new ByteArrayInputStream(str.getBytes()) );                
147          }          }
148  }  }

Legend:
Removed from v.319  
changed lines
  Added in v.1006

  ViewVC Help
Powered by ViewVC 1.1.20