1 28 29 package org.jibx.binding.def; 30 31 import org.jibx.binding.classes.ClassCache; 32 import org.jibx.binding.classes.ClassFile; 33 import org.jibx.binding.classes.ContextMethodBuilder; 34 import org.jibx.runtime.JiBXException; 35 36 47 48 public class StructureReference extends PassThroughComponent 49 { 50 51 private final IContainer m_container; 52 53 54 private final IContextObj m_contextObject; 55 56 57 private final PropertyDefinition m_property; 58 59 60 private final String m_label; 61 62 63 private boolean m_skipMarshal; 64 65 66 private boolean m_needLoad; 67 68 77 78 public StructureReference(IContainer contain, String label, 79 PropertyDefinition prop, boolean hasname, IContextObj cobj) { 80 super(); 81 m_container = contain; 82 m_contextObject = cobj; 83 m_property = prop; 84 m_skipMarshal = hasname && prop != null && prop.isOptional(); 85 m_label = label; 86 } 87 88 91 public void genAttributeMarshal(ContextMethodBuilder mb) 92 throws JiBXException { 93 if (m_needLoad) { 94 mb.loadObject(); 95 } 96 m_component.genAttributeMarshal(mb); 97 } 98 99 public void genContentMarshal(ContextMethodBuilder mb) 100 throws JiBXException { 101 if (m_needLoad) { 102 mb.loadObject(); 103 } 104 m_component.genContentMarshal(mb); 105 } 106 107 public void setLinkages() throws JiBXException { 108 109 DefinitionContext defc = m_container.getDefinitionContext(); 111 IComponent impl = defc.getNamedStructure(m_label); 112 113 String type = (m_property == null) ? 115 m_contextObject.getBoundClass().getClassName() : 116 m_property.getTypeName(); 117 ClassFile cf = ClassCache.getClassFile(type); 118 String itype = impl.getType(); 119 if (!cf.isSuperclass(itype)) { 120 throw new JiBXException("Reference to structure " + m_label + 121 " has object of type " + type + " rather than required " + 122 itype); 123 } 124 125 IComponent wrap; 127 if (impl instanceof DirectObject) { 128 wrap = new DirectProperty(m_property, (DirectObject)impl); 129 } else if (m_property == null || m_property.isImplicit()) { 130 wrap = impl; 131 m_needLoad = impl instanceof ObjectBinding; 132 } else { 133 wrap = new ComponentProperty(m_property, impl, m_skipMarshal); 134 } 135 136 setWrappedComponent(wrap); 138 m_component.setLinkages(); 139 } 140 141 public void print(int depth) { 143 BindingDefinition.indent(depth); 144 System.out.print("structure reference to " + m_label); 145 if (m_property != null) { 146 System.out.println(" using " + m_property.toString()); 147 } 148 System.out.println(); 149 } 150 } 151 | Popular Tags |