001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: XSLTServletWithParams.java,v 1.1 2006-06-27 14:42:52 sinisa Exp $
018: */
019: package servlet;
020:
021: import javax.servlet.*;
022: import javax.servlet.http.*;
023: import java.io.*;
024: import java.util.Enumeration;
025: import java.net.URL;
026:
027: import org.xml.sax.*;
028: import javax.xml.transform.TransformerFactory;
029: import javax.xml.transform.Transformer;
030: import javax.xml.transform.Source;
031: import javax.xml.transform.stream.StreamSource;
032: import javax.xml.transform.stream.StreamResult;
033:
034: /*
035: * This sample takes input parameters in the request URL: a URL
036: * parameter for the XML input, an xslURL parameter for the stylesheet,
037: * and optional stylesheet parameters.
038: * To run the equivalent of SimplestXSLServlet (with the documents in the
039: * servlet document root directory), the request URL is
040: * http://<server/servletpath>servlet.SimpleXSLServlet?URL=file:todo.xml&xslURL=file:todo.xsl
041: *
042: * Using a stylesheet Processing Instruction:
043: * If the XML document includes a stylesheet PI that you want to use,
044: * omit the xslURL parameter.
045: *
046: * Sending stylesheet parameters:
047: * If, for example, a servlet takes a stylesheet parameter named param1
048: * param1 that you want to set to foo, include param1=foo in the URL.
049: */
050:
051: public class XSLTServletWithParams extends HttpServlet {
052:
053: /**
054: * String representing the file separator characters for the System.
055: */
056: public final static String FS = System
057: .getProperty("file.separator");
058:
059: public void init(ServletConfig config) throws ServletException {
060: super .init(config);
061: }
062:
063: public void doGet(HttpServletRequest request,
064: HttpServletResponse response) throws ServletException,
065: IOException {
066: // The servlet returns HTML; charset is UTF8.
067: // See ApplyXSLT.getContentType() to get output properties from <xsl:output>.
068: response.setContentType("text/html; charset=UTF-8");
069: PrintWriter out = response.getWriter();
070: try {
071: TransformerFactory tFactory = TransformerFactory
072: .newInstance();
073: // Get params from URL.
074: String xml = getRequestParam(request, "URL");
075: String xsl = getRequestParam(request, "xslURL");
076: Source xmlSource = null;
077: Source xslSource = null;
078: Transformer transformer = null;
079: //get the real path for xml and xsl files.
080: String ctx = getServletContext().getRealPath("") + FS;
081:
082: // Get the XML input document.
083: if (xml != null && xml.length() > 0)
084: xmlSource = new StreamSource(new URL("file", "", ctx
085: + xml).openStream());
086: // Get the stylesheet.
087: if (xsl != null && xsl.length() > 0)
088: xslSource = new StreamSource(new URL("file", "", ctx
089: + xsl).openStream());
090: if (xmlSource != null) // We have an XML input document.
091: {
092: if (xslSource == null) // If no stylesheet, look for PI in XML input document.
093: {
094: String media = null, title = null, charset = null;
095: xslSource = tFactory.getAssociatedStylesheet(
096: xmlSource, media, title, charset);
097: }
098: if (xslSource != null) // Now do we have a stylesheet?
099: {
100: transformer = tFactory.newTransformer(xslSource);
101: setParameters(transformer, request); // Set stylesheet params.
102: // Perform the transformation.
103: transformer.transform(xmlSource, new StreamResult(
104: out));
105: } else
106: out.write("No Stylesheet!");
107: } else
108: out.write("No XML Input Document!");
109: } catch (Exception e) {
110: e.printStackTrace(out);
111: }
112: out.close();
113: }
114:
115: // Get parameters from the request URL.
116: String getRequestParam(HttpServletRequest request, String param) {
117: if (request != null) {
118: String paramVal = request.getParameter(param);
119: return paramVal;
120: }
121: return null;
122: }
123:
124: // Set stylesheet parameters from the request URL.
125: void setParameters(Transformer transformer,
126: HttpServletRequest request) {
127: Enumeration paramNames = request.getParameterNames();
128: while (paramNames.hasMoreElements()) {
129: String paramName = (String) paramNames.nextElement();
130: try {
131: String paramVal = request.getParameter(paramName);
132: if (paramVal != null)
133: transformer.setParameter(paramName, paramVal);
134: } catch (Exception e) {
135: }
136: }
137: }
138: }
|