KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > servlet > XSLTServletWithParams


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: XSLTServletWithParams.java,v 1.7 2004/02/17 19:13:22 minchau Exp $
18  */

19 package servlet;
20 import javax.servlet.*;
21 import javax.servlet.http.*;
22 import java.io.*;
23 import java.util.Enumeration JavaDoc;
24 import java.net.URL JavaDoc;
25
26 import org.xml.sax.*;
27 import javax.xml.transform.TransformerFactory JavaDoc;
28 import javax.xml.transform.Transformer JavaDoc;
29 import javax.xml.transform.Source JavaDoc;
30 import javax.xml.transform.stream.StreamSource JavaDoc;
31 import javax.xml.transform.stream.StreamResult JavaDoc;
32
33 /*
34  * This sample takes input parameters in the request URL: a URL
35  * parameter for the XML input, an xslURL parameter for the stylesheet,
36  * and optional stylesheet parameters.
37  * To run the equivalent of SimplestXSLServlet (with the documents in the
38  * servlet document root directory), the request URL is
39  * http://<server/servletpath>servlet.SimpleXSLServlet?URL=file:todo.xml&xslURL=file:todo.xsl
40  *
41  * Using a stylesheet Processing Instruction:
42  * If the XML document includes a stylesheet PI that you want to use,
43  * omit the xslURL parameter.
44  *
45  * Sending stylesheet parameters:
46  * If, for example, a servlet takes a stylesheet parameter named param1
47  * param1 that you want to set to foo, include param1=foo in the URL.
48  */

49
50 public class XSLTServletWithParams extends HttpServlet {
51
52   /**
53    * String representing the file separator characters for the System.
54    */

55   public final static String JavaDoc FS = System.getProperty("file.separator");
56   
57   public void init(ServletConfig config) throws ServletException
58   {
59     super.init(config);
60   }
61
62   public void doGet (HttpServletRequest request,
63                      HttpServletResponse response)
64     throws ServletException, IOException
65   {
66     // The servlet returns HTML; charset is UTF8.
67
// See ApplyXSLT.getContentType() to get output properties from <xsl:output>.
68
response.setContentType("text/html; charset=UTF-8");
69     PrintWriter out = response.getWriter();
70     try
71     {
72       TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
73       // Get params from URL.
74
String JavaDoc xml = getRequestParam(request, "URL");
75       String JavaDoc xsl = getRequestParam(request, "xslURL");
76       Source JavaDoc xmlSource = null;
77       Source JavaDoc xslSource = null;
78       Transformer JavaDoc transformer = null;
79 //get the real path for xml and xsl files.
80
String JavaDoc ctx = getServletContext().getRealPath("") + FS;
81       
82       // Get the XML input document.
83
if (xml != null && xml.length()> 0)
84         xmlSource = new StreamSource JavaDoc(new URL JavaDoc("file", "", ctx + xml).openStream());
85       // Get the stylesheet.
86
if (xsl != null && xsl.length()> 0)
87         xslSource = new StreamSource JavaDoc(new URL JavaDoc("file", "", ctx + xsl).openStream());
88       if (xmlSource != null) // We have an XML input document.
89
{
90         if (xslSource == null) // If no stylesheet, look for PI in XML input document.
91
{
92             String JavaDoc media= null , title = null, charset = null;
93           xslSource = tFactory.getAssociatedStylesheet(xmlSource,media, title, charset);
94         }
95         if (xslSource != null) // Now do we have a stylesheet?
96
{
97           transformer = tFactory.newTransformer(xslSource);
98           setParameters(transformer, request); // Set stylesheet params.
99
// Perform the transformation.
100
transformer.transform(xmlSource, new StreamResult JavaDoc(out));
101         }
102         else
103           out.write("No Stylesheet!");
104       }
105       else
106         out.write("No XML Input Document!");
107     }
108     catch (Exception JavaDoc e)
109     {
110       e.printStackTrace(out);
111     }
112     out.close();
113   }
114   
115   // Get parameters from the request URL.
116
String JavaDoc getRequestParam(HttpServletRequest request, String JavaDoc param)
117   {
118       if (request != null)
119     {
120         String JavaDoc paramVal = request.getParameter(param);
121           return paramVal;
122       }
123       return null;
124   }
125   
126   // Set stylesheet parameters from the request URL.
127
void setParameters(Transformer JavaDoc transformer, HttpServletRequest request)
128   {
129     Enumeration JavaDoc paramNames = request.getParameterNames();
130     while (paramNames.hasMoreElements())
131     {
132       String JavaDoc paramName = (String JavaDoc) paramNames.nextElement();
133       try
134       {
135         String JavaDoc paramVal = request.getParameter(paramName);
136         if (paramVal != null)
137           transformer.setParameter(paramName, paramVal);
138       }
139       catch (Exception JavaDoc e)
140       {
141       }
142     }
143   }
144 }
145
Popular Tags