1 16 17 package org.apache.commons.jelly.tags.define; 18 19 import java.lang.reflect.Method ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import org.apache.commons.jelly.JellyTagException; 24 import org.apache.commons.jelly.MissingAttributeException; 25 import org.apache.commons.jelly.Tag; 26 import org.apache.commons.jelly.XMLOutput; 27 import org.apache.commons.jelly.impl.Attribute; 28 import org.apache.commons.jelly.impl.DynamicBeanTag; 29 import org.apache.commons.jelly.impl.TagFactory; 30 import org.apache.commons.jelly.util.ClassLoaderUtils; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 import org.xml.sax.Attributes ; 36 37 44 public class BeanTag extends DefineTagSupport { 45 46 47 private static final Log log = LogFactory.getLog(BeanTag.class); 48 49 50 private static final Map EMPTY_MAP = new HashMap (); 51 52 53 private String name; 54 55 56 private String className; 57 58 59 private ClassLoader classLoader; 60 61 62 private String varAttribute = "var"; 63 64 65 private Map attributes; 66 67 70 public void addAttribute(Attribute attribute) { 71 if ( attributes == null ) { 72 attributes = new HashMap (); 73 } 74 attributes.put( attribute.getName(), attribute ); 75 } 76 77 public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException { 80 invokeBody(output); 81 82 if (name == null) { 83 throw new MissingAttributeException("name"); 84 } 85 if (className == null) { 86 throw new MissingAttributeException("className"); 87 } 88 89 Class theClass = null; 90 try { 91 ClassLoader classLoader = getClassLoader(); 92 theClass = ClassLoaderUtils.loadClass(className, getClassLoader(), getContext().getUseContextClassLoader(), getClass()); 93 } 94 catch (ClassNotFoundException e) { 95 log.error( "Could not load class: " + className + " exception: " + e, e ); 96 throw new JellyTagException( 97 "Could not find class: " 98 + className 99 + " using ClassLoader: " 100 + classLoader); 101 } 102 103 final Class beanClass = theClass; 104 final Method invokeMethod = getInvokeMethod( theClass ); 105 final Map beanAttributes = (attributes != null) ? attributes : EMPTY_MAP; 106 107 TagFactory factory = new TagFactory() { 108 public Tag createTag(String name, Attributes attributes) { 109 return new DynamicBeanTag(beanClass, beanAttributes, varAttribute, invokeMethod); 110 } 111 }; 112 113 getTagLibrary().registerBeanTag(name, factory); 114 115 attributes = null; 117 } 118 119 120 123 126 public void setName(String name) { 127 this.name = name; 128 } 129 130 133 public void setClassName(String className) { 134 this.className = className; 135 } 136 137 142 public void setClassLoader(ClassLoader classLoader) { 143 this.classLoader = classLoader; 144 } 145 146 150 public ClassLoader getClassLoader() { 151 return ClassLoaderUtils.getClassLoader(classLoader, getContext().getUseContextClassLoader(), getClass()); 152 } 153 154 159 public void setVarAttribute(String varAttribute) { 160 this.varAttribute = varAttribute; 161 } 162 163 164 167 170 protected Method getInvokeMethod( Class theClass ) { 171 return null; 172 } 173 } 174 | Popular Tags |