/[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 310 - (show annotations) (download)
Thu Sep 10 19:09:09 2009 UTC (14 years, 8 months ago) by torben
File size: 3150 byte(s)
Make code work with new servlets (and new db layout)
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(int stationID) {
30 departures.clear();
31 try
32 {
33 //String url = "http://t-hoerup.dk/tog/xml_display.php?stationcode="+stationCode;
34 String url = "http://app.t-hoerup.dk/TrainInfoService/DepartureServlet?format=xml&station=" + stationID;
35 Log.i("xmlurl",url);
36 String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1");
37
38 InputSource source = new InputSource( new StringReader(doc));
39
40 SAXParserFactory spf = SAXParserFactory.newInstance();
41 SAXParser sp = spf.newSAXParser();
42 XMLReader xr = sp.getXMLReader();
43
44 xr.setContentHandler(this);
45 xr.setErrorHandler(this);
46 xr.setDTDHandler(this);
47 xr.parse(source);
48 } catch (Exception e) {
49 Log.e("XmlDepartureProvider", "looupFunction", e);
50 }
51 }
52
53 @Override
54 public List<DepartureBean> getDepartures() {
55 return departures;
56 }
57
58 // this can be called several times fore the same text-node if there are many chardata / lines
59 @Override
60 public void characters (char ch[], int start, int length)
61 {
62 for (int i= start; i<start+length; i++)
63 builder.append(ch[i]);
64 }
65
66 @Override
67 public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException
68 {
69 if (name.equalsIgnoreCase("train"))
70 tempDeparture = new DepartureBean();
71
72 builder.setLength(0); //reset StringBuilder
73 }
74
75 @Override
76 public void endElement (String uri, String name, String qName) throws SAXException
77 {
78 if (name.equals("train")) {
79 departures.add( tempDeparture );
80 } else if (name.equals("time")) {
81 tempDeparture.setTime(builder.toString().trim());
82 } else if (name.equals("updated")) {
83 tempDeparture.setLastUpdate(builder.toString().trim());
84 } else if (name.equals("trainnumber")) {
85 tempDeparture.setTrainNumber(builder.toString().trim());
86 } else if (name.equals("destination")) {
87 tempDeparture.setDestination(builder.toString().trim());
88 } else if (name.equals("origin")) {
89 tempDeparture.setOrigin(builder.toString().trim());
90 } else if (name.equals("location")) {
91 tempDeparture.setLocation(builder.toString().trim());
92 } else if (name.equals("status")) {
93 tempDeparture.setStatus(builder.toString().trim());
94 } else if (name.equals("note")) {
95 tempDeparture.setNote(builder.toString().trim());
96 }
97 }
98 }

  ViewVC Help
Powered by ViewVC 1.1.20