schemagenerator.java
来自「对xml很好的java处理引擎,编译中绑定xml」· Java 代码 · 共 996 行 · 第 1/3 页
JAVA
996 行
* @param qname name referenced by this schema * @param hold schema holder */ private void checkDependency(QName qname, SchemaHolder hold) { if (qname != null && !isEqual(qname.getUri(), hold.getNamespace())) { String uri = qname.getUri(); SchemaHolder tohold = findSchema(uri); hold.addReference(tohold); hold.getPrefix(uri); } } /** * Build a schema element description from a binding content component. * * @param comp source component * @param repeat repeated element flag * @param holder * @return element */ private ElementElement buildElement(IComponent comp, boolean repeat, SchemaHolder holder) { // create the basic element structure ElementElement elem = new ElementElement(); if (repeat) { elem.setMinOccurs(Count.COUNT_ZERO); elem.setMaxOccurs(Count.COUNT_UNBOUNDED); } else if (comp.isOptional()) { elem.setMinOccurs(Count.COUNT_ZERO); } // set or create the element type definition boolean isref = false; if (comp instanceof ValueElement) { QName qname = getSimpleTypeQName(comp); if (qname == null) { elem.setInlineType(buildSimpleType(comp)); } else { elem.setType(qname); } addItemDocumentation((ValueElement)comp, elem); } else if (comp instanceof CollectionElement) { CollectionElement coll = (CollectionElement)comp; if (coll.children().size() > 0) { // collection with children, choice or sequence from order ComplexTypeElement type = new ComplexTypeElement(); if (coll.isOrdered()) { // ordered children go to sequence element, repeating SequenceElement seq = new SequenceElement(); type.setContentDefinition(seq); } else { // unordered children go to repeated choice element ChoiceElement choice = new ChoiceElement(); type.setContentDefinition(choice); } type.setContentDefinition(buildCompositor(coll, 0, true, holder)); elem.setInlineType(type); } else { // empty collection, item-type must reference a concrete mapping String itype = coll.getItemTypeName(); TemplateElementBase ref = coll.getDefinitions().getSpecificTemplate(itype); if (ref instanceof MappingElement) { // item type with concrete mapping, make it an element SchemaMappingDetail detail = m_detailDirectory.getMappingDetail((MappingElement)ref); checkDependency(detail.getOtherName(), holder); ComplexTypeElement type = new ComplexTypeElement(); SequenceElement seq = new SequenceElement(); type.setContentDefinition(seq); ElementElement item = new ElementElement(); item.setRef(detail.getOtherName()); item.setMinOccurs(Count.COUNT_ZERO); item.setMaxOccurs(Count.COUNT_UNBOUNDED); seq.getParticleList().add(item); elem.setInlineType(type); addItemDocumentation(coll, item); } else { // TODO: handle this with xs:any strict? m_context.addWarning("Handling not implemented for unspecified mapping", coll); } } } else { // must be a structure, check children StructureElement struct = (StructureElement)comp; if (struct.children().size() > 0) { // structure with children, choice or sequence from order ComplexTypeElement type = new ComplexTypeElement(); if (struct.isOrdered()) { // ordered children go to sequence element SequenceElement seq = new SequenceElement(); type.setContentDefinition(seq); } else { // unordered children go to repeated choice element ChoiceElement choice = new ChoiceElement(); type.setContentDefinition(choice); } type.setContentDefinition(buildCompositor(struct, 0, false, holder)); fillAttributes(struct, 0, type.getAttributeList(), holder); elem.setInlineType(type); } else { // no children, must be a mapping reference MappingElement ref = (MappingElement)struct.getEffectiveMapping(); if (ref == null) { // TODO: handle this with xs:any strict? m_context.addWarning("Handling not implemented for unspecified mapping", struct); } else { // implicit mapping reference, find the handling SchemaMappingDetail detail = m_detailDirectory.getMappingDetail(ref); if (detail.isElement()) { // structure with concrete mapping checkDependency(detail.getOtherName(), holder); elem.setRef(detail.getOtherName()); isref = true; } else { // set element type to that constructed from mapping checkDependency(detail.getTypeName(), holder); elem.setType(detail.getTypeName()); } } } addItemDocumentation(struct, elem); } if (!isref) { elem.setName(comp.getName()); } return elem; } /** * Build compositor for type definition. This creates and returns the * appropriate form of compositor for the container, populated with * particles matching the child element components of the binding container. * * @param cont container element defining structure * @param offset offset for first child component to process * @param repeat repeated item flag * @param holder holder for schema under construction * @return constructed compositor */ private CommonCompositorDefinition buildCompositor (ContainerElementBase cont, int offset, boolean repeat, SchemaHolder holder) { // start with the appropriate type of compositor CommonCompositorDefinition cdef; if (cont.isChoice()) { // choice container goes directly to choice compositor cdef = new ChoiceElement(); } else if (cont.isOrdered()) { // ordered container goes directly to sequence compositor cdef = new SequenceElement(); } else if (repeat) { // unordered repeat treated as repeated choice compositor cdef = new ChoiceElement(); cdef.setMaxOccurs(Count.COUNT_UNBOUNDED); } else { // unordered non-repeat treated as all compositor // TODO: verify conditions for all cdef = new AllElement(); } // generate schema equivalents for content components of container ArrayList comps = cont.getContentComponents(); for (int i = offset; i < comps.size(); i++) { IComponent comp = (IComponent)comps.get(i); if (comp.hasName()) { // create element for named content component ElementElement element = buildElement(comp, repeat, holder); if (comp instanceof StructureElementBase) { addItemDocumentation((StructureElementBase)comp, element); } cdef.getParticleList().add(element); } else if (comp instanceof ContainerElementBase) { ContainerElementBase contain = (ContainerElementBase)comp; boolean iscoll = comp instanceof CollectionElement; if (contain.children().size() > 0) { // no element name, but children; handle with recursive call CommonCompositorDefinition part = buildCompositor(contain, 0, iscoll, holder); cdef.getParticleList().add(part); } else if (iscoll) { // collection without a wrapper element CollectionElement coll = (CollectionElement)comp; String itype = coll.getItemTypeName(); TemplateElementBase ref = coll.getDefinitions().getSpecificTemplate(itype); if (ref instanceof MappingElement) { // item type with concrete mapping, make it an element SchemaMappingDetail detail = m_detailDirectory.getMappingDetail((MappingElement)ref); checkDependency(detail.getOtherName(), holder); ElementElement item = new ElementElement(); item.setRef(detail.getOtherName()); item.setMinOccurs(Count.COUNT_ZERO); item.setMaxOccurs(Count.COUNT_UNBOUNDED); addItemDocumentation(coll, item); cdef.getParticleList().add(item); } else { // TODO: handle this with xs:any strict? m_context.addWarning("Handling not implemented for unspecified mapping", coll); } } else if (comp instanceof StructureElement) { // no children, must be mapping reference StructureElement struct = (StructureElement)comp; MappingElement ref = (MappingElement)struct.getEffectiveMapping(); if (ref == null) { // TODO: handle this with xs:any strict? m_context.addWarning("Handling not implemented for unspecified mapping", struct); } else { // handle mapping reference based on form and name use SchemaMappingDetail detail = m_detailDirectory.getMappingDetail(ref); checkDependency(detail.getOtherName(), holder); if (ref.isAbstract()) { // abstract inline treated as group GroupRefElement group = new GroupRefElement(); group.setRef(detail.getOtherName()); if (comp.isOptional()) { group.setMinOccurs(Count.COUNT_ZERO); } cdef.getParticleList().add(group); } else { // concrete treated as element reference ElementElement elem = new ElementElement(); elem.setRef(detail.getOtherName()); if (comp.isOptional()) { elem.setMinOccurs(Count.COUNT_ZERO); } addItemDocumentation(struct, elem); cdef.getParticleList().add(elem); } } } else { m_context.addError("Unsupported binding construct", comp); } } else { m_context.addError("Unsupported component", comp); } } // simplify by eliminating this level if only child a compositor if ((cont.isOrdered() || cont.isChoice()) && cdef.getParticleList().size() == 1) { Object child = cdef.getParticleList().get(0); if (child instanceof CommonCompositorDefinition) { cdef = (CommonCompositorDefinition)child; } } return cdef; } /** * Build attributes as part of complex type definition. * * @param cont container element defining structure * @param offset offset for first child component to process * @param attrs complex type attribute list * @param holder holder for schema under construction */ private void fillAttributes(ContainerElementBase cont, int offset, AbstractList attrs, SchemaHolder holder) { // add child attribute components ArrayList comps = cont.getAttributeComponents(); for (int i = 0; i < comps.size(); i++) { IComponent comp = (IComponent)comps.get(i); if (comp instanceof ValueElement) { // simple attribute definition AttributeElement attr = new AttributeElement(); attr.setName(comp.getName()); QName qname = getSimpleTypeQName(comp); if (qname == null) { attr.setInlineType(buildSimpleType(comp)); } else { attr.setType(qname); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?