/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java
ViewVC logotype

Contents of /android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 386 - (show annotations) (download)
Fri Oct 2 13:44:31 2009 UTC (14 years, 7 months ago) by torben
File size: 2420 byte(s)
When bane.dk is pressed it needs some more time to respond
1 package dk.thoerup.traininfoservice.banedk;
2
3
4 import java.io.IOException;
5 import java.net.URL;
6 import java.util.ArrayList;
7 import java.util.List;
8 import java.util.logging.Logger;
9
10 import com.gargoylesoftware.htmlunit.*;
11 import com.gargoylesoftware.htmlunit.html.*;
12
13 public class TimetableFetcher {
14
15 class NullRefreshHandler implements RefreshHandler {
16 public void handleRefresh(Page arg0, URL arg1, int arg2) throws IOException {
17 }
18
19 }
20
21 Logger logger = Logger.getLogger(TimetableFetcher.class.getName());
22
23 List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception {
24 List<TimetableBean> timetableList = new ArrayList<TimetableBean>();
25
26 String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";
27
28
29 final WebClient webClient = new WebClient();
30 webClient.setTimeout(2500);
31 webClient.setJavaScriptEnabled(false);
32 webClient.setRefreshHandler( new NullRefreshHandler() );
33 webClient.setCssEnabled(false);
34
35
36 final HtmlPage page = webClient.getPage(url);
37
38
39 boolean currentStation = false;
40 boolean currentStationSaved = false;
41
42 List<HtmlElement> tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute");
43 if (tables.size() == 1) {
44 HtmlElement timetable = tables.get(0);
45 DomNodeList<HtmlElement> rows = timetable.getElementsByTagName("tr");
46
47 for (int i=0; i<rows.size(); i++) {
48 if (i==0) //First row is column headers
49 continue;
50
51
52 HtmlElement row = rows.get(i);
53 DomNodeList<HtmlElement> fields = row.getElementsByTagName("td");
54
55 if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) {
56 currentStation = true;
57 continue;
58 }
59
60 TimetableBean bean = new TimetableBean();
61 bean.setStation( fields.get(0).asText() );
62 bean.setArrival( fields.get(1).asText() );
63 bean.setDeparture( fields.get(2).asText() );
64
65 if (currentStation == true && currentStationSaved == false ) {
66 bean.setCurrent(currentStation);
67 currentStationSaved = true;
68 }
69
70 timetableList.add(bean);
71
72 }
73
74 } else {
75 logger.warning("No time table found, trainID=" + trainID + " type=" + type);
76 }
77
78 return timetableList;
79 }
80
81 }

  ViewVC Help
Powered by ViewVC 1.1.20