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

Diff of /android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 837 by torben, Fri Jun 11 17:16:06 2010 UTC revision 992 by torben, Wed Jul 14 08:05:31 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfoservice.banedk;  package dk.thoerup.traininfoservice.banedk;
2    
3    
4  import java.io.IOException;  
5  import java.net.URL;  import java.net.URL;
6  import java.sql.SQLException;  import java.sql.SQLException;
7  import java.util.ArrayList;  import java.util.ArrayList;
# Line 10  import java.util.Map; Line 10  import java.util.Map;
10  import java.util.logging.Level;  import java.util.logging.Level;
11  import java.util.logging.Logger;  import java.util.logging.Logger;
12    
13  import com.gargoylesoftware.htmlunit.BrowserVersion;  import org.jsoup.nodes.Document;
14  import com.gargoylesoftware.htmlunit.Page;  import org.jsoup.nodes.Element;
15  import com.gargoylesoftware.htmlunit.RefreshHandler;  import org.jsoup.select.Elements;
 import com.gargoylesoftware.htmlunit.WebClient;  
 import com.gargoylesoftware.htmlunit.html.DomNodeList;  
 import com.gargoylesoftware.htmlunit.html.HtmlElement;  
 import com.gargoylesoftware.htmlunit.html.HtmlPage;  
16    
17  import dk.thoerup.circuitbreaker.CircuitBreaker;  import dk.thoerup.circuitbreaker.CircuitBreaker;
18  import dk.thoerup.circuitbreaker.CircuitBreakerManager;  import dk.thoerup.circuitbreaker.CircuitBreakerManager;
# Line 24  import dk.thoerup.traininfoservice.Stati Line 20  import dk.thoerup.traininfoservice.Stati
20  import dk.thoerup.traininfoservice.Statistics;  import dk.thoerup.traininfoservice.Statistics;
21    
22  public class TimetableFetcher {  public class TimetableFetcher {
23            
         class NullRefreshHandler implements RefreshHandler {  
                 public void handleRefresh(Page arg0, URL arg1, int arg2) throws IOException {                    
                 }  
                   
         }  
24                                    
25          Map<String, List<TimetableBean>> cache;          Map<String, List<TimetableBean>> cache;
26          Map<String, Integer> stationCache;          Map<String, Integer> stationCache;
# Line 76  public class TimetableFetcher { Line 67  public class TimetableFetcher {
67                                    
68                  if (id == null) {                  if (id == null) {
69                          try {                          try {
70                                  id = stationDao.getBySpecificName(name);                                  id = stationDao.getIdByName(name);
71                                  stationCache.put(name, id);                                  stationCache.put(name, id);
72                          } catch (SQLException e) {                          } catch (SQLException e) {
73                                  logger.log(Level.SEVERE, "getStationId failed", e);                                  logger.log(Level.SEVERE, "getStationId failed", e);
# Line 90  public class TimetableFetcher { Line 81  public class TimetableFetcher {
81          List<TimetableBean> lookupTimetableRealSite(String trainID, String type) throws Exception {                      List<TimetableBean> lookupTimetableRealSite(String trainID, String type) throws Exception {            
82                  List<TimetableBean> timetableList = new ArrayList<TimetableBean>();                  List<TimetableBean> timetableList = new ArrayList<TimetableBean>();
83                                    
84                  String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";                  //String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";
85                                                    String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                
86    
             final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);  
             webClient.setTimeout(2500);  
             webClient.setJavaScriptEnabled(false);          
             webClient.setRefreshHandler( new NullRefreshHandler() );  
             webClient.setCssEnabled(false);  
87                            
88                            JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , 2500);
             BanedkInvocation wrapper = new BanedkInvocation(webClient, url);  
89              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
90                            
91              HtmlPage page = (HtmlPage) breaker.invoke(wrapper);              Document doc = (Document) breaker.invoke(wrapper);
92                                            
93                            
94              boolean currentStation = false;              boolean currentStation = false;
95              boolean currentStationSaved = false;              boolean currentStationSaved = false;
96                            
97              List<HtmlElement> tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute");              Elements tables = doc.getElementsByClass("Rute");
98                
99              if (tables.size() == 1) {              if (tables.size() == 1) {
100                  HtmlElement timetable = tables.get(0);                  Element timetable = tables.get(0);
101                  DomNodeList<HtmlElement> rows = timetable.getElementsByTagName("tr");                  Elements rows = timetable.getElementsByTag("tr");
102                                    
103                  for (int i=0; i<rows.size(); i++) {                  for (int i=0; i<rows.size(); i++) {
104                          if (i==0) //First row is column headers                          if (i==0) //First row is column headers
105                                  continue;                                  continue;
106                                                    
107                                                    
108                          HtmlElement row = rows.get(i);                          Element row = rows.get(i);
109                          DomNodeList<HtmlElement> fields = row.getElementsByTagName("td");                          Elements fields = row.getElementsByTag("td");
110    
111                                                    
112                          if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) {                          if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) {
113                                  currentStation = true;                                  currentStation = true;
114                                  continue;                                  continue;
115                          }                          }
116                                                    
117                          TimetableBean bean = new TimetableBean();                          TimetableBean bean = new TimetableBean();
118                                                    
119                          String station = fields.get(0).asText() ;                          String station = fields.get(0).text() ;
120                          if (station.equals("København"))                          if (station.equals("København"))
121                                  station = "København H"; //correct inconsistency in naming                                  station = "København H"; //correct inconsistency in naming
122                                                    
123                          bean.setStation( station );                          bean.setStation( station );
124                          bean.setArrival( fields.get(1).asText() );                          bean.setArrival( fields.get(1).text() );
125                          bean.setDeparture( fields.get(2).asText() );                          bean.setDeparture( fields.get(2).text() );
126                            
127                            boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst");
128                            bean.setCancelled(cancelled);
129                                                    
130                          if (currentStation == true && currentStationSaved == false ) {                          if (currentStation == true && currentStationSaved == false ) {
131                                  bean.setCurrent(currentStation);                                  bean.setCurrent(currentStation);
# Line 145  public class TimetableFetcher { Line 135  public class TimetableFetcher {
135                          bean.setStationId( getStationId( station ));                          bean.setStationId( getStationId( station ));
136                                                    
137                          timetableList.add(bean);                          timetableList.add(bean);
138                    }
139                    
140                    //TODO: There is an off-by-one error in this cancelled parser thingie
141                    final String cancelledString = "Aflyst";
142                    for (int i=0;i<timetableList.size(); i++) { //handle cancelled labels
143                            final int lastIdx = (timetableList.size() - 1);
144                            
145                            TimetableBean current = timetableList.get(i);
146                            if (current.isCancelled()) {
147                                    if (i == 0) {
148                                            current.setDeparture(cancelledString);
149                                    } else if (i == lastIdx) {
150                                            current.setArrival(cancelledString);
151                                    } else if (i>0 && i<lastIdx) {
152                                            TimetableBean next = timetableList.get(i+1);
153                                            TimetableBean prev = timetableList.get(i-1);
154                                            
155                                            if (next.isCancelled())
156                                                    current.setDeparture(cancelledString);
157                                            if (prev.isCancelled())
158                                                    current.setArrival(cancelledString);
159                                    }
160                            }
161                  }                  }
162                                    
163              } else {              } else {
164                  logger.warning("No time table found, trainID=" + trainID + " type=" + type);                  logger.warning("No time table found, trainID=" + trainID + " type=" + type);
165              }              }
166              webClient.closeAllWindows();              
167                                    
168                  return timetableList;                  return timetableList;
169          }          }

Legend:
Removed from v.837  
changed lines
  Added in v.992

  ViewVC Help
Powered by ViewVC 1.1.20