1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
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
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 }