/[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 390 - (show annotations) (download)
Fri Oct 2 19:01:53 2009 UTC (14 years, 7 months ago) by torben
File size: 4039 byte(s)
Restructured client-side cache to a structure similar to that used in the servlets
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 class CacheEntry {
27 public long timestamp;
28 public List<DepartureBean> departures;
29 }
30
31
32 AndroidTimeoutCache<Integer,List<DepartureBean>> departureCache = new AndroidTimeoutCache<Integer,List<DepartureBean>>(CACHE_TIMEOUT);
33
34 List<DepartureBean> departures;
35
36
37 DepartureBean tempDeparture;
38 StringBuilder builder = new StringBuilder(512);
39
40 @Override
41 public boolean lookupDepartures(int stationID) {
42 boolean success;
43
44 departures = departureCache.get(stationID);
45
46 if (departures == null) {
47 success = lookupDeparturesWorker(stationID);
48
49 if (success) {
50 departureCache.put(stationID, departures);
51 }
52
53 } else {
54 Log.i("XmlDepartureProvider", "cache hit !!!");
55 success = true;
56 }
57
58 return success;
59 }
60
61 private boolean lookupDeparturesWorker(int stationID) {
62 boolean success = false;
63 departures = new ArrayList<DepartureBean>();
64 try
65 {
66
67 String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID;
68 Log.i("xmlurl",url);
69 String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1");
70
71 InputSource source = new InputSource( new StringReader(doc));
72
73 SAXParserFactory spf = SAXParserFactory.newInstance();
74 SAXParser sp = spf.newSAXParser();
75 XMLReader xr = sp.getXMLReader();
76
77 xr.setContentHandler(this);
78 xr.setErrorHandler(this);
79 xr.parse(source);
80 success = true;
81
82 } catch (Exception e) {
83 Log.e("XmlDepartureProvider", "looupFunction", e);
84 }
85 return success;
86 }
87
88 @Override
89 public List<DepartureBean> getDepartures(int station) {
90 List<DepartureBean> list = departureCache.get(station);
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 }
138 }
139 }

  ViewVC Help
Powered by ViewVC 1.1.20