1 5 package com.opensymphony.webwork.views.xslt; 6 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 10 import java.beans.IntrospectionException ; 11 import java.beans.Introspector ; 12 import java.beans.PropertyDescriptor ; 13 import java.lang.reflect.Method ; 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 20 25 public class BeanAdapter extends DefaultElementAdapter { 26 28 private static final Object [] NULLPARAMS = new Object [0]; 29 30 34 private static Map propertyDescriptorCache; 35 36 38 private Log log = LogFactory.getLog(this.getClass()); 39 40 42 public BeanAdapter(DOMAdapter rootAdapter, AdapterNode parent, String propertyName, Object value) { 43 super(rootAdapter, parent, propertyName, value); 44 } 45 46 48 public String getTagName() { 49 return getPropertyName(); 50 } 51 52 protected List buildChildrenAdapters() { 53 List newAdapters = new ArrayList (); 54 Class type = getValue().getClass(); 55 PropertyDescriptor [] props = getPropertyDescriptors(getValue()); 56 57 if (props.length > 0) { 58 for (int i = 0; i < props.length; i++) { 59 Method m = props[i].getReadMethod(); 60 61 if (m == null) { 62 continue; 64 } 65 66 String propertyName = props[i].getName(); 67 Object propertyValue; 68 69 73 try { 74 propertyValue = m.invoke(getValue(), NULLPARAMS); 75 } catch (Exception e) { 76 continue; 77 } 78 79 80 AdapterNode childAdapter; 81 82 if (propertyValue == null) { 83 childAdapter = getRootAdapter().adaptNullValue(getRootAdapter(), this, propertyName); 84 } else { 85 childAdapter = getRootAdapter().adapt(getRootAdapter(), this, propertyName, propertyValue); 86 } 87 88 newAdapters.add(childAdapter); 89 90 if (log.isDebugEnabled()) { 91 log.debug(this + " adding adapter: " + childAdapter); 92 } 93 } 94 } else { 95 log.info("Class " + type.getName() + " has no readable properties, " + " trying to adapt " + getPropertyName() + " with ToStringAdapter..."); 97 98 } 100 101 return newAdapters; 102 } 103 104 107 private synchronized PropertyDescriptor [] getPropertyDescriptors(Object bean) { 108 try { 109 if (propertyDescriptorCache == null) { 110 propertyDescriptorCache = new HashMap (); 111 } 112 113 PropertyDescriptor [] props = (PropertyDescriptor []) propertyDescriptorCache.get(bean.getClass()); 114 115 if (props == null) { 116 log.debug("Caching property descriptor for " + bean.getClass().getName()); 117 props = Introspector.getBeanInfo(bean.getClass(), Object .class).getPropertyDescriptors(); 118 propertyDescriptorCache.put(bean.getClass(), props); 119 } 120 121 return props; 122 } catch (IntrospectionException e) { 123 e.printStackTrace(); 124 throw new RuntimeException ("Error getting property descriptors for " + bean + " : " + e.getMessage()); 125 } 126 } 127 } 128 | Popular Tags |