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 |
@Override |
27 |
public List<StationBean> getStations() { |
28 |
return stations; |
29 |
} |
30 |
|
31 |
|
32 |
@Override |
33 |
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 |
|
51 |
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 |
result = true; |
72 |
} else { |
73 |
result = fetchStations(url); |
74 |
|
75 |
if (result == true) { |
76 |
stationCache.put(url, stations); |
77 |
} |
78 |
} |
79 |
|
80 |
return result; |
81 |
} |
82 |
|
83 |
|
84 |
public boolean fetchStations(String url) { |
85 |
boolean success = false; |
86 |
|
87 |
try { |
88 |
stations.clear(); |
89 |
|
90 |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
91 |
|
92 |
|
93 |
Document doc = XmlUtil.parseXML(xml); |
94 |
Node rootNode = doc.getDocumentElement(); // stations |
95 |
NodeList stationList = rootNode.getChildNodes(); |
96 |
|
97 |
|
98 |
for (int i=0; i<stationList.getLength(); i++) { |
99 |
Node stationNode = stationList.item(i); |
100 |
|
101 |
if (! stationNode.getNodeName().equals("station")) |
102 |
continue; |
103 |
|
104 |
StationBean station = new StationBean(); |
105 |
|
106 |
NodeList stationElements = stationNode.getChildNodes(); |
107 |
for (int j=0; j<stationElements.getLength(); j++) { |
108 |
Node current = stationElements.item(j); |
109 |
|
110 |
String content = null; |
111 |
if (current.getFirstChild() != null) |
112 |
content = current.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value |
113 |
|
114 |
String nodeName = current.getNodeName(); |
115 |
|
116 |
if (nodeName.equals("id")) |
117 |
station.setId( Integer.parseInt(content)); |
118 |
|
119 |
if (nodeName.equals("name")) |
120 |
station.setName(content); |
121 |
|
122 |
if (nodeName.equals("latitude")) |
123 |
station.setLatitude( Double.parseDouble(content) ); |
124 |
|
125 |
if (nodeName.equals("longitude")) |
126 |
station.setLongitude( Double.parseDouble(content) ); |
127 |
|
128 |
if (nodeName.equals("calcdist")) |
129 |
station.setDistance( (int) Double.parseDouble(content) ); |
130 |
|
131 |
if (nodeName.equals("address")) |
132 |
station.setAddress( content ); |
133 |
|
134 |
if (nodeName.equals("regional")) |
135 |
station.setRegional( Boolean.parseBoolean(content) ); |
136 |
|
137 |
if (nodeName.equals("strain")) |
138 |
station.setSTrain( Boolean.parseBoolean(content) ); |
139 |
|
140 |
if (nodeName.equals("metro")) |
141 |
station.setMetro( Boolean.parseBoolean(content) ); |
142 |
|
143 |
} |
144 |
stations.add(station); |
145 |
} |
146 |
success = true; |
147 |
|
148 |
|
149 |
} catch (Exception e) { |
150 |
Log.e("XmlStationProvider", "lookupStations: ", e); |
151 |
} |
152 |
return success; |
153 |
} |
154 |
} |