1/*2 * Copyright 2000-2001,2004 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.util.servlet;
1819// Java classes20import java.io.OutputStream;
21import java.io.PrintWriter;
22import java.io.StringReader;
23import java.util.Map;
2425// SAX Classes26import org.xml.sax.InputSource;
2728// ECS classes29import org.apache.ecs.ConcreteElement;
3031// Jetspeed classes32import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
33import org.apache.jetspeed.services.logging.JetspeedLogger;
34import org.apache.jetspeed.util.SimpleTransform;
3536/***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 without45 * buffering strings with the transformation results. Transformation is invoked when46 * traversal of an ECS tree during writing reaches the EcsStylesheetElement.47 *48 * @author Thomas Schaeck (schaeck@de.ibm.com) 49 */50publicclassEcsStylesheetElementextends ConcreteElement
51 {
52/***53 * Static initialization of the logger for this class54 */55privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(EcsStylesheetElement.class.getName());
5657/***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 */63publicvoid output(OutputStream out)
64 {
65 output(new PrintWriter(out));
66 }
6768/***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 */74publicvoid output(PrintWriter out)
75 {
76try {
7778 StringReader rdr = new StringReader (SimpleTransform.transform( content_, stylesheet_, params_ ) );
79int count = 0;
80char buff[] = newchar[1024];
81while( (count = rdr.read( buff, 0, buff.length ) ) > 0 ) {
82 out.write( buff, 0, count );
83 }
8485/* // Get a new XSLT Processor 86 XSLTProcessor processor = XSLTProcessorFactory.getProcessor();8788 // set the parameters for the stylesheet89 if (params_ != null) 90 {91 Enumeration keys = params_.keys();92 while (keys.hasMoreElements()) 93 {94 String name = (String) keys.nextElement();95 processor.setStylesheetParam(name, (String) params_.get(name));96 }97 }9899 // process the stylesheet100 processor.process( content_, stylesheet_, new XSLTResultTarget(out) ); */101102 } 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 }
110111112/*** XML content to be processed. */113private InputSource content_;
114115/*** Parameters to be used by the stylesheet. */116private Map params_;
117118/*** XSLT stylesheet to be used for rendering the content. */119private InputSource stylesheet_;
120121/***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 processed126 * @param stylesheet XSLT stylesheet to be used for processing the content127 * @param params parameters for the stylesheet128 */129publicEcsStylesheetElement( InputSource content,
130 InputSource stylesheet,
131 Map params )
132 {
133 content_ = content;
134 stylesheet_ = stylesheet;
135 params_ = params;
136137 }
138139 }