/[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 283 - (show annotations) (download)
Thu Aug 27 08:34:50 2009 UTC (14 years, 8 months ago) by torben
File size: 3006 byte(s)
Make downloadUtil a little more intelligent

1 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 import dk.thoerup.traininfo.util.DownloadUtil;
19
20 public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {
21
22 ArrayList<DepartureBean> departures = new ArrayList<DepartureBean>();
23
24
25 DepartureBean tempDeparture;
26 StringBuilder builder = new StringBuilder(512);
27
28 @Override
29 public void lookupDepartures(String station) {
30 departures.clear();
31 try
32 { String url = "http://t-hoerup.dk/tog/xml_display.php?stationname="+station;
33 String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1");
34
35 InputSource source = new InputSource( new StringReader(doc));
36
37 SAXParserFactory spf = SAXParserFactory.newInstance();
38 SAXParser sp = spf.newSAXParser();
39 XMLReader xr = sp.getXMLReader();
40
41 xr.setContentHandler(this);
42 xr.setErrorHandler(this);
43 xr.setDTDHandler(this);
44 xr.parse(source);
45 } catch (Exception e) {
46 Log.e("XmlDepartureProvider", "looupFunction", e);
47 }
48 }
49
50 @Override
51 public List<DepartureBean> getDepartures() {
52 return departures;
53 }
54
55 // this can be called several times fore the same text-node if there are many chardata / lines
56 @Override
57 public void characters (char ch[], int start, int length)
58 {
59 for (int i= start; i<start+length; i++)
60 builder.append(ch[i]);
61 }
62
63 @Override
64 public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException
65 {
66 if (name.equalsIgnoreCase("train"))
67 tempDeparture = new DepartureBean();
68
69 builder.setLength(0); //reset StringBuilder
70 }
71
72 @Override
73 public void endElement (String uri, String name, String qName) throws SAXException
74 {
75 if (name.equals("train")) {
76 departures.add( tempDeparture );
77 } else if (name.equals("time")) {
78 tempDeparture.setTime(builder.toString().trim());
79 } else if (name.equals("updated")) {
80 tempDeparture.setLastUpdate(builder.toString().trim());
81 } else if (name.equals("trainnumber")) {
82 tempDeparture.setTrainNumber(builder.toString().trim());
83 } else if (name.equals("destination")) {
84 tempDeparture.setDestination(builder.toString().trim());
85 } else if (name.equals("origin")) {
86 tempDeparture.setOrigin(builder.toString().trim());
87 } else if (name.equals("location")) {
88 tempDeparture.setLocation(builder.toString().trim());
89 } else if (name.equals("status")) {
90 tempDeparture.setStatus(builder.toString().trim());
91 } else if (name.equals("note")) {
92 tempDeparture.setNote(builder.toString().trim());
93 }
94 }
95 }

  ViewVC Help
Powered by ViewVC 1.1.20