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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 389 - (hide annotations) (download)
Fri Oct 2 17:18:31 2009 UTC (14 years, 7 months ago) by torben
File size: 3102 byte(s)
Don't log on cache miss - and loglevel=info should be sufficient for cache hit messages
1 torben 350 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 torben 387 import com.gargoylesoftware.htmlunit.Page;
11     import com.gargoylesoftware.htmlunit.RefreshHandler;
12     import com.gargoylesoftware.htmlunit.WebClient;
13     import com.gargoylesoftware.htmlunit.html.DomNodeList;
14     import com.gargoylesoftware.htmlunit.html.HtmlElement;
15     import com.gargoylesoftware.htmlunit.html.HtmlPage;
16 torben 350
17     public class TimetableFetcher {
18    
19     class NullRefreshHandler implements RefreshHandler {
20     public void handleRefresh(Page arg0, URL arg1, int arg2) throws IOException {
21     }
22    
23     }
24 torben 387
25     TimeoutCache<String, List<TimetableBean>> cache = new TimeoutCache<String,List<TimetableBean>>(120 * 1000);
26 torben 350
27 torben 387
28 torben 350 Logger logger = Logger.getLogger(TimetableFetcher.class.getName());
29 torben 387
30    
31     List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception {
32     String key = trainID+type;
33     List<TimetableBean> list = cache.get(key);
34    
35     if (list == null) {
36     list = lookupTimetable(trainID,type);
37     cache.put(key, list);
38     } else {
39 torben 389 logger.info("Timetable: Cache hit " + trainID);
40 torben 387 }
41     return list;
42     }
43 torben 350
44 torben 387 List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception {
45 torben 350 List<TimetableBean> timetableList = new ArrayList<TimetableBean>();
46    
47     String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";
48    
49    
50     final WebClient webClient = new WebClient();
51 torben 386 webClient.setTimeout(2500);
52 torben 350 webClient.setJavaScriptEnabled(false);
53     webClient.setRefreshHandler( new NullRefreshHandler() );
54     webClient.setCssEnabled(false);
55    
56    
57     final HtmlPage page = webClient.getPage(url);
58    
59    
60     boolean currentStation = false;
61     boolean currentStationSaved = false;
62    
63     List<HtmlElement> tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute");
64     if (tables.size() == 1) {
65     HtmlElement timetable = tables.get(0);
66     DomNodeList<HtmlElement> rows = timetable.getElementsByTagName("tr");
67    
68     for (int i=0; i<rows.size(); i++) {
69     if (i==0) //First row is column headers
70     continue;
71    
72    
73     HtmlElement row = rows.get(i);
74     DomNodeList<HtmlElement> fields = row.getElementsByTagName("td");
75    
76     if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) {
77     currentStation = true;
78     continue;
79     }
80    
81     TimetableBean bean = new TimetableBean();
82     bean.setStation( fields.get(0).asText() );
83     bean.setArrival( fields.get(1).asText() );
84     bean.setDeparture( fields.get(2).asText() );
85    
86     if (currentStation == true && currentStationSaved == false ) {
87     bean.setCurrent(currentStation);
88     currentStationSaved = true;
89     }
90    
91     timetableList.add(bean);
92    
93     }
94    
95     } else {
96     logger.warning("No time table found, trainID=" + trainID + " type=" + type);
97     }
98    
99     return timetableList;
100     }
101    
102     }

  ViewVC Help
Powered by ViewVC 1.1.20