--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2010/07/10 09:57:04 977 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2010/07/14 08:05:31 992 @@ -1,7 +1,7 @@ package dk.thoerup.traininfoservice.banedk; -import java.io.IOException; + import java.net.URL; import java.sql.SQLException; import java.util.ArrayList; @@ -10,13 +10,9 @@ import java.util.logging.Level; import java.util.logging.Logger; -import com.gargoylesoftware.htmlunit.BrowserVersion; -import com.gargoylesoftware.htmlunit.Page; -import com.gargoylesoftware.htmlunit.RefreshHandler; -import com.gargoylesoftware.htmlunit.WebClient; -import com.gargoylesoftware.htmlunit.html.DomNodeList; -import com.gargoylesoftware.htmlunit.html.HtmlElement; -import com.gargoylesoftware.htmlunit.html.HtmlPage; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; import dk.thoerup.circuitbreaker.CircuitBreaker; import dk.thoerup.circuitbreaker.CircuitBreakerManager; @@ -24,12 +20,7 @@ import dk.thoerup.traininfoservice.Statistics; public class TimetableFetcher { - - class NullRefreshHandler implements RefreshHandler { - public void handleRefresh(Page arg0, URL arg1, int arg2) throws IOException { - } - - } + Map> cache; Map stationCache; @@ -93,51 +84,47 @@ //String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; - final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3); - webClient.setTimeout(2500); - webClient.setJavaScriptEnabled(false); - webClient.setRefreshHandler( new NullRefreshHandler() ); - webClient.setCssEnabled(false); - - HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, url); + JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , 2500); CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); - HtmlPage page = (HtmlPage) breaker.invoke(wrapper); + Document doc = (Document) breaker.invoke(wrapper); boolean currentStation = false; boolean currentStationSaved = false; - List tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute"); + Elements tables = doc.getElementsByClass("Rute"); + if (tables.size() == 1) { - HtmlElement timetable = tables.get(0); - DomNodeList rows = timetable.getElementsByTagName("tr"); + Element timetable = tables.get(0); + Elements rows = timetable.getElementsByTag("tr"); for (int i=0; i fields = row.getElementsByTagName("td"); + Element row = rows.get(i); + Elements fields = row.getElementsByTag("td"); + - if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) { + if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) { currentStation = true; continue; } TimetableBean bean = new TimetableBean(); - String station = fields.get(0).asText() ; + String station = fields.get(0).text() ; if (station.equals("København")) station = "København H"; //correct inconsistency in naming bean.setStation( station ); - bean.setArrival( fields.get(1).asText() ); - bean.setDeparture( fields.get(2).asText() ); + bean.setArrival( fields.get(1).text() ); + bean.setDeparture( fields.get(2).text() ); - boolean cancelled = fields.get(3).asText().equalsIgnoreCase("aflyst"); + boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst"); bean.setCancelled(cancelled); if (currentStation == true && currentStationSaved == false ) { @@ -176,7 +163,7 @@ } else { logger.warning("No time table found, trainID=" + trainID + " type=" + type); } - webClient.closeAllWindows(); + return timetableList; }