1 55 56 package org.jboss.axis.encoding.ser; 57 58 import org.jboss.axis.description.TypeDesc; 59 import org.jboss.axis.encoding.DeserializationContext; 60 import org.jboss.axis.encoding.Deserializer; 61 import org.jboss.axis.encoding.DeserializerImpl; 62 import org.jboss.axis.encoding.SimpleType; 63 import org.jboss.axis.encoding.TypeMapping; 64 import org.jboss.axis.message.SOAPHandler; 65 import org.jboss.axis.utils.BeanPropertyDescriptor; 66 import org.jboss.axis.utils.BeanUtils; 67 import org.jboss.axis.utils.Messages; 68 import org.jboss.logging.Logger; 69 import org.xml.sax.Attributes ; 70 import org.xml.sax.SAXException ; 71 72 import javax.xml.namespace.QName ; 73 import java.io.CharArrayWriter ; 74 import java.lang.reflect.Constructor ; 75 import java.lang.reflect.InvocationTargetException ; 76 import java.util.HashMap ; 77 import java.util.Iterator ; 78 import java.util.Map ; 79 import java.util.Set ; 80 81 90 public class SimpleDeserializer extends DeserializerImpl 91 { 92 private static Logger log = Logger.getLogger(SimpleDeserializer.class.getName()); 93 94 private final CharArrayWriter val = new CharArrayWriter (); 96 private Constructor constructor = null; 97 private Map propertyMap = null; 98 private HashMap attributeMap = null; 99 100 public QName xmlType; 101 public Class javaType; 102 103 private TypeDesc typeDesc = null; 104 105 protected SimpleDeserializer cacheStringDSer = null; 106 protected QName cacheXMLType = null; 107 108 112 public SimpleDeserializer(Class javaType, QName xmlType) 113 { 114 this.xmlType = xmlType; 115 this.javaType = javaType; 116 117 init(); 118 } 119 120 public SimpleDeserializer(Class javaType, QName xmlType, TypeDesc typeDesc) 121 { 122 this.xmlType = xmlType; 123 this.javaType = javaType; 124 this.typeDesc = typeDesc; 125 126 init(); 127 } 128 129 132 private void init() 133 { 134 if (SimpleType.class.isAssignableFrom(javaType)) 137 { 138 if (typeDesc == null) 140 { 141 typeDesc = TypeDesc.getTypeDescForClass(javaType); 142 } 143 if (typeDesc != null) 146 { 147 propertyMap = typeDesc.getPropertyDescriptorMap(); 148 } 149 else 150 { 151 BeanPropertyDescriptor[] pd = BeanUtils.getPd(javaType, null); 152 propertyMap = new HashMap (); 153 for (int i = 0; i < pd.length; i++) 154 { 155 BeanPropertyDescriptor descriptor = pd[i]; 156 propertyMap.put(descriptor.getName(), descriptor); 157 } 158 } 159 } 160 } 161 162 165 public void reset() 166 { 167 val.reset(); 169 attributeMap = null; isNil = false; isEnded = false; } 173 174 179 public void removeValueTargets() 180 { 181 if (targets != null) 182 { 183 targets.clear(); 184 } 186 } 187 188 191 public void setConstructor(Constructor c) 192 { 193 constructor = c; 194 } 195 196 199 public SOAPHandler onStartChild(String namespace, 200 String localName, 201 String prefix, 202 Attributes attributes, 203 DeserializationContext context) 204 throws SAXException 205 { 206 throw new SAXException (Messages.getMessage("cantHandle00", "SimpleDeserializer")); 207 } 208 209 213 public void characters(char[] chars, int start, int end) 214 throws SAXException 215 { 216 val.write(chars, start, end); 218 } 219 220 224 public void onEndElement(String namespace, String localName, 225 DeserializationContext context) 226 throws SAXException 227 { 228 if (isNil) 230 { 231 value = null; 232 return; 233 } 234 try 235 { 236 value = makeValue(val.toString()); 237 } 238 catch (InvocationTargetException ite) 239 { 240 Throwable realException = ite.getTargetException(); 241 if (realException instanceof Exception ) 242 throw new SAXException ((Exception )realException); 243 else 244 throw new SAXException (ite.getMessage()); 245 } 246 catch (Exception e) 247 { 248 throw new SAXException (e); 249 } 250 251 setSimpleTypeAttributes(); 253 } 254 255 263 public Object makeValue(String source) throws Exception 264 { 265 266 log.debug("Making value [" + source + "] for javaType: " + javaType); 267 268 if (javaType == boolean.class || javaType == Boolean .class) 270 { 271 switch (source.charAt(0)) 273 { 274 case '0': 275 case 'f': 276 case 'F': 277 return Boolean.FALSE; 278 279 case '1': 280 case 't': 281 case 'T': 282 return Boolean.TRUE; 283 284 default: 285 throw new NumberFormatException (Messages.getMessage("badBool00")); 286 } 287 288 } 289 290 if (javaType == float.class || 292 javaType == java.lang.Float .class) 293 { 294 if (source.equals("NaN")) 295 { 296 return new Float (Float.NaN); 297 } 298 else if (source.equals("INF")) 299 { 300 return new Float (Float.POSITIVE_INFINITY); 301 } 302 else if (source.equals("-INF")) 303 { 304 return new Float (Float.NEGATIVE_INFINITY); 305 } 306 } 307 308 if (javaType == double.class || 309 javaType == java.lang.Double .class) 310 { 311 if (source.equals("NaN")) 312 { 313 return new Double (Double.NaN); 314 } 315 else if (source.equals("INF")) 316 { 317 return new Double (Double.POSITIVE_INFINITY); 318 } 319 else if (source.equals("-INF")) 320 { 321 return new Double (Double.NEGATIVE_INFINITY); 322 } 323 } 324 325 return constructor.newInstance(new Object []{source}); 326 } 327 328 340 public void onStartElement(String namespace, String localName, 341 String prefix, Attributes attributes, 342 DeserializationContext context) 343 throws SAXException 344 { 345 346 if (typeDesc == null) 348 return; 349 350 for (int i = 0; i < attributes.getLength(); i++) 353 { 354 QName attrQName = new QName (attributes.getURI(i), 355 attributes.getLocalName(i)); 356 String fieldName = typeDesc.getFieldNameForAttribute(attrQName); 357 if (fieldName != null) 358 { 359 BeanPropertyDescriptor bpd = (BeanPropertyDescriptor)propertyMap.get(fieldName); 361 if (bpd != null) 362 { 363 if (!bpd.isWriteable() || bpd.isIndexed()) 364 continue; 365 366 TypeMapping tm = context.getTypeMapping(); 368 Class type = bpd.getType(); 369 QName qn = tm.getTypeQName(type); 370 if (qn == null) 371 throw new SAXException (Messages.getMessage("unregistered00", type.toString())); 372 373 Deserializer dSer = context.getDeserializerForType(qn); 375 if (dSer == null) 376 throw new SAXException (Messages.getMessage("noDeser00", type.toString())); 377 if (!(dSer instanceof SimpleDeserializer)) 378 throw new SAXException (Messages.getMessage("AttrNotSimpleType00", bpd.getName(), type.toString())); 379 380 if (attributeMap == null) 383 { 384 attributeMap = new HashMap (); 385 } 386 try 387 { 388 Object val = ((SimpleDeserializer)dSer).makeValue(attributes.getValue(i)); 389 attributeMap.put(fieldName, val); 390 } 391 catch (Exception e) 392 { 393 throw new SAXException (e); 394 } 395 } 396 } 397 } } 400 403 private void setSimpleTypeAttributes() throws SAXException 404 { 405 if (!SimpleType.class.isAssignableFrom(javaType) || 407 attributeMap == null) 408 return; 409 410 Set entries = attributeMap.entrySet(); 412 for (Iterator iterator = entries.iterator(); iterator.hasNext();) 413 { 414 Map.Entry entry = (Map.Entry )iterator.next(); 415 String name = (String )entry.getKey(); 416 Object val = entry.getValue(); 417 418 BeanPropertyDescriptor bpd = 419 (BeanPropertyDescriptor)propertyMap.get(name); 420 if (!bpd.isWriteable() || bpd.isIndexed()) continue; 421 try 422 { 423 bpd.set(value, val); 424 } 425 catch (Exception e) 426 { 427 throw new SAXException (e); 428 } 429 } 430 } 431 432 } 433 | Popular Tags |