/[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 310 by torben, Thu Sep 10 19:09:09 2009 UTC revision 1066 by torben, Thu Sep 16 15:32:42 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    
8  import javax.xml.parsers.DocumentBuilder;  import org.simpleframework.xml.Serializer;
9  import javax.xml.parsers.DocumentBuilderFactory;  import org.simpleframework.xml.core.Persister;
 import javax.xml.parsers.ParserConfigurationException;  
   
10  import org.w3c.dom.Document;  import org.w3c.dom.Document;
11  import org.w3c.dom.Node;  import org.w3c.dom.Node;
12  import org.w3c.dom.NodeList;  import org.w3c.dom.NodeList;
 import org.xml.sax.SAXException;  
13    
14  import android.location.Location;  import android.location.Location;
15  import android.util.Log;  import android.util.Log;
16  import dk.thoerup.traininfo.StationBean;  import dk.thoerup.android.traininfo.common.StationBean;
17    import dk.thoerup.android.traininfo.common.TimetableBean;
18    import dk.thoerup.traininfo.util.AndroidTimeoutCache;
19  import dk.thoerup.traininfo.util.DownloadUtil;  import dk.thoerup.traininfo.util.DownloadUtil;
20    import dk.thoerup.traininfo.util.XmlUtil;
21    
22  public class XmlStationProvider implements StationProvider {  public class XmlStationProvider implements StationProvider {
23            
24            final static int CACHE_TIMEOUT = 300*1000;
25    
26          List<StationBean> stations = new ArrayList<StationBean>();                //List<StationBean> stations = new ArrayList<StationBean>();    
27            AndroidTimeoutCache<String, StationBean> stationCache = new AndroidTimeoutCache<String, StationBean>(CACHE_TIMEOUT);
28                    
29    
30            double roundToPlaces(double value, int places) {
31                    double pow = Math.pow(10, places);
32                    double temp = Math.round( value*pow );
33                    
34                    return temp / pow;
35            }
36    
37            @Override
38            public StationBean lookupStations(Location location) {
39                    double lat = roundToPlaces(location.getLatitude(), 4);
40                    double lng = roundToPlaces(location.getLongitude(), 4);
41                    
42                    String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + lat + "&longitude=" + lng;
43                    Log.i("url", url);
44                    return lookupStationsWorker(url);
45            }
46                    
47          @Override          @Override
48          public List<StationBean> getStations() {          public StationBean lookupStationsByName(String name) {          
49                  return stations;                  
50            //      String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name);
51                    String url = "";
52                    
53                    try {
54                            url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + URLEncoder.encode(name, "ISO8859-1");    
55                    } catch (Exception e) {
56                            Log.e("lookupStations", "Encoding failed", e);
57                    }
58                    
59                    Log.i("url", url);
60                    return lookupStationsWorker(url);
61          }          }
62            
63            
64          @Override          @Override
65          public void lookupStations(Location location) {          public StationBean lookupStationsByIds(String ids) {
66                    String url = "";
67                    url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids;
68                                    
                 //String url = "http://pumba.t-hoerup.dk:8080/TrainInfoService/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude();  
                 String url = "http://app.t-hoerup.dk/TrainInfoService/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude();  
69                  Log.i("url", url);                  Log.i("url", url);
70                    return lookupStationsWorker(url);
71            }
72            
73            public StationBean lookupStationsWorker(String url) {
74                    
75                    StationBean tmpStations = stationCache.get(url);
76                    
77                    if (tmpStations != null) {
78                            Log.i("lookupStations", "cache hit " + url);
79                    } else {
80                            tmpStations = fetchStations(url);
81                            
82                            if (tmpStations != null) {
83                                    stationCache.put(url, tmpStations);
84                            }
85                    }
86                    
87                    return tmpStations;
88            }
89    
90    
91            public StationBean fetchStations(String url) {
92    
93                  try {                  try {
                         stations.clear();  
94                                                    
95                          String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");                          String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
96                                                    
97                            Serializer serializer = new Persister();
98    
99                            StationBean stations = serializer.read(StationBean.class, xml);
100                            
101                                                    
102                          Document doc = parseXML(xml);                          return stations;
                         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();  
                                           
                                         Log.i("XML", "" + nodeName + "=" + content);  
                                           
                                         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) );  
   
                                 }  
                                 stations.add(station);                            
                         }                
103                                                    
104                                                    
105                  } catch (Exception e) {                  } catch (Exception e) {
106                          Log.e("XmlStationProvider", "lookupStations: ", e);                          Log.e("XmlStationProvider", "lookupStations: ", e);
107                            return null;
108                  }                  }
109          }          }
   
         public Document parseXML(String str) throws SAXException, IOException, ParserConfigurationException  
         {  
                 DocumentBuilder builder =  DocumentBuilderFactory.newInstance().newDocumentBuilder();  
                 return  builder.parse( new ByteArrayInputStream(str.getBytes()) );                
         }  
110  }  }

Legend:
Removed from v.310  
changed lines
  Added in v.1066

  ViewVC Help
Powered by ViewVC 1.1.20