KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jelly > impl > DynamicTag


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 JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Iterator JavaDoc;
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 JavaDoc attributes = new HashMap JavaDoc();
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         attributes.put("org.apache.commons.jelly.body.scope", context);
66
67         // create new context based on current attributes
68
JellyContext newJellyContext = context.newJellyContext(attributes);
69         Map JavaDoc attrMap = new HashMap JavaDoc();
70         for ( Iterator JavaDoc keyIter = this.attributes.keySet().iterator();
71               keyIter.hasNext();) {
72             String JavaDoc key = (String JavaDoc) keyIter.next();
73             if ( key.endsWith( "Attr" ) ) {
74                 Object JavaDoc value = this.attributes.get( key );
75                 attrMap.put( key, value );
76                 attrMap.put( key.substring( 0, key.length()-4 ), value );
77             }
78         }
79         newJellyContext.setVariable( "attrs", attrMap );
80         getTemplate().run(newJellyContext, output);
81     }
82
83     // DynaTag interface
84
//-------------------------------------------------------------------------
85
public void setAttribute(String JavaDoc name, Object JavaDoc value) {
86         attributes.put(name, value);
87         attributes.put(name + "Attr", value);
88     }
89
90     // Properties
91
//-------------------------------------------------------------------------
92
/** The template to be executed by this tag which may well
93      * invoke this instances body from inside the template
94      */

95     public Script getTemplate() {
96         return template;
97     }
98
99     public void setTemplate(Script template) {
100         this.template = template;
101     }
102 }
103
Popular Tags