[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: A community project

Subject: RE: A community project
From: "Ivan Pedruzzi" <ivan@xxxxxxxxxxxxxxx>
Date: Wed, 30 Jan 2002 22:15:58 -0500
getrequestparam
May be this servlet from xalan's examples can help you

package servlet;
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;


public class XSLTServletWithParams extends HttpServlet {

  public void init(ServletConfig config) throws ServletException
  {
    super.init(config);
  }

  public void doGet (HttpServletRequest request,
                     HttpServletResponse response)
    throws ServletException, IOException
  {
    // The servlet returns HTML; charset is UTF8.
    // See ApplyXSLT.getContentType() to get output properties from
<xsl:output>.
    response.setContentType("text/html; charset=UTF-8"); 
    PrintWriter out = response.getWriter();
    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.
          // Perform the transformation.
          transformer.transform(xmlSource, new StreamResult(out)); 
        }
        else
          out.write("No Stylesheet!");
      }
      else
        out.write("No XML Input Document!");
    }
    catch (Exception e)
    {
      e.printStackTrace(out);    
    }
    out.close();
  }
  
  // Get parameters from the request URL.
  String getRequestParam(HttpServletRequest request, String param)
  {
	  if (request != null) 
    { 
	    String paramVal = request.getParameter(param); 
		  return paramVal;
	  }
	  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 paramVal = request.getParameter(paramName);
        if (paramVal != null)
          transformer.setParameter(paramName, paramVal);

      }
      catch (Exception e)
      {
      }
    }
  }  
}

Ivan Pedruzzi
eXcelon Corporation
http://www.stylusstudio.com



> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx 
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of 
> Adam van den Hoven
> Sent: Wednesday, January 30, 2002 9:22 PM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject:  A community project
> 
> 
> More times than I can count I've written ASP code that takes 
> an xml document and an xsl document and performed the 
> transform using a number of global paramters. 
> 
> However time has past, jobs have changed and I am thrust into 
> a Java environment. Despite the fact that I was once a 
> promising Java coder, almost 3 years of ASP has eroded my 
> ability to do it. 
> 
> Does anyone have a JSP file they would care to share with the 
> rest of the world that takes xml and xsl files names from the 
> command line plus an arbitrary number of global parameters 
> and returns the resulting output? I'm thinking of something 
> like the following url:
> 
http://www.adamvandenhoven.com/transform.jsp?XML=/testsource.xsl&XSL=/te
stTrans.xsl&SomeParam=value&SomeOtherParam=AnotherValue

and the assumption is that testTrans.xsl looks something like:
<xsl:stylesheet>  <xsl:param name="SomeParam"/>  <xsl:param
name="SomeOtherParam"/>  <xsl:template match="/"> . . .


All I'm looking for the the basic JSP. There is no need to add caching
or doc pooling or whatever. I know guys who can add that for me. I also
don't care which parser to use.

Its a useful piece of code to have around. If I dig out the ASP version
from the bowels of my old work, I'll post it.

Have a great day,
Adam



__________________________________________________
D O T E A S Y - "Join the web hosting revolution!"
             http://www.doteasy.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.