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.define;
17  
18  import org.apache.commons.jelly.JellyTagException;
19  import org.apache.commons.jelly.Script;
20  import org.apache.commons.jelly.Tag;
21  import org.apache.commons.jelly.TagSupport;
22  import org.apache.commons.jelly.XMLOutput;
23  import org.apache.commons.jelly.impl.DynamicTag;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  /***
28   * <invokeBody> tag is used inside a <tag> tag
29   * (i.e. the definition of a dynamic tag) to invoke the tags body when
30   * the tag is invoked.
31   *
32   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
33   * @version $Revision: 155420 $
34   */
35  public class InvokeBodyTag extends TagSupport {
36  
37      /*** The Log to which logging calls will be made. */
38      private static final Log log = LogFactory.getLog(InvokeBodyTag.class);
39  
40      public InvokeBodyTag() {
41      }
42  
43      // Tag interface
44      //-------------------------------------------------------------------------
45      public void doTag(XMLOutput output) throws JellyTagException {
46          // Try find find the body from the reserved 'org.apache.commons.jelly.body' variable
47          Script script = (Script) context.getVariable("org.apache.commons.jelly.body");
48          if (script != null) {
49              script.run(context, output);
50          }
51          else {
52              // note this mechanism does not work properly for arbitrarily
53              // nested dynamic tags. A better way is required.
54              Tag tag = findAncestorWithClass(this, DynamicTag.class);
55              if (tag == null) {
56                  throw new JellyTagException("Cannot invoke body, no dynamic tag is defined in this block");
57              }
58              else {
59                  tag.invokeBody(output);
60              }
61          }
62      }
63  }