/[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 391 by torben, Sat Oct 3 10:55:43 2009 UTC revision 1160 by torben, Mon Oct 4 08:42:12 2010 UTC
# Line 2  package dk.thoerup.traininfo.provider; Line 2  package dk.thoerup.traininfo.provider;
2    
3    
4  import java.net.URLEncoder;  import java.net.URLEncoder;
 import java.util.ArrayList;  
 import java.util.List;  
5    
6  import org.w3c.dom.Document;  import org.simpleframework.xml.Serializer;
7  import org.w3c.dom.Node;  import org.simpleframework.xml.core.Persister;
8  import org.w3c.dom.NodeList;  
9    
10  import android.location.Location;  import android.location.Location;
11  import android.util.Log;  import android.util.Log;
12  import dk.thoerup.traininfo.StationBean;  import dk.thoerup.android.traininfo.common.StationBean;
13    import dk.thoerup.traininfo.util.AndroidTimeoutCache;
14  import dk.thoerup.traininfo.util.DownloadUtil;  import dk.thoerup.traininfo.util.DownloadUtil;
15  import dk.thoerup.traininfo.util.XmlUtil;  import dk.thoerup.traininfo.util.XmlUtil;
16    
17  public class XmlStationProvider implements StationProvider {  public class XmlStationProvider implements StationProvider {
   
         List<StationBean> stations = new ArrayList<StationBean>();        
18                    
19            final static int CACHE_TIMEOUT = 300*1000;
20    
21    
22            AndroidTimeoutCache<String, StationBean> stationCache = new AndroidTimeoutCache<String, StationBean>(CACHE_TIMEOUT);
23                    
         @Override  
         public List<StationBean> getStations() {  
                 return stations;  
         }  
24    
25            double roundToPlaces(double value, int places) {
26                    double pow = Math.pow(10, places);
27                    double temp = Math.round( value*pow );
28                    
29                    return temp / pow;
30            }
31    
32          @Override          @Override
33          public boolean lookupStations(Location location) {          public StationBean lookupStations(Location location) {
34                  String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude();                  double lat = roundToPlaces(location.getLatitude(), 4);
35                    double lng = roundToPlaces(location.getLongitude(), 4);
36                    
37                    String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + lat + "&longitude=" + lng;
38                  Log.i("url", url);                  Log.i("url", url);
39                  return lookupStationsWorker(url);                  return lookupStationsWorker(url);
40          }          }
41                    
42          @Override          @Override
43          public boolean lookupStations(String name) {                      public StationBean lookupStationsByName(String name) {          
                   
         //      String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name);  
                 String url = "";  
44                                    
45                  try {                  try {
46                          url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + URLEncoder.encode(name, "ISO8859-1");                              name = URLEncoder.encode(name, "ISO8859-1");    
47                  } catch (Exception e) {                  } catch (Exception e) {
48                          Log.e("lookupStations", "Encoding failed", e);                          Log.e("lookupStations", "Encoding failed", e);//if encoding fails use original and hope for the best
49                  }                  }
50                                    
51                    String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + name;
52                    
53                    
54                  Log.i("url", url);                  Log.i("url", url);
55                  return lookupStationsWorker(url);                  return lookupStationsWorker(url);
56          }          }
57                    
58                    
59          public boolean lookupStationsWorker(String url) {          @Override
60                  boolean success = false;          public StationBean lookupStationsByIds(String ids) {
61                    String url = "";
62                    url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids;
63                    
64                    Log.i("url", url);
65                    return lookupStationsWorker(url);
66            }
67            
68            public StationBean lookupStationsWorker(String url) {
69                    
70                    StationBean tmpStations = stationCache.get(url);
71                    
72                    if (tmpStations != null) {
73                            Log.i("lookupStations", "cache hit " + url);
74                    } else {
75                            tmpStations = fetchStations(url);
76                            
77                            if (tmpStations != null) {
78                                    stationCache.put(url, tmpStations);
79                            }
80                    }
81                    
82                    return tmpStations;
83            }
84    
85    
86            public StationBean fetchStations(String url) {
87    
88                  try {                  try {
                         stations.clear();  
89                                                    
90                          String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");                          String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
91                                                    
92                                                    Serializer serializer = new Persister();
                         Document doc = XmlUtil.parseXML(xml);  
                         Node rootNode = doc.getDocumentElement(); // stations  
                         NodeList stationList = rootNode.getChildNodes();  
                           
   
                         for (int i=0; i<stationList.getLength(); i++) {  
                                 Node stationNode = stationList.item(i);  
                                   
                                 if (! stationNode.getNodeName().equals("station"))  
                                         continue;  
           
                                 StationBean station = new StationBean();  
                                   
                                 NodeList stationElements = stationNode.getChildNodes();  
                                 for (int j=0; j<stationElements.getLength(); j++) {      
                                         Node current = stationElements.item(j);  
                                                                                   
                                         String content = null;  
                                         if (current.getFirstChild() != null)  
                                                 content = current.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value  
                                           
                                         String nodeName = current.getNodeName();  
                                           
                                         if (nodeName.equals("id"))  
                                                 station.setId( Integer.parseInt(content));  
                                           
                                         if (nodeName.equals("name"))  
                                                 station.setName(content);  
                                           
                                         if (nodeName.equals("latitude"))  
                                                 station.setLatitude( Double.parseDouble(content) );  
                                           
                                         if (nodeName.equals("longitude"))  
                                                 station.setLongitude( Double.parseDouble(content) );  
                                           
                                         if (nodeName.equals("stationcode"))  
                                                 station.setCode(content);  
                                           
                                         if (nodeName.equals("calcdist"))  
                                                 station.setDistance( (int) Double.parseDouble(content) );  
93    
94                                  }                          StationBean stations = serializer.read(StationBean.class, xml);
                                 stations.add(station);                            
                         }  
                         success = true;  
95                                                    
96                            return stations;
97                                                    
98                  } catch (Exception e) {                  } catch (Exception e) {
99                          Log.e("XmlStationProvider", "lookupStations: ", e);                          Log.e("XmlStationProvider", "lookupStations: ", e);
100                            return null;
101                  }                  }
102                  return success;          }
103            
104            @Override
105            public void purgeOldEntries() {
106                    stationCache.purgeOldEntries();        
107          }          }
108  }  }

Legend:
Removed from v.391  
changed lines
  Added in v.1160

  ViewVC Help
Powered by ViewVC 1.1.20