FileDocCategorySizeDatePackage
StyleServlet.javaAPI DocExample2343Sun Feb 08 21:34:10 GMT 2004None

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 contains
		 * context-param name & value elements; this retrieves 'em.
		 */
		ServletContext ctx = getServletContext();
		String value = ctx.getInitParameter("name");
		XML_FILE = ctx.getInitParameter("xml_file_name");
		SHEET_FILE = ctx.getInitParameter("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)

		FileIO.copyFile(XML_FILE, out, false);
	
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());
		}