Why not just put the xml parser that you are using on the classpath
before
your servlet engine's classes.  The Servlet spec does not lay out a
standard
way for the servlet engine to be started, which is where this needs to
be
done.

For example, suppose your are using Xalan / Xerces with Weblogic:

CLASSPATH = xalan.jar:xerces.jar:weblogicaux.jar:others
java -classpath $CLASSPATH MainClass

I need to do just this since Weblogic's parser doesn't contain featues
required by Xalan.  It seems to be working ok for me.  In fact that
is how many vendors apply service packs and patches, just by putting
the patch/service pack in the classpath in front of the main classes.

I don't think that a special class loader is necessary.

-----Original Message-----
From: Dave Ford [mailto:[EMAIL PROTECTED]]
Sent: Sunday, May 06, 2001 1:50 PM
To: [EMAIL PROTECTED]
Subject: xsl library conflict


Problem:

Suppose one wants to use xsl transforms in a web-app and the web-app
must be
servlet-engine-independent. As it turns out, this is no easy task. Why?
Because most servlet engines ship with a built-in xml library. Your
servlet
can, if desired, use this built-in xml library. The problem is, your
servlet
is no longer servlet-engine-independent because you are relying on the
servlet-engine's built-in xml library. And not ALL servlet-engines have
a
built-in xml library. And even if they did, there is no guarantee that
it
would be the same version as that required by your servlet.

One possible solution might be to ship your preferred xml-library as
part of
the web-app. That, after all, is why WEB-INF/lib was invented.
Unfortunately, this won't work. Why? Because the servlet-engine's
built-in
xml parser will probably conflict with the one you ship with your
web-app
(i.e. the one in WEB-INF/lib).

Solution:

After struggling with this problem for a week or so, I finally came up
with
a solution (actually I got the idea from my friend Jordan and the people
on
this mailing list): Use a separate class loader to load your xml
libraries.
Thus, I created a servlet called XSLServlet. This can be dropped in to
any
web-app to provide servlet-engine-independent xsl support.

It works like this:

http://yourhost/yourwebapp/somexmlfile.xml?xsl=/somestylesheet.xsl

Three things to note: 1. the style sheet path is web-app relative. 2.
you
must add a servlet-mapping entry to your web.xml file for this to work.
3.
Place the 3 jaxp xml jar files into your web-app in folder called
c:\MyWebApp\lib (substitute your web-app's path)

I thought this might be useful to others, so I have included the source
code
at the end of this e-mail.

Dave Ford
Smart Soft - The Java Training Company
http://www.smart-soft.com

=========================================================
package ss.util;

import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.reflect.*;

public class XSLServlet extends HttpServlet{

  public void doGet(HttpServletRequest request, HttpServletResponse
response) throws IOException,ServletException{
    ServletContext application = getServletContext();
    URL[] urls = new URL[3];
    urls[0] = new URL("file:/" +
application.getRealPath("/lib/xalan.jar"));
    urls[1] = new URL("file:/" +
application.getRealPath("/lib/crimson.jar"));
    urls[2] = new URL("file:/" +
application.getRealPath("/lib/jaxp.jar"));

    URLClassLoader loader = new URLClassLoader(urls,null);
    try{
      Class transformerFactoryClass =
loader.loadClass("javax.xml.transform.TransformerFactory");
      Class transformerClass =
loader.loadClass("javax.xml.transform.Transformer");
      Class streamSourceClass =
loader.loadClass("javax.xml.transform.stream.StreamSource");
      Class streamResultClass =
loader.loadClass("javax.xml.transform.stream.StreamResult");
      Class sourceClass =
loader.loadClass("javax.xml.transform.Source");
      Class resultClass =
loader.loadClass("javax.xml.transform.Result");

      Method transformerFactoryNewInstanceMethod =
transformerFactoryClass.getMethod("newInstance",null);
      Constructor streamSourceContructor =
streamSourceClass.getConstructor(new Class[]{String.class});
      Constructor streamResultContructor =
streamResultClass.getConstructor(new Class[]{Writer.class});
      Method transformerFactoryNewTransformerMethod =
transformerFactoryClass.getMethod("newTransformer",new
Class[]{sourceClass});
      Method transformerTransformMethod =
transformerClass.getMethod("transform",new
Class[]{sourceClass,resultClass});

      response.setContentType("text/html");
      PrintWriter out = response.getWriter();

      String servletPath = request.getServletPath();
      if(servletPath==null || servletPath.trim().equals("")){
        throw new IllegalArgumentException("servletPath missing");
      }
      String xmlpath = application.getRealPath(servletPath);

      String xsl = request.getParameter("xsl");
      String xslpath;
      if(xsl==null || xsl.trim().equals("")){
        String xmlfilename =
servletPath.substring(servletPath.lastIndexOf("/")+1);
        xsl = xmlfilename.substring(0,xmlfilename.length()-4) + ".xsl";
      }
      xslpath = application.getRealPath("/StyleSheets/" + xsl );

      Object xslsource = streamSourceContructor.newInstance(new
Object[]{xslpath});
      Object xmlsource = streamSourceContructor.newInstance(new
Object[]{xmlpath});
      Object result  = streamResultContructor.newInstance(new
Object[]{out});
      Object transformerFactory =
transformerFactoryNewInstanceMethod.invoke(null,null);
      Object transformer =
transformerFactoryNewTransformerMethod.invoke(transformerFactory ,new
Object[]{xslsource});
      transformerTransformMethod.invoke(transformer,new
Object[]{xmlsource,result});
    }
    catch(Exception ex){
      System.out.println(ex);
      throw new RuntimeException(ex.toString());
    }

  }
}

________________________________________________________________________
___
To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to