1 55 56 package org.jboss.axis.encoding.ser; 57 58 import org.jboss.axis.encoding.Target; 59 import org.jboss.axis.utils.BeanPropertyDescriptor; 60 import org.jboss.axis.utils.JavaUtils; 61 import org.jboss.axis.utils.Messages; 62 import org.jboss.logging.Logger; 63 import org.xml.sax.SAXException ; 64 65 import java.lang.reflect.Array ; 66 import java.lang.reflect.InvocationTargetException ; 67 68 71 public class BeanPropertyTarget implements Target 72 { 73 private static Logger log = Logger.getLogger(BeanPropertyTarget.class.getName()); 74 75 private Object object; 76 private BeanPropertyDescriptor pd; 77 private int index = -1; 78 79 protected DeferredBeanConstruction deferedConstruction; 83 protected BeanDeserializer beanDeserializer; 84 85 91 public BeanPropertyTarget(Object object, BeanPropertyDescriptor pd) 92 { 93 this.object = object; 94 this.pd = pd; 95 this.index = -1; } 97 98 105 public BeanPropertyTarget(Object object, BeanPropertyDescriptor pd, int i) 106 { 107 this.object = object; 108 this.pd = pd; 109 this.index = i; 110 } 111 112 115 public BeanPropertyTarget(BeanDeserializer beanDeserializer, DeferredBeanConstruction deferedConstruction, BeanPropertyDescriptor pd) 116 { 117 this.beanDeserializer = beanDeserializer; 118 this.deferedConstruction = deferedConstruction; 119 this.pd = pd; 120 this.index = -1; } 122 123 128 public void set(Object value) throws SAXException 129 { 130 131 if (object == null && deferedConstruction == null) 132 throw new IllegalStateException ("Target object does not exist"); 133 134 if (object == null && deferedConstruction != null) 135 { 136 object = deferedConstruction.newBeanInstance(value); 137 beanDeserializer.setValue(object); 138 return; 139 } 140 141 try 142 { 143 if (index < 0) 147 { 148 pd.set(object, value); 149 } 150 else 151 { 152 pd.set(object, index, value); 153 } 154 } 155 catch (Exception e) 156 { 157 158 try 159 { 160 Class type = pd.getType(); 164 if (JavaUtils.isConvertable(value, type)) 165 { 166 value = JavaUtils.convert(value, type); 167 if (index < 0) 168 { 169 try 173 { 174 pd.set(object, value); 175 } 176 catch (IllegalAccessException e1) 177 { 178 log.warn("Can not set " + value + " on " + object + 179 " as it does not have a setter for this value."); 180 } 181 } 182 else 183 pd.set(object, index, value); 184 } 185 else 186 { 187 if (index == 0 && 193 value.getClass().isArray() && 194 !type.getClass().isArray()) 195 { 196 for (int i = 0; i < Array.getLength(value); i++) 197 { 198 Object item = 199 JavaUtils.convert(Array.get(value, i), type); 200 pd.set(object, i, item); 201 } 202 } 203 else 204 { 205 throw e; 208 } 209 } 210 } 211 catch (Exception ex) 212 { 213 String field = pd.getName(); 216 if (index >= 0) 217 { 218 field += "[" + index + "]"; 219 } 220 221 String valueType = "null"; 222 if (value != null) 223 valueType = value.getClass().getName(); 224 225 log.error(Messages.getMessage("cantConvert02", 226 new String []{valueType, field, pd.getType().getName()})); 227 228 if (ex instanceof InvocationTargetException ) 229 { 230 Throwable t = ((InvocationTargetException )ex).getTargetException(); 231 if (t != null) 232 { 233 String classname = this.object.getClass().getName(); 234 throw new SAXException (Messages.getMessage("cantConvert04", 236 new String []{ 237 classname, 238 field, 239 (value == null) ? null : value.toString(), 240 t.getMessage()})); 241 } 242 } 243 throw new SAXException (ex); 244 } 245 } 246 } 247 } 248 249 | Popular Tags |