1 |
torben |
294 |
package dk.thoerup.traininfo.provider; |
2 |
|
|
|
3 |
torben |
391 |
|
4 |
torben |
383 |
import java.net.URLEncoder; |
5 |
torben |
294 |
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.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 |
564 |
String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + (float)location.getLatitude() + "&longitude=" + (float)location.getLongitude(); |
32 |
torben |
294 |
Log.i("url", url); |
33 |
torben |
381 |
return lookupStationsWorker(url); |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
@Override |
37 |
torben |
433 |
public boolean lookupStationsByName(String name) { |
38 |
torben |
383 |
|
39 |
|
|
// String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name); |
40 |
|
|
String url = ""; |
41 |
|
|
|
42 |
|
|
try { |
43 |
|
|
url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + URLEncoder.encode(name, "ISO8859-1"); |
44 |
|
|
} catch (Exception e) { |
45 |
|
|
Log.e("lookupStations", "Encoding failed", e); |
46 |
|
|
} |
47 |
|
|
|
48 |
torben |
381 |
Log.i("url", url); |
49 |
|
|
return lookupStationsWorker(url); |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
|
53 |
torben |
433 |
@Override |
54 |
|
|
public boolean lookupStationsByIds(String ids) { |
55 |
|
|
String url = ""; |
56 |
|
|
url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids; |
57 |
|
|
|
58 |
|
|
Log.i("url", url); |
59 |
|
|
return lookupStationsWorker(url); |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
|
63 |
torben |
381 |
public boolean lookupStationsWorker(String url) { |
64 |
|
|
boolean success = false; |
65 |
|
|
|
66 |
torben |
294 |
try { |
67 |
|
|
stations.clear(); |
68 |
|
|
|
69 |
torben |
302 |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
70 |
torben |
294 |
|
71 |
|
|
|
72 |
torben |
352 |
Document doc = XmlUtil.parseXML(xml); |
73 |
torben |
294 |
Node rootNode = doc.getDocumentElement(); // stations |
74 |
|
|
NodeList stationList = rootNode.getChildNodes(); |
75 |
|
|
|
76 |
|
|
|
77 |
|
|
for (int i=0; i<stationList.getLength(); i++) { |
78 |
|
|
Node stationNode = stationList.item(i); |
79 |
|
|
|
80 |
|
|
if (! stationNode.getNodeName().equals("station")) |
81 |
|
|
continue; |
82 |
|
|
|
83 |
|
|
StationBean station = new StationBean(); |
84 |
|
|
|
85 |
|
|
NodeList stationElements = stationNode.getChildNodes(); |
86 |
|
|
for (int j=0; j<stationElements.getLength(); j++) { |
87 |
|
|
Node current = stationElements.item(j); |
88 |
|
|
|
89 |
|
|
String content = null; |
90 |
|
|
if (current.getFirstChild() != null) |
91 |
|
|
content = current.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value |
92 |
|
|
|
93 |
|
|
String nodeName = current.getNodeName(); |
94 |
|
|
|
95 |
torben |
310 |
if (nodeName.equals("id")) |
96 |
|
|
station.setId( Integer.parseInt(content)); |
97 |
|
|
|
98 |
torben |
294 |
if (nodeName.equals("name")) |
99 |
|
|
station.setName(content); |
100 |
|
|
|
101 |
|
|
if (nodeName.equals("latitude")) |
102 |
|
|
station.setLatitude( Double.parseDouble(content) ); |
103 |
|
|
|
104 |
|
|
if (nodeName.equals("longitude")) |
105 |
torben |
574 |
station.setLongitude( Double.parseDouble(content) ); |
106 |
torben |
294 |
|
107 |
|
|
if (nodeName.equals("calcdist")) |
108 |
|
|
station.setDistance( (int) Double.parseDouble(content) ); |
109 |
torben |
493 |
|
110 |
|
|
if (nodeName.equals("address")) |
111 |
|
|
station.setAddress( content ); |
112 |
torben |
551 |
|
113 |
|
|
if (nodeName.equals("regional")) |
114 |
|
|
station.setRegional( Boolean.parseBoolean(content) ); |
115 |
|
|
|
116 |
|
|
if (nodeName.equals("strain")) |
117 |
|
|
station.setSTrain( Boolean.parseBoolean(content) ); |
118 |
|
|
|
119 |
|
|
if (nodeName.equals("metro")) |
120 |
|
|
station.setMetro( Boolean.parseBoolean(content) ); |
121 |
torben |
294 |
|
122 |
|
|
} |
123 |
|
|
stations.add(station); |
124 |
torben |
319 |
} |
125 |
|
|
success = true; |
126 |
torben |
294 |
|
127 |
|
|
|
128 |
|
|
} catch (Exception e) { |
129 |
|
|
Log.e("XmlStationProvider", "lookupStations: ", e); |
130 |
|
|
} |
131 |
torben |
319 |
return success; |
132 |
torben |
294 |
} |
133 |
|
|
} |