/[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 699 - (hide annotations) (download)
Mon May 3 11:19:18 2010 UTC (14 years ago) by torben
File size: 4034 byte(s)
Switch to using type ("S2" vs "FJRN") for differentiating between regional trains and s-tog (corresponding change R697 on TrainInfoService)
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 391
26 torben 237
27 torben 390 AndroidTimeoutCache<Integer,List<DepartureBean>> departureCache = new AndroidTimeoutCache<Integer,List<DepartureBean>>(CACHE_TIMEOUT);
28    
29     List<DepartureBean> departures;
30 torben 319
31    
32 torben 237 DepartureBean tempDeparture;
33     StringBuilder builder = new StringBuilder(512);
34    
35     @Override
36 torben 390 public boolean lookupDepartures(int stationID) {
37 torben 319 boolean success;
38 torben 390
39     departures = departureCache.get(stationID);
40 torben 319
41 torben 390 if (departures == null) {
42 torben 319 success = lookupDeparturesWorker(stationID);
43    
44 torben 390 if (success) {
45     departureCache.put(stationID, departures);
46     }
47 torben 319
48     } else {
49     Log.i("XmlDepartureProvider", "cache hit !!!");
50     success = true;
51     }
52    
53     return success;
54     }
55    
56     private boolean lookupDeparturesWorker(int stationID) {
57     boolean success = false;
58     departures = new ArrayList<DepartureBean>();
59 torben 237 try
60 torben 294 {
61 torben 357
62 torben 358 String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID;
63 torben 294 Log.i("xmlurl",url);
64 torben 283 String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1");
65 torben 237
66     InputSource source = new InputSource( new StringReader(doc));
67    
68     SAXParserFactory spf = SAXParserFactory.newInstance();
69     SAXParser sp = spf.newSAXParser();
70     XMLReader xr = sp.getXMLReader();
71    
72     xr.setContentHandler(this);
73     xr.setErrorHandler(this);
74     xr.parse(source);
75 torben 319 success = true;
76    
77 torben 237 } catch (Exception e) {
78     Log.e("XmlDepartureProvider", "looupFunction", e);
79     }
80 torben 319 return success;
81 torben 237 }
82    
83     @Override
84 torben 319 public List<DepartureBean> getDepartures(int station) {
85 torben 390 List<DepartureBean> list = departureCache.get(station);
86 torben 319
87 torben 390 if (list == null) {
88     list = new ArrayList<DepartureBean>();
89     }
90 torben 319
91 torben 390 return list;
92 torben 237 }
93    
94     // this can be called several times fore the same text-node if there are many chardata / lines
95     @Override
96     public void characters (char ch[], int start, int length)
97     {
98     for (int i= start; i<start+length; i++)
99     builder.append(ch[i]);
100     }
101    
102     @Override
103     public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException
104     {
105     if (name.equalsIgnoreCase("train"))
106     tempDeparture = new DepartureBean();
107    
108     builder.setLength(0); //reset StringBuilder
109     }
110    
111     @Override
112     public void endElement (String uri, String name, String qName) throws SAXException
113     {
114     if (name.equals("train")) {
115     departures.add( tempDeparture );
116     } else if (name.equals("time")) {
117 torben 244 tempDeparture.setTime(builder.toString().trim());
118 torben 237 } else if (name.equals("updated")) {
119 torben 244 tempDeparture.setLastUpdate(builder.toString().trim());
120 torben 237 } else if (name.equals("trainnumber")) {
121 torben 244 tempDeparture.setTrainNumber(builder.toString().trim());
122 torben 237 } else if (name.equals("destination")) {
123 torben 244 tempDeparture.setDestination(builder.toString().trim());
124 torben 237 } else if (name.equals("origin")) {
125 torben 244 tempDeparture.setOrigin(builder.toString().trim());
126 torben 237 } else if (name.equals("location")) {
127 torben 244 tempDeparture.setLocation(builder.toString().trim());
128 torben 237 } else if (name.equals("status")) {
129 torben 244 tempDeparture.setStatus(builder.toString().trim());
130 torben 237 } else if (name.equals("note")) {
131 torben 244 tempDeparture.setNote(builder.toString().trim());
132 torben 699 } else if (name.equals("type")) {
133     tempDeparture.setType(builder.toString().trim());
134     }
135 torben 237 }
136     }

  ViewVC Help
Powered by ViewVC 1.1.20