1 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 39 public class DynamicTag extends DynaTagSupport { 40 41 42 private static final Log log = LogFactory.getLog(DynamicTag.class); 43 44 45 private Script template; 46 47 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 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 JellyContext newJellyContext = context.newJellyContext(attributes); 69 Map attrMap = new HashMap (); 70 for ( Iterator keyIter = this.attributes.keySet().iterator(); 71 keyIter.hasNext();) { 72 String key = (String ) keyIter.next(); 73 if ( key.endsWith( "Attr" ) ) { 74 Object 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 public void setAttribute(String name, Object value) { 86 attributes.put(name, value); 87 attributes.put(name + "Attr", value); 88 } 89 90 95 public Script getTemplate() { 96 return template; 97 } 98 99 public void setTemplate(Script template) { 100 this.template = template; 101 } 102 } 103 | Popular Tags |