FileDocCategorySizeDatePackage
StyleServlet.javaAPI DocExample1987Tue Nov 20 12:37:22 GMT 2001None

StyleServlet

public class StyleServlet extends HttpServlet
Output the given XML file in XML if viewable, else in HTML.

Fields Summary
public String
XML_FILE
public String
SHEET_FILE
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)


		/** Servlet API 2.1: Web App Root/WEB-INF/web.xml includes
		 * context-param name & value elements; this retrieves 'em.
		 */
		ServletContext ctx = getServletContext();
             String value = getServletContext().getInitParameter("name");
		XML_FILE = ctx.getInitParam("xml_file_name");
		SHEET_FILE = ctx.getInitParam("xsl_sheet_name"); 

		String browser = request.getHeader("user-agent");
		PrintWriter out = response.getWriter();

		// At this point in time, MSIE5 is the only one that can do XML.
		if (browser.indexOf("MSIE5") != -1) {

			out.println("<?xml version=\"1.0\"?>");

			output_stylesheet_ref(out);

			output_body(XML_FILE, out);

		} else {
			// Any other browser, output HTML
			transform_into_html(out);
		} 
	
voidoutput_body(java.lang.String XML_FILE, java.io.PrintWriter out)

		com.darwinsys.util.FileIO.copy(XML_FILE, out);
	
voidoutput_stylesheet_ref(java.io.PrintWriter out)

		out.print("<?xml-stylesheet type=\"text/xsl\" href=\"" +
			SHEET_FILE + "\"?>");
	
voidtransform_into_html(java.io.PrintWriter out)


		try {
			XSLTProcessor myProcessor = XSLTProcessorFactory.getProcessor();
			XSLTInputSource xmlSource = new XSLTInputSource(XML_FILE);
			XSLTInputSource xslStylesheet = new XSLTInputSource(SHEET_FILE);
			XSLTResultTarget xmlOutput = new XSLTResultTarget(out);
			myProcessor.process(xmlSource, xslStylesheet, xmlOutput);
		}
		catch (org.xml.sax.SAXException exc) {
			throw new ServletException("XML error: " + exc.toString());
		}
		catch (Exception exc) {
			throw new ServletException(exc.toString());
		}