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