/[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 1004 by torben, Mon Aug 2 23:01:47 2010 UTC revision 1079 by torben, Mon Sep 20 06:41:59 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;  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;
# Line 20  public class XmlStationProvider implemen Line 18  public class XmlStationProvider implemen
18                    
19          final static int CACHE_TIMEOUT = 300*1000;          final static int CACHE_TIMEOUT = 300*1000;
20    
21          List<StationBean> stations = new ArrayList<StationBean>();        
22          AndroidTimeoutCache<String, List<StationBean>> stationCache = new AndroidTimeoutCache<String, List<StationBean>>(CACHE_TIMEOUT);          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=" + (float)location.getLatitude() + "&longitude=" + (float)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 lookupStationsByName(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          @Override          @Override
60          public boolean lookupStationsByIds(String ids) {          public StationBean lookupStationsByIds(String ids) {
61                  String url = "";                  String url = "";
62                  url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids;                  url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids;
63                                    
# Line 62  public class XmlStationProvider implemen Line 65  public class XmlStationProvider implemen
65                  return lookupStationsWorker(url);                  return lookupStationsWorker(url);
66          }          }
67                    
68          public boolean lookupStationsWorker(String url) {          public StationBean lookupStationsWorker(String url) {
69                  boolean result;                  
70                  List<StationBean> tmpStations = stationCache.get(url);                  StationBean tmpStations = stationCache.get(url);
71                                    
72                  if (tmpStations != null) {                  if (tmpStations != null) {
73                          Log.i("lookupStations", "cache hit " + url);                          Log.i("lookupStations", "cache hit " + url);
                         result = true;  
74                  } else {                  } else {
75                          result = fetchStations(url);                          tmpStations = fetchStations(url);
76                                                    
77                          if (result == true) {                          if (tmpStations != null) {
78                                  stationCache.put(url, stations);                                  stationCache.put(url, tmpStations);
79                          }                          }
80                  }                  }
81                                    
82                  return result;                  return tmpStations;
83          }          }
84    
85    
86          public boolean fetchStations(String url) {          public StationBean fetchStations(String url) {
                 boolean success = false;  
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();  
                           
93    
94                          for (int i=0; i<stationList.getLength(); i++) {                          StationBean stations = serializer.read(StationBean.class, xml);
                                 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("calcdist"))  
                                                 station.setDistance( (int) Double.parseDouble(content) );  
                                           
                                         if (nodeName.equals("address"))  
                                                 station.setAddress( content );  
                                           
                                         if (nodeName.equals("regional"))  
                                                 station.setRegional( Boolean.parseBoolean(content) );  
                                           
                                         if (nodeName.equals("strain"))  
                                                 station.setSTrain( Boolean.parseBoolean(content) );  
                                           
                                         if (nodeName.equals("metro"))  
                                                 station.setMetro( Boolean.parseBoolean(content) );  
   
                                 }  
                                 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                  }                  }
                 return success;  
102          }          }
103  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20