xml-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From LotusXSL_Team/CAM/Lotus%LO...@lotus.com
Subject Re: a simple transform servlet, please
Date Thu, 22 Feb 2001 18:42:04 GMT

Heather,

Here are two simple sample servlets that use Xalan to perform
transformations and return HTML to the client. The first includes hardcoded
references to the stylesheet and XML document:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.net.URL;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;

/*
 * This sample applies the todo.xsl stylesheet to the
 * todo.xml XML document, and returns the transformation
 * output (HTML) to the client browser.
 *
 * IMPORTANT: For this to work, you must place todo.xsl and todo.xml
 * in the servlet root directory for documents.
 *
 */

public class SimplestXSLServlet extends HttpServlet {

  public void doGet (HttpServletRequest request,
                     HttpServletResponse response)
    throws ServletException, IOException
  {
    // Output goes in the response stream.
    PrintWriter out = new PrintWriter (response.getOutputStream());
    // The servlet returns HTML.
    response.setContentType("text/html");
    try
    {
      TransformerFactory tFactory = TransformerFactory.newInstance();
      // Get the XML input document and the stylesheet.
      Source xmlSource = new StreamSource(new URL
("file:todo.xml").openStream());
      Source xslSource = new StreamSource(new URL
("file:todo.xsl").openStream());
      // Generate the transformer.
      Transformer transformer = tFactory.newTransformer(xslSource);
      // Perform the transformation, sending the output to the response.
      transformer.transform(xmlSource, new StreamResult(out));
    }
    catch (Exception e)
    {
      out.write(e.getMessage());
      e.printStackTrace(out);
    }
    out.close();
  }
}

The second servlet is slightly more complicated servlet. It takes input
parameters on the request URL for stylesheet, XML document, and any
stylesheet parameters:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.Enumeration;
import java.net.URL;

import org.xml.sax.*;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;

/*
 * This sample takes input parameters in the request URL: a URL
 * parameter for the XML input, an xslURL parameter for the stylesheet,
 * and optional stylesheet parameters.
 * To run the equivalent of SimplestXSLServlet (with the documents in the
 * servlet document root directory), the request URL is
 *
http://<server/servletpath>servlet.SimpleXSLServlet?URL=file:todo.xml&xslURL=file:todo.xsl
 *
 * Using a stylesheet Processing Instruction:
 * If the XML document includes a stylesheet PI that you want to use,
 * omit the xslURL parameter.
 *
 * Sending stylesheet parameters:
 * If, for example, a servlet takes a stylesheet parameter named param1
 * param1 that you want to set to foo, include param1=foo in the URL.
 */

public class SimpleXSLServlet extends HttpServlet {

  public void doGet (HttpServletRequest request,
                     HttpServletResponse response)
    throws ServletException, IOException
  {
    // Output goes in the response stream.
    PrintWriter out = new PrintWriter (response.getOutputStream());
    // This servlet is intended to return HTML.
    response.setContentType("text/html");
    try
    {
      TransformerFactory tFactory = TransformerFactory.newInstance();
      // Get params from URL.
      String xml = getRequestParam(request, "URL");
      String xsl = getRequestParam(request, "xslURL");
      Source xmlSource = null;
      Source xslSource = null;
      Transformer transformer = null;
      // Get the XML input document.
      if (xml != null && xml.length()> 0)
        xmlSource = new StreamSource(new URL(xml).openStream());
      // Get the stylesheet.
      if (xsl != null && xsl.length()> 0)
        xslSource = new StreamSource(new URL(xsl).openStream());
      if (xmlSource != null) // We have an XML input document.
      {
        if (xslSource == null) // If no stylesheet, look for PI in XML
input document.
        {
              String media= null , title = null, charset = null;
          xslSource = tFactory.getAssociatedStylesheet(xmlSource,media,
title, charset);
        }
        if (xslSource != null) // Now do we have a stylesheet?
        {
          transformer = tFactory.newTransformer(xslSource);
          setParameters(transformer, request); // Set stylesheet params.
          transformer.transform(xmlSource, new StreamResult(out)); //
Perform the transformation.
        }
        else
          out.write("No Stylesheet!");
      }
      else
        out.write("No XML Input Document!");
    }
    catch (Exception e)
    {
      out.write(e.getMessage());
      e.printStackTrace(out);
    }
    out.close();
  }

  // Get parameters from the request URL.
  String getRequestParam(HttpServletRequest request, String param)
  {
       if (request != null)
    {
         String[] paramVals = request.getParameterValues(param);
         if (paramVals != null)
          return paramVals[0];
       }
       return null;
  }

  // Set stylesheet parameters from the request URL.
  void setParameters(Transformer transformer, HttpServletRequest request)
  {
    Enumeration paramNames = request.getParameterNames();
    while (paramNames.hasMoreElements())
    {
      String paramName = (String) paramNames.nextElement();
      try
      {
        String[] paramVals = request.getParameterValues(paramName);
        if (paramVals != null)
            transformer.setParameter(paramName, paramVals[0]);
      }
      catch (Exception e)
      {
      }
    }
  }
}


                                                                                         
                         
                    "Heather                                                             
                         
                    Lloyd"               To:     <general@xml.apache.org>          
                               
                    <hlloyd@prodi        cc:     (bcc: Lotusxsl_Team)                 
                            
                    gy.net>              Subject:     a simple transform servlet, please
                          
                                                                                         
                         
                    02/05/2001                                                           
                         
                    10:23 PM                                                             
                         
                    Please                                                               
                         
                    respond to                                                           
                         
                    general                                                              
                         
                                                                                         
                         
                                                                                         
                         




i'm looking for a really really really simple example of a servlet that
extends httpServlet, takes a really really really simple xml and xsl file
from a directory, gets an XSLTProcessor Object, transforms the xml, and
writes the output to a desktop browser.  i want to run the servlet in
tomcat's web server, so i need to know how to configure it to find these
things.  Please do not refer me to the DefaultApplyXSL class which is part
of the xalan-j_1_2_2 xalansamples.jar distribution from xml.apache.org.  i
can't get it to run, and it's way more complicated than necessary for my
purposes.


here are my digits:

red hat linux 6.1
jdk 1.3
tomcat 3-2.1-1.noarch.rpm from rpmfind.net
xalan-j_1_2_2 from xml.apache.org

my TOMCAT_HOME/lib has the xerces.jar, xalan.jar, and xalansamples.jar from
the above listed distribution, as well as the usual suspects.


thanks in advance


---------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          general-unsubscribe@xml.apache.org
For additional commands, e-mail: general-help@xml.apache.org






Mime
View raw message