1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.util.servlet;
18
19
20 import java.io.OutputStream;
21 import java.io.PrintWriter;
22 import java.io.StringReader;
23 import java.util.Map;
24
25
26 import org.xml.sax.InputSource;
27
28
29 import org.apache.ecs.ConcreteElement;
30
31
32 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
33 import org.apache.jetspeed.services.logging.JetspeedLogger;
34 import org.apache.jetspeed.util.SimpleTransform;
35
36 /***
37 * NOTE: The use of Ecs for aggregating portlet content is deprecated!
38 * This utility class will be removed once we don't have the ecs
39 * dependency any more.
40 *
41 * EcsStylesheetElement encapsulates XML data, a stylesheet and the parameters for
42 * processing the XML data within the context of ECS markup.
43 *
44 * This is a workaround to allow invoking stylesheets from JetSpeed Portlets without
45 * buffering strings with the transformation results. Transformation is invoked when
46 * traversal of an ECS tree during writing reaches the EcsStylesheetElement.
47 *
48 * @author Thomas Schaeck (schaeck@de.ibm.com)
49 */
50 public class EcsStylesheetElement extends ConcreteElement
51 {
52 /***
53 * Static initialization of the logger for this class
54 */
55 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(EcsStylesheetElement.class.getName());
56
57 /***
58 * Processes the referenced XML content using the referenced XSLT stylesheet and
59 * parameters.
60 *
61 * @param out The output stream to which the result shall be written.
62 */
63 public void output(OutputStream out)
64 {
65 output(new PrintWriter(out));
66 }
67
68 /***
69 * Processes the referenced XML content using the referenced XSLT stylesheet and
70 * parameters.
71 *
72 * @param out The print writer to be used for writing the result.
73 */
74 public void output(PrintWriter out)
75 {
76 try {
77
78 StringReader rdr = new StringReader (SimpleTransform.transform( content_, stylesheet_, params_ ) );
79 int count = 0;
80 char buff[] = new char[1024];
81 while( (count = rdr.read( buff, 0, buff.length ) ) > 0 ) {
82 out.write( buff, 0, count );
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 } catch (Exception e)
103 {
104 String message = "ECSStylesheetElement.output(PrintWriter): error processing stylesheet" + e.getMessage();
105 logger.error(message, e);
106 out.print(message);
107 e.printStackTrace(out);
108 }
109 }
110
111
112 /*** XML content to be processed. */
113 private InputSource content_;
114
115 /*** Parameters to be used by the stylesheet. */
116 private Map params_;
117
118 /*** XSLT stylesheet to be used for rendering the content. */
119 private InputSource stylesheet_;
120
121 /***
122 * Construct an ECS element that will render a given XML dicument using a given
123 * stylesheet and parameters when one of its output methods is invoked.
124 *
125 * @param content XML content to be processed
126 * @param stylesheet XSLT stylesheet to be used for processing the content
127 * @param params parameters for the stylesheet
128 */
129 public EcsStylesheetElement( InputSource content,
130 InputSource stylesheet,
131 Map params )
132 {
133 content_ = content;
134 stylesheet_ = stylesheet;
135 params_ = params;
136
137 }
138
139 }