1 16 17 package org.apache.axis.encoding.ser; 18 19 import org.apache.axis.description.TypeDesc; 20 import org.apache.axis.encoding.DeserializationContext; 21 import org.apache.axis.encoding.Deserializer; 22 import org.apache.axis.encoding.DeserializerImpl; 23 import org.apache.axis.encoding.SimpleType; 24 import org.apache.axis.encoding.TypeMapping; 25 import org.apache.axis.message.SOAPHandler; 26 import org.apache.axis.utils.BeanPropertyDescriptor; 27 import org.apache.axis.utils.BeanUtils; 28 import org.apache.axis.utils.Messages; 29 import org.xml.sax.Attributes ; 30 import org.xml.sax.SAXException ; 31 32 import javax.xml.namespace.QName ; 33 import java.io.CharArrayWriter ; 34 import java.lang.reflect.Constructor ; 35 import java.lang.reflect.InvocationTargetException ; 36 import java.util.HashMap ; 37 import java.util.Iterator ; 38 import java.util.Map ; 39 import java.util.Set ; 40 41 50 public class SimpleDeserializer extends DeserializerImpl { 51 52 private static final Class [] STRING_STRING_CLASS = 53 new Class [] {String .class, String .class}; 54 55 public static final Class [] STRING_CLASS = 56 new Class [] {String .class}; 57 58 private final CharArrayWriter val = new CharArrayWriter (); 59 private Constructor constructor = null; 60 private Map propertyMap = null; 61 private HashMap attributeMap = null; 62 63 public QName xmlType; 64 public Class javaType; 65 66 private TypeDesc typeDesc = null; 67 68 protected DeserializationContext context = null; 69 protected SimpleDeserializer cacheStringDSer = null; 70 protected QName cacheXMLType = null; 71 75 public SimpleDeserializer(Class javaType, QName xmlType) { 76 this.xmlType = xmlType; 77 this.javaType = javaType; 78 79 init(); 80 } 81 82 public SimpleDeserializer(Class javaType, QName xmlType, TypeDesc typeDesc) { 83 this.xmlType = xmlType; 84 this.javaType = javaType; 85 this.typeDesc = typeDesc; 86 87 init(); 88 } 89 90 93 private void init() { 94 if (SimpleType.class.isAssignableFrom(javaType)) { 97 if (typeDesc == null) { 99 typeDesc = TypeDesc.getTypeDescForClass(javaType); 100 } 101 } 102 103 if (typeDesc != null) { 106 propertyMap = typeDesc.getPropertyDescriptorMap(); 107 } else { 108 BeanPropertyDescriptor[] pd = BeanUtils.getPd(javaType, null); 109 propertyMap = new HashMap (); 110 for (int i = 0; i < pd.length; i++) { 111 BeanPropertyDescriptor descriptor = pd[i]; 112 propertyMap.put(descriptor.getName(), descriptor); 113 } 114 } 115 } 116 117 120 public void reset() { 121 val.reset(); 122 attributeMap = null; isNil = false; isEnded = false; } 126 127 130 public void setConstructor(Constructor c) 131 { 132 constructor = c; 133 } 134 135 138 public SOAPHandler onStartChild(String namespace, 139 String localName, 140 String prefix, 141 Attributes attributes, 142 DeserializationContext context) 143 throws SAXException 144 { 145 throw new SAXException ( 146 Messages.getMessage("cantHandle00", "SimpleDeserializer")); 147 } 148 149 153 public void characters(char [] chars, int start, int end) 154 throws SAXException 155 { 156 val.write(chars,start,end); 157 } 158 159 163 public void onEndElement(String namespace, String localName, 164 DeserializationContext context) 165 throws SAXException 166 { 167 if (isNil) { 168 value = null; 169 return; 170 } 171 try { 172 value = makeValue(val.toString()); 173 } catch (InvocationTargetException ite) { 174 Throwable realException = ite.getTargetException(); 175 if (realException instanceof Exception ) 176 throw new SAXException ((Exception )realException); 177 else 178 throw new SAXException (ite.getMessage()); 179 } catch (Exception e) { 180 throw new SAXException (e); 181 } 182 183 setSimpleTypeAttributes(); 185 } 186 187 194 public Object makeValue(String source) throws Exception 195 { 196 if (javaType == java.lang.String .class) { 197 return source; 198 } 199 200 source = source.trim(); 202 203 if (source.length() == 0 && typeDesc == null) { 204 return null; 205 } 206 207 if (this.constructor == null) { 209 Object value = makeBasicValue(source); 210 if (value != null) { 211 return value; 212 } 213 } 214 215 Object [] args = null; 216 217 boolean isQNameSubclass = QName .class.isAssignableFrom(javaType); 218 219 if (isQNameSubclass) { 220 int colon = source.lastIndexOf(":"); 221 String namespace = colon < 0 ? "" : 222 context.getNamespaceURI(source.substring(0, colon)); 223 String localPart = colon < 0 ? source : source.substring(colon + 1); 224 args = new Object [] {namespace, localPart}; 225 } 226 227 if (constructor == null) { 228 try { 229 if (isQNameSubclass) { 230 constructor = 231 javaType.getDeclaredConstructor(STRING_STRING_CLASS); 232 } else { 233 constructor = 234 javaType.getDeclaredConstructor(STRING_CLASS); 235 } 236 } catch (Exception e) { 237 return null; 238 } 239 } 240 241 if(constructor.getParameterTypes().length==0){ 242 try { 243 Object obj = constructor.newInstance(new Object []{}); 244 obj.getClass().getMethod("set_value", new Class []{String .class}) 245 .invoke(obj, new Object []{source}); 246 return obj; 247 } catch (Exception e){ 248 } 250 } 251 if (args == null) { 252 args = new Object []{source}; 253 } 254 return constructor.newInstance(args); 255 } 256 257 private Object makeBasicValue(String source) throws Exception { 258 if (javaType == boolean.class || 260 javaType == Boolean .class) { 261 switch (source.charAt(0)) { 263 case '0': case 'f': case 'F': 264 return Boolean.FALSE; 265 266 case '1': case 't': case 'T': 267 return Boolean.TRUE; 268 269 default: 270 throw new NumberFormatException ( 271 Messages.getMessage("badBool00")); 272 } 273 274 } 275 276 if (javaType == float.class || 278 javaType == java.lang.Float .class) { 279 if (source.equals("NaN")) { 280 return new Float (Float.NaN); 281 } else if (source.equals("INF")) { 282 return new Float (Float.POSITIVE_INFINITY); 283 } else if (source.equals("-INF")) { 284 return new Float (Float.NEGATIVE_INFINITY); 285 } else { 286 return new Float (source); 287 } 288 } 289 290 if (javaType == double.class || 291 javaType == java.lang.Double .class) { 292 if (source.equals("NaN")) { 293 return new Double (Double.NaN); 294 } else if (source.equals("INF")) { 295 return new Double (Double.POSITIVE_INFINITY); 296 } else if (source.equals("-INF")) { 297 return new Double (Double.NEGATIVE_INFINITY); 298 } else { 299 return new Double (source); 300 } 301 } 302 303 if (javaType == int.class || 304 javaType == java.lang.Integer .class) { 305 return new Integer (source); 306 } 307 308 if (javaType == short.class || 309 javaType == java.lang.Short .class) { 310 return new Short (source); 311 } 312 313 if (javaType == long.class || 314 javaType == java.lang.Long .class) { 315 return new Long (source); 316 } 317 318 if (javaType == byte.class || 319 javaType == java.lang.Byte .class) { 320 return new Byte (source); 321 } 322 323 if (javaType == org.apache.axis.types.URI.class) { 324 return new org.apache.axis.types.URI(source); 325 } 326 327 return null; 328 } 329 330 341 public void onStartElement(String namespace, String localName, 342 String prefix, Attributes attributes, 343 DeserializationContext context) 344 throws SAXException 345 { 346 347 this.context = context; 348 349 for (int i=0; i < attributes.getLength(); i++) { 352 QName attrQName = new QName (attributes.getURI(i), 353 attributes.getLocalName(i)); 354 355 String fieldName = attributes.getLocalName(i); 356 357 if(typeDesc != null) { 358 fieldName = typeDesc.getFieldNameForAttribute(attrQName); 359 if (fieldName == null) 360 continue; 361 } 362 363 if (propertyMap == null) 364 continue; 365 366 BeanPropertyDescriptor bpd = 368 (BeanPropertyDescriptor) propertyMap.get(fieldName); 369 if (bpd != null) { 370 if (!bpd.isWriteable() || bpd.isIndexed() ) continue ; 371 372 TypeMapping tm = context.getTypeMapping(); 374 Class type = bpd.getType(); 375 QName qn = tm.getTypeQName(type); 376 if (qn == null) 377 throw new SAXException ( 378 Messages.getMessage("unregistered00", type.toString())); 379 380 Deserializer dSer = context.getDeserializerForType(qn); 382 if (dSer == null) 383 throw new SAXException ( 384 Messages.getMessage("noDeser00", type.toString())); 385 if (! (dSer instanceof SimpleDeserializer)) 386 throw new SAXException ( 387 Messages.getMessage("AttrNotSimpleType00", 388 bpd.getName(), 389 type.toString())); 390 391 if (attributeMap == null) { 394 attributeMap = new HashMap (); 395 } 396 try { 397 Object val = ((SimpleDeserializer)dSer). 398 makeValue(attributes.getValue(i)); 399 attributeMap.put(fieldName, val); 400 } catch (Exception e) { 401 throw new SAXException (e); 402 } 403 } } } 407 410 private void setSimpleTypeAttributes() throws SAXException { 411 if (attributeMap == null) 412 return; 413 414 Set entries = attributeMap.entrySet(); 416 for (Iterator iterator = entries.iterator(); iterator.hasNext();) { 417 Map.Entry entry = (Map.Entry ) iterator.next(); 418 String name = (String ) entry.getKey(); 419 Object val = entry.getValue(); 420 421 BeanPropertyDescriptor bpd = 422 (BeanPropertyDescriptor) propertyMap.get(name); 423 if (!bpd.isWriteable() || bpd.isIndexed()) continue; 424 try { 425 bpd.set(value, val ); 426 } catch (Exception e) { 427 throw new SAXException (e); 428 } 429 } 430 } 431 432 } 433 | Popular Tags |