1 |
package dk.thoerup.traininfoservice.banedk; |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
3 |
|
import java.io.ByteArrayOutputStream; |
4 |
import java.io.IOException; |
import java.io.IOException; |
5 |
import java.util.logging.Level; |
import java.util.logging.Level; |
6 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
10 |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServlet; |
11 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
12 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
|
import javax.xml.parsers.DocumentBuilder; |
|
13 |
import javax.xml.parsers.DocumentBuilderFactory; |
import javax.xml.parsers.DocumentBuilderFactory; |
|
import javax.xml.transform.OutputKeys; |
|
|
import javax.xml.transform.Transformer; |
|
14 |
import javax.xml.transform.TransformerFactory; |
import javax.xml.transform.TransformerFactory; |
|
import javax.xml.transform.dom.DOMSource; |
|
|
import javax.xml.transform.stream.StreamResult; |
|
15 |
|
|
16 |
import org.w3c.dom.DOMImplementation; |
import org.simpleframework.xml.Serializer; |
17 |
import org.w3c.dom.Document; |
import org.simpleframework.xml.core.Persister; |
|
import org.w3c.dom.Element; |
|
18 |
|
|
19 |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
20 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
104 |
} |
} |
105 |
|
|
106 |
protected String formatXml(DepartureBean beans, boolean arrival) throws ServletException{ |
protected String formatXml(DepartureBean beans, boolean arrival) throws ServletException{ |
107 |
String xml = ""; |
|
108 |
try { |
Serializer serializer = new Persister(); |
|
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); |
|
|
DOMImplementation impl = builder.getDOMImplementation(); |
|
|
|
|
|
|
|
|
Document doc = impl.createDocument(null,null,null); |
|
|
Element root = doc.createElement("departureinfo"); |
|
|
root.setAttribute("station", beans.getStationName()); |
|
|
root.setAttribute("arrival", Boolean.toString(arrival) ); |
|
|
|
|
|
for (String notif : beans.notifications) { |
|
|
Element notElem = doc.createElement("notification"); |
|
|
notElem.setTextContent(notif); |
|
|
root.appendChild(notElem); |
|
|
} |
|
|
|
|
|
for (DepartureEntry departure : beans.departureEntries) { |
|
|
Element train = doc.createElement("train"); |
|
|
|
|
|
Element time = doc.createElement("time"); |
|
|
time.setTextContent( departure.getTime() ); |
|
|
train.appendChild(time); |
|
|
|
|
|
Element updated = doc.createElement("updated"); |
|
|
updated.setTextContent( String.valueOf(departure.getUpdated()) ); |
|
|
train.appendChild(updated); |
|
|
|
|
|
Element trainNumber = doc.createElement("trainnumber"); |
|
|
trainNumber.setTextContent( departure.getTrainNumber() ); |
|
|
train.appendChild(trainNumber); |
|
|
|
|
|
Element destination = doc.createElement("destination"); |
|
|
destination.setTextContent( departure.getDestination()); |
|
|
train.appendChild(destination); |
|
|
|
|
|
Element origin = doc.createElement("origin"); |
|
|
origin.setTextContent( departure.getOrigin() ); |
|
|
train.appendChild(origin); |
|
|
|
|
|
Element location= doc.createElement("location"); |
|
|
location.setTextContent( departure.getLocation() ); |
|
|
train.appendChild(location); |
|
|
|
|
|
Element status = doc.createElement("status"); |
|
|
status.setTextContent( departure.getStatus() ); |
|
|
train.appendChild(status); |
|
|
|
|
|
Element note = doc.createElement("note"); |
|
|
note.setTextContent( departure.getNote() ); |
|
|
train.appendChild(note); |
|
|
|
|
|
Element type = doc.createElement("type"); |
|
|
type.setTextContent( departure.getType() ); |
|
|
train.appendChild(type); |
|
|
|
|
|
root.appendChild(train); |
|
|
} |
|
|
|
|
|
doc.appendChild(root); |
|
|
|
|
|
|
|
|
DOMSource domSource = new DOMSource(doc); |
|
|
|
|
|
Transformer transformer = transformerFactory.newTransformer(); |
|
|
//transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
|
|
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); |
|
|
transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1"); |
|
|
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "1"); |
|
|
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
|
|
java.io.StringWriter sw = new java.io.StringWriter(); |
|
|
StreamResult sr = new StreamResult(sw); |
|
|
transformer.transform(domSource, sr); |
|
|
xml = sw.toString(); |
|
109 |
|
|
110 |
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
111 |
|
|
112 |
|
try { |
113 |
|
serializer.write(beans, out); |
114 |
} catch (Exception e) { |
} catch (Exception e) { |
115 |
throw new ServletException(e); |
throw new ServletException(e); |
116 |
} |
} |
117 |
return xml; |
|
118 |
|
return out.toString(); |
119 |
} |
} |
120 |
|
|
121 |
} |
} |