1 |
package dk.thoerup.traininfo.provider; |
package dk.thoerup.traininfo.provider; |
2 |
|
|
3 |
import java.util.ArrayList; |
import org.simpleframework.xml.Serializer; |
4 |
import java.util.List; |
import org.simpleframework.xml.core.Persister; |
|
|
|
|
import org.w3c.dom.Document; |
|
|
|
|
|
import org.w3c.dom.Node; |
|
|
import org.w3c.dom.NodeList; |
|
5 |
|
|
6 |
import android.util.Log; |
import android.util.Log; |
7 |
import dk.thoerup.traininfo.TimetableBean; |
import dk.thoerup.android.traininfo.common.TimetableBean; |
8 |
import dk.thoerup.traininfo.util.AndroidTimeoutCache; |
import dk.thoerup.traininfo.util.AndroidTimeoutCache; |
9 |
import dk.thoerup.traininfo.util.DownloadUtil; |
import dk.thoerup.traininfo.util.DownloadUtil; |
10 |
import dk.thoerup.traininfo.util.XmlUtil; |
import dk.thoerup.traininfo.util.XmlUtil; |
13 |
|
|
14 |
final static int CACHE_TIMEOUT = 60*1000; |
final static int CACHE_TIMEOUT = 60*1000; |
15 |
|
|
16 |
AndroidTimeoutCache<String,List<TimetableBean>> timetableCache = new AndroidTimeoutCache<String,List<TimetableBean>>(CACHE_TIMEOUT); |
AndroidTimeoutCache<String,TimetableBean> timetableCache = new AndroidTimeoutCache<String,TimetableBean>(CACHE_TIMEOUT); |
17 |
|
|
18 |
@Override |
@Override |
19 |
public List<TimetableBean> lookupTimetable(String type, String trainID) { |
public TimetableBean lookupTimetable(String type, String trainID) { |
20 |
|
|
21 |
String trainNumber = extractTrainNumber(trainID); |
String trainNumber = extractTrainNumber(trainID); |
22 |
|
|
23 |
String key = type + "-" + trainID; |
String key = type + "-" + trainID; |
24 |
List<TimetableBean> timetables = timetableCache.get(key); |
TimetableBean timetables = timetableCache.get(key); |
25 |
|
|
26 |
if (timetables == null) { |
if (timetables == null) { |
27 |
timetables = lookupTimetableWorker(type, trainNumber); |
timetables = lookupTimetableWorker(type, trainNumber); |
38 |
} |
} |
39 |
|
|
40 |
|
|
41 |
public List<TimetableBean> lookupTimetableWorker(String type, String trainNumber) { |
public TimetableBean lookupTimetableWorker(String type, String trainNumber) { |
42 |
|
|
43 |
String url = XmlUtil.SERVICE_BASE + "/TimetableServlet?train=" + trainNumber + "&type=" + type; |
String url = XmlUtil.SERVICE_BASE + "/TimetableServlet?train=" + trainNumber + "&type=" + type; |
44 |
Log.i("url", url); |
Log.i("url", url); |
45 |
try { |
try { |
|
List<TimetableBean> timetables = new ArrayList<TimetableBean>(); |
|
46 |
|
|
47 |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
48 |
|
|
49 |
|
Serializer serializer = new Persister(); |
50 |
|
|
51 |
|
TimetableBean timetables = serializer.read(TimetableBean.class, xml); |
52 |
|
|
|
Document doc = XmlUtil.parseXML(xml); |
|
|
Node rootNode = doc.getDocumentElement(); // stations |
|
|
NodeList stationList = rootNode.getChildNodes(); |
|
53 |
|
|
|
|
|
|
for (int i=0; i<stationList.getLength(); i++) { |
|
|
Node entryNode = stationList.item(i); |
|
|
|
|
|
if (! entryNode.getNodeName().equals("entry")) |
|
|
continue; |
|
|
|
|
|
TimetableBean timetable = new TimetableBean(); |
|
|
|
|
|
NodeList entries = entryNode.getChildNodes(); |
|
|
|
|
|
if (entryNode.hasAttributes() && entryNode.getAttributes().getNamedItem("current") != null) { |
|
|
timetable.setCurrent( Boolean.parseBoolean(entryNode.getAttributes().getNamedItem("current").getNodeValue())); |
|
|
} else { |
|
|
timetable.setCurrent(false); |
|
|
} |
|
|
|
|
|
for (int j=0; j<entries.getLength(); j++) { |
|
|
Node current = entries.item(j); |
|
|
|
|
|
String content = null; |
|
|
if (current.getFirstChild() != null) |
|
|
content = current.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value |
|
|
|
|
|
String nodeName = current.getNodeName(); |
|
|
|
|
|
if (nodeName.equals("station")) |
|
|
timetable.setStation( content ); |
|
|
|
|
|
if (nodeName.equals("arrival")) |
|
|
timetable.setArrival( content ); |
|
|
|
|
|
if (nodeName.equals("departure")) |
|
|
timetable.setDeparture( content ); |
|
|
|
|
|
if (nodeName.equals("stationid")) |
|
|
timetable.setStationId( Integer.parseInt(content)); |
|
|
|
|
|
/*if (nodeName.equals("current")) |
|
|
timetable.setCurrent( Boolean.parseBoolean(content) );*/ |
|
|
|
|
|
} |
|
|
timetables.add(timetable); |
|
|
} |
|
54 |
return timetables; |
return timetables; |
55 |
|
|
56 |
|
|