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 |
double roundToPlaces(double value, int places) { |
28 |
double pow = Math.pow(10, places); |
29 |
double temp = Math.round( value*pow ); |
30 |
|
31 |
return temp / pow; |
32 |
} |
33 |
|
34 |
@Override |
35 |
public List<StationBean> lookupStations(Location location) { |
36 |
double lat = roundToPlaces(location.getLatitude(), 4); |
37 |
double lng = roundToPlaces(location.getLongitude(), 4); |
38 |
|
39 |
String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + lat + "&longitude=" + lng; |
40 |
Log.i("url", url); |
41 |
return lookupStationsWorker(url); |
42 |
} |
43 |
|
44 |
@Override |
45 |
public List<StationBean> lookupStationsByName(String name) { |
46 |
|
47 |
// String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name); |
48 |
String url = ""; |
49 |
|
50 |
try { |
51 |
url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + URLEncoder.encode(name, "ISO8859-1"); |
52 |
} catch (Exception e) { |
53 |
Log.e("lookupStations", "Encoding failed", e); |
54 |
} |
55 |
|
56 |
Log.i("url", url); |
57 |
return lookupStationsWorker(url); |
58 |
} |
59 |
|
60 |
|
61 |
@Override |
62 |
public List<StationBean> lookupStationsByIds(String ids) { |
63 |
String url = ""; |
64 |
url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids; |
65 |
|
66 |
Log.i("url", url); |
67 |
return lookupStationsWorker(url); |
68 |
} |
69 |
|
70 |
public List<StationBean> lookupStationsWorker(String url) { |
71 |
|
72 |
List<StationBean> tmpStations = stationCache.get(url); |
73 |
|
74 |
if (tmpStations != null) { |
75 |
Log.i("lookupStations", "cache hit " + url); |
76 |
} else { |
77 |
tmpStations = fetchStations(url); |
78 |
|
79 |
if (tmpStations != null) { |
80 |
stationCache.put(url, tmpStations); |
81 |
} |
82 |
} |
83 |
|
84 |
return tmpStations; |
85 |
} |
86 |
|
87 |
|
88 |
public List<StationBean> fetchStations(String url) { |
89 |
|
90 |
try { |
91 |
List<StationBean> stations = new ArrayList<StationBean>(); |
92 |
|
93 |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
94 |
|
95 |
|
96 |
Document doc = XmlUtil.parseXML(xml); |
97 |
Node rootNode = doc.getDocumentElement(); // stations |
98 |
NodeList stationList = rootNode.getChildNodes(); |
99 |
|
100 |
|
101 |
for (int i=0; i<stationList.getLength(); i++) { |
102 |
Node stationNode = stationList.item(i); |
103 |
|
104 |
if (! stationNode.getNodeName().equals("station")) |
105 |
continue; |
106 |
|
107 |
StationBean station = new StationBean(); |
108 |
|
109 |
NodeList stationElements = stationNode.getChildNodes(); |
110 |
for (int j=0; j<stationElements.getLength(); j++) { |
111 |
Node current = stationElements.item(j); |
112 |
|
113 |
String content = null; |
114 |
if (current.getFirstChild() != null) |
115 |
content = current.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value |
116 |
|
117 |
String nodeName = current.getNodeName(); |
118 |
|
119 |
if (nodeName.equals("id")) |
120 |
station.setId( Integer.parseInt(content)); |
121 |
|
122 |
if (nodeName.equals("name")) |
123 |
station.setName(content); |
124 |
|
125 |
if (nodeName.equals("latitude")) |
126 |
station.setLatitude( Double.parseDouble(content) ); |
127 |
|
128 |
if (nodeName.equals("longitude")) |
129 |
station.setLongitude( Double.parseDouble(content) ); |
130 |
|
131 |
if (nodeName.equals("calcdist")) |
132 |
station.setDistance( (int) Double.parseDouble(content) ); |
133 |
|
134 |
if (nodeName.equals("address")) |
135 |
station.setAddress( content ); |
136 |
|
137 |
if (nodeName.equals("regional")) |
138 |
station.setRegional( Boolean.parseBoolean(content) ); |
139 |
|
140 |
if (nodeName.equals("strain")) |
141 |
station.setSTrain( Boolean.parseBoolean(content) ); |
142 |
|
143 |
if (nodeName.equals("metro")) |
144 |
station.setMetro( Boolean.parseBoolean(content) ); |
145 |
|
146 |
} |
147 |
stations.add(station); |
148 |
} |
149 |
return stations; |
150 |
|
151 |
|
152 |
} catch (Exception e) { |
153 |
Log.e("XmlStationProvider", "lookupStations: ", e); |
154 |
return null; |
155 |
} |
156 |
} |
157 |
} |