/[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 428 - (show annotations) (download)
Fri Oct 9 08:52:37 2009 UTC (14 years, 7 months ago) by torben
File size: 3400 byte(s)
Refactor TimeoutCache into a real Map
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.curcuitbreaker.CircuitBreaker;
19 import dk.thoerup.curcuitbreaker.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
35 List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Throwable {
36 String key = trainID+type;
37 List<TimetableBean> list = cache.get(key);
38
39 if (list == null) {
40 list = lookupTimetable(trainID,type);
41 cache.put(key, list);
42 } else {
43 logger.info("Timetable: Cache hit " + trainID);
44 }
45 return list;
46 }
47
48 List<TimetableBean> lookupTimetable(String trainID, String type) throws Throwable {
49 List<TimetableBean> timetableList = new ArrayList<TimetableBean>();
50
51 String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";
52
53
54 final WebClient webClient = new WebClient();
55 webClient.setTimeout(2500);
56 webClient.setJavaScriptEnabled(false);
57 webClient.setRefreshHandler( new NullRefreshHandler() );
58 webClient.setCssEnabled(false);
59
60
61 BanedkInvocation wrapper = new BanedkInvocation(webClient, url);
62 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
63
64 HtmlPage page = (HtmlPage) breaker.invoke(wrapper);
65
66
67 boolean currentStation = false;
68 boolean currentStationSaved = false;
69
70 List<HtmlElement> tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute");
71 if (tables.size() == 1) {
72 HtmlElement timetable = tables.get(0);
73 DomNodeList<HtmlElement> rows = timetable.getElementsByTagName("tr");
74
75 for (int i=0; i<rows.size(); i++) {
76 if (i==0) //First row is column headers
77 continue;
78
79
80 HtmlElement row = rows.get(i);
81 DomNodeList<HtmlElement> fields = row.getElementsByTagName("td");
82
83 if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) {
84 currentStation = true;
85 continue;
86 }
87
88 TimetableBean bean = new TimetableBean();
89 bean.setStation( fields.get(0).asText() );
90 bean.setArrival( fields.get(1).asText() );
91 bean.setDeparture( fields.get(2).asText() );
92
93 if (currentStation == true && currentStationSaved == false ) {
94 bean.setCurrent(currentStation);
95 currentStationSaved = true;
96 }
97
98 timetableList.add(bean);
99
100 }
101
102 } else {
103 logger.warning("No time table found, trainID=" + trainID + " type=" + type);
104 }
105
106 return timetableList;
107 }
108
109 }

  ViewVC Help
Powered by ViewVC 1.1.20