211
|
1 import java.io.*;
|
|
2 import java.net.*;
|
|
3 import java.util.*;
|
|
4 import javax.servlet.*;
|
|
5 import javax.servlet.http.*;
|
|
6 import javax.xml.transform.*;
|
|
7 import javax.xml.transform.dom.*;
|
|
8 import javax.xml.transform.stream.*;
|
|
9 import org.apache.xerces.parsers.DOMParser;
|
|
10 import org.apache.xpath.*;
|
|
11 import org.w3c.dom.*;
|
|
12
|
|
13 public class Relato extends HttpServlet {
|
|
14
|
|
15 public final static String FS = System.getProperty("file.separator");
|
|
16
|
|
17 // Respond to HTTP GET requests from browsers.
|
|
18 public void doGet (HttpServletRequest request, HttpServletResponse response)
|
|
19 throws ServletException, IOException {
|
|
20
|
|
21 Hashtable params = new Hashtable();
|
|
22 Enumeration enum = request.getParameterNames();
|
|
23 while (enum.hasMoreElements()) {
|
|
24 String pName = (String) enum.nextElement();
|
|
25 params.put(pName, request.getParameter(pName));
|
|
26
|
|
27 }
|
|
28 // Set content type for HTML.
|
|
29 response.setContentType("text/html; charset=UTF-8");
|
|
30 // Output goes to the response PrintWriter.
|
|
31 PrintWriter out = response.getWriter();
|
|
32 DOMParser parser = new DOMParser();
|
|
33
|
|
34 try {
|
|
35 TransformerFactory tFactory = TransformerFactory.newInstance();
|
|
36 //get the real path for xml and xsl files.
|
|
37 String ctx = getServletContext().getRealPath("") + FS;
|
|
38
|
|
39 parser.parse(ctx + (String) params.get("xml"));
|
|
40 Document document = parser.getDocument();
|
|
41
|
|
42 Element topFrameset = (Element) XPathAPI.selectSingleNode(document, "/relato/frames/frameset");
|
|
43 topFrameset.setAttribute("onload", "init();");
|
|
44
|
|
45 NodeList nodelist = XPathAPI.selectNodeList(document, "//frameset/frame");
|
|
46 for (int i = 0; i < nodelist.getLength(); i++) {
|
|
47 Element elem = (Element)nodelist.item(i);
|
|
48 String name = elem.getAttribute("name");
|
|
49 if (name != "") {
|
|
50 if (params.containsKey(name)) {
|
|
51 String src = (String) params.get(name);
|
|
52 elem.setAttribute("src", src);
|
|
53 }
|
|
54 }
|
|
55 }
|
|
56
|
|
57 Source xmlSource = new DOMSource (document);
|
|
58 Source xslSource = new StreamSource (new URL("file", "", ctx+"relato/relato.xsl").openStream());
|
|
59 // Generate the transformer.
|
|
60 Transformer transformer = tFactory.newTransformer(xslSource);
|
|
61 // Perform the transformation, sending the output to the response.
|
|
62 transformer.transform(xmlSource, new StreamResult(out));
|
|
63 }
|
|
64
|
|
65 // If an Exception occurs, return the error to the client.
|
|
66 catch (Exception e) {
|
|
67 out.write(e.getMessage());
|
|
68 e.printStackTrace(out);
|
|
69 }
|
|
70
|
|
71 // Close the PrintWriter.
|
|
72 out.close();
|
|
73 }
|
|
74 }
|