1 |
package dk.thoerup.traininfoservice.banedk; |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
3 |
import java.io.IOException; |
import java.io.IOException; |
|
import java.util.List; |
|
4 |
import java.util.logging.Level; |
import java.util.logging.Level; |
5 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
6 |
|
|
66 |
int station = Integer.parseInt( req.getParameter("station") ); |
int station = Integer.parseInt( req.getParameter("station") ); |
67 |
String format = req.getParameter("format"); |
String format = req.getParameter("format"); |
68 |
|
|
69 |
List<DepartureBean> beans; |
DepartureBean beans; |
|
|
|
|
String stationName = StationDAO.getStationName(station); |
|
70 |
|
|
71 |
try { |
try { |
72 |
beans = fetcher.cachedLookupDepartures(station, arrival); |
beans = fetcher.cachedLookupDepartures(station, arrival); |
92 |
|
|
93 |
if (format.equalsIgnoreCase("xml")) { |
if (format.equalsIgnoreCase("xml")) { |
94 |
resp.setContentType("text/xml"); |
resp.setContentType("text/xml"); |
95 |
resp.getWriter().print( formatXml(beans, stationName, arrival) ); |
resp.getWriter().print( formatXml(beans, arrival) ); |
96 |
} else if (format.equalsIgnoreCase("html")) { |
} else if (format.equalsIgnoreCase("html")) { |
97 |
|
|
98 |
String advStr = req.getParameter("advanced"); |
String advStr = req.getParameter("advanced"); |
99 |
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; |
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; |
100 |
|
|
101 |
req.setAttribute("advanced", advanced); |
req.setAttribute("advanced", advanced); |
|
req.setAttribute("stationname", stationName ); |
|
102 |
req.setAttribute("departurebeans", beans); |
req.setAttribute("departurebeans", beans); |
103 |
req.setAttribute("stationID", station ); |
req.setAttribute("stationID", station ); |
104 |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp); |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp); |
108 |
|
|
109 |
} |
} |
110 |
|
|
111 |
protected String formatXml(List<DepartureBean> beans, String stationName, boolean arrival) throws ServletException{ |
protected String formatXml(DepartureBean beans, boolean arrival) throws ServletException{ |
112 |
String xml = ""; |
String xml = ""; |
113 |
try { |
try { |
114 |
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); |
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); |
117 |
|
|
118 |
Document doc = impl.createDocument(null,null,null); |
Document doc = impl.createDocument(null,null,null); |
119 |
Element root = doc.createElement("departureinfo"); |
Element root = doc.createElement("departureinfo"); |
120 |
root.setAttribute("station", stationName); |
root.setAttribute("station", beans.getStationName()); |
121 |
root.setAttribute("arrival", Boolean.toString(arrival) ); |
root.setAttribute("arrival", Boolean.toString(arrival) ); |
122 |
for (DepartureBean departure : beans) { |
|
123 |
|
for (String notif : beans.notifications) { |
124 |
|
Element notElem = doc.createElement("notification"); |
125 |
|
notElem.setTextContent(notif); |
126 |
|
root.appendChild(notElem); |
127 |
|
} |
128 |
|
|
129 |
|
for (DepartureEntry departure : beans.departureEntries) { |
130 |
Element train = doc.createElement("train"); |
Element train = doc.createElement("train"); |
131 |
|
|
132 |
Element time = doc.createElement("time"); |
Element time = doc.createElement("time"); |