/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlDepartureProvider.java
ViewVC logotype

Contents of /android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlDepartureProvider.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 835 - (show annotations) (download)
Fri Jun 11 14:05:24 2010 UTC (13 years, 11 months ago) by torben
File size: 4240 byte(s)
Add support for fetching arrivals as well
1 package dk.thoerup.traininfo.provider;
2
3 import java.io.StringReader;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import javax.xml.parsers.SAXParser;
8 import javax.xml.parsers.SAXParserFactory;
9
10 import org.xml.sax.Attributes;
11 import org.xml.sax.InputSource;
12 import org.xml.sax.SAXException;
13 import org.xml.sax.XMLReader;
14 import org.xml.sax.helpers.DefaultHandler;
15
16 import android.util.Log;
17 import dk.thoerup.traininfo.DepartureBean;
18 import dk.thoerup.traininfo.util.AndroidTimeoutCache;
19 import dk.thoerup.traininfo.util.DownloadUtil;
20 import dk.thoerup.traininfo.util.XmlUtil;
21
22 public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {
23
24 final static int CACHE_TIMEOUT = 60*1000;
25
26
27 AndroidTimeoutCache<String,List<DepartureBean>> departureCache = new AndroidTimeoutCache<String,List<DepartureBean>>(CACHE_TIMEOUT);
28
29 List<DepartureBean> departures;
30
31
32 DepartureBean tempDeparture;
33 StringBuilder builder = new StringBuilder(512);
34
35 @Override
36 public boolean lookupDepartures(int stationID, boolean arrival) {
37 boolean success;
38
39 String key = "" + stationID + ":" + arrival;
40
41 departures = departureCache.get(key);
42
43 if (departures == null) {
44 success = lookupDeparturesWorker(stationID, arrival);
45
46 if (success) {
47 departureCache.put(key, departures);
48 }
49
50 } else {
51 Log.i("XmlDepartureProvider", "cache hit !!!");
52 success = true;
53 }
54
55 return success;
56 }
57
58 private boolean lookupDeparturesWorker(int stationID, boolean arrival) {
59 boolean success = false;
60 departures = new ArrayList<DepartureBean>();
61 try
62 {
63 int iArrival = arrival ? 1 : 0;
64 String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID + "&arrival=" + iArrival;
65 Log.i("xmlurl",url);
66 String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1");
67
68 InputSource source = new InputSource( new StringReader(doc));
69
70 SAXParserFactory spf = SAXParserFactory.newInstance();
71 SAXParser sp = spf.newSAXParser();
72 XMLReader xr = sp.getXMLReader();
73
74 xr.setContentHandler(this);
75 xr.setErrorHandler(this);
76 xr.parse(source);
77 success = true;
78
79 } catch (Exception e) {
80 Log.e("XmlDepartureProvider", "looupFunction", e);
81 }
82 return success;
83 }
84
85 @Override
86 public List<DepartureBean> getDepartures(int station,boolean arrival) {
87
88 String key = "" + station + ":" + arrival;
89
90 List<DepartureBean> list = departureCache.get(key);
91
92 if (list == null) {
93 list = new ArrayList<DepartureBean>();
94 }
95
96 return list;
97 }
98
99 // this can be called several times fore the same text-node if there are many chardata / lines
100 @Override
101 public void characters (char ch[], int start, int length)
102 {
103 for (int i= start; i<start+length; i++)
104 builder.append(ch[i]);
105 }
106
107 @Override
108 public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException
109 {
110 if (name.equalsIgnoreCase("train"))
111 tempDeparture = new DepartureBean();
112
113 builder.setLength(0); //reset StringBuilder
114 }
115
116 @Override
117 public void endElement (String uri, String name, String qName) throws SAXException
118 {
119 if (name.equals("train")) {
120 departures.add( tempDeparture );
121 } else if (name.equals("time")) {
122 tempDeparture.setTime(builder.toString().trim());
123 } else if (name.equals("updated")) {
124 tempDeparture.setLastUpdate(builder.toString().trim());
125 } else if (name.equals("trainnumber")) {
126 tempDeparture.setTrainNumber(builder.toString().trim());
127 } else if (name.equals("destination")) {
128 tempDeparture.setDestination(builder.toString().trim());
129 } else if (name.equals("origin")) {
130 tempDeparture.setOrigin(builder.toString().trim());
131 } else if (name.equals("location")) {
132 tempDeparture.setLocation(builder.toString().trim());
133 } else if (name.equals("status")) {
134 tempDeparture.setStatus(builder.toString().trim());
135 } else if (name.equals("note")) {
136 tempDeparture.setNote(builder.toString().trim());
137 } else if (name.equals("type")) {
138 tempDeparture.setType(builder.toString().trim());
139 }
140 }
141 }

  ViewVC Help
Powered by ViewVC 1.1.20