01: /*
02: * Copyright 1999-2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: /*
17: * $Id: UseStylesheetParamServlet.java,v 1.1 2006-06-27 14:42:52 sinisa Exp $
18: */
19:
20: /*
21: Simple Servlet Example using a stylesheet parameter
22: */
23: package servlet;
24:
25: // Imported TraX classes
26: import javax.xml.transform.TransformerFactory;
27: import javax.xml.transform.Transformer;
28: import javax.xml.transform.stream.StreamSource;
29: import javax.xml.transform.stream.StreamResult;
30: import javax.xml.transform.TransformerException;
31: import javax.xml.transform.TransformerConfigurationException;
32:
33: // Imported SAX classes
34: import org.xml.sax.SAXException;
35:
36: // Imported java.io and javax.servlet classes
37: import java.io.*;
38: import javax.servlet.*;
39: import javax.servlet.http.*;
40:
41: public class UseStylesheetParamServlet extends HttpServlet {
42:
43: /**
44: * String representing the file separator characters for the System.
45: */
46: public final static String FS = System
47: .getProperty("file.separator");
48:
49: PrintWriter out;
50: String xslFile, xmlFile, paramValue;
51:
52: public void doGet(HttpServletRequest req, HttpServletResponse res)
53: throws ServletException, IOException {
54: try {
55: res.setContentType("text/html; charset=UTF-8");
56: out = res.getWriter();
57:
58: paramValue = req.getParameter("PVAL");
59: xmlFile = req.getParameter("XML");
60: xslFile = req.getParameter("XSL");
61: if (paramValue == null) {
62: out.println("<h1>No input for paramValue</h1>");
63: return;
64: }
65: if (xmlFile == null) {
66: out.println("<h1>No input for xmlFile</h1>");
67: return;
68: }
69: if (xslFile == null) {
70: out.println("<h1>No input for xslFile</h1>");
71: return;
72: }
73:
74: // get the real path for xml and xsl files;
75: String ctx = getServletContext().getRealPath("") + FS;
76: xslFile = ctx + xslFile;
77: xmlFile = ctx + xmlFile;
78:
79: TransformerFactory tFactory = TransformerFactory
80: .newInstance();
81: Transformer transformer = tFactory
82: .newTransformer(new StreamSource(xslFile));
83:
84: // Set the stylesheet parameter (named param1).
85: transformer.setParameter("param1", paramValue);
86: // Perform the transformation.
87: transformer.transform(new StreamSource(xmlFile),
88: new StreamResult(out));
89: } catch (IOException e) {
90: e.printStackTrace();
91: System.exit(-1);
92: } catch (TransformerException e) {
93: e.printStackTrace(out);
94: return;
95: }
96: }
97: }
|