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.TagSupport;
20 import org.apache.commons.jelly.XMLOutput;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.dom4j.rule.Stylesheet;
24 import org.jaxen.XPath;
25
26 /***
27 * Implements the apply templates function in the stylesheet, similar to the XSLT equivalent.
28 * a JSP include.
29 *
30 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31 * @version $Revision: 155420 $
32 */
33 public class ApplyTemplatesTag extends TagSupport {
34
35 /*** The Log to which logging calls will be made. */
36 private Log log = LogFactory.getLog(ApplyTemplatesTag.class);
37
38 /*** Holds value of property mode. */
39 private String mode;
40
41 /*** Holds the XPath object */
42 private XPath select;
43
44
45 public ApplyTemplatesTag() {
46 }
47
48
49
50 /*** By default just evaluate the body */
51 public void doTag(XMLOutput output) throws JellyTagException {
52 StylesheetTag tag = (StylesheetTag) findAncestorWithClass( StylesheetTag.class );
53 if (tag == null) {
54 throw new JellyTagException(
55 "<applyTemplates> tag must be inside a <stylesheet> tag"
56 );
57 }
58 Stylesheet stylesheet = tag.getStylesheet();
59
60 XMLOutput oldOutput = tag.getStylesheetOutput();
61 tag.setStylesheetOutput(output);
62
63 Object source = tag.getXPathSource();
64
65 try {
66 if ( select != null ) {
67 stylesheet.applyTemplates( source, select, mode );
68 }
69 else {
70 stylesheet.applyTemplates( source, mode );
71 }
72 }
73 catch (Exception e) {
74 throw new JellyTagException(e);
75 }
76
77 tag.setStylesheetOutput(oldOutput);
78
79
80
81 }
82
83
84
85
86 public void setSelect( XPath select ) {
87 this.select = select;
88 }
89
90 /*** Sets the mode.
91 * @param mode New value of property mode.
92 */
93 public void setMode(String mode) {
94 this.mode = mode;
95 }
96 }