1 28 29 package org.jibx.binding.def; 30 31 import org.apache.bcel.classfile.Utility; 32 33 import org.jibx.binding.classes.*; 34 import org.jibx.runtime.JiBXException; 35 36 45 46 public abstract class StringConversion 47 { 48 51 protected static final String UNMARSHAL_OPT_ATTRIBUTE = 52 "org.jibx.runtime.impl.UnmarshallingContext.attributeText"; 53 protected static final String UNMARSHAL_OPT_ELEMENT = 54 "org.jibx.runtime.impl.UnmarshallingContext.parseElementText"; 55 protected static final String UNMARSHAL_OPT_SIGNATURE = 56 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)" + 57 "Ljava/lang/String;"; 58 protected static final String UNMARSHAL_REQ_ATTRIBUTE = 59 "org.jibx.runtime.impl.UnmarshallingContext.attributeText"; 60 protected static final String UNMARSHAL_REQ_ELEMENT = 61 "org.jibx.runtime.impl.UnmarshallingContext.parseElementText"; 62 protected static final String UNMARSHAL_REQ_SIGNATURE = 63 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"; 64 protected static final String MARSHAL_ATTRIBUTE = 65 "org.jibx.runtime.impl.MarshallingContext.attribute"; 66 protected static final String MARSHAL_ELEMENT = 67 "org.jibx.runtime.impl.MarshallingContext.element"; 68 protected static final String MARSHAL_SIGNATURE = 69 "(ILjava/lang/String;Ljava/lang/String;)" + 70 "Lorg/jibx/runtime/impl/MarshallingContext;"; 71 protected static final String COMPARE_OBJECTS_METHOD = 72 "org.jibx.runtime.Utility.isEqual"; 73 protected static final String COMPARE_OBJECTS_SIGNATURE = 74 "(Ljava/lang/Object;Ljava/lang/Object;)Z"; 75 76 public static final int MARSHAL_NAME_VALUES = 2; 78 79 82 83 protected String m_formatName; 84 85 87 protected Object m_default; 88 89 90 protected ClassItem m_serializer; 91 92 93 protected ClassItem m_deserializer; 94 95 96 protected String m_typeName; 97 98 99 protected String m_typeSignature; 100 101 106 107 private StringConversion(String type) { 108 m_typeName = type; 109 m_typeSignature = Utility.getSignature(type); 110 } 111 112 119 120 protected StringConversion(String type, StringConversion inherit) { 121 this(type); 122 m_default = inherit.m_default; 123 m_serializer = inherit.m_serializer; 124 m_deserializer = inherit.m_deserializer; 125 } 126 127 138 139 StringConversion(Object dflt, String ser, String deser, 140 String type) { 141 this(type); 142 m_default = dflt; 143 try { 144 if (ser != null) { 145 setSerializer(ser); 146 } 147 if (deser != null) { 148 setDeserializer(deser); 149 } 150 } catch (JiBXException ex) { 151 throw new IllegalArgumentException (ex.getMessage()); 152 } 153 } 154 155 160 161 public String getTypeName() { 162 return m_typeName; 163 } 164 165 174 175 public abstract void genFromText(MethodBuilder mb) throws JiBXException; 176 177 190 191 public abstract void genParseOptional(boolean attr, MethodBuilder mb) 192 throws JiBXException; 193 194 206 207 public abstract void genParseRequired(boolean attr, MethodBuilder ub) 208 throws JiBXException; 209 210 221 222 public void genWriteText(boolean attr, MethodBuilder mb) 223 throws JiBXException { 224 225 String name = attr ? MARSHAL_ATTRIBUTE : MARSHAL_ELEMENT; 228 mb.appendCallVirtual(name, MARSHAL_SIGNATURE); 229 } 230 231 237 238 public void genPopValues(int count, MethodBuilder mb) { 239 while (--count >= 0) { 240 if (mb.isStackTopLong()) { 241 mb.appendPOP2(); 242 } else { 243 mb.appendPOP(); 244 } 245 } 246 } 247 248 263 264 protected abstract BranchWrapper genToOptionalText(String type, 265 MethodBuilder mb, int extra) throws JiBXException; 266 267 277 278 public void genToText(String type, MethodBuilder mb) throws JiBXException { 279 280 if (m_serializer != null) { 282 283 if (!isPrimitive()) { 287 mb.appendCreateCast(type, m_serializer.getArgumentType(0)); 288 } 289 mb.addMethodExceptions(m_serializer); 290 mb.appendCall(m_serializer); 291 292 } else { 293 294 mb.appendCreateCast(type, "java.lang.String"); 296 } 297 } 298 299 312 313 public void genWriteOptional(boolean attr, String type, MethodBuilder mb) 314 throws JiBXException { 315 316 BranchWrapper toend = genToOptionalText(type, mb, MARSHAL_NAME_VALUES); 319 320 genWriteText(attr, mb); 322 if (toend != null) { 323 mb.targetNext(toend); 324 } 325 } 326 327 339 340 public void genWriteRequired(boolean attr, String type, MethodBuilder mb) 341 throws JiBXException { 342 343 genToText(type, mb); 345 genWriteText(attr, mb); 346 } 347 348 354 355 public abstract boolean isPrimitive(); 356 357 367 368 protected void setSerializer(String ser) throws JiBXException { 369 370 String [] tsigs = ClassItem.getSignatureVariants(m_typeName); 372 String [] msigs = new String [tsigs.length]; 373 for (int i = 0; i < tsigs.length; i++) { 374 msigs[i] = "(" + tsigs[i] + ")Ljava/lang/String;"; 375 } 376 377 ClassItem method = ClassItem.findStaticMethod(ser, msigs); 379 380 if (method == null) { 382 throw new JiBXException("Serializer " + ser + " not found"); 383 } else { 384 m_serializer = method; 385 } 386 } 387 388 398 399 protected void setDeserializer(String deser) throws JiBXException { 400 401 String [] msigs = new String [] { "(Ljava/lang/String;)" }; 403 ClassItem method = ClassItem.findStaticMethod(deser, msigs); 404 405 if (method == null) { 407 throw new JiBXException("Deserializer " + deser + " not found"); 408 } else if (ClassItem.isAssignable(method.getTypeName(), m_typeName)) { 409 m_deserializer = method; 410 } else { 411 throw new JiBXException("Deserializer " + deser + 412 " returns wrong type"); 413 } 414 } 415 416 424 425 protected abstract Object convertDefault(String text) throws JiBXException; 426 427 443 444 public abstract StringConversion derive(String type, String ser, 445 String dser, String dlft) throws JiBXException; 446 } | Popular Tags |