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

  ViewVC Help
Powered by ViewVC 1.1.20