KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > servlet > UseStylesheetParamServlet


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.5 2004/02/17 19:13:22 minchau Exp $
18  */

19
20 /*
21 Simple Servlet Example using a stylesheet parameter
22  */

23 package servlet;
24 // Imported TraX classes
25
import javax.xml.transform.TransformerFactory JavaDoc;
26 import javax.xml.transform.Transformer JavaDoc;
27 import javax.xml.transform.stream.StreamSource JavaDoc;
28 import javax.xml.transform.stream.StreamResult JavaDoc;
29 import javax.xml.transform.TransformerException JavaDoc;
30 import javax.xml.transform.TransformerConfigurationException JavaDoc;
31
32 // Imported SAX classes
33
import org.xml.sax.SAXException JavaDoc;
34
35 // Imported java.io and javax.servlet classes
36
import java.io.*;
37 import javax.servlet.*;
38 import javax.servlet.http.*;
39
40 public class UseStylesheetParamServlet extends HttpServlet {
41     
42
43    /**
44     * String representing the file separator characters for the System.
45     */

46     public final static String JavaDoc FS = System.getProperty("file.separator");
47     
48     PrintWriter out;
49     String JavaDoc xslFile, xmlFile, paramValue;
50     public void doGet(HttpServletRequest req,
51         HttpServletResponse res)
52             throws ServletException, IOException {
53         try {
54             res.setContentType("text/html; charset=UTF-8");
55             out = res.getWriter();
56
57       paramValue = req.getParameter("PVAL");
58             xmlFile = req.getParameter("XML");
59             xslFile = req.getParameter("XSL");
60         if (paramValue == null) {
61             out.println(
62             "<h1>No input for paramValue</h1>");
63             return;
64         }
65         if ( xmlFile == null) {
66             out.println(
67             "<h1>No input for xmlFile</h1>");
68             return;
69         }
70         if ( xslFile == null) {
71             out.println(
72             "<h1>No input for xslFile</h1>");
73             return;
74         }
75         
76         // get the real path for xml and xsl files;
77
String JavaDoc ctx = getServletContext().getRealPath("") + FS;
78         xslFile = ctx + xslFile;
79         xmlFile = ctx + xmlFile;
80          
81         TransformerFactory JavaDoc tFactory =
82             TransformerFactory.newInstance();
83         Transformer JavaDoc transformer =
84             tFactory.newTransformer(new StreamSource JavaDoc(xslFile));
85
86     // Set the stylesheet parameter (named param1).
87
transformer.setParameter("param1", paramValue);
88     // Perform the transformation.
89
transformer.transform(new StreamSource JavaDoc(xmlFile),
90                                       new StreamResult JavaDoc(out));
91         }
92     catch (IOException e) {
93             e.printStackTrace();
94             System.exit(-1);
95         }
96         catch (TransformerException JavaDoc e) {
97       e.printStackTrace(out);
98             return;
99         }
100     }
101 }
102
Popular Tags