/[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 357 - (hide annotations) (download)
Tue Sep 29 19:06:34 2009 UTC (14 years, 8 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 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 357 import dk.thoerup.traininfo.util.XmlUtil;
22 torben 237
23     public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {
24    
25 torben 319 final static long CACHE_TIMEOUT = 60*1000;
26 torben 237
27 torben 319 class CacheEntry {
28     public long timestamp;
29     public List<DepartureBean> departures;
30     }
31 torben 237
32 torben 319 HashMap<Integer, CacheEntry> departureCache = new HashMap<Integer,CacheEntry>();
33     ArrayList<DepartureBean> departures;
34    
35    
36 torben 237 DepartureBean tempDeparture;
37     StringBuilder builder = new StringBuilder(512);
38    
39     @Override
40 torben 319 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 torben 237 try
68 torben 294 {
69 torben 357
70     String url = XmlUtil.SERVICE_BASE + "/TrainInfoService/DepartureServlet?format=xml&station=" + stationID;
71 torben 294 Log.i("xmlurl",url);
72 torben 283 String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1");
73 torben 237
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 torben 319 success = true;
84    
85 torben 237 } catch (Exception e) {
86     Log.e("XmlDepartureProvider", "looupFunction", e);
87     }
88 torben 319 return success;
89 torben 237 }
90    
91     @Override
92 torben 319 public List<DepartureBean> getDepartures(int station) {
93     CacheEntry entry = departureCache.get(station);
94    
95     if (entry != null) {
96 torben 320 return entry.departures;
97 torben 319 } else {
98     return new ArrayList<DepartureBean>();
99     }
100    
101 torben 237 }
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 torben 244 tempDeparture.setTime(builder.toString().trim());
127 torben 237 } else if (name.equals("updated")) {
128 torben 244 tempDeparture.setLastUpdate(builder.toString().trim());
129 torben 237 } else if (name.equals("trainnumber")) {
130 torben 244 tempDeparture.setTrainNumber(builder.toString().trim());
131 torben 237 } else if (name.equals("destination")) {
132 torben 244 tempDeparture.setDestination(builder.toString().trim());
133 torben 237 } else if (name.equals("origin")) {
134 torben 244 tempDeparture.setOrigin(builder.toString().trim());
135 torben 237 } else if (name.equals("location")) {
136 torben 244 tempDeparture.setLocation(builder.toString().trim());
137 torben 237 } else if (name.equals("status")) {
138 torben 244 tempDeparture.setStatus(builder.toString().trim());
139 torben 237 } else if (name.equals("note")) {
140 torben 244 tempDeparture.setNote(builder.toString().trim());
141 torben 237 }
142     }
143     }

  ViewVC Help
Powered by ViewVC 1.1.20