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.impl;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  import java.util.Iterator;
21  
22  import org.apache.commons.jelly.DynaTagSupport;
23  import org.apache.commons.jelly.JellyContext;
24  import org.apache.commons.jelly.JellyTagException;
25  import org.apache.commons.jelly.Script;
26  import org.apache.commons.jelly.XMLOutput;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  /***
31   * <p><code>DynamicTag</code> is a tag that is created from
32   * inside a Jelly script as a Jelly template and will invoke a
33   * given script, passing in its instantiation attributes
34   * as variables and will allow the template to invoke its instance body.</p>
35   *
36   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
37   * @version $Revision: 155420 $
38   */
39  public class DynamicTag extends DynaTagSupport {
40  
41      /*** The Log to which logging calls will be made. */
42      private static final Log log = LogFactory.getLog(DynamicTag.class);
43  
44      /*** The template script */
45      private Script template;
46  
47      /*** The instance attributes */
48      private Map attributes = new HashMap();
49  
50      public DynamicTag() {
51      }
52  
53      public DynamicTag(Script template) {
54          this.template = template;
55      }
56  
57  
58      // Tag interface
59      //-------------------------------------------------------------------------
60      public void doTag(XMLOutput output) throws JellyTagException {
61          if ( log.isDebugEnabled() ) {
62              log.debug("Invoking dynamic tag with attributes: " + attributes);
63          }
64          attributes.put("org.apache.commons.jelly.body", getBody());
65  
66          // create new context based on current attributes
67          JellyContext newJellyContext = context.newJellyContext(attributes);
68          Map attrMap = new HashMap();
69          for ( Iterator keyIter = this.attributes.keySet().iterator();
70                keyIter.hasNext();) {
71              String key = (String) keyIter.next();
72              if ( key.endsWith( "Attr" ) ) {
73                  Object value = this.attributes.get( key );
74                  attrMap.put( key, value );
75                  attrMap.put( key.substring( 0, key.length()-4 ), value );
76              }
77          }
78          newJellyContext.setVariable( "attrs", attrMap );
79          getTemplate().run(newJellyContext, output);
80      }
81  
82      // DynaTag interface
83      //-------------------------------------------------------------------------
84      public void setAttribute(String name, Object value) {
85          attributes.put(name, value);
86          attributes.put(name + "Attr", value);
87      }
88  
89      // Properties
90      //-------------------------------------------------------------------------
91      /*** The template to be executed by this tag which may well
92       * invoke this instances body from inside the template
93       */
94      public Script getTemplate() {
95          return template;
96      }
97  
98      public void setTemplate(Script template) {
99          this.template = template;
100     }
101 }