1 package org.astrogrid.common.j2ee;
2
3 import java.io.InputStream;
4 import java.io.StringWriter;
5 import javax.servlet.ServletContext;
6 import javax.xml.transform.Transformer;
7 import javax.xml.transform.TransformerFactory;
8 import javax.xml.transform.stream.StreamResult;
9 import javax.xml.transform.stream.StreamSource;
10
11 /***
12 * A Java bean, for use in JSPs, that tabulates the env-entry
13 * elements in the deployment descriptor of a web-application.
14 * The bean parses web.xml in its parent web-application and
15 * generates an HTML table showing the names, types and current
16 * values of the environment entries. The values are marked up
17 * as input elements. Hence, the table may be included in an
18 * HTML form to make an editor for the values.
19 *
20 * To use this class, instantiate it as a bean from a JSP
21 * using the jsp:useBean tag. Then set the context property to
22 * the value of the JSP's "application" variable: this passes
23 * the servlet context of the JSP into the bean. Finally, read
24 * the table property to extract the HTML. If you use the
25 * jsp:getProperty tag to do this then the table will be added
26 * in-line to the HTML output of the JSP. If this method fails to
27 * derive the table then it returns an HTML paragraph containing
28 * an error report; it never throws exceptions.
29 *
30 * Constructing the bean and setting the context property do little
31 * processing. The main work of transforming the XML to HTML is done
32 * when the table property is read.
33 *
34 * XSLT is used to transform the XML into HTML. The transformation
35 * script is read from an external file. @TODO: fix this?
36 *
37 * This class will only work inside a servlet container; it uses
38 * classes from javax.servlet to access web.xml.
39 *
40 * @deprecated Use the classes in
41 * {@link org.astrogrid.common.j2ee.environment} instead of this bean.
42 *
43 * @author Guy Rixon
44 */
45 public class EnvEntriesTabulator {
46
47 /***
48 * Constructs an EnvEntriesTabulator.
49 */
50 public EnvEntriesTabulator() {}
51
52 /***
53 * The servlet context to be used for finding web.xml.
54 */
55 private ServletContext context;
56
57 /***
58 * Sets the context property.
59 *
60 * @param context The servlet context of the calling servlet or JSP.
61 */
62 public void setContext(ServletContext context) {
63 this.context = context;
64 }
65
66 /***
67 * Gets the HTML table derived from the deployment descriptor.
68 * This accessor actually computes the table when it is asked for.
69 *
70 * @return The HTML table, or an error report if the table can't be derived.
71 */
72 public String getTable() {
73
74
75 if (this.context == null) {
76 return "<p>Error: the context property is not set " +
77 "in the tabulator bean.</p>";
78 }
79
80
81 try {
82
83
84 InputStream webXmlStream =
85 this.context.getResourceAsStream("/WEB-INF/web.xml");
86 StreamSource webXmlSource = new StreamSource(webXmlStream);
87
88
89 InputStream xsltStream =
90 this.context.getResourceAsStream("/webDotXmlToForm.xsl");
91 StreamSource xsltSource = new StreamSource(xsltStream);
92 TransformerFactory factory = TransformerFactory.newInstance();
93 Transformer transformer = factory.newTransformer(xsltSource);
94
95
96
97 StringWriter writer = new StringWriter();
98 StreamResult result = new StreamResult(writer);
99
100
101 transformer.transform(webXmlSource, result);
102
103
104 return writer.getBuffer().toString();
105
106 }
107 catch (Exception e) {
108 return "<p>Error: failed to extract the table: " + e + "</p>";
109 }
110 }
111
112 }