1 16 package org.apache.commons.jelly.tags.core; 17 18 import java.lang.reflect.InvocationTargetException ; 19 import java.util.Map ; 20 21 import org.apache.commons.beanutils.BeanUtils; 22 23 import org.apache.commons.jelly.JellyException; 24 import org.apache.commons.jelly.JellyTagException; 25 import org.apache.commons.jelly.MissingAttributeException; 26 import org.apache.commons.jelly.MapTagSupport; 27 import org.apache.commons.jelly.XMLOutput; 28 import org.apache.commons.jelly.impl.BeanSource; 29 30 47 public class SetPropertiesTag extends MapTagSupport { 48 49 public SetPropertiesTag(){ 50 } 51 52 public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException { 55 Map attributes = getAttributes(); 56 Object bean = attributes.remove( "object" ); 57 if ( bean == null ) { 58 BeanSource tag = (BeanSource) findAncestorWithClass(BeanSource.class); 60 if (tag != null) { 61 try { 62 bean = tag.getBean(); 63 } catch (JellyException e) { 64 throw new JellyTagException(e); 65 } 66 } 67 if (bean == null) { 68 throw new MissingAttributeException("bean"); 69 } 70 } 71 setBeanProperties(bean, attributes); 72 } 73 74 77 81 protected void setBeanProperties(Object bean, Map attributes) throws JellyTagException { 82 try { 83 BeanUtils.populate(bean, attributes); 84 } catch (IllegalAccessException e) { 85 throw new JellyTagException("could not set the properties on a bean",e); 86 } catch (InvocationTargetException e) { 87 throw new JellyTagException("could not set the properties on a bean",e); 88 } 89 } 90 } 91 | Popular Tags |