KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > xsl > ApplyTag


1 /*
2  * Copyright 1999,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.taglibs.xsl;
18
19
20 import java.io.InputStream JavaDoc;
21 import java.io.Reader JavaDoc;
22 import java.io.StringReader JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
27 import org.apache.xalan.xslt.XSLTInputSource;
28 import org.apache.xalan.xslt.XSLTProcessor;
29 import org.apache.xalan.xslt.XSLTProcessorFactory;
30 import org.apache.xalan.xslt.XSLTResultTarget;
31 import org.w3c.dom.Node JavaDoc;
32 import org.xml.sax.InputSource JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34
35
36 /**
37  * Apply an XSL stylesheet to an XML data source, rendering the output
38  * to the writer of our JSP page. This tag uses the Xalan XSLT processor,
39  * available at <code>http://xml.apache.org</code>.
40  *
41  * @author Craig R. McClanahan
42  * @version $Revision: 1.2 $ $Date: 2004/03/08 02:41:31 $
43  */

44
45 public class ApplyTag extends BodyTagSupport JavaDoc {
46
47
48     // ------------------------------------------------------------- Properties
49

50
51     /**
52      * The body content of this tag, if we are using it as the data source.
53      */

54     private String JavaDoc body = null;
55
56
57     /**
58      * The name of the XML data bean.
59      */

60     private String JavaDoc nameXml = null;
61
62     public String JavaDoc getNameXml() {
63     return (this.nameXml);
64     }
65
66     public void setNameXml(String JavaDoc nameXml) {
67     this.nameXml = nameXml;
68     }
69
70
71     /**
72      * The name of the XSL stylesheet bean.
73      */

74     private String JavaDoc nameXsl = null;
75
76     public String JavaDoc getNameXsl() {
77     return (this.nameXsl);
78     }
79
80     public void setNameXsl(String JavaDoc nameXsl) {
81     this.nameXsl = nameXsl;
82     }
83
84
85     /**
86      * The property of the XML data bean.
87      */

88     private String JavaDoc propertyXml = null;
89
90     public String JavaDoc getPropertyXml() {
91     return (this.propertyXml);
92     }
93
94     public void setPropertyXml(String JavaDoc propertyXml) {
95     this.propertyXml = propertyXml;
96     }
97
98
99     /**
100      * The property of the XSL stylesheet bean.
101      */

102     private String JavaDoc propertyXsl = null;
103
104     public String JavaDoc getPropertyXsl() {
105     return (this.propertyXsl);
106     }
107
108     public void setPropertyXsl(String JavaDoc propertyXsl) {
109     this.propertyXsl = propertyXsl;
110     }
111
112
113     /**
114      * The XML data resource.
115      */

116     private String JavaDoc xml = null;
117
118     public String JavaDoc getXml() {
119     return (this.xml);
120     }
121
122     public void setXml(String JavaDoc xml) {
123     this.xml = xml;
124     }
125
126
127     /**
128      * The XSL stylesheet resource.
129      */

130     private String JavaDoc xsl = null;
131
132     public String JavaDoc getXsl() {
133     return (this.xsl);
134     }
135
136     public void setXsl(String JavaDoc xsl) {
137     this.xsl = xsl;
138     }
139
140
141     // --------------------------------------------------------- Public Methods
142

143
144     /**
145      * Validate the attributes that were specified for consistency.
146      * Evaluate the body content of this tag if we will be using it as the
147      * XML data source; otherwise skip it.
148      *
149      * @exception JspException if a JSP error occurs
150      */

151     public int doStartTag() throws JspException JavaDoc {
152
153     // Validate the data source attributes
154
if (nameXml != null) {
155         if (xml != null)
156         throw new JspException JavaDoc
157             ("Cannot specify both 'nameXml' and 'xml'");
158     } else if (propertyXml != null) {
159         throw new JspException JavaDoc
160         ("Cannot specify 'propertyXml' without 'nameXml'");
161     }
162
163     // Validate the stylesheet source attributes
164
if (nameXsl != null) {
165         if (xsl != null)
166         throw new JspException JavaDoc
167             ("Cannot specify both 'nameXsl' and 'xsl'");
168     } else if (propertyXsl != null) {
169         throw new JspException JavaDoc
170         ("Cannot specify 'propertyXsl' without 'nameXsl'");
171     }
172     if ((nameXsl == null) && (xsl == null)) {
173         throw new JspException JavaDoc
174         ("Must specify either 'nameXsl' or 'xsl'");
175     }
176
177     // Evaluate the tag body only if we need it
178
if ((nameXml == null) && (xml == null))
179         return (EVAL_BODY_TAG);
180     else
181         return (SKIP_BODY);
182
183     }
184
185
186     /**
187      * Save the body content that has been processed, but do not iterate.
188      *
189      * @exception JspException if a JSP error has occurred
190      */

191     public int doAfterBody() throws JspException JavaDoc {
192
193     if (bodyContent == null)
194         body = "";
195     else
196         body = bodyContent.getString().trim();
197     return (SKIP_BODY);
198
199     }
200
201
202     /**
203      * Finish up by performing the transformation and rendering the output.
204      *
205      * @exception JspException if a JSP exception occurs
206      */

207     public int doEndTag() throws JspException JavaDoc {
208
209     // Prepare an input source for the data
210
XSLTInputSource data = null;
211     if (body != null)
212         data = new XSLTInputSource(new StringReader JavaDoc(body));
213     else
214         data = getInputSource(nameXml, propertyXml, xml);
215
216     // Prepare an input source for the stylesheet
217
XSLTInputSource style =
218         getInputSource(nameXsl, propertyXsl, xsl);
219
220     // Prepare an output source for the results
221
XSLTResultTarget result =
222         new XSLTResultTarget(pageContext.getOut());
223
224     // Create an XSLT processor and use it to perform the transformation
225
XSLTProcessor processor = null;
226     try {
227         processor = XSLTProcessorFactory.getProcessor();
228         processor.process(data, style, result);
229     } catch (SAXException JavaDoc e) {
230         throw new JspException JavaDoc(e.toString());
231     }
232     return (EVAL_PAGE);
233
234     }
235
236
237     /**
238      * Release any allocated resources.
239      */

240     public void release() {
241
242     this.body = null;
243
244     }
245
246
247     // -------------------------------------------------------- Private Methods
248

249
250     /**
251      * Construct and return an XSLTInputSource based on the specified
252      * parameters.
253      *
254      * @param name Name of a bean containing the input source (or has a
255      * property that returns the input source)
256      * @param property Name of a property of the specified bean that
257      * returns the input source
258      * @param resource Context-relative path to an application resource
259      * that provides the input source
260      *
261      * @exception JspException if a JSP error occurs
262      */

263     private XSLTInputSource getInputSource(String JavaDoc name, String JavaDoc property,
264                        String JavaDoc resource)
265     throws JspException JavaDoc {
266
267
268     // If the resource is specified, use that for the input source
269
if (resource != null) {
270         ServletContext JavaDoc context = pageContext.getServletContext();
271         if (context == null)
272         throw new JspException JavaDoc("Cannot find servlet context");
273         InputStream JavaDoc stream =
274         context.getResourceAsStream(resource);
275         if (stream == null)
276         throw new JspException JavaDoc("Missing resource '" + resource + "'");
277         return new XSLTInputSource(stream);
278     }
279
280     // Locate the source object
281
Object JavaDoc source = null;
282     Object JavaDoc bean = pageContext.findAttribute(name);
283     if (bean == null)
284         throw new JspException JavaDoc("Missing bean '" + name + "'");
285     if (property == null)
286         source = bean;
287     else {
288         try {
289         char first = Character.toUpperCase(property.charAt(0));
290         String JavaDoc methodName = "get" + first + property.substring(1);
291         Class JavaDoc paramTypes[] = new Class JavaDoc[0];
292         Method JavaDoc method =
293             bean.getClass().getMethod(methodName, paramTypes);
294         source = method.invoke(bean, new Object JavaDoc[0]);
295         } catch (Exception JavaDoc e) {
296         throw new JspException JavaDoc(e.toString());
297         }
298     }
299
300
301     // Create an XSLTInputSource for the specified source object
302
if (source instanceof XSLTInputSource)
303         return ((XSLTInputSource) source);
304     else if (source instanceof String JavaDoc)
305         return (new XSLTInputSource(new StringReader JavaDoc((String JavaDoc) source)));
306     else if (source instanceof InputSource)
307         return (new XSLTInputSource((InputSource) source));
308     else if (source instanceof InputStream JavaDoc)
309         return (new XSLTInputSource((InputStream JavaDoc) source));
310     else if (source instanceof Node JavaDoc)
311         return (new XSLTInputSource((Node JavaDoc) source));
312     else if (source instanceof Reader JavaDoc)
313         return (new XSLTInputSource((Reader JavaDoc) source));
314     else
315         throw new JspException JavaDoc("Invalid input source type '" +
316                    source.getClass().getName() + "'");
317
318     }
319
320
321 }
322
Popular Tags