1 |
package dk.thoerup.traininfo.provider; |
2 |
|
3 |
import java.io.ByteArrayInputStream; |
4 |
import java.io.IOException; |
5 |
import java.util.ArrayList; |
6 |
import java.util.List; |
7 |
|
8 |
import javax.xml.parsers.DocumentBuilder; |
9 |
import javax.xml.parsers.DocumentBuilderFactory; |
10 |
import javax.xml.parsers.ParserConfigurationException; |
11 |
|
12 |
import org.w3c.dom.Document; |
13 |
import org.w3c.dom.Node; |
14 |
import org.w3c.dom.NodeList; |
15 |
import org.w3c.dom.Text; |
16 |
import org.xml.sax.SAXException; |
17 |
|
18 |
import android.location.Location; |
19 |
import android.util.Log; |
20 |
import dk.thoerup.traininfo.StationBean; |
21 |
import dk.thoerup.traininfo.util.DownloadUtil; |
22 |
|
23 |
public class XmlStationProvider implements StationProvider { |
24 |
|
25 |
List<StationBean> stations = new ArrayList<StationBean>(); |
26 |
|
27 |
|
28 |
@Override |
29 |
public List<StationBean> getStations() { |
30 |
return stations; |
31 |
} |
32 |
|
33 |
@Override |
34 |
public void lookupStations(Location location) { |
35 |
|
36 |
//String url = "http://pumba.t-hoerup.dk:8080/TrainInfoService/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude(); |
37 |
String url = "http://app.t-hoerup.dk/TrainInfoService/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude(); |
38 |
Log.i("url", url); |
39 |
try { |
40 |
stations.clear(); |
41 |
|
42 |
String xml = DownloadUtil.getContentString(url, 15000); |
43 |
|
44 |
|
45 |
Document doc = parseXML(xml); |
46 |
Node rootNode = doc.getDocumentElement(); // stations |
47 |
NodeList stationList = rootNode.getChildNodes(); |
48 |
|
49 |
|
50 |
for (int i=0; i<stationList.getLength(); i++) { |
51 |
Node stationNode = stationList.item(i); |
52 |
|
53 |
if (! stationNode.getNodeName().equals("station")) |
54 |
continue; |
55 |
|
56 |
StationBean station = new StationBean(); |
57 |
|
58 |
NodeList stationElements = stationNode.getChildNodes(); |
59 |
for (int j=0; j<stationElements.getLength(); j++) { |
60 |
Node current = stationElements.item(j); |
61 |
|
62 |
String content = null; |
63 |
if (current.getFirstChild() != null) |
64 |
content = current.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value |
65 |
|
66 |
String nodeName = current.getNodeName(); |
67 |
|
68 |
Log.i("XML", "" + nodeName + "=" + content); |
69 |
|
70 |
if (nodeName.equals("name")) |
71 |
station.setName(content); |
72 |
|
73 |
if (nodeName.equals("latitude")) |
74 |
station.setLatitude( Double.parseDouble(content) ); |
75 |
|
76 |
if (nodeName.equals("longitude")) |
77 |
station.setLongitude( Double.parseDouble(content) ); |
78 |
|
79 |
if (nodeName.equals("stationcode")) |
80 |
station.setCode(content); |
81 |
|
82 |
if (nodeName.equals("calcdist")) |
83 |
station.setDistance( (int) Double.parseDouble(content) ); |
84 |
|
85 |
} |
86 |
stations.add(station); |
87 |
} |
88 |
|
89 |
|
90 |
} catch (Exception e) { |
91 |
Log.e("XmlStationProvider", "lookupStations: ", e); |
92 |
} |
93 |
} |
94 |
|
95 |
public Document parseXML(String str) throws SAXException, IOException, ParserConfigurationException |
96 |
{ |
97 |
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
98 |
return builder.parse( new ByteArrayInputStream(str.getBytes()) ); |
99 |
} |
100 |
} |