1 |
package dk.thoerup.traininfo.provider; |
package dk.thoerup.traininfo.provider; |
2 |
|
|
3 |
import java.io.StringReader; |
import java.io.StringReader; |
|
import java.util.ArrayList; |
|
|
import java.util.List; |
|
4 |
|
|
5 |
import javax.xml.parsers.SAXParser; |
import javax.xml.parsers.SAXParser; |
6 |
import javax.xml.parsers.SAXParserFactory; |
import javax.xml.parsers.SAXParserFactory; |
13 |
|
|
14 |
import android.util.Log; |
import android.util.Log; |
15 |
import dk.thoerup.traininfo.DepartureBean; |
import dk.thoerup.traininfo.DepartureBean; |
16 |
|
import dk.thoerup.traininfo.DepartureEntry; |
17 |
import dk.thoerup.traininfo.util.AndroidTimeoutCache; |
import dk.thoerup.traininfo.util.AndroidTimeoutCache; |
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; |
23 |
final static int CACHE_TIMEOUT = 60*1000; |
final static int CACHE_TIMEOUT = 60*1000; |
24 |
|
|
25 |
|
|
26 |
AndroidTimeoutCache<String,List<DepartureBean>> departureCache = new AndroidTimeoutCache<String,List<DepartureBean>>(CACHE_TIMEOUT); |
AndroidTimeoutCache<String,DepartureBean> departureCache = new AndroidTimeoutCache<String,DepartureBean>(CACHE_TIMEOUT); |
27 |
|
|
28 |
List<DepartureBean> departures; |
DepartureBean departures; |
29 |
|
|
30 |
|
|
31 |
DepartureBean tempDeparture; |
DepartureEntry tempDeparture; |
32 |
StringBuilder builder = new StringBuilder(512); |
StringBuilder builder = new StringBuilder(512); |
33 |
|
|
34 |
@Override |
@Override |
56 |
|
|
57 |
private boolean lookupDeparturesWorker(int stationID, boolean arrival) { |
private boolean lookupDeparturesWorker(int stationID, boolean arrival) { |
58 |
boolean success = false; |
boolean success = false; |
59 |
departures = new ArrayList<DepartureBean>(); |
departures = new DepartureBean(); |
60 |
try |
try |
61 |
{ |
{ |
62 |
int iArrival = arrival ? 1 : 0; |
int iArrival = arrival ? 1 : 0; |
82 |
} |
} |
83 |
|
|
84 |
@Override |
@Override |
85 |
public List<DepartureBean> getDepartures(int station,boolean arrival) { |
public DepartureBean getDepartures(int station,boolean arrival) { |
86 |
|
|
87 |
String key = "" + station + ":" + arrival; |
String key = "" + station + ":" + arrival; |
88 |
|
|
89 |
List<DepartureBean> list = departureCache.get(key); |
DepartureBean bean = departureCache.get(key); |
90 |
|
|
91 |
if (list == null) { |
if (bean == null) { |
92 |
list = new ArrayList<DepartureBean>(); |
bean = new DepartureBean(); |
93 |
} |
} |
94 |
|
|
95 |
return list; |
return bean; |
96 |
} |
} |
97 |
|
|
98 |
// this can be called several times fore the same text-node if there are many chardata / lines |
// this can be called several times fore the same text-node if there are many chardata / lines |
107 |
public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException |
public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException |
108 |
{ |
{ |
109 |
if (name.equalsIgnoreCase("train")) |
if (name.equalsIgnoreCase("train")) |
110 |
tempDeparture = new DepartureBean(); |
tempDeparture = new DepartureEntry(); |
111 |
|
|
112 |
builder.setLength(0); //reset StringBuilder |
builder.setLength(0); //reset StringBuilder |
113 |
} |
} |
115 |
@Override |
@Override |
116 |
public void endElement (String uri, String name, String qName) throws SAXException |
public void endElement (String uri, String name, String qName) throws SAXException |
117 |
{ |
{ |
118 |
if (name.equals("train")) { |
if (name.equals("train")) { |
119 |
departures.add( tempDeparture ); |
departures.entries.add( tempDeparture ); |
120 |
} else if (name.equals("time")) { |
} else if (name.equals("time")) { |
121 |
tempDeparture.setTime(builder.toString().trim()); |
tempDeparture.setTime(builder.toString().trim()); |
122 |
} else if (name.equals("updated")) { |
} else if (name.equals("updated")) { |
135 |
tempDeparture.setNote(builder.toString().trim()); |
tempDeparture.setNote(builder.toString().trim()); |
136 |
} else if (name.equals("type")) { |
} else if (name.equals("type")) { |
137 |
tempDeparture.setType(builder.toString().trim()); |
tempDeparture.setType(builder.toString().trim()); |
138 |
|
} else if (name.equals("notification")) { |
139 |
|
departures.notifications.add( builder.toString().trim() ); |
140 |
} |
} |
141 |
} |
} |
142 |
} |
} |