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

  ViewVC Help
Powered by ViewVC 1.1.20