/[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 1005 by torben, Mon Aug 2 23:03:11 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          @Override          @Override
27          public List<StationBean> getStations() {          public List<StationBean> getStations() {
28                  return stations;                  return stations;
29          }          }
30    
31    
32          @Override          @Override
33          public void lookupStations(Location location) {          public boolean lookupStations(Location location) {
34                    String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + (float)location.getLatitude() + "&longitude=" + (float)location.getLongitude();
35                    Log.i("url", url);
36                    return lookupStationsWorker(url);
37            }
38            
39            @Override
40            public boolean lookupStationsByName(String name) {              
41                    
42            //      String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name);
43                    String url = "";
44                    
45                    try {
46                            url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + URLEncoder.encode(name, "ISO8859-1");    
47                    } catch (Exception e) {
48                            Log.e("lookupStations", "Encoding failed", e);
49                    }
50                                    
                 //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();  
51                  Log.i("url", url);                  Log.i("url", url);
52                    return lookupStationsWorker(url);
53            }
54            
55            
56            @Override
57            public boolean lookupStationsByIds(String ids) {
58                    String url = "";
59                    url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids;
60                    
61                    Log.i("url", url);
62                    return lookupStationsWorker(url);
63            }
64            
65            public boolean lookupStationsWorker(String url) {
66                    boolean result;
67                    List<StationBean> tmpStations = stationCache.get(url);
68                    
69                    if (tmpStations != null) {
70                            Log.i("lookupStations", "cache hit " + url);
71                            stations = tmpStations;
72                            result = true;
73                    } else {
74                            result = fetchStations(url);
75                            
76                            if (result == true) {
77                                    stationCache.put(url, stations);
78                            }
79                    }
80                    
81                    return result;
82            }
83    
84    
85            public boolean fetchStations(String url) {
86                    boolean success = false;
87    
88                  try {                  try {
89                          stations.clear();                          stations.clear();
90                                                    
91                          String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");                          String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
92                                                    
93                                                    
94                          Document doc = parseXML(xml);                          Document doc = XmlUtil.parseXML(xml);
95                          Node rootNode = doc.getDocumentElement(); // stations                          Node rootNode = doc.getDocumentElement(); // stations
96                          NodeList stationList = rootNode.getChildNodes();                          NodeList stationList = rootNode.getChildNodes();
97                                                    
# Line 64  public class XmlStationProvider implemen Line 114  public class XmlStationProvider implemen
114                                                                                    
115                                          String nodeName = current.getNodeName();                                          String nodeName = current.getNodeName();
116                                                                                    
                                         Log.i("XML", "" + nodeName + "=" + content);  
                                           
117                                          if (nodeName.equals("id"))                                          if (nodeName.equals("id"))
118                                                  station.setId( Integer.parseInt(content));                                                  station.setId( Integer.parseInt(content));
119                                                                                    
# Line 78  public class XmlStationProvider implemen Line 126  public class XmlStationProvider implemen
126                                          if (nodeName.equals("longitude"))                                          if (nodeName.equals("longitude"))
127                                                  station.setLongitude( Double.parseDouble(content) );                                                  station.setLongitude( Double.parseDouble(content) );
128                                                                                    
                                         if (nodeName.equals("stationcode"))  
                                                 station.setCode(content);  
                                           
129                                          if (nodeName.equals("calcdist"))                                          if (nodeName.equals("calcdist"))
130                                                  station.setDistance( (int) Double.parseDouble(content) );                                                  station.setDistance( (int) Double.parseDouble(content) );
131                                            
132                                            if (nodeName.equals("address"))
133                                                    station.setAddress( content );
134                                            
135                                            if (nodeName.equals("regional"))
136                                                    station.setRegional( Boolean.parseBoolean(content) );
137                                            
138                                            if (nodeName.equals("strain"))
139                                                    station.setSTrain( Boolean.parseBoolean(content) );
140                                            
141                                            if (nodeName.equals("metro"))
142                                                    station.setMetro( Boolean.parseBoolean(content) );
143    
144                                  }                                  }
145                                  stations.add(station);                                                            stations.add(station);                          
146                          }                                        }
147                            success = true;
148                                                    
149                                                    
150                  } catch (Exception e) {                  } catch (Exception e) {
151                          Log.e("XmlStationProvider", "lookupStations: ", e);                          Log.e("XmlStationProvider", "lookupStations: ", e);
152                  }                  }
153          }                  return success;
   
         public Document parseXML(String str) throws SAXException, IOException, ParserConfigurationException  
         {  
                 DocumentBuilder builder =  DocumentBuilderFactory.newInstance().newDocumentBuilder();  
                 return  builder.parse( new ByteArrayInputStream(str.getBytes()) );                
154          }          }
155  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20