1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
83
84 public void setAttribute(String name, Object value) {
85 attributes.put(name, value);
86 attributes.put(name + "Attr", value);
87 }
88
89
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 }