1 |
torben |
237 |
package dk.thoerup.traininfo.provider;
|
2 |
|
|
|
3 |
|
|
import java.io.BufferedReader;
|
4 |
|
|
import java.io.IOException;
|
5 |
|
|
import java.io.InputStream;
|
6 |
|
|
import java.io.InputStreamReader;
|
7 |
|
|
import java.io.StringReader;
|
8 |
|
|
import java.net.URL;
|
9 |
|
|
import java.net.URLConnection;
|
10 |
|
|
import java.util.ArrayList;
|
11 |
|
|
import java.util.List;
|
12 |
|
|
|
13 |
|
|
import javax.xml.parsers.SAXParser;
|
14 |
|
|
import javax.xml.parsers.SAXParserFactory;
|
15 |
|
|
|
16 |
|
|
import org.xml.sax.Attributes;
|
17 |
|
|
import org.xml.sax.InputSource;
|
18 |
|
|
import org.xml.sax.SAXException;
|
19 |
|
|
import org.xml.sax.XMLReader;
|
20 |
|
|
import org.xml.sax.helpers.DefaultHandler;
|
21 |
|
|
|
22 |
|
|
import android.util.Log;
|
23 |
|
|
import dk.thoerup.traininfo.DepartureBean;
|
24 |
|
|
|
25 |
|
|
public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {
|
26 |
|
|
|
27 |
|
|
ArrayList<DepartureBean> departures = new ArrayList<DepartureBean>();
|
28 |
|
|
|
29 |
|
|
|
30 |
|
|
DepartureBean tempDeparture;
|
31 |
|
|
StringBuilder builder = new StringBuilder(512);
|
32 |
|
|
|
33 |
|
|
@Override
|
34 |
|
|
public void lookupDepartures(String station) {
|
35 |
|
|
departures.clear();
|
36 |
|
|
try
|
37 |
|
|
{
|
38 |
|
|
String doc = getUrlContents("http://t-hoerup.dk/tog/xml_display.php?stationname="+station);
|
39 |
|
|
|
40 |
|
|
InputSource source = new InputSource( new StringReader(doc));
|
41 |
|
|
|
42 |
|
|
SAXParserFactory spf = SAXParserFactory.newInstance();
|
43 |
|
|
SAXParser sp = spf.newSAXParser();
|
44 |
|
|
XMLReader xr = sp.getXMLReader();
|
45 |
|
|
|
46 |
|
|
xr.setContentHandler(this);
|
47 |
|
|
xr.setErrorHandler(this);
|
48 |
|
|
xr.setDTDHandler(this);
|
49 |
|
|
xr.parse(source);
|
50 |
|
|
} catch (Exception e) {
|
51 |
|
|
Log.e("XmlDepartureProvider", "looupFunction", e);
|
52 |
|
|
}
|
53 |
|
|
}
|
54 |
|
|
|
55 |
|
|
@Override
|
56 |
|
|
public List<DepartureBean> getDepartures() {
|
57 |
|
|
return departures;
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
private String getUrlContents(String uri) throws IOException
|
61 |
|
|
{
|
62 |
|
|
URL url = new URL(uri);
|
63 |
|
|
URLConnection conn = url.openConnection();
|
64 |
|
|
conn.setConnectTimeout(5000);
|
65 |
|
|
InputStream stream = conn.getInputStream();
|
66 |
|
|
|
67 |
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(stream, "ISO-8859-1"),8192);
|
68 |
|
|
|
69 |
|
|
StringBuilder sbuilder = new StringBuilder();
|
70 |
|
|
|
71 |
|
|
String line;
|
72 |
|
|
while ( (line = in.readLine()) != null) {
|
73 |
|
|
sbuilder.append(line);
|
74 |
|
|
sbuilder.append("\r\n");
|
75 |
|
|
}
|
76 |
|
|
|
77 |
|
|
return sbuilder.toString();
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
// this can be called several times fore the same text-node if there are many chardata / lines
|
81 |
|
|
@Override
|
82 |
|
|
public void characters (char ch[], int start, int length)
|
83 |
|
|
{
|
84 |
|
|
for (int i= start; i<start+length; i++)
|
85 |
|
|
builder.append(ch[i]);
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
@Override
|
89 |
|
|
public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException
|
90 |
|
|
{
|
91 |
|
|
if (name.equalsIgnoreCase("train"))
|
92 |
|
|
tempDeparture = new DepartureBean();
|
93 |
|
|
|
94 |
|
|
builder.setLength(0); //reset StringBuilder
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
@Override
|
98 |
|
|
public void endElement (String uri, String name, String qName) throws SAXException
|
99 |
|
|
{
|
100 |
|
|
if (name.equals("train")) {
|
101 |
|
|
departures.add( tempDeparture );
|
102 |
|
|
} else if (name.equals("time")) {
|
103 |
|
|
tempDeparture.setTime(builder.toString());
|
104 |
|
|
} else if (name.equals("updated")) {
|
105 |
|
|
tempDeparture.setLastUpdate(builder.toString());
|
106 |
|
|
} else if (name.equals("trainnumber")) {
|
107 |
|
|
tempDeparture.setTrainNumber(builder.toString());
|
108 |
|
|
} else if (name.equals("destination")) {
|
109 |
|
|
tempDeparture.setDestination(builder.toString());
|
110 |
|
|
} else if (name.equals("origin")) {
|
111 |
|
|
tempDeparture.setOrigin(builder.toString());
|
112 |
|
|
} else if (name.equals("location")) {
|
113 |
|
|
tempDeparture.setLocation(builder.toString());
|
114 |
|
|
} else if (name.equals("status")) {
|
115 |
|
|
tempDeparture.setStatus(builder.toString());
|
116 |
|
|
} else if (name.equals("note")) {
|
117 |
|
|
tempDeparture.setNote(builder.toString());
|
118 |
|
|
}
|
119 |
|
|
}
|
120 |
|
|
}
|