/[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 390 - (hide 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 torben 237 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 torben 390 import dk.thoerup.traininfo.util.AndroidTimeoutCache;
19 torben 238 import dk.thoerup.traininfo.util.DownloadUtil;
20 torben 357 import dk.thoerup.traininfo.util.XmlUtil;
21 torben 237
22     public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {
23    
24 torben 390 final static int 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 390
32     AndroidTimeoutCache<Integer,List<DepartureBean>> departureCache = new AndroidTimeoutCache<Integer,List<DepartureBean>>(CACHE_TIMEOUT);
33    
34     List<DepartureBean> departures;
35 torben 319
36    
37 torben 237 DepartureBean tempDeparture;
38     StringBuilder builder = new StringBuilder(512);
39    
40     @Override
41 torben 390 public boolean lookupDepartures(int stationID) {
42 torben 319 boolean success;
43 torben 390
44     departures = departureCache.get(stationID);
45 torben 319
46 torben 390 if (departures == null) {
47 torben 319 success = lookupDeparturesWorker(stationID);
48    
49 torben 390 if (success) {
50     departureCache.put(stationID, departures);
51     }
52 torben 319
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 torben 237 try
65 torben 294 {
66 torben 357
67 torben 358 String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID;
68 torben 294 Log.i("xmlurl",url);
69 torben 283 String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1");
70 torben 237
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 torben 319 success = true;
81    
82 torben 237 } catch (Exception e) {
83     Log.e("XmlDepartureProvider", "looupFunction", e);
84     }
85 torben 319 return success;
86 torben 237 }
87    
88     @Override
89 torben 319 public List<DepartureBean> getDepartures(int station) {
90 torben 390 List<DepartureBean> list = departureCache.get(station);
91 torben 319
92 torben 390 if (list == null) {
93     list = new ArrayList<DepartureBean>();
94     }
95 torben 319
96 torben 390 return list;
97 torben 237 }
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 torben 244 tempDeparture.setTime(builder.toString().trim());
123 torben 237 } else if (name.equals("updated")) {
124 torben 244 tempDeparture.setLastUpdate(builder.toString().trim());
125 torben 237 } else if (name.equals("trainnumber")) {
126 torben 244 tempDeparture.setTrainNumber(builder.toString().trim());
127 torben 237 } else if (name.equals("destination")) {
128 torben 244 tempDeparture.setDestination(builder.toString().trim());
129 torben 237 } else if (name.equals("origin")) {
130 torben 244 tempDeparture.setOrigin(builder.toString().trim());
131 torben 237 } else if (name.equals("location")) {
132 torben 244 tempDeparture.setLocation(builder.toString().trim());
133 torben 237 } else if (name.equals("status")) {
134 torben 244 tempDeparture.setStatus(builder.toString().trim());
135 torben 237 } else if (name.equals("note")) {
136 torben 244 tempDeparture.setNote(builder.toString().trim());
137 torben 237 }
138     }
139     }

  ViewVC Help
Powered by ViewVC 1.1.20