1 |
torben |
294 |
package dk.thoerup.traininfo.provider; |
2 |
|
|
|
3 |
torben |
381 |
import java.net.URI; |
4 |
torben |
294 |
import java.util.ArrayList; |
5 |
|
|
import java.util.List; |
6 |
|
|
|
7 |
|
|
import org.w3c.dom.Document; |
8 |
|
|
import org.w3c.dom.Node; |
9 |
|
|
import org.w3c.dom.NodeList; |
10 |
|
|
|
11 |
|
|
import android.location.Location; |
12 |
torben |
381 |
import android.net.Uri; |
13 |
torben |
294 |
import android.util.Log; |
14 |
|
|
import dk.thoerup.traininfo.StationBean; |
15 |
|
|
import dk.thoerup.traininfo.util.DownloadUtil; |
16 |
torben |
352 |
import dk.thoerup.traininfo.util.XmlUtil; |
17 |
torben |
294 |
|
18 |
|
|
public class XmlStationProvider implements StationProvider { |
19 |
|
|
|
20 |
|
|
List<StationBean> stations = new ArrayList<StationBean>(); |
21 |
|
|
|
22 |
|
|
|
23 |
|
|
@Override |
24 |
|
|
public List<StationBean> getStations() { |
25 |
|
|
return stations; |
26 |
|
|
} |
27 |
|
|
|
28 |
torben |
381 |
|
29 |
torben |
294 |
@Override |
30 |
torben |
319 |
public boolean lookupStations(Location location) { |
31 |
torben |
357 |
String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude(); |
32 |
torben |
294 |
Log.i("url", url); |
33 |
torben |
381 |
return lookupStationsWorker(url); |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
@Override |
37 |
|
|
public boolean lookupStations(String name) { |
38 |
|
|
String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name); |
39 |
|
|
Log.i("url", url); |
40 |
|
|
return lookupStationsWorker(url); |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
|
44 |
|
|
public boolean lookupStationsWorker(String url) { |
45 |
|
|
boolean success = false; |
46 |
|
|
|
47 |
torben |
294 |
try { |
48 |
|
|
stations.clear(); |
49 |
|
|
|
50 |
torben |
302 |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
51 |
torben |
294 |
|
52 |
|
|
|
53 |
torben |
352 |
Document doc = XmlUtil.parseXML(xml); |
54 |
torben |
294 |
Node rootNode = doc.getDocumentElement(); // stations |
55 |
|
|
NodeList stationList = rootNode.getChildNodes(); |
56 |
|
|
|
57 |
|
|
|
58 |
|
|
for (int i=0; i<stationList.getLength(); i++) { |
59 |
|
|
Node stationNode = stationList.item(i); |
60 |
|
|
|
61 |
|
|
if (! stationNode.getNodeName().equals("station")) |
62 |
|
|
continue; |
63 |
|
|
|
64 |
|
|
StationBean station = new StationBean(); |
65 |
|
|
|
66 |
|
|
NodeList stationElements = stationNode.getChildNodes(); |
67 |
|
|
for (int j=0; j<stationElements.getLength(); j++) { |
68 |
|
|
Node current = stationElements.item(j); |
69 |
|
|
|
70 |
|
|
String content = null; |
71 |
|
|
if (current.getFirstChild() != null) |
72 |
|
|
content = current.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value |
73 |
|
|
|
74 |
|
|
String nodeName = current.getNodeName(); |
75 |
|
|
|
76 |
torben |
310 |
if (nodeName.equals("id")) |
77 |
|
|
station.setId( Integer.parseInt(content)); |
78 |
|
|
|
79 |
torben |
294 |
if (nodeName.equals("name")) |
80 |
|
|
station.setName(content); |
81 |
|
|
|
82 |
|
|
if (nodeName.equals("latitude")) |
83 |
|
|
station.setLatitude( Double.parseDouble(content) ); |
84 |
|
|
|
85 |
|
|
if (nodeName.equals("longitude")) |
86 |
|
|
station.setLongitude( Double.parseDouble(content) ); |
87 |
|
|
|
88 |
|
|
if (nodeName.equals("stationcode")) |
89 |
|
|
station.setCode(content); |
90 |
|
|
|
91 |
|
|
if (nodeName.equals("calcdist")) |
92 |
|
|
station.setDistance( (int) Double.parseDouble(content) ); |
93 |
|
|
|
94 |
|
|
} |
95 |
|
|
stations.add(station); |
96 |
torben |
319 |
} |
97 |
|
|
success = true; |
98 |
torben |
294 |
|
99 |
|
|
|
100 |
|
|
} catch (Exception e) { |
101 |
|
|
Log.e("XmlStationProvider", "lookupStations: ", e); |
102 |
|
|
} |
103 |
torben |
319 |
return success; |
104 |
torben |
294 |
} |
105 |
|
|
} |