/[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 581 - (show annotations) (download)
Tue Feb 2 18:47:55 2010 UTC (14 years, 3 months ago) by torben
File size: 3775 byte(s)
Also make timetable degrade gracefully while bane.dk's temporary site is in place
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.Map;
9 import java.util.logging.Logger;
10
11 import com.gargoylesoftware.htmlunit.Page;
12 import com.gargoylesoftware.htmlunit.RefreshHandler;
13 import com.gargoylesoftware.htmlunit.WebClient;
14 import com.gargoylesoftware.htmlunit.html.DomNodeList;
15 import com.gargoylesoftware.htmlunit.html.HtmlElement;
16 import com.gargoylesoftware.htmlunit.html.HtmlPage;
17
18 import dk.thoerup.circuitbreaker.CircuitBreaker;
19 import dk.thoerup.circuitbreaker.CircuitBreakerManager;
20
21 public class TimetableFetcher {
22
23 class NullRefreshHandler implements RefreshHandler {
24 public void handleRefresh(Page arg0, URL arg1, int arg2) throws IOException {
25 }
26
27 }
28
29 Map<String, List<TimetableBean>> cache = new TimeoutMap<String,List<TimetableBean>>(120 * 1000);
30
31
32 Logger logger = Logger.getLogger(TimetableFetcher.class.getName());
33
34 private boolean useTempSite;
35
36 public TimetableFetcher(boolean tmpSite) {
37 useTempSite = tmpSite;
38 }
39
40
41 List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception {
42 String key = trainID+type;
43 List<TimetableBean> list = cache.get(key);
44
45 if (list == null) {
46 list = lookupTimetable(trainID,type);
47 cache.put(key, list);
48 } else {
49 logger.info("Timetable: Cache hit " + trainID);
50 }
51 return list;
52 }
53
54 List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception {
55 if (useTempSite == false ){
56 return lookupTimetableRealSite(trainID, type);
57 } else {
58 return new ArrayList<TimetableBean>(); // no timetable data on temp site
59 }
60 }
61
62 List<TimetableBean> lookupTimetableRealSite(String trainID, String type) throws Exception {
63 List<TimetableBean> timetableList = new ArrayList<TimetableBean>();
64
65 String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";
66
67
68 final WebClient webClient = new WebClient();
69 webClient.setTimeout(2500);
70 webClient.setJavaScriptEnabled(false);
71 webClient.setRefreshHandler( new NullRefreshHandler() );
72 webClient.setCssEnabled(false);
73
74
75 BanedkInvocation wrapper = new BanedkInvocation(webClient, url);
76 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
77
78 HtmlPage page = (HtmlPage) breaker.invoke(wrapper);
79
80
81 boolean currentStation = false;
82 boolean currentStationSaved = false;
83
84 List<HtmlElement> tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute");
85 if (tables.size() == 1) {
86 HtmlElement timetable = tables.get(0);
87 DomNodeList<HtmlElement> rows = timetable.getElementsByTagName("tr");
88
89 for (int i=0; i<rows.size(); i++) {
90 if (i==0) //First row is column headers
91 continue;
92
93
94 HtmlElement row = rows.get(i);
95 DomNodeList<HtmlElement> fields = row.getElementsByTagName("td");
96
97 if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) {
98 currentStation = true;
99 continue;
100 }
101
102 TimetableBean bean = new TimetableBean();
103 bean.setStation( fields.get(0).asText() );
104 bean.setArrival( fields.get(1).asText() );
105 bean.setDeparture( fields.get(2).asText() );
106
107 if (currentStation == true && currentStationSaved == false ) {
108 bean.setCurrent(currentStation);
109 currentStationSaved = true;
110 }
111
112 timetableList.add(bean);
113
114 }
115
116 } else {
117 logger.warning("No time table found, trainID=" + trainID + " type=" + type);
118 }
119
120 return timetableList;
121 }
122
123 }

  ViewVC Help
Powered by ViewVC 1.1.20