/[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 584 - (show annotations) (download)
Fri Feb 5 13:57:39 2010 UTC (14 years, 3 months ago) by torben
File size: 3808 byte(s)
Move some various parameters to web.xml
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;
30
31
32 Logger logger = Logger.getLogger(TimetableFetcher.class.getName());
33
34 private boolean useTempSite;
35
36 public TimetableFetcher(boolean tmpSite, int cacheTimeout) {
37 useTempSite = tmpSite;
38
39 cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout);
40 }
41
42
43 List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception {
44 String key = trainID+type;
45 List<TimetableBean> list = cache.get(key);
46
47 if (list == null) {
48 list = lookupTimetable(trainID,type);
49 cache.put(key, list);
50 } else {
51 logger.info("Timetable: Cache hit " + trainID);
52 }
53 return list;
54 }
55
56 List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception {
57 if (useTempSite == false ){
58 return lookupTimetableRealSite(trainID, type);
59 } else {
60 return new ArrayList<TimetableBean>(); // no timetable data on temp site
61 }
62 }
63
64 List<TimetableBean> lookupTimetableRealSite(String trainID, String type) throws Exception {
65 List<TimetableBean> timetableList = new ArrayList<TimetableBean>();
66
67 String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";
68
69
70 final WebClient webClient = new WebClient();
71 webClient.setTimeout(2500);
72 webClient.setJavaScriptEnabled(false);
73 webClient.setRefreshHandler( new NullRefreshHandler() );
74 webClient.setCssEnabled(false);
75
76
77 BanedkInvocation wrapper = new BanedkInvocation(webClient, url);
78 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
79
80 HtmlPage page = (HtmlPage) breaker.invoke(wrapper);
81
82
83 boolean currentStation = false;
84 boolean currentStationSaved = false;
85
86 List<HtmlElement> tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute");
87 if (tables.size() == 1) {
88 HtmlElement timetable = tables.get(0);
89 DomNodeList<HtmlElement> rows = timetable.getElementsByTagName("tr");
90
91 for (int i=0; i<rows.size(); i++) {
92 if (i==0) //First row is column headers
93 continue;
94
95
96 HtmlElement row = rows.get(i);
97 DomNodeList<HtmlElement> fields = row.getElementsByTagName("td");
98
99 if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) {
100 currentStation = true;
101 continue;
102 }
103
104 TimetableBean bean = new TimetableBean();
105 bean.setStation( fields.get(0).asText() );
106 bean.setArrival( fields.get(1).asText() );
107 bean.setDeparture( fields.get(2).asText() );
108
109 if (currentStation == true && currentStationSaved == false ) {
110 bean.setCurrent(currentStation);
111 currentStationSaved = true;
112 }
113
114 timetableList.add(bean);
115
116 }
117
118 } else {
119 logger.warning("No time table found, trainID=" + trainID + " type=" + type);
120 }
121
122 return timetableList;
123 }
124
125 }

  ViewVC Help
Powered by ViewVC 1.1.20