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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 319 - (hide annotations) (download)
Fri Sep 11 12:24:53 2009 UTC (14 years, 8 months ago) by torben
File size: 4310 byte(s)
Provider's : differentiate between an empty returned list or a communication error

Re-organize some of the background work
1 torben 237 package dk.thoerup.traininfo.provider;
2    
3     import java.io.StringReader;
4     import java.util.ArrayList;
5 torben 319 import java.util.Collections;
6     import java.util.HashMap;
7 torben 237 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 torben 238 import dk.thoerup.traininfo.util.DownloadUtil;
21 torben 237
22     public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {
23    
24 torben 319 final static long CACHE_TIMEOUT = 60*1000;
25 torben 237
26 torben 319 class CacheEntry {
27     public long timestamp;
28     public List<DepartureBean> departures;
29     }
30 torben 237
31 torben 319 HashMap<Integer, CacheEntry> departureCache = new HashMap<Integer,CacheEntry>();
32     ArrayList<DepartureBean> departures;
33    
34    
35 torben 237 DepartureBean tempDeparture;
36     StringBuilder builder = new StringBuilder(512);
37    
38     @Override
39 torben 319 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 torben 237 try
67 torben 294 {
68 torben 310 //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 torben 294 Log.i("xmlurl",url);
71 torben 283 String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1");
72 torben 237
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 torben 319 success = true;
83    
84 torben 237 } catch (Exception e) {
85     Log.e("XmlDepartureProvider", "looupFunction", e);
86     }
87 torben 319 return success;
88 torben 237 }
89    
90     @Override
91 torben 319 public List<DepartureBean> getDepartures(int station) {
92     CacheEntry entry = departureCache.get(station);
93    
94     if (entry != null) {
95     return Collections.unmodifiableList(entry.departures);
96     } else {
97     return new ArrayList<DepartureBean>();
98     }
99    
100 torben 237 }
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 torben 244 tempDeparture.setTime(builder.toString().trim());
126 torben 237 } else if (name.equals("updated")) {
127 torben 244 tempDeparture.setLastUpdate(builder.toString().trim());
128 torben 237 } else if (name.equals("trainnumber")) {
129 torben 244 tempDeparture.setTrainNumber(builder.toString().trim());
130 torben 237 } else if (name.equals("destination")) {
131 torben 244 tempDeparture.setDestination(builder.toString().trim());
132 torben 237 } else if (name.equals("origin")) {
133 torben 244 tempDeparture.setOrigin(builder.toString().trim());
134 torben 237 } else if (name.equals("location")) {
135 torben 244 tempDeparture.setLocation(builder.toString().trim());
136 torben 237 } else if (name.equals("status")) {
137 torben 244 tempDeparture.setStatus(builder.toString().trim());
138 torben 237 } else if (name.equals("note")) {
139 torben 244 tempDeparture.setNote(builder.toString().trim());
140 torben 237 }
141     }
142     }

  ViewVC Help
Powered by ViewVC 1.1.20