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

  ViewVC Help
Powered by ViewVC 1.1.20