XslApplyTag.java.

Graphic Java
Create XML files using JSP
David Geary
Listing 5. XslApplyTag.java.


package com.companyName.tags.xsl;

import java.io.File;
import javax.servlet.*;
import javax.servlet.jsp.JspException;
import javax.servlet.http.*;
import javax.servlet.jsp.tagext.TagSupport;
import com.companyName.beans.xsl.XslServletBean;

public class XslApplyTag extends TagSupport {
	private String xml, xsl;

	public void setXml(String xml) { this.xml = xml; }
	public void setXsl(String xsl) { this.xsl = xsl; }

	public int doEndTag() throws JspException {
		File tempDir = (File)pageContext.getServletContext().
			  getAttribute("javax.servlet.context.tempdir");
		File xmlFile = new File(tempDir.getAbsolutePath() + "/" + xml);
		
		ServletRequest req = pageContext.getRequest();
		ServletResponse res = pageContext.getResponse();

		req.setAttribute("xml-file", xml);
		req.setAttribute("xsl-file", xsl);

		XslServletBean xslBean = new XslServletBean();
		ServletContext context = pageContext.getServletContext();

		try {
			xslBean.process(xmlFile,
					context.getResource(xsl),
					(HttpServletRequest)req,
					(HttpServletResponse)pageContext.getResponse());
		}
		catch(Exception ex) {
			throw new JspException(ex.getMessage());
		}
		return EVAL_PAGE;
	}
}

About the Author

David Geary was the lead engineer for the Java Management API GUI toolkit, and is now an independent Java consultant. David is the author of Graphic Java—Mastering the JFC. He can be contacted at [email protected].