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.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      // Tag interface
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          // for some reason, these DOM4J methods only throw Exception
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          // #### should support MODE!!!
80  
81      }
82  
83      // Properties
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  }