WEB-INF/classes/beans/xsl/XslProcessorBean.java.

Graphic Java
Servlet filters

David Geary
Listing 2. WEB-INF/classes/beans/xsl/XslProcessorBean.java.


package beans.xsl;

import java.io.*;
import java.net.URL;
import javax.servlet.*;
import javax.servlet.http.*;

import org.xml.sax.InputSource;
import org.apache.trax.*;

public class XslProcessorBean implements Serializable {
   public XslProcessorBean() {}

   public void process(Reader xmlstream,   InputStream xslstream,
                       ServletRequest req, ServletResponse res)
                             throws IOException, ServletException {
      Processor processor = createProcessor();
      InputSource xmlSource = new InputSource(xmlstream),
                  xslSource = new InputSource(xslstream);

      if(xmlSource != null && xslSource != null) {
         try {
            Templates templates = processor.process(xslSource);
            Transformer transformer = templates.newTransformer();
            transformer.transform(xmlSource,
                          new Result(res.getOutputStream()));
         }
         catch(Exception ex) {
            throw new ServletException(ex);
         }
      }
   }
   private Processor createProcessor() throws ServletException {
      Processor processor = null;
      try {
         processor = Processor.newInstance(‘xslt’);
      }
      catch(Exception ex) {
         throw new ServletException(ex);
      }
      return processor;
   }
}

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 sabreware@tri-lakesonline.net.

Featured

Most   Popular
Upcoming Events

AppTrends

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.