1 28 29 package org.jibx.binding.def; 30 31 import java.lang.reflect.InvocationTargetException ; 32 import java.lang.reflect.Method ; 33 34 import org.apache.bcel.generic.*; 35 36 import org.jibx.binding.classes.*; 37 import org.jibx.runtime.JiBXException; 38 39 46 47 public class PrimitiveStringConversion extends StringConversion 48 { 49 52 private static ClassFile s_unmarshalClass; 53 { 54 try { 55 s_unmarshalClass = ClassCache.getClassFile 56 ("org.jibx.runtime.impl.UnmarshallingContext"); 57 } catch (JiBXException ex) { } 58 } 59 60 private static final int INT_TYPE = 0; 63 private static final int LONG_TYPE = 1; 64 private static final int FLOAT_TYPE = 2; 65 private static final int DOUBLE_TYPE = 3; 66 67 70 71 private static final String UTILITY_CLASS_NAME = 72 "org.jibx.runtime.Utility"; 73 74 75 private static final String UNMARSHAL_CLASS_NAME = 76 "org.jibx.runtime.impl.UnmarshallingContext"; 77 78 79 private static final String UNMARSHAL_SIG_LEAD = 80 "(Ljava/lang/String;Ljava/lang/String;"; 81 82 83 private static final Class [] SINGLE_STRING_ARGS = 84 new Class [] { String .class }; 85 86 89 90 private boolean m_isMarshalText; 91 92 93 private boolean m_isUnmarshalText; 94 95 96 private ClassItem m_unmarshalOptAttribute; 97 98 99 private ClassItem m_unmarshalOptElement; 100 101 102 private ClassItem m_unmarshalReqAttribute; 103 104 105 private ClassItem m_unmarshalReqElement; 106 107 109 private int m_valueType; 110 111 112 private String m_stackType; 113 114 121 122 protected PrimitiveStringConversion(String type, 123 PrimitiveStringConversion inherit) { 124 super(type, inherit); 125 m_isMarshalText = inherit.m_isMarshalText; 126 m_isUnmarshalText = inherit.m_isUnmarshalText; 127 m_unmarshalOptAttribute = inherit.m_unmarshalOptAttribute; 128 m_unmarshalOptElement = inherit.m_unmarshalOptElement; 129 m_unmarshalReqAttribute = inherit.m_unmarshalReqAttribute; 130 m_unmarshalReqElement = inherit.m_unmarshalReqElement; 131 m_valueType = inherit.m_valueType; 132 m_stackType = inherit.m_stackType; 133 } 134 135 149 150 public PrimitiveStringConversion(Class cls, Object dflt, String code, 151 String ts, String fs, String uattr, String uelem) { 152 super(dflt, UTILITY_CLASS_NAME+'.'+ts, UTILITY_CLASS_NAME+'.'+fs, 153 cls.getName()); 154 m_isMarshalText = m_isUnmarshalText = false; 155 String sig = UNMARSHAL_SIG_LEAD + code + ')' + code; 156 m_unmarshalOptAttribute = s_unmarshalClass.getMethod(uattr, sig); 157 m_unmarshalOptElement = s_unmarshalClass.getMethod(uelem, sig); 158 sig = UNMARSHAL_SIG_LEAD + ')' + code; 159 m_unmarshalReqAttribute = s_unmarshalClass.getMethod(uattr, sig); 160 m_unmarshalReqElement = s_unmarshalClass.getMethod(uelem, sig); 161 if (cls == Long.TYPE) { 162 m_valueType = LONG_TYPE; 163 m_stackType = "long"; 164 } else if (cls == Float.TYPE) { 165 m_valueType = FLOAT_TYPE; 166 m_stackType = "float"; 167 } else if (cls == Double.TYPE) { 168 m_valueType = DOUBLE_TYPE; 169 m_stackType = "double"; 170 } else { 171 m_valueType = INT_TYPE; 172 m_stackType = "int"; 173 } 174 } 175 176 184 185 public void genFromText(MethodBuilder mb) { 186 187 if (m_deserializer != null) { 189 190 mb.addMethodExceptions(m_deserializer); 194 mb.appendCall(m_deserializer); 195 } 196 } 197 198 204 205 protected void pushDefault(MethodBuilder mb) { 206 mb.appendLoadConstant(m_default); 207 } 208 209 220 221 public void genParseOptional(boolean attr, MethodBuilder mb) 222 throws JiBXException { 223 224 if (m_isUnmarshalText) { 226 227 String dflt; 231 if (m_default instanceof String || m_default == null) { 232 dflt = (String )m_default; 233 } else { 234 dflt = m_default.toString(); 235 } 236 mb.appendLoadConstant(dflt); 237 String name = attr ? UNMARSHAL_OPT_ATTRIBUTE : 238 UNMARSHAL_OPT_ELEMENT; 239 mb.appendCallVirtual(name, UNMARSHAL_OPT_SIGNATURE); 240 241 genFromText(mb); 243 244 } else { 245 246 pushDefault(mb); 250 mb.appendCall(attr ? 251 m_unmarshalOptAttribute : m_unmarshalOptElement); 252 253 } 254 } 255 256 267 268 public void genParseRequired(boolean attr, MethodBuilder mb) 269 throws JiBXException { 270 271 if (m_isUnmarshalText) { 273 274 String name = attr ? UNMARSHAL_REQ_ATTRIBUTE : 278 UNMARSHAL_REQ_ELEMENT; 279 mb.appendCallVirtual(name, UNMARSHAL_REQ_SIGNATURE); 280 281 genFromText(mb); 283 284 } else { 285 286 mb.appendCall(attr ? 289 m_unmarshalReqAttribute : m_unmarshalReqElement); 290 291 } 292 } 293 294 308 309 protected BranchWrapper genToOptionalText(String type, MethodBuilder mb, 310 int extra) throws JiBXException { 311 312 StackInstruction dup; 314 StackInstruction pop; 315 if (m_valueType == LONG_TYPE || m_valueType == DOUBLE_TYPE) { 316 mb.appendDUP2(); 317 } else { 318 mb.appendDUP(); 319 } 320 extra++; 321 322 326 Object value = m_default; 328 if (m_isUnmarshalText) { 329 try { 330 String mname = m_deserializer.getName(); 331 String cname = m_deserializer.getClassFile().getName(); 332 Class clas = ClassFile.loadClass(cname); 333 if (clas == null) { 334 throw new JiBXException("Deserializer class " + cname + 335 " not found for converting default value"); 336 } else { 337 338 Method meth; 340 try { 341 meth = clas.getDeclaredMethod(mname, 342 SINGLE_STRING_ARGS); 343 meth.setAccessible(true); 344 } catch (NoSuchMethodException ex) { 345 meth = clas.getMethod(mname, SINGLE_STRING_ARGS); 346 } 347 String text; 348 if (value instanceof String || value == null) { 349 text = (String )value; 350 } else { 351 text = value.toString(); 352 } 353 value = meth.invoke(null, new Object [] { text }); 354 355 } 356 } catch (IllegalAccessException ex) { 357 throw new JiBXException("Conversion method not accessible", ex); 358 } catch (InvocationTargetException ex) { 359 throw new JiBXException("Internal error", ex); 360 } catch (NoSuchMethodException ex) { 361 throw new JiBXException("Internal error", ex); 362 } 363 } 364 mb.appendLoadConstant(value); 365 366 BranchWrapper ifne = null; 367 switch (m_valueType) { 368 369 case LONG_TYPE: 370 mb.appendLCMP(); 371 break; 372 373 case FLOAT_TYPE: 374 mb.appendFCMPG(); 375 break; 376 377 case DOUBLE_TYPE: 378 mb.appendDCMPG(); 379 break; 380 381 default: 382 ifne = mb.appendIF_ICMPNE(this); 383 break; 384 385 } 386 if (ifne == null) { 387 ifne = mb.appendIFNE(this); 388 } 389 390 genPopValues(extra, mb); 393 BranchWrapper toend = mb.appendUnconditionalBranch(this); 394 mb.targetNext(ifne); 395 genToText(m_stackType, mb); 396 return toend; 397 } 398 399 409 410 protected Object convertDefault(String text) throws JiBXException { 411 if (!m_isUnmarshalText) { 412 try { 413 String mname = m_deserializer.getName(); 414 String cname = m_deserializer.getClassFile().getName(); 415 Class clas = ClassFile.loadClass(cname); 416 if (clas == null) { 417 throw new JiBXException("Deserializer class " + cname + 418 " not found for converting default value"); 419 } else { 420 421 Method meth; 423 try { 424 meth = clas.getDeclaredMethod(mname, 425 SINGLE_STRING_ARGS); 426 meth.setAccessible(true); 427 } catch (NoSuchMethodException ex) { 428 meth = clas.getMethod(mname, SINGLE_STRING_ARGS); 429 } 430 return meth.invoke(null, new Object [] { text }); 431 432 } 433 } catch (IllegalAccessException ex) { 434 throw new JiBXException("Conversion method not accessible", ex); 435 } catch (InvocationTargetException ex) { 436 throw new JiBXException("Internal error", ex); 437 } catch (NoSuchMethodException ex) { 438 throw new JiBXException("Internal error", ex); 439 } 440 } else { 441 return text; 442 } 443 } 444 445 450 451 public boolean isPrimitive() { 452 return true; 453 } 454 455 463 464 protected void setSerializer(String ser) throws JiBXException { 465 super.setSerializer(ser); 466 m_isMarshalText = true; 467 } 468 469 478 479 protected void setDeserializer(String deser) throws JiBXException { 480 super.setDeserializer(deser); 481 m_isUnmarshalText = true; 482 } 483 484 499 500 public StringConversion derive(String type, String ser, String dser, 501 String dflt) throws JiBXException { 502 if (type == null) { 503 type = m_typeName; 504 } 505 StringConversion inst = new PrimitiveStringConversion(type, this); 506 if (ser != null) { 507 inst.setSerializer(ser); 508 } 509 if (dser != null) { 510 inst.setDeserializer(dser); 511 } 512 if (dflt != null) { 513 inst.m_default = inst.convertDefault(dflt); 514 } 515 return inst; 516 } 517 } 518 | Popular Tags |