View Javadoc

1   /*
2    * Copyright 2002,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  package org.apache.commons.jelly.tags.jsl;
17  
18  import org.apache.commons.jelly.JellyTagException;
19  import org.apache.commons.jelly.MissingAttributeException;
20  import org.apache.commons.jelly.XMLOutput;
21  import org.apache.commons.jelly.xpath.XPathTagSupport;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.dom4j.rule.Stylesheet;
25  import org.jaxen.JaxenException;
26  import org.jaxen.XPath;
27  
28  /***
29   * This tag performs a JSL stylesheet which was previously
30   * created via an <stylesheet> tag.
31   *
32   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
33   * @version $Revision: 155420 $
34   */
35  public class StyleTag extends XPathTagSupport {
36  
37      /*** The Log to which logging calls will be made. */
38      private Log log = LogFactory.getLog(StyleTag.class);
39  
40  
41      /*** Holds the stylesheet which will be applied to the source context. */
42      private Stylesheet stylesheet;
43  
44      /*** The XPath expression to evaluate. */
45      private XPath select;
46  
47      public StyleTag() {
48      }
49  
50      // Tag interface
51      //-------------------------------------------------------------------------
52      public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
53          Stylesheet stylesheet = getStylesheet();
54          if (stylesheet == null) {
55              throw new MissingAttributeException("stylesheet");
56          }
57  
58          if (stylesheet instanceof JellyStylesheet) {
59              JellyStylesheet jellyStyle = (JellyStylesheet) stylesheet;
60              jellyStyle.setOutput(output);
61          }
62  
63          // dom4j only seems to throw Exception
64          try {
65              Object source = getSource();
66              if (log.isDebugEnabled()) {
67                  log.debug("About to evaluate stylesheet on source: " + source);
68              }
69  
70              stylesheet.run(source);
71          } catch (Exception e) {
72              throw new JellyTagException(e);
73          }
74      }
75  
76  
77      // Properties
78      //-------------------------------------------------------------------------
79  
80      public Stylesheet getStylesheet() {
81          return stylesheet;
82      }
83  
84      /***
85       * Sets the stylesheet to use to style this tags body
86       */
87      public void setStylesheet(Stylesheet stylesheet) {
88          this.stylesheet = stylesheet;
89      }
90  
91      /*** Sets the XPath expression to evaluate. */
92      public void setSelect(XPath select) {
93          this.select = select;
94      }
95  
96      // Implementation methods
97      //-------------------------------------------------------------------------
98  
99      /*** @return the source on which the stylesheet should run
100      */
101     protected Object getSource() throws JaxenException {
102         Object source = getXPathContext();
103         if ( select != null ) {
104             return select.evaluate(source);
105         }
106         return source;
107     }
108 }