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

  ViewVC Help
Powered by ViewVC 1.1.20