1 28 29 package org.jibx.binding.model; 30 31 import org.jibx.binding.util.StringArray; 32 33 40 41 public class ObjectAttributes extends AttributeBase 42 { 43 44 public static final StringArray s_allowedAttributes = 45 new StringArray(new String [] { "factory", "marshaller", "post-set", 46 "pre-get", "pre-set", "unmarshaller" }); 47 48 51 private static final String [] MARSHAL_HOOK_SIGNATURES = 53 { 54 "(Lorg/jibx/runtime/IMarshallingContext;)V", 55 "(Ljava/lang/Object;)V", 56 "()V" 57 }; 58 59 private static final String [] FACTORY_HOOK_SIGNATURES = 61 { 62 "(Lorg/jibx/runtime/IUnmarshallingContext;)", 63 "(Ljava/lang/Object;)", 64 "()" 65 }; 66 67 private static final String [] UNMARSHAL_HOOK_SIGNATURES = 69 { 70 "(Lorg/jibx/runtime/IUnmarshallingContext;)V", 71 "(Ljava/lang/Object;)V", 72 "()V" 73 }; 74 75 private static final String UNMARSHALLER_INTERFACE = 77 "org.jibx.runtime.IUnmarshaller"; 78 private static final String MARSHALLER_INTERFACE = 79 "org.jibx.runtime.IMarshaller"; 80 private static final String UNMARSHALLER_INTERFACETYPE = 81 "Lorg/jibx/runtime/IUnmarshaller;"; 82 private static final String MARSHALLER_INTERFACETYPE = 83 "Lorg/jibx/runtime/IMarshaller;"; 84 85 88 89 private String m_factoryName; 90 91 92 private String m_preSetName; 93 94 95 private String m_postSetName; 96 97 98 private String m_preGetName; 99 100 101 private String m_marshallerName; 102 103 104 private String m_unmarshallerName; 105 106 107 private IClassItem m_factoryItem; 108 109 110 private IClassItem m_preSetItem; 111 112 113 private IClassItem m_postSetItem; 114 115 116 private IClassItem m_preGetItem; 117 118 119 private IClass m_marshallerClass; 120 121 122 private IClass m_unmarshallerClass; 123 124 129 public ObjectAttributes() {} 130 131 137 public String getFactoryName() { 138 return m_factoryName; 139 } 140 141 147 public IClassItem getFactory() { 148 return m_factoryItem; 149 } 150 151 156 public void setFactoryName(String name) { 157 m_factoryName = name; 158 } 159 160 165 public String getPresetName() { 166 return m_preSetName; 167 } 168 169 175 public IClassItem getPreset() { 176 return m_preSetItem; 177 } 178 179 184 public void setPresetName(String name) { 185 m_preSetName = name; 186 } 187 188 193 public String getPostsetName() { 194 return m_postSetName; 195 } 196 197 203 public IClassItem getPostset() { 204 return m_postSetItem; 205 } 206 207 212 public void setPostsetName(String name) { 213 m_postSetName = name; 214 } 215 216 221 public String getPregetName() { 222 return m_preGetName; 223 } 224 225 231 public IClassItem getPreget() { 232 return m_preGetItem; 233 } 234 235 240 public void setPreget(String name) { 241 m_preGetName = name; 242 } 243 244 249 public String getMarshallerName() { 250 return m_marshallerName; 251 } 252 253 259 public IClass getMarshaller() { 260 return m_marshallerClass; 261 } 262 263 268 public void setMarshallerName(String name) { 269 m_marshallerName = name; 270 } 271 272 277 public String getUnmarshallerName() { 278 return m_unmarshallerName; 279 } 280 281 287 public IClass getUnmarshaller() { 288 return m_unmarshallerClass; 289 } 290 291 296 public void setUnmarshallerName(String name) { 297 m_unmarshallerName = name; 298 } 299 300 303 public void prevalidate(ValidationContext vctx) { 304 305 IClass iclas; 307 ElementBase element = vctx.getParentElement(0); 308 if (element instanceof StructureElementBase) { 309 iclas = ((StructureElementBase)element).getType(); 310 } else if (element instanceof MappingElement) { 311 iclas = ((MappingElement)element).getHandledClass(); 312 } else { 313 throw new IllegalStateException 314 ("Unknown element for object attributes"); 315 } 316 String type = iclas.getName(); 317 if (m_factoryName != null && m_factoryItem == null) { 318 if (iclas == null) { 319 vctx.addWarning 320 ("No object for structure; factory attribute ignored"); 321 } else { 322 323 m_factoryItem = ClassUtils.findStaticMethod(m_factoryName, 325 FACTORY_HOOK_SIGNATURES, vctx); 326 if (m_factoryItem == null) { 327 vctx.addError("Static factory method " + m_factoryName + 328 " not found"); 329 } else if (!m_factoryItem.getTypeName().equals(type)) { 330 vctx.addError("Static factory method " + m_factoryName + 332 " return type is not " + type); 333 } 334 } 335 } 336 337 if (vctx.isInBinding()) { 339 if (m_preSetName != null) { 340 if (iclas == null) { 341 vctx.addWarning 342 ("No object for structure; pre-set attribute ignored"); 343 } else { 344 m_preSetItem = iclas.getMethod(m_preSetName, 345 UNMARSHAL_HOOK_SIGNATURES); 346 if (m_preSetItem == null) { 347 vctx.addError("Nonstatic pre-set method " + 348 m_preSetName + " not found"); 349 } 350 } 351 } 352 if (m_postSetName != null) { 353 if (iclas == null) { 354 vctx.addWarning 355 ("No object for structure; post-set attribute ignored"); 356 } else { 357 m_postSetItem = iclas.getMethod(m_postSetName, 358 UNMARSHAL_HOOK_SIGNATURES); 359 if (m_postSetItem == null) { 360 vctx.addError("Nonstatic post-set method " + 361 m_postSetName + " not found"); 362 } 363 } 364 } 365 } else { 366 if (iclas == null) { 367 vctx.addWarning 368 ("No object for structure; pre-get attribute ignored"); 369 } else { 370 if (m_preGetName != null) { 371 m_preGetItem = iclas.getMethod(m_preGetName, 372 MARSHAL_HOOK_SIGNATURES); 373 if (m_preGetItem == null) { 374 vctx.addError("Nonstatic pre-get method " + 375 m_preGetName + " not found"); 376 } 377 } 378 } 379 } 380 381 NestingElementBase parent = vctx.getParentElement(); 383 if (!vctx.isInBinding() && m_marshallerName != null) { 384 if (iclas == null) { 385 vctx.addWarning 386 ("No object for structure; marshaller attribute ignored"); 387 } else { 388 IClass mclas = vctx.getClassInfo(m_marshallerName); 389 if (mclas == null) { 390 vctx.addError("Marshaller class " + m_marshallerName + 391 " not found"); 392 } else if (!mclas.isImplements(MARSHALLER_INTERFACETYPE)) { 393 vctx.addError("Marshaller class " + m_marshallerName + 394 " does not implement interface " + 395 MARSHALLER_INTERFACE); 396 } else { 397 m_marshallerClass = mclas; 398 } 399 } 400 } 401 if (vctx.isInBinding() && m_unmarshallerName != null) { 402 if (iclas == null) { 403 vctx.addWarning 404 ("No object for structure; unmarshaller attribute ignored"); 405 } else { 406 IClass uclas = vctx.getClassInfo(m_unmarshallerName); 407 if (uclas == null) { 408 vctx.addError("Unmarshaller class " + m_unmarshallerName + 409 " not found"); 410 } else if (!uclas.isImplements(UNMARSHALLER_INTERFACETYPE)) { 411 vctx.addError("Unmarshaller class " + m_unmarshallerName + 412 " does not implement interface " + 413 UNMARSHALLER_INTERFACE); 414 } else { 415 m_unmarshallerClass = uclas; 416 } 417 } 418 } 419 } 420 } | Popular Tags |