/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.java
ViewVC logotype

Contents of /android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1006 - (show annotations) (download)
Mon Aug 2 23:18:53 2010 UTC (13 years, 9 months ago) by torben
File size: 4075 byte(s)
Go away from 2-step station lookup (so now station-cache actually works)

No i just need to do the same operation for the 2 other xml providers
1 package dk.thoerup.traininfo.provider;
2
3
4 import java.net.URLEncoder;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import org.w3c.dom.Document;
9 import org.w3c.dom.Node;
10 import org.w3c.dom.NodeList;
11
12 import android.location.Location;
13 import android.util.Log;
14 import dk.thoerup.traininfo.StationBean;
15 import dk.thoerup.traininfo.util.AndroidTimeoutCache;
16 import dk.thoerup.traininfo.util.DownloadUtil;
17 import dk.thoerup.traininfo.util.XmlUtil;
18
19 public class XmlStationProvider implements StationProvider {
20
21 final static int CACHE_TIMEOUT = 300*1000;
22
23 //List<StationBean> stations = new ArrayList<StationBean>();
24 AndroidTimeoutCache<String, List<StationBean>> stationCache = new AndroidTimeoutCache<String, List<StationBean>>(CACHE_TIMEOUT);
25
26
27
28 @Override
29 public List<StationBean> lookupStations(Location location) {
30 String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + (float)location.getLatitude() + "&longitude=" + (float)location.getLongitude();
31 Log.i("url", url);
32 return lookupStationsWorker(url);
33 }
34
35 @Override
36 public List<StationBean> lookupStationsByName(String name) {
37
38 // String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name);
39 String url = "";
40
41 try {
42 url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + URLEncoder.encode(name, "ISO8859-1");
43 } catch (Exception e) {
44 Log.e("lookupStations", "Encoding failed", e);
45 }
46
47 Log.i("url", url);
48 return lookupStationsWorker(url);
49 }
50
51
52 @Override
53 public List<StationBean> lookupStationsByIds(String ids) {
54 String url = "";
55 url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids;
56
57 Log.i("url", url);
58 return lookupStationsWorker(url);
59 }
60
61 public List<StationBean> lookupStationsWorker(String url) {
62
63 List<StationBean> tmpStations = stationCache.get(url);
64
65 if (tmpStations != null) {
66 Log.i("lookupStations", "cache hit " + url);
67 } else {
68 tmpStations = fetchStations(url);
69
70 if (tmpStations != null) {
71 stationCache.put(url, tmpStations);
72 }
73 }
74
75 return tmpStations;
76 }
77
78
79 public List<StationBean> fetchStations(String url) {
80
81 try {
82 List<StationBean> stations = new ArrayList<StationBean>();
83
84 String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
85
86
87 Document doc = XmlUtil.parseXML(xml);
88 Node rootNode = doc.getDocumentElement(); // stations
89 NodeList stationList = rootNode.getChildNodes();
90
91
92 for (int i=0; i<stationList.getLength(); i++) {
93 Node stationNode = stationList.item(i);
94
95 if (! stationNode.getNodeName().equals("station"))
96 continue;
97
98 StationBean station = new StationBean();
99
100 NodeList stationElements = stationNode.getChildNodes();
101 for (int j=0; j<stationElements.getLength(); j++) {
102 Node current = stationElements.item(j);
103
104 String content = null;
105 if (current.getFirstChild() != null)
106 content = current.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value
107
108 String nodeName = current.getNodeName();
109
110 if (nodeName.equals("id"))
111 station.setId( Integer.parseInt(content));
112
113 if (nodeName.equals("name"))
114 station.setName(content);
115
116 if (nodeName.equals("latitude"))
117 station.setLatitude( Double.parseDouble(content) );
118
119 if (nodeName.equals("longitude"))
120 station.setLongitude( Double.parseDouble(content) );
121
122 if (nodeName.equals("calcdist"))
123 station.setDistance( (int) Double.parseDouble(content) );
124
125 if (nodeName.equals("address"))
126 station.setAddress( content );
127
128 if (nodeName.equals("regional"))
129 station.setRegional( Boolean.parseBoolean(content) );
130
131 if (nodeName.equals("strain"))
132 station.setSTrain( Boolean.parseBoolean(content) );
133
134 if (nodeName.equals("metro"))
135 station.setMetro( Boolean.parseBoolean(content) );
136
137 }
138 stations.add(station);
139 }
140 return stations;
141
142
143 } catch (Exception e) {
144 Log.e("XmlStationProvider", "lookupStations: ", e);
145 return null;
146 }
147 }
148 }

  ViewVC Help
Powered by ViewVC 1.1.20