/[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 294 by torben, Tue Sep 1 20:28:55 2009 UTC revision 1160 by torben, Mon Oct 4 08:42:12 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;  
6  import java.util.List;  import org.simpleframework.xml.Serializer;
7    import org.simpleframework.xml.core.Persister;
8  import javax.xml.parsers.DocumentBuilder;  
 import javax.xml.parsers.DocumentBuilderFactory;  
 import javax.xml.parsers.ParserConfigurationException;  
   
 import org.w3c.dom.Document;  
 import org.w3c.dom.Node;  
 import org.w3c.dom.NodeList;  
 import org.w3c.dom.Text;  
 import org.xml.sax.SAXException;  
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;
16    
17  public class XmlStationProvider implements StationProvider {  public class XmlStationProvider implements StationProvider {
18            
19            final static int CACHE_TIMEOUT = 300*1000;
20    
21    
22          List<StationBean> stations = new ArrayList<StationBean>();                AndroidTimeoutCache<String, StationBean> stationCache = new AndroidTimeoutCache<String, StationBean>(CACHE_TIMEOUT);
23                    
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
33            public StationBean lookupStations(Location location) {
34                    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);
39                    return lookupStationsWorker(url);
40            }
41                    
42          @Override          @Override
43          public List<StationBean> getStations() {          public StationBean lookupStationsByName(String name) {          
44                  return stations;                  
45                    try {
46                            name = URLEncoder.encode(name, "ISO8859-1");    
47                    } catch (Exception e) {
48                            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);
55                    return lookupStationsWorker(url);
56          }          }
57            
58            
59          @Override          @Override
60          public void lookupStations(Location location) {          public StationBean lookupStationsByIds(String ids) {
61                    String url = "";
62                    url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids;
63                                    
                 //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();  
64                  Log.i("url", url);                  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();  
                           
                         String xml = DownloadUtil.getContentString(url, 15000);  
89                                                    
90                            String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
91                                                    
92                          Document doc = parseXML(xml);                          Serializer serializer = new Persister();
                         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();  
                                           
                                         Log.i("XML", "" + nodeName + "=" + 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);                            
                         }                
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          }          }
103            
104          public Document parseXML(String str) throws SAXException, IOException, ParserConfigurationException          @Override
105          {          public void purgeOldEntries() {
106                  DocumentBuilder builder =  DocumentBuilderFactory.newInstance().newDocumentBuilder();                  stationCache.purgeOldEntries();        
                 return  builder.parse( new ByteArrayInputStream(str.getBytes()) );                
107          }          }
108  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20