This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: A community project



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@lists.mulberrytech.com 
> [mailto:owner-xsl-list@lists.mulberrytech.com] On Behalf Of 
> Adam van den Hoven
> Sent: Wednesday, January 30, 2002 9:22 PM
> To: xsl-list@lists.mulberrytech.com
> Subject: [xsl] 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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]