📄 structureelement.java
字号:
* @param addc context to be merged into * @param vctx */ private void mergeNamespaces(DefinitionContext defc, DefinitionContext addc, ValidationContext vctx) { // check for namespaces present in context ArrayList nss = defc.getNamespaces(); if (nss != null) { // merge namespaces with those defined by containing mapping for (int i = 0; i < nss.size(); i++) { NamespaceElement ns = (NamespaceElement)nss.get(i); ValidationProblem prob = addc.addImpliedNamespace(ns, this); if (prob != null) { vctx.addProblem(prob); } } } } /** * Check for conflicts on namespace prefix usage. Abstract mappings may * define namespaces, but the prefixes used by the abstract mappings must * not conflict with those used at the point of reference. This allows the * namespace definitions from the abstract mapping to be promoted to the * containing element. * * @param base * @param vctx */ private void checkNamespaceUsage(TemplateElementBase base, ValidationContext vctx) { // check for namespace definitions needing to be handled if (base instanceof MappingElement && ((MappingElement)base).isAbstract()) { // merge namespaces defined within this mapping DefinitionContext addc = vctx.getFormatDefinitions(); DefinitionContext defc = base.getDefinitions(); if (defc != null) { mergeNamespaces(defc, addc, vctx); } } } /** * Classify child components as contributing attributes, content, or both. * This method is needed to handle on-demand classification during * validation. When a child component is another instance of this class, the * method calls itself on the child component prior to checking the child * component's contribution. * * @param vctx */ protected void classifyComponents(ValidationContext vctx) { // check for classification already run if (!isClassified()) { // check if there's a mapping if used without children // TODO: this isn't correct, but validation may not have reached // this element yet so the context may not have been set. Clean // this with parent link in all elements. DefinitionContext dctx = vctx.getDefinitions(); if (children().size() == 0) { if (m_mapAsQName == null) { // make sure not just a name, allowed for skipped element if (hasProperty() || getDeclaredType() != null) { // see if this is using implicit marshaller/unmarshaller if ((vctx.isInBinding() && getUnmarshallerName() == null) || (vctx.isOutBinding() && getMarshallerName() == null)) { if (getUsing() == null) { // check for specific type known IClass type = getType(); if (!"java.lang.Object".equals(type.getName())) { setMappingReference(vctx, dctx, type); } } } } } else { // find mapping by type name or class name TemplateElementBase base = dctx.getNamedTemplate(m_mapAsQName.toString()); if (base == null) { base = dctx.getSpecificTemplate(m_mapAsName); if (base == null) { vctx.addFatal("No mapping with type name " + m_mapAsQName.toString()); } } if (base != null) { // make sure type is compatible IClass type = getType(); if (type != null) { if (!type.isAssignable(base.getHandledClass()) && !base.getHandledClass().isAssignable(type)) { vctx.addError("Object type " + type.getName() + " is incompatible with binding for class " + base.getClassName()); } } m_effectiveMapping = base; // check for namespace conflicts checkNamespaceUsage(base, vctx); // set flag for mapping with name m_hasMappingName = base instanceof MappingElement && !((MappingElement)base).isAbstract(); } } // classify mapping reference as providing content, attributes, or both if (m_effectiveMapping instanceof MappingElement) { MappingElement mapping = (MappingElement)m_effectiveMapping; mapping.classifyComponents(vctx); if (mapping.getName() == null) { ArrayList attribs = EmptyList.INSTANCE; ArrayList contents = EmptyList.INSTANCE; if (mapping.getContentComponents().size() > 0) { contents = new ArrayList(); contents.add(m_effectiveMapping); } if (mapping.getAttributeComponents().size() > 0) { attribs = new ArrayList(); attribs.add(m_effectiveMapping); } setComponents(attribs, contents); } else { ArrayList contents = new ArrayList(); contents.add(m_effectiveMapping); setComponents(EmptyList.INSTANCE, contents); } } } else if (m_mapAsName != null) { vctx.addError("map-as attribute cannot be used with children"); } } // pass on call to superclass implementation super.classifyComponents(vctx); } /* (non-Javadoc) * @see org.jibx.binding.model.ElementBase#validate(org.jibx.binding.model.ValidationContext) */ public void validate(ValidationContext vctx) { // set up child components and effective mapping classifyComponents(vctx); IClass type = getType(); if (type != null) { // check each child component for compatible type ArrayList children = children(); if (hasProperty() || getDeclaredType() != null) { checkCompatibleChildren(vctx, type, children); } // check for only set-method supplied if (!vctx.isOutBinding() && getField() == null && getGet() == null && getSet() != null) { // no way to handle both elements and attributes if (hasAttribute() && hasContent()) { vctx.addError("Need way to load existing object instance " + "to support combined attribute and element values"); } else { vctx.addWarning("No way to load prior value - " + "new instance will be created on each unmarshalling"); } } } // make sure name is defined if nillable if (isNillable() && !hasName()) { vctx.addError("Need element name for nillable='true'"); } super.validate(vctx); } /** * Validate mapping reference. * * @param vctx validation context * @param dctx definition context * @param type referenced type */ private void setMappingReference(ValidationContext vctx, DefinitionContext dctx, IClass type) { // first try to find mapping by specific type m_effectiveMapping = dctx.getNamedTemplate(type.getName()); if (m_effectiveMapping == null) { // see if there's a mapping specific to the reference type String tname = type.getName(); TemplateElementBase match = dctx.getSpecificTemplate(tname); if (match != null) { m_effectiveMapping = match; if (match instanceof MappingElement) { // set flag for name defined by mapping MappingElement base = (MappingElement)match; m_hasMappingName = !base.isAbstract(); checkNamespaceUsage(base, vctx); // check name usage on non-abstract or base mapping if (super.hasName()) { if (!base.isAbstract()) { vctx.addError("name attribute not allowed on concrete mapping reference"); } else if (base.getExtensionTypes().size() > 0) { vctx.addError("name attribute not allowed on reference to mapping with extensions"); } } } } else if (!dctx.isCompatibleTemplateType(type)) { vctx.addFatal("No compatible mapping defined for type " + tname); } } else if (m_effectiveMapping instanceof MappingElement) { // set flag for name defined by mapping MappingElement base = (MappingElement)m_effectiveMapping; m_hasMappingName = !base.isAbstract(); checkNamespaceUsage(base, vctx); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -