1 16 package org.apache.commons.jelly.tags.core; 17 18 import java.lang.reflect.InvocationTargetException ; 19 import java.util.HashMap ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.Set ; 24 25 import org.apache.commons.beanutils.BeanUtils; 26 import org.apache.commons.beanutils.PropertyUtils; 27 import org.apache.commons.jelly.JellyTagException; 28 import org.apache.commons.jelly.MapTagSupport; 29 import org.apache.commons.jelly.MissingAttributeException; 30 import org.apache.commons.jelly.XMLOutput; 31 import org.apache.commons.jelly.impl.BeanSource; 32 import org.apache.commons.jelly.util.ClassLoaderUtils; 33 34 51 public class UseBeanTag extends MapTagSupport implements BeanSource { 52 53 54 private Object bean; 55 56 57 private Class defaultClass; 58 59 63 private Set ignoreProperties; 64 65 71 private boolean ignoreUnknownProperties = false; 72 73 74 public UseBeanTag() { 75 } 76 77 public UseBeanTag(Class defaultClass) { 78 this.defaultClass = defaultClass; 79 } 80 81 84 87 public Object getBean() { 88 return bean; 89 } 90 91 92 public void doTag(XMLOutput output) throws JellyTagException { 95 Map attributes = getAttributes(); 96 String var = (String ) attributes.get( "var" ); 97 Object classObject = attributes.get( "class" ); 98 addIgnoreProperty("class"); 99 addIgnoreProperty("var"); 100 101 try { 102 Class theClass = convertToClass(classObject); 104 105 bean = newInstance(theClass, attributes, output); 106 setBeanProperties(bean, attributes); 107 108 invokeBody(output); 110 111 processBean(var, bean); 112 } 113 catch (ClassNotFoundException e) { 114 throw new JellyTagException(e); 115 } 116 } 117 118 121 124 protected void setBean(Object bean) { 125 this.bean = bean; 126 } 127 128 134 protected Class convertToClass(Object classObject) 135 throws MissingAttributeException, ClassNotFoundException { 136 if (classObject instanceof Class ) { 137 return (Class ) classObject; 138 } 139 else if ( classObject == null ) { 140 Class theClass = getDefaultClass(); 141 if (theClass == null) { 142 throw new MissingAttributeException("class"); 143 } 144 return theClass; 145 } 146 else { 147 String className = classObject.toString(); 148 return loadClass(className); 149 } 150 } 151 152 157 protected Class loadClass(String className) throws ClassNotFoundException { 158 return ClassLoaderUtils.loadClass(className, getClass()); 159 } 160 161 166 protected Object newInstance(Class theClass, Map attributes, XMLOutput output) 167 throws JellyTagException { 168 try { 169 return theClass.newInstance(); 170 } catch (IllegalAccessException e) { 171 throw new JellyTagException(e.toString()); 172 } catch (InstantiationException e) { 173 throw new JellyTagException(e.toString()); 174 } 175 } 176 177 183 protected void setBeanProperties(Object bean, Map attributes) throws JellyTagException { 184 Map attrsToUse = new HashMap (attributes); 185 attrsToUse.keySet().removeAll(getIgnorePropertySet()); 186 187 validateBeanProperties(bean, attrsToUse); 188 189 try { 190 BeanUtils.populate(bean, attrsToUse); 191 } catch (IllegalAccessException e) { 192 throw new JellyTagException("could not set the properties of the bean",e); 193 } catch (InvocationTargetException e) { 194 throw new JellyTagException("could not set the properties of the bean",e); 195 } 196 } 197 198 206 protected void validateBeanProperties(Object bean, Map attributes) throws JellyTagException { 207 if (!isIgnoreUnknownProperties()) { 208 for (Iterator i=attributes.keySet().iterator();i.hasNext();) { 209 String attrName = (String )i.next(); 210 if (! PropertyUtils.isWriteable(bean, attrName)) { 211 throw new JellyTagException("No bean property found: " + attrName); 212 } 213 } 214 } 215 } 216 217 222 protected void processBean(String var, Object bean) throws JellyTagException { 223 if (var != null) { 224 context.setVariable(var, bean); 225 } 226 else { 227 ArgTag parentArg = (ArgTag)(findAncestorWithClass(ArgTag.class)); 228 if(null != parentArg) { 229 parentArg.setValue(bean); 230 } 231 } 232 } 233 234 237 protected Class getDefaultClass() { 238 return defaultClass; 239 } 240 241 247 protected void addIgnoreProperty(String name) { 248 getIgnorePropertySet().add(name); 249 } 250 251 255 protected Set getIgnorePropertySet() { 256 if (ignoreProperties == null) { 257 ignoreProperties = new HashSet (); 258 } 259 260 return ignoreProperties; 261 } 262 263 267 public boolean isIgnoreUnknownProperties() { 268 return ignoreUnknownProperties; 269 } 270 271 277 public void setIgnoreUnknownProperties(boolean ignoreUnknownProps) { 278 this.ignoreUnknownProperties = ignoreUnknownProps; 279 } 280 } 281 | Popular Tags |