1 |
package dk.thoerup.traininfo.provider; |
package dk.thoerup.traininfo.provider; |
2 |
|
|
|
import java.io.StringReader; |
|
3 |
|
|
|
import javax.xml.parsers.SAXParser; |
|
|
import javax.xml.parsers.SAXParserFactory; |
|
4 |
|
|
5 |
import org.xml.sax.Attributes; |
import org.simpleframework.xml.Serializer; |
6 |
import org.xml.sax.InputSource; |
import org.simpleframework.xml.core.Persister; |
7 |
import org.xml.sax.SAXException; |
|
|
import org.xml.sax.XMLReader; |
|
|
import org.xml.sax.helpers.DefaultHandler; |
|
8 |
|
|
9 |
import android.util.Log; |
import android.util.Log; |
10 |
import dk.thoerup.traininfo.DepartureBean; |
import dk.thoerup.android.traininfo.common.DepartureBean; |
|
import dk.thoerup.traininfo.DepartureEntry; |
|
11 |
import dk.thoerup.traininfo.util.AndroidTimeoutCache; |
import dk.thoerup.traininfo.util.AndroidTimeoutCache; |
12 |
import dk.thoerup.traininfo.util.DownloadUtil; |
import dk.thoerup.traininfo.util.DownloadUtil; |
13 |
import dk.thoerup.traininfo.util.XmlUtil; |
import dk.thoerup.traininfo.util.XmlUtil; |
21 |
|
|
22 |
|
|
23 |
|
|
|
DepartureEntry tempDeparture; |
|
|
StringBuilder builder = new StringBuilder(512); |
|
|
|
|
|
|
|
|
|
|
24 |
@Override |
@Override |
25 |
public DepartureBean lookupDepartures(int stationID, boolean arrival) { |
public DepartureBean lookupDepartures(int stationID, boolean arrival,String type) { |
26 |
|
|
27 |
String key = "" + stationID + ":" + arrival; |
String key = "" + stationID + ":" + arrival + ":" + type; |
28 |
|
|
29 |
DepartureBean departures = departureCache.get(key); |
DepartureBean departures = departureCache.get(key); |
30 |
|
|
31 |
if (departures == null) { |
if (departures == null) { |
32 |
departures = lookupDeparturesWorker(stationID, arrival); |
departures = lookupDeparturesWorker(stationID, arrival, type); |
33 |
|
|
34 |
if (departures != null) { |
if (departures != null) { |
35 |
departureCache.put(key, departures); |
departureCache.put(key, departures); |
42 |
return departures; |
return departures; |
43 |
} |
} |
44 |
|
|
45 |
private DepartureBean lookupDeparturesWorker(int stationID, boolean arrival) { |
private DepartureBean lookupDeparturesWorker(int stationID, boolean arrival, String type) { |
46 |
|
|
47 |
try |
try |
48 |
{ |
{ |
49 |
int iArrival = arrival ? 1 : 0; |
int iArrival = arrival ? 1 : 0; |
50 |
String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID + "&arrival=" + iArrival; |
String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID + "&arrival=" + iArrival + "&type=" + type; |
51 |
Log.i("xmlurl",url); |
Log.i("xmlurl",url); |
52 |
String doc = DownloadUtil.getContentString(url, 30000, "ISO-8859-1"); |
String doc = DownloadUtil.getContentString(url, 30000, "ISO-8859-1"); |
53 |
|
|
54 |
|
Serializer serializer = new Persister(); |
55 |
|
|
56 |
|
DepartureBean departures = serializer.read(DepartureBean.class, doc); |
57 |
|
|
|
InputSource source = new InputSource( new StringReader(doc)); |
|
58 |
|
|
59 |
SAXParserFactory spf = SAXParserFactory.newInstance(); |
return departures; |
|
SAXParser sp = spf.newSAXParser(); |
|
|
XMLReader xr = sp.getXMLReader(); |
|
|
|
|
|
DepartureParser departureParser = new DepartureParser(); |
|
|
xr.setContentHandler(departureParser); |
|
|
xr.setErrorHandler(departureParser); |
|
|
xr.parse(source); |
|
60 |
|
|
|
return departureParser.getDepartures(); |
|
61 |
|
|
62 |
} catch (Exception e) { |
} catch (Exception e) { |
63 |
Log.e("XmlDepartureProvider", "looupFunction", e); |
Log.e("XmlDepartureProvider", "looupFunction", e); |
64 |
return null; |
return null; |
65 |
} |
} |
66 |
} |
} |
|
|
|
|
|
|
|
class DepartureParser extends DefaultHandler { |
|
|
|
|
|
private DepartureBean departures = new DepartureBean(); |
|
67 |
|
|
68 |
public DepartureBean getDepartures() { |
@Override |
69 |
return departures; |
public void purgeOldEntries() { |
70 |
} |
departureCache.purgeOldEntries(); |
|
|
|
|
// this can be called several times fore the same text-node if there are many chardata / lines |
|
|
@Override |
|
|
public void characters (char ch[], int start, int length) |
|
|
{ |
|
|
builder.append(ch, start, length); |
|
|
} |
|
|
|
|
|
@Override |
|
|
public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException |
|
|
{ |
|
|
if (name.equalsIgnoreCase("train")) |
|
|
tempDeparture = new DepartureEntry(); |
|
|
|
|
|
builder.setLength(0); //reset StringBuilder |
|
|
} |
|
|
|
|
|
@Override |
|
|
public void endElement (String uri, String name, String qName) throws SAXException |
|
|
{ |
|
|
if (name.equals("train")) { |
|
|
departures.entries.add( tempDeparture ); |
|
|
} else if (name.equals("time")) { |
|
|
tempDeparture.setTime(builder.toString().trim()); |
|
|
} else if (name.equals("updated")) { |
|
|
tempDeparture.setLastUpdate(builder.toString().trim()); |
|
|
} else if (name.equals("trainnumber")) { |
|
|
tempDeparture.setTrainNumber(builder.toString().trim()); |
|
|
} else if (name.equals("destination")) { |
|
|
tempDeparture.setDestination(builder.toString().trim()); |
|
|
} else if (name.equals("origin")) { |
|
|
tempDeparture.setOrigin(builder.toString().trim()); |
|
|
} else if (name.equals("location")) { |
|
|
tempDeparture.setLocation(builder.toString().trim()); |
|
|
} else if (name.equals("status")) { |
|
|
tempDeparture.setStatus(builder.toString().trim()); |
|
|
} else if (name.equals("note")) { |
|
|
tempDeparture.setNote(builder.toString().trim()); |
|
|
} else if (name.equals("type")) { |
|
|
tempDeparture.setType(builder.toString().trim()); |
|
|
} else if (name.equals("notification")) { |
|
|
departures.notifications.add( builder.toString().trim() ); |
|
|
} |
|
|
} |
|
71 |
} |
} |
72 |
|
|
73 |
} |
} |