KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > servlet > http > XSLFilter


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.servlet.http;
8
9 import java.io.IOException JavaDoc;
10 import java.io.PrintWriter JavaDoc;
11 import java.io.StringReader JavaDoc;
12 import java.io.StringWriter JavaDoc;
13
14 import javax.servlet.Filter JavaDoc;
15 import javax.servlet.FilterChain JavaDoc;
16 import javax.servlet.FilterConfig JavaDoc;
17 import javax.servlet.ServletException JavaDoc;
18 import javax.servlet.ServletRequest JavaDoc;
19 import javax.servlet.ServletResponse JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21 import javax.xml.transform.Source JavaDoc;
22 import javax.xml.transform.Transformer JavaDoc;
23 import javax.xml.transform.TransformerFactory JavaDoc;
24 import javax.xml.transform.stream.StreamResult JavaDoc;
25 import javax.xml.transform.stream.StreamSource JavaDoc;
26
27 import org.apache.log4j.Logger;
28
29 /**
30  * HTTP Servlet filter that applies a XSL stylesheet on a XML stream.
31  *
32  * @author Laurent Etiemble
33  * @version $Revision: 1.3 $
34  */

35 public class XSLFilter implements Filter JavaDoc
36 {
37    /** Filter configuration */
38    protected FilterConfig JavaDoc config = null;
39    /** Content type of the output */
40    protected String JavaDoc contentType = null;
41    /** Absolute path the stylesheet */
42    protected String JavaDoc stylePath = null;
43    /** XSL transformer */
44    protected Transformer JavaDoc transformer = null;
45    /** Log4j Logger */
46    protected static Logger logger = Logger.getLogger(XSLFilter.class);
47
48
49    /** Destroy the filter */
50    public void destroy()
51    {
52       this.config = null;
53    }
54
55
56    /**
57     * Main method of the filter
58     *
59     * @param request Incoming request
60     * @param response Outgoing response
61     * @param chain Filter chain to forward the request and the response
62     * @exception IOException In case of error
63     * @exception ServletException In case of error
64     */

65    public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain)
66       throws IOException JavaDoc, ServletException JavaDoc
67    {
68       // Set content type and get the original response
69
response.setContentType(contentType);
70       PrintWriter JavaDoc out = response.getWriter();
71
72       // Wrap the response with a buffer and follow the chain
73
StringResponseWrapper wrapper = new StringResponseWrapper((HttpServletResponse JavaDoc) response);
74       chain.doFilter(request, wrapper);
75
76       // Create a reader with the result
77
StringReader JavaDoc reader = new StringReader JavaDoc(wrapper.toString());
78       Source JavaDoc source = new StreamSource JavaDoc(reader);
79
80       try
81       {
82          // Create the final response writer
83
logger.debug("Creating Writer...");
84          StringWriter JavaDoc writer = new StringWriter JavaDoc();
85          StreamResult JavaDoc result = new StreamResult JavaDoc(writer);
86
87          // Transform according to XSL stylesheet
88
logger.debug("Applying stylesheet...");
89          transformer.transform(source, result);
90
91          // Print out the result
92
logger.debug("Ouputing...");
93          response.setContentLength(writer.toString().length());
94          out.println(writer.toString());
95       }
96       catch (Exception JavaDoc e)
97       {
98          logger.error("Error during transformation " + e.getMessage());
99          out.println(wrapper.toString());
100       }
101    }
102
103
104    /**
105     * Performs the initialization of the filter
106     *
107     * @param config The filter configuration
108     * @exception ServletException Exception thrown if the filter was not properly configured
109     */

110    public void init(FilterConfig JavaDoc config)
111       throws ServletException JavaDoc
112    {
113       this.config = config;
114
115       // Get the content type
116
this.contentType = config.getInitParameter("mime-type");
117       logger.debug("Acquiring parameter mime-type=" + contentType);
118
119       // Get the stylesheet
120
String JavaDoc styleSheet = config.getInitParameter("stylesheet");
121       logger.debug("Acquiring parameter stylesheet=" + styleSheet);
122
123       // Set the absolute position of the stylesheet
124
String JavaDoc stylePath = config.getServletContext().getRealPath(styleSheet);
125
126       try
127       {
128          // Prepare the XSL transformer
129
logger.debug("Compiling stylesheet...");
130          TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
131          transformer = tFactory.newTransformer(new StreamSource JavaDoc(stylePath));
132       }
133       catch (Exception JavaDoc e)
134       {
135          logger.fatal("Failed to init the filter " + e.getMessage());
136          throw new ServletException JavaDoc(e.getMessage());
137       }
138    }
139 }
140
Popular Tags