18 |
import dk.thoerup.traininfo.util.DownloadUtil; |
import dk.thoerup.traininfo.util.DownloadUtil; |
19 |
import dk.thoerup.traininfo.util.XmlUtil; |
import dk.thoerup.traininfo.util.XmlUtil; |
20 |
|
|
21 |
public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider { |
public class XmlDepartureProvider implements DepartureProvider { |
22 |
|
|
23 |
final static int CACHE_TIMEOUT = 60*1000; |
final static int CACHE_TIMEOUT = 60*1000; |
24 |
|
|
25 |
|
|
26 |
AndroidTimeoutCache<String,DepartureBean> departureCache = new AndroidTimeoutCache<String,DepartureBean>(CACHE_TIMEOUT); |
AndroidTimeoutCache<String,DepartureBean> departureCache = new AndroidTimeoutCache<String,DepartureBean>(CACHE_TIMEOUT); |
27 |
|
|
|
DepartureBean departures; |
|
28 |
|
|
29 |
|
|
30 |
DepartureEntry tempDeparture; |
DepartureEntry tempDeparture; |
31 |
StringBuilder builder = new StringBuilder(512); |
StringBuilder builder = new StringBuilder(512); |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
@Override |
@Override |
36 |
public boolean lookupDepartures(int stationID, boolean arrival) { |
public DepartureBean lookupDepartures(int stationID, boolean arrival) { |
|
boolean success; |
|
37 |
|
|
38 |
String key = "" + stationID + ":" + arrival; |
String key = "" + stationID + ":" + arrival; |
39 |
|
|
40 |
departures = departureCache.get(key); |
DepartureBean departures = departureCache.get(key); |
41 |
|
|
42 |
if (departures == null) { |
if (departures == null) { |
43 |
success = lookupDeparturesWorker(stationID, arrival); |
departures = lookupDeparturesWorker(stationID, arrival); |
44 |
|
|
45 |
if (success) { |
if (departures != null) { |
46 |
departureCache.put(key, departures); |
departureCache.put(key, departures); |
47 |
} |
} |
48 |
|
|
49 |
} else { |
} else { |
50 |
Log.i("XmlDepartureProvider", "cache hit !!!"); |
Log.i("XmlDepartureProvider", "cache hit !!!"); |
|
success = true; |
|
51 |
} |
} |
52 |
|
|
53 |
return success; |
return departures; |
54 |
} |
} |
55 |
|
|
56 |
private boolean lookupDeparturesWorker(int stationID, boolean arrival) { |
private DepartureBean lookupDeparturesWorker(int stationID, boolean arrival) { |
57 |
boolean success = false; |
|
|
departures = new DepartureBean(); |
|
58 |
try |
try |
59 |
{ |
{ |
60 |
int iArrival = arrival ? 1 : 0; |
int iArrival = arrival ? 1 : 0; |
68 |
SAXParser sp = spf.newSAXParser(); |
SAXParser sp = spf.newSAXParser(); |
69 |
XMLReader xr = sp.getXMLReader(); |
XMLReader xr = sp.getXMLReader(); |
70 |
|
|
71 |
xr.setContentHandler(this); |
DepartureParser departureParser = new DepartureParser(); |
72 |
xr.setErrorHandler(this); |
xr.setContentHandler(departureParser); |
73 |
|
xr.setErrorHandler(departureParser); |
74 |
xr.parse(source); |
xr.parse(source); |
75 |
success = true; |
|
76 |
|
return departureParser.getDepartures(); |
77 |
|
|
78 |
} catch (Exception e) { |
} catch (Exception e) { |
79 |
Log.e("XmlDepartureProvider", "looupFunction", e); |
Log.e("XmlDepartureProvider", "looupFunction", e); |
80 |
} |
return null; |
81 |
return success; |
} |
82 |
} |
} |
83 |
|
|
84 |
@Override |
|
85 |
public DepartureBean getDepartures(int station,boolean arrival) { |
class DepartureParser extends DefaultHandler { |
|
|
|
|
String key = "" + station + ":" + arrival; |
|
|
|
|
|
DepartureBean bean = departureCache.get(key); |
|
|
|
|
|
if (bean == null) { |
|
|
bean = new DepartureBean(); |
|
|
} |
|
86 |
|
|
87 |
return bean; |
private DepartureBean departures = new DepartureBean(); |
88 |
} |
|
89 |
|
public DepartureBean getDepartures() { |
90 |
// this can be called several times fore the same text-node if there are many chardata / lines |
return departures; |
91 |
@Override |
} |
|
public void characters (char ch[], int start, int length) |
|
|
{ |
|
|
for (int i= start; i<start+length; i++) |
|
|
builder.append(ch[i]); |
|
|
} |
|
92 |
|
|
93 |
@Override |
// this can be called several times fore the same text-node if there are many chardata / lines |
94 |
public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException |
@Override |
95 |
{ |
public void characters (char ch[], int start, int length) |
96 |
if (name.equalsIgnoreCase("train")) |
{ |
97 |
tempDeparture = new DepartureEntry(); |
for (int i= start; i<start+length; i++) |
98 |
|
builder.append(ch[i]); |
99 |
|
} |
100 |
|
|
101 |
builder.setLength(0); //reset StringBuilder |
@Override |
102 |
} |
public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException |
103 |
|
{ |
104 |
@Override |
if (name.equalsIgnoreCase("train")) |
105 |
public void endElement (String uri, String name, String qName) throws SAXException |
tempDeparture = new DepartureEntry(); |
106 |
{ |
|
107 |
if (name.equals("train")) { |
builder.setLength(0); //reset StringBuilder |
108 |
departures.entries.add( tempDeparture ); |
} |
109 |
} else if (name.equals("time")) { |
|
110 |
tempDeparture.setTime(builder.toString().trim()); |
@Override |
111 |
} else if (name.equals("updated")) { |
public void endElement (String uri, String name, String qName) throws SAXException |
112 |
tempDeparture.setLastUpdate(builder.toString().trim()); |
{ |
113 |
} else if (name.equals("trainnumber")) { |
if (name.equals("train")) { |
114 |
tempDeparture.setTrainNumber(builder.toString().trim()); |
departures.entries.add( tempDeparture ); |
115 |
} else if (name.equals("destination")) { |
} else if (name.equals("time")) { |
116 |
tempDeparture.setDestination(builder.toString().trim()); |
tempDeparture.setTime(builder.toString().trim()); |
117 |
} else if (name.equals("origin")) { |
} else if (name.equals("updated")) { |
118 |
tempDeparture.setOrigin(builder.toString().trim()); |
tempDeparture.setLastUpdate(builder.toString().trim()); |
119 |
} else if (name.equals("location")) { |
} else if (name.equals("trainnumber")) { |
120 |
tempDeparture.setLocation(builder.toString().trim()); |
tempDeparture.setTrainNumber(builder.toString().trim()); |
121 |
} else if (name.equals("status")) { |
} else if (name.equals("destination")) { |
122 |
tempDeparture.setStatus(builder.toString().trim()); |
tempDeparture.setDestination(builder.toString().trim()); |
123 |
} else if (name.equals("note")) { |
} else if (name.equals("origin")) { |
124 |
tempDeparture.setNote(builder.toString().trim()); |
tempDeparture.setOrigin(builder.toString().trim()); |
125 |
} else if (name.equals("type")) { |
} else if (name.equals("location")) { |
126 |
tempDeparture.setType(builder.toString().trim()); |
tempDeparture.setLocation(builder.toString().trim()); |
127 |
} else if (name.equals("notification")) { |
} else if (name.equals("status")) { |
128 |
departures.notifications.add( builder.toString().trim() ); |
tempDeparture.setStatus(builder.toString().trim()); |
129 |
|
} else if (name.equals("note")) { |
130 |
|
tempDeparture.setNote(builder.toString().trim()); |
131 |
|
} else if (name.equals("type")) { |
132 |
|
tempDeparture.setType(builder.toString().trim()); |
133 |
|
} else if (name.equals("notification")) { |
134 |
|
departures.notifications.add( builder.toString().trim() ); |
135 |
|
} |
136 |
} |
} |
137 |
} |
} |
138 |
} |
} |