/[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 1024 - (show annotations) (download)
Tue Aug 31 05:44:45 2010 UTC (13 years, 8 months ago) by torben
File size: 4243 byte(s)
use append(char[], int offset, int length) as it is more effective
1 package dk.thoerup.traininfo.provider;
2
3 import java.io.StringReader;
4
5 import javax.xml.parsers.SAXParser;
6 import javax.xml.parsers.SAXParserFactory;
7
8 import org.xml.sax.Attributes;
9 import org.xml.sax.InputSource;
10 import org.xml.sax.SAXException;
11 import org.xml.sax.XMLReader;
12 import org.xml.sax.helpers.DefaultHandler;
13
14 import android.util.Log;
15 import dk.thoerup.traininfo.DepartureBean;
16 import dk.thoerup.traininfo.DepartureEntry;
17 import dk.thoerup.traininfo.util.AndroidTimeoutCache;
18 import dk.thoerup.traininfo.util.DownloadUtil;
19 import dk.thoerup.traininfo.util.XmlUtil;
20
21 public class XmlDepartureProvider implements DepartureProvider {
22
23 final static int CACHE_TIMEOUT = 60*1000;
24
25
26 AndroidTimeoutCache<String,DepartureBean> departureCache = new AndroidTimeoutCache<String,DepartureBean>(CACHE_TIMEOUT);
27
28
29
30 DepartureEntry tempDeparture;
31 StringBuilder builder = new StringBuilder(512);
32
33
34
35 @Override
36 public DepartureBean lookupDepartures(int stationID, boolean arrival) {
37
38 String key = "" + stationID + ":" + arrival;
39
40 DepartureBean departures = departureCache.get(key);
41
42 if (departures == null) {
43 departures = lookupDeparturesWorker(stationID, arrival);
44
45 if (departures != null) {
46 departureCache.put(key, departures);
47 }
48
49 } else {
50 Log.i("XmlDepartureProvider", "cache hit !!!");
51 }
52
53 return departures;
54 }
55
56 private DepartureBean lookupDeparturesWorker(int stationID, boolean arrival) {
57
58 try
59 {
60 int iArrival = arrival ? 1 : 0;
61 String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID + "&arrival=" + iArrival;
62 Log.i("xmlurl",url);
63 String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1");
64
65 InputSource source = new InputSource( new StringReader(doc));
66
67 SAXParserFactory spf = SAXParserFactory.newInstance();
68 SAXParser sp = spf.newSAXParser();
69 XMLReader xr = sp.getXMLReader();
70
71 DepartureParser departureParser = new DepartureParser();
72 xr.setContentHandler(departureParser);
73 xr.setErrorHandler(departureParser);
74 xr.parse(source);
75
76 return departureParser.getDepartures();
77
78 } catch (Exception e) {
79 Log.e("XmlDepartureProvider", "looupFunction", e);
80 return null;
81 }
82 }
83
84
85 class DepartureParser extends DefaultHandler {
86
87 private DepartureBean departures = new DepartureBean();
88
89 public DepartureBean getDepartures() {
90 return departures;
91 }
92
93 // this can be called several times fore the same text-node if there are many chardata / lines
94 @Override
95 public void characters (char ch[], int start, int length)
96 {
97 builder.append(ch, start, length);
98 }
99
100 @Override
101 public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException
102 {
103 if (name.equalsIgnoreCase("train"))
104 tempDeparture = new DepartureEntry();
105
106 builder.setLength(0); //reset StringBuilder
107 }
108
109 @Override
110 public void endElement (String uri, String name, String qName) throws SAXException
111 {
112 if (name.equals("train")) {
113 departures.entries.add( tempDeparture );
114 } else if (name.equals("time")) {
115 tempDeparture.setTime(builder.toString().trim());
116 } else if (name.equals("updated")) {
117 tempDeparture.setLastUpdate(builder.toString().trim());
118 } else if (name.equals("trainnumber")) {
119 tempDeparture.setTrainNumber(builder.toString().trim());
120 } else if (name.equals("destination")) {
121 tempDeparture.setDestination(builder.toString().trim());
122 } else if (name.equals("origin")) {
123 tempDeparture.setOrigin(builder.toString().trim());
124 } else if (name.equals("location")) {
125 tempDeparture.setLocation(builder.toString().trim());
126 } else if (name.equals("status")) {
127 tempDeparture.setStatus(builder.toString().trim());
128 } else if (name.equals("note")) {
129 tempDeparture.setNote(builder.toString().trim());
130 } else if (name.equals("type")) {
131 tempDeparture.setType(builder.toString().trim());
132 } else if (name.equals("notification")) {
133 departures.notifications.add( builder.toString().trim() );
134 }
135 }
136 }
137 }

  ViewVC Help
Powered by ViewVC 1.1.20