|
Subject: XSLServlet & Apache From: "Clark C. Evans" <clark.evans@xxxxxxxxxxxxxxxxxxxx> Date: Sat, 2 Oct 1999 23:05:39 -0400 (EDT) |
Hello all. JClark's XSLServlet that comes with XT
doesn't work "out-of-the-box" with Apache's JServ.
Here is a modified version that does, it also
re-loads the stylesheet if/when it is changed.
Big thanks to James Clark for XT... very cool.
The following is copyright by James Clark, with
mods by Clark Evans. My chanes are hereby placed
into the public domain; free without restriction.
...
import java.io.IOException;
import java.io.File;
import java.io.Writer;
import java.net.URL;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
import org.xml.sax.*;
import com.jclark.xsl.sax.*;
public class XSLServlet extends HttpServlet {
// stylesheet cashing
private transient XSLProcessor cache_processor = null;
private transient File cache_file = null;
private transient long cache_timestamp = 0L;
private transient Parser cache_parser = null;
// error handling
private transient String err_str = null;
private transient Exception err_obj = null;
private transient String err_place = "?";
public void init(ServletConfig config) throws ServletException
{
super.init(config);
try
{
String stylesheet = config.getInitParameter("stylesheet");
if (stylesheet == null) {
err_obj = null;
err_str = "The 'stylesheet' parameter is not found.";
return;
}
cache_file = new File(stylesheet);
if(!cache_file.isFile()) {
err_str = "<p>Stylesheet '"+ stylesheet+ "' not found.";
err_obj = null;
return;
}
}
catch (Exception e)
{
err_str = "Could not initialize XSL Servlet";
err_obj = e;
return;
}
try
{
String parserClass =
System.getProperty("com.jclark.xsl.sax.parser");
if (parserClass == null)
parserClass = System.getProperty("org.xml.sax.parser");
if (parserClass == null)
parserClass = "com.jclark.xml.sax.CommentDriver";
cache_parser = (Parser)Class.forName(parserClass).newInstance();
}
catch (Exception e)
{
err_str = "Could not create XML Parser object";
err_obj = e;
return;
}
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
if(err_str != null)
{
writeBadNews(response);
return;
}
XSLProcessor xsl = null;
String path = null;
try
{
if( cache_file.lastModified() != cache_timestamp )
{
cache_timestamp = cache_file.lastModified();
cache_processor = new XSLProcessorImpl();
cache_processor.setParser(cache_parser);
cache_processor.loadStylesheet(fileInputSource(cache_file));
}
xsl = (XSLProcessor) cache_processor.clone();
}
catch (Exception e)
{
err_str = "Could not create XSL processor";
err_obj = e;
writeBadNews(response);
return;
}
try
{
path = request.getPathTranslated();
File inputFile = new File(path);
if( null == inputFile || !inputFile.isFile()) {
err_str = "File not found: " + path;
writeBadNews(response);
return;
}
for (Enumeration e = request.getParameterNames();
e.hasMoreElements(); )
{
String name = (String)e.nextElement();
// What to do about multiple values?
xsl.setParameter(name, request.getParameter(name));
}
OutputMethodHandlerImpl outputMethodHandler =
new OutputMethodHandlerImpl(xsl);
xsl.setOutputMethodHandler(outputMethodHandler);
outputMethodHandler.setDestination(new
ServletDestination(response));
xsl.parse(fileInputSource(inputFile));
} catch (Exception e) {
err_str = "Error while serving up: " + path;
err_obj = e;
writeBadNews(response);
}
}
/**
* Generates an <code>InputSource</code> from a file name.
*/
static public InputSource fileInputSource(File file) {
String path = file.getAbsolutePath();
String fSep = System.getProperty("file.separator");
if (fSep != null && fSep.length() == 1)
path = path.replace(fSep.charAt(0), '/');
if (path.length() > 0 && path.charAt(0) != '/')
path = '/' + path;
try {
return new InputSource(new URL("file", "", path).toString());
}
catch (java.net.MalformedURLException e) {
/* According to the spec this could only happen if the file
protocol were not recognized. */
throw new Error("unexpected MalformedURLException");
}
}
private void writeBadNews(HttpServletResponse response)
throws ServletException, IOException
{
java.io.PrintWriter out;
String title = "XSL Servlet Error";
response.setContentType("text/html");
out = response.getWriter();
out.println("<HTML><HEAD><TITLE>");
out.println(title);
out.println("</TITLE></HEAD><BODY bgcolor=\"#FFFFFF\">");
out.println("<H1>" + title + "</H1>");
out.println("<H2>" + err_str + "</H2><br>");
out.println("<b> #"+ err_place + "#</b><br>");
if(err_obj != null)
{
out.println(err_obj.toString());
err_obj.printStackTrace(out);
}
out.println("</BODY></HTML>");
out.close();
err_obj = null;
err_str = null;
}
}
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
| Current Thread |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| Re: XT Javadocs?, zun | Thread | select-distinct in for-each ?, Clark C. Evans |
| decode( source,'if-one','1', ... , , Clark C. Evans | Date | select-distinct in for-each ?, Clark C. Evans |
| Month |