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

Contents of /android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureServlet.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 307 - (show annotations) (download)
Thu Sep 10 18:11:53 2009 UTC (14 years, 8 months ago) by torben
File size: 5637 byte(s)
Finish new bane.dk screen-scraper
1 package dk.thoerup.traininfoservice.banedk;
2
3 import java.io.IOException;
4 import java.sql.Connection;
5 import java.sql.ResultSet;
6 import java.sql.Statement;
7 import java.util.List;
8
9 import javax.servlet.ServletException;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 import javax.xml.parsers.DocumentBuilder;
14 import javax.xml.parsers.DocumentBuilderFactory;
15 import javax.xml.transform.OutputKeys;
16 import javax.xml.transform.Transformer;
17 import javax.xml.transform.TransformerFactory;
18 import javax.xml.transform.dom.DOMSource;
19 import javax.xml.transform.stream.StreamResult;
20
21 import org.w3c.dom.DOMImplementation;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
24
25 import dk.thoerup.traininfoservice.DBConnection;
26
27 /**
28 * Servlet implementation class DepartureServlet
29 */
30 public class DepartureServlet extends HttpServlet {
31 private static final long serialVersionUID = 1L;
32
33 DepartureFetcher fetcher = new DepartureFetcher();
34 TransformerFactory transformerFactory = TransformerFactory.newInstance();
35 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
36
37
38 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
39 int station = Integer.parseInt( req.getParameter("station") );
40 String format = req.getParameter("format");
41
42 List<DepartureBean> beans;
43
44 String stationName = getStationName(station);
45
46 try {
47 beans = fetcher.lookupDepartures(station);
48 } catch (Exception e) {
49 throw new ServletException(e);
50 }
51
52 resp.setDateHeader("Expires", 0);
53 resp.setHeader("Cache-Control", "no-cache, must-revalidate");
54
55 if (format.equalsIgnoreCase("xml")) {
56 resp.setContentType("text/xml");
57 resp.getWriter().print( formatXml(beans, stationName) );
58 } else if (format.equalsIgnoreCase("html")) {
59 req.setAttribute("stationname", stationName );
60 req.setAttribute("departurebeans", beans);
61 req.setAttribute("stationID", station );
62 getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp);
63 } else {
64 throw new ServletException("Unknown format");
65 }
66
67 }
68
69 protected String formatXml(List<DepartureBean> beans, String stationName) throws ServletException{
70 String xml = "";
71 try {
72 DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
73 DOMImplementation impl = builder.getDOMImplementation();
74
75
76 Document doc = impl.createDocument(null,null,null);
77 Element root = doc.createElement("departureinfo");
78 root.setAttribute("station", stationName);
79 for (DepartureBean departure : beans) {
80 Element train = doc.createElement("train");
81
82 Element time = doc.createElement("time");
83 time.setTextContent( departure.getTime() );
84 train.appendChild(time);
85
86 Element updated = doc.createElement("updated");
87 updated.setTextContent( String.valueOf(departure.getUpdated()) );
88 train.appendChild(updated);
89
90 Element trainNumber = doc.createElement("trainnumber");
91 trainNumber.setTextContent( departure.getTrainNumber() );
92 train.appendChild(trainNumber);
93
94 Element destination = doc.createElement("destination");
95 destination.setTextContent( departure.getDestination());
96 train.appendChild(destination);
97
98 Element origin = doc.createElement("origin");
99 origin.setTextContent( departure.getOrigin() );
100 train.appendChild(origin);
101
102 Element location= doc.createElement("location");
103 location.setTextContent( departure.getLocation() );
104 train.appendChild(location);
105
106 Element status = doc.createElement("status");
107 status.setTextContent( departure.getStatus() );
108 train.appendChild(status);
109
110 Element note = doc.createElement("note");
111 note.setTextContent( departure.getNote() );
112 train.appendChild(note);
113
114 root.appendChild(train);
115 }
116
117 doc.appendChild(root);
118
119
120 DOMSource domSource = new DOMSource(doc);
121
122 Transformer transformer = transformerFactory.newTransformer();
123 //transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
124 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
125 transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
126 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
127 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
128 java.io.StringWriter sw = new java.io.StringWriter();
129 StreamResult sr = new StreamResult(sw);
130 transformer.transform(domSource, sr);
131 xml = sw.toString();
132
133
134 } catch (Exception e) {
135 throw new ServletException(e);
136 }
137 return xml;
138 }
139
140 protected String getStationName(int stationID) {
141 String station = "";
142
143 Connection conn = null;
144 try {
145 conn = DBConnection.getConnection();
146 Statement stmt = conn.createStatement();
147 ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID);
148 if (rs.next()) {
149 station = rs.getString(1);
150 }
151
152 } catch (Exception e) {
153 } finally {
154 try {
155 if (conn != null && !conn.isClosed())
156 conn.close();
157 } catch (Exception e) {}
158 }
159
160 return station;
161 }
162
163 }

  ViewVC Help
Powered by ViewVC 1.1.20