1 16 package org.apache.commons.jelly.impl; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 import org.apache.commons.jelly.JellyException; 22 import org.apache.commons.jelly.Script; 23 import org.apache.commons.jelly.Tag; 24 import org.apache.commons.jelly.TagLibrary; 25 import org.xml.sax.Attributes ; 26 27 34 public class DynamicTagLibrary extends TagLibrary { 35 36 private String uri; 37 private Map templates = new HashMap (); 38 private TagLibrary parent; 39 40 public DynamicTagLibrary() { 41 } 42 43 public DynamicTagLibrary(String uri) { 44 this.uri = uri; 45 } 46 47 48 public TagScript createTagScript(final String name, final Attributes attributes) 49 throws JellyException { 50 51 return new TagScript( 52 new TagFactory() { 53 public Tag createTag(String name, Attributes attributes) throws JellyException { 54 return DynamicTagLibrary.this.createTag(name, attributes); 55 } 56 } 57 ); 58 } 59 60 61 public Tag createTag(String name, Attributes attributes) 62 throws JellyException { 63 64 Object value = templates.get(name); 65 if ( value instanceof Script ) { 66 Script template = (Script) value; 67 return new DynamicTag(template); 68 } 69 else if ( value instanceof TagFactory ) { 70 TagFactory factory = (TagFactory) value; 71 return factory.createTag(name, attributes); 72 } 73 else if ( parent != null ) { 74 return parent.createTag(name, attributes); 76 } 77 78 return null; 79 } 80 81 84 public void registerDynamicTag(String name, Script template) { 85 templates.put(name, template); 86 } 87 88 91 public void registerBeanTag(String name, TagFactory factory) { 92 templates.put(name, factory); 93 } 94 95 102 public Script getDynamicTag(String name) { 103 Object result = templates.get(name); 104 return (result instanceof Script) ? (Script) result : null; 105 } 106 107 117 public DynamicTagLibrary find(String name) { 118 DynamicTagLibrary result = null; 119 if (templates.get(name) != null) { 120 result = this; 121 } 122 else if (parent instanceof DynamicTagLibrary) { 123 result = ((DynamicTagLibrary) parent).find(name); 124 } 125 return result; 126 } 127 128 public String getUri() { 131 return uri; 132 } 133 134 public void setUri(String uri) { 135 this.uri = uri; 136 } 137 138 139 143 public TagLibrary getParent() { 144 return parent; 145 } 146 147 151 public void setParent(TagLibrary parent) { 152 this.parent = parent; 153 } 154 155 } 156 | Popular Tags |