1 16 17 package org.apache.axis.encoding.ser; 18 19 import org.apache.axis.components.logger.LogFactory; 20 import org.apache.axis.encoding.Target; 21 import org.apache.axis.utils.BeanPropertyDescriptor; 22 import org.apache.axis.utils.JavaUtils; 23 import org.apache.axis.utils.Messages; 24 import org.apache.commons.logging.Log; 25 import org.xml.sax.SAXException ; 26 27 import java.lang.reflect.Array ; 28 import java.lang.reflect.InvocationTargetException ; 29 30 33 public class BeanPropertyTarget implements Target { 34 protected static Log log = 35 LogFactory.getLog(BeanPropertyTarget.class.getName()); 36 37 private Object object; 38 private BeanPropertyDescriptor pd; 39 private int index = -1; 40 41 46 public BeanPropertyTarget(Object object, BeanPropertyDescriptor pd) { 47 this.object = object; 48 this.pd = pd; 49 this.index = -1; } 51 52 58 public BeanPropertyTarget(Object object, BeanPropertyDescriptor pd, int i) { 59 this.object = object; 60 this.pd = pd; 61 this.index = i; 62 } 63 64 68 public void set(Object value) throws SAXException { 69 70 try { 71 if (index < 0) { 75 pd.set(object, value); 76 } else { 77 pd.set(object, index, value); 78 } 79 } catch (Exception e) { 80 81 try { 82 Class type = pd.getType(); 86 87 88 if (value.getClass().isArray() 89 && value.getClass().getComponentType().isPrimitive() 90 && type.isArray() 91 && type.getComponentType().equals(Object .class)) 92 { 93 type = Array.newInstance(JavaUtils.getWrapperClass(value.getClass().getComponentType()),0).getClass(); 95 } 96 97 if (JavaUtils.isConvertable(value, type)) { 98 value = JavaUtils.convert(value, type); 99 if (index < 0) 100 pd.set(object, value); 101 else 102 pd.set(object, index, value); 103 } else { 104 if (index == 0 && 110 value.getClass().isArray() && 111 !type.getClass().isArray()) { 112 for (int i=0; i<Array.getLength(value); i++) { 113 Object item = 114 JavaUtils.convert(Array.get(value, i), type); 115 pd.set(object, i, item); 116 } 117 } else { 118 throw e; 121 } 122 } 123 } catch (Exception ex) { 124 String field= pd.getName(); 127 if (index >=0) { 128 field += "[" + index + "]"; 129 } 130 if (log.isErrorEnabled()) { 131 String valueType = "null"; 133 if (value != null) 134 valueType = value.getClass().getName(); 135 log.error(Messages.getMessage("cantConvert02", 136 new String []{ 137 valueType, 138 field, 139 (index >= 0) ? 140 pd.getType().getComponentType().getName() : 141 pd.getType().getName() 142 })); 143 } 144 if(ex instanceof InvocationTargetException ) { 145 Throwable t = ((InvocationTargetException )ex).getTargetException(); 146 if( t != null) { 147 String classname = this.object.getClass().getName(); 148 throw new SAXException (Messages.getMessage("cantConvert04", 150 new String [] { 151 classname, 152 field, 153 (value==null)?null:value.toString(), 154 t.getMessage()})); 155 } 156 } 157 throw new SAXException (ex); 158 } 159 } 160 } 161 } 162 163 | Popular Tags |