1 28 29 package org.jibx.binding.model; 30 31 import java.util.ArrayList ; 32 33 import org.jibx.binding.util.StringArray; 34 import org.jibx.runtime.IUnmarshallingContext; 35 import org.jibx.runtime.JiBXException; 36 37 43 44 public class StructureElement extends StructureElementBase 45 { 46 47 public static final StringArray s_allowedAttributes = 48 new StringArray(new String [] { "map-as" }, 49 StructureElementBase.s_allowedAttributes); 50 51 52 private String m_mapAsName; 53 54 55 private TemplateElementBase m_mapAsMapping; 56 57 60 public StructureElement() { 61 super(STRUCTURE_ELEMENT); 62 } 63 64 69 public String getMapAsName() { 70 return m_mapAsName; 71 } 72 73 78 public void setMapAsName(String name) { 79 m_mapAsName = name; 80 } 81 82 88 public TemplateElementBase getMapAsMapping() { 89 return m_mapAsMapping; 90 } 91 92 95 101 public IClass getEffectiveType() { 102 return getType(); 103 } 104 105 112 public IClass getActualType() { 113 if (hasType()) { 114 return getType(); 115 } else { 116 return null; 117 } 118 } 119 120 123 public boolean hasName() { 124 if (m_mapAsMapping instanceof MappingElement) { 125 if (((MappingElement)m_mapAsMapping).getName() != null) { 126 return true; 127 } 128 } 129 if (children().size() == 0) { 130 return true; 132 } 133 return super.hasName(); 134 } 135 136 139 public boolean hasType() { 140 return m_mapAsMapping != null || super.hasType(); 141 } 142 143 146 public IClass getType() { 147 if (m_mapAsMapping == null) { 148 return super.getType(); 149 } else { 150 return m_mapAsMapping.getHandledClass(); 151 } 152 } 153 154 157 163 private void preSet(IUnmarshallingContext uctx) throws JiBXException { 164 validateAttributes(uctx, s_allowedAttributes); 165 } 166 167 170 public void validate(ValidationContext vctx) { 171 172 DefinitionContext dctx = vctx.getDefinitions(); 174 if (children().size() == 0) { 175 if (m_mapAsName == null) { 176 177 if ((vctx.isInBinding() && getUnmarshallerName() == null) || 179 (vctx.isOutBinding() && getMarshallerName() == null)) { 180 if (getUsing() == null) { 181 if (!dctx.isCompatibleTemplateType(getType())) { 182 vctx.addFatal 183 ("No compatible mapping defined for type " + 184 getType().getName()); 185 } 186 } 187 } 188 } else { 189 m_mapAsMapping = dctx.getSpecificTemplate(m_mapAsName); 190 } 191 } else if (m_mapAsName != null) { 192 vctx.addError("map-as attribute cannot be used with children"); 193 } 194 IClass type = getType(); 195 if (type != null) { 196 197 ArrayList children = children(); 199 for (int i = 0; i < children.size(); i++) { 200 ElementBase child = (ElementBase)children.get(i); 201 if (child instanceof IComponent) { 202 IComponent comp = (IComponent)child; 203 if (comp.isImplicit() && comp.hasType()) { 204 IClass ctype = comp.getType(); 205 if (!ctype.isAssignable(type)) { 206 vctx.addFatal("Implicit references to structure " + 207 "object must have types compatible with " + 208 "structure type", child); 209 } 210 } 211 } 212 } 213 } 214 super.validate(vctx); 215 } 216 } | Popular Tags |