View Javadoc

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 at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * 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 and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.util.servlet;
18  
19  // Java classes
20  import java.io.OutputStream;
21  import java.io.PrintWriter;
22  import java.io.StringReader;
23  import java.util.Map;
24  
25  // SAX Classes
26  import org.xml.sax.InputSource;
27  
28  // ECS classes
29  import org.apache.ecs.ConcreteElement;
30  
31  // Jetspeed classes
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          /*    // Get a new XSLT Processor 
86              XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
87  
88              // set the parameters for the stylesheet
89              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              }
98  
99              //  process the stylesheet
100             processor.process( content_, stylesheet_, new XSLTResultTarget(out) ); */
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 }