📄 schemagenerator.java
字号:
if (!comp.isOptional()) { attr.setUse(AttributeElement.REQUIRED_USE); } addItemDocumentation((ValueElement)comp, attr); attrs.add(attr); } else if (comp instanceof ContainerElementBase) { ContainerElementBase contain = (ContainerElementBase)comp; if (contain.children().size() > 0) { // handle children with recursive call fillAttributes(contain, 0, attrs, holder); } else if (comp instanceof StructureElement) { // no children, must be mapping reference StructureElement struct = (StructureElement)comp; if (struct.isOptional()) { m_context.addError("No schema equivalent for optional abstract mapping with attributes", comp); } else { MappingElement ref = (MappingElement)struct.getEffectiveMapping(); if (ref != null && ref.isAbstract()) { // abstract inline treated as group SchemaMappingDetail detail = m_detailDirectory.getMappingDetail(ref); checkDependency(detail.getOtherName(), holder); AttributeGroupRefElement group = new AttributeGroupRefElement(); group.setRef(detail.getOtherName()); attrs.add(group); } else { throw new IllegalStateException("Unsupported binding construct " + comp); } } } else { m_context.addError("Unsupported binding construct", comp); } } else { m_context.addError("Unsupported component", comp); } } } /** * Check if a container element of the binding represents complex content. * * @param cont * @return <code>true</code> if complex content, <code>false</code> if not */ private static boolean isComplexContent(ContainerElementBase cont) { ArrayList conts = cont.getContentComponents(); for (int i = 0; i < conts.size(); i++) { IComponent comp = (IComponent)conts.get(i); if (comp.hasName()) { return true; } else if (comp instanceof ContainerElementBase && isComplexContent((ContainerElementBase)comp)) { return true; } } return false; } /** * Build the complex type definition for a mapping. * * @param detail mapping detail * @param hold target schema for definition * @return constructed complex type */ private ComplexTypeElement buildComplexType(SchemaMappingDetail detail, SchemaHolder hold) { // create the type and compositor ComplexTypeElement type = new ComplexTypeElement(); MappingElement mapping = detail.getMapping(); MappingElement base = detail.getExtensionBase(); if (base == null) { if (detail.isGroup()) { // create type using references to group and/or attributeGroup SequenceElement seq = new SequenceElement(); if (detail.hasChild()) { GroupRefElement gref = new GroupRefElement(); gref.setRef(detail.getOtherName()); seq.getParticleList().add(gref); } type.setContentDefinition(seq); if (detail.hasAttribute()) { AttributeGroupRefElement gref = new AttributeGroupRefElement(); gref.setRef(detail.getOtherName()); type.getAttributeList().add(gref); } } else { // create type directly type.setContentDefinition(buildCompositor(mapping, 0, false, hold)); fillAttributes(mapping, 0, type.getAttributeList(), hold); } } else { // create type as extension of base type ComplexExtensionElement ext = new ComplexExtensionElement(); SchemaMappingDetail basedet = m_detailDirectory.getMappingDetail(base); ext.setBase(basedet.getTypeName()); ext.setContentDefinition(buildCompositor(mapping, 1, false, hold)); if (isComplexContent(base) || isComplexContent(mapping)) { ComplexContentElement cont = new ComplexContentElement(); cont.setDerivation(ext); type.setContentType(cont); } else { SimpleContentElement cont = new SimpleContentElement(); cont.setDerivation(ext); type.setContentType(cont); } } return type; } /** * Add mapping to schema definitions. This generates the appropriate schema * representation for the mapping based on the detail flags, which may * include group and/or attributeGroup, complexType, and element * definitions. * * @param detail */ private void addMapping(SchemaMappingDetail detail) { // get the documentation to be used for type IClass info = null; if (m_locator != null) { info = m_locator.getClassInfo(detail.getMapping().getClassName()); } // start by generating group/attributeGroup schema components MappingElement mapping = detail.getMapping(); if (detail.isGroup()) { // TODO: extend base type for group/attributeGroup? QName qname = detail.getOtherName(); SchemaHolder hold = findSchema(qname.getUri()); if (detail.hasChild()) { GroupElement group = new GroupElement(); group.setName(qname.getName()); group.setDefinition(buildCompositor(mapping, 0, false, hold)); addDocumentation(info, group); hold.getSchema().getTopLevelChildren().add(group); } if (detail.hasAttribute()) { AttributeGroupElement attgrp = new AttributeGroupElement(); attgrp.setName(qname.getName()); fillAttributes(mapping, 0, attgrp.getAttributeList(), hold); addDocumentation(info, attgrp); hold.getSchema().getTopLevelChildren().add(attgrp); } } // next generate the complex type definition if (detail.isType()) { // set up the basic definition structure QName qname = detail.getTypeName(); SchemaHolder hold = findSchema(qname.getUri()); ComplexTypeElement type = buildComplexType(detail, hold); type.setName(qname.getName()); addDocumentation(info, type); hold.getSchema().getTopLevelChildren().add(type); } // finish by generating element definition if (detail.isElement()) { QName qname = detail.getOtherName(); SchemaHolder hold = findSchema(qname.getUri()); ElementElement elem = new ElementElement(); elem.setName(qname.getName()); elem.setSubstitutionGroup(detail.getSubstitution()); if (detail.isType()) { elem.setType(detail.getTypeName()); } else { // check for just an element wrapper around type reference MappingElement ext = detail.getExtensionBase(); if (ext != null && mapping.getContentComponents().size() == 1) { elem.setType(ext.getTypeQName()); } else { // add documentation to element which is not also a type addDocumentation(info, elem); elem.setInlineType(buildComplexType(detail, hold)); } } hold.getSchema().getTopLevelChildren().add(elem); } } /** * Generate a list of schemas from a list of bindings. The two lists are not * necessarily in any particular relationship to each other. * * @param holders list of {@link BindingHolder} * @return schemas list of {@link SchemaHolder} */ public ArrayList generate(ArrayList holders) { // start by scanning all bindings to build mapping and enum details m_detailDirectory.populate(holders); // process all the simple type definitions (formats or enums) Collection simples = m_detailDirectory.getSimpleDetails(); for (Iterator iter = simples.iterator(); iter.hasNext();) { SchemaEnumDetail detail = (SchemaEnumDetail)iter.next(); if (detail.isGlobal()) { ClassCustom custom = detail.getCustom(); SimpleTypeElement type = buildSimpleType(custom.getClassInformation()); type.setName(custom.getTypeName()); SchemaHolder hold = findSchema(custom.getNamespace()); hold.getSchema().getTopLevelChildren().add(type); m_simpleTypeMap.put(custom.getName(), custom.getTypeQName()); } } // process all the mapping definitions from directory Collection mappings = m_detailDirectory.getComplexDetails(); for (Iterator iter = mappings.iterator(); iter.hasNext();) { SchemaMappingDetail detail = (SchemaMappingDetail)iter.next(); addMapping(detail); } // report any problems found in schema generation ArrayList probs = m_context.getProblems(); boolean error = m_context.getErrorCount() > 0 || m_context.getFatalCount() > 0; if (probs.size() > 0) { System.out.print(error ? "Errors" : "Warnings"); System.out.println(" in generated binding:"); for (int j = 0; j < probs.size(); j++) { ValidationProblem prob = (ValidationProblem)probs.get(j); System.out.print(prob.getSeverity() >= ValidationProblem.ERROR_LEVEL ? "Error: " : "Warning: "); System.out.println(prob.getDescription()); } } // fix all references between schemas ArrayList schemas = new ArrayList(m_uriSchemaMap.values()); for (int i = 0; i < schemas.size(); i++) { SchemaHolder hold = (SchemaHolder)schemas.get(i); hold.fixReferences(); } // validate the schemas and report any problems org.jibx.schema.validation.ValidationContext vctx = new org.jibx.schema.validation.ValidationContext(); for (int i = 0; i < schemas.size(); i++) { SchemaHolder holder = (SchemaHolder)schemas.get(i); String id = holder.getFileName(); SchemaElement schema = holder.getSchema(); schema.setResolver(new MemoryResolver(id)); vctx.setSchema(id, schema); } TreeWalker wlkr = new TreeWalker(vctx, vctx); vctx.clearTraversed(); for (int i = 0; i < schemas.size(); i++) { wlkr.walkSchema(((SchemaHolder)schemas.get(i)).getSchema(), new PrevalidationVisitor(vctx)); } vctx.clearTraversed(); for (int i = 0; i < schemas.size(); i++) { wlkr.walkSchema(((SchemaHolder)schemas.get(i)).getSchema(), new RegistrationVisitor(vctx)); } vctx.clearTraversed(); for (int i = 0; i < schemas.size(); i++) { wlkr.walkSchema(((SchemaHolder)schemas.get(i)).getSchema(), new ValidationVisitor(vctx)); } probs = vctx.getProblems(); if (probs.size() > 0) { for (int j = 0; j < probs.size(); j++) { org.jibx.schema.validation.ValidationProblem prob = (org.jibx.schema.validation.ValidationProblem)probs.get(j); System.out.print(prob.getSeverity() >= ValidationProblem.ERROR_LEVEL ? "Error: " : "Warning: "); System.out.println(prob.getDescription()); } } return schemas; } /** * Get details of schema handling of a mapping. * * @param map * @return mapping details */ public SchemaMappingDetail getMappingDetail(MappingElement map) { return m_detailDirectory.forceMappingDetail(map); } private static class MemoryResolver implements ISchemaResolver { private final String m_id; public MemoryResolver(String id) { m_id = id; } public InputStream getContent() throws IOException { throw new IOException("Source not available"); } public String getId() { return m_id; } public ISchemaResolver resolve(String loc) throws IOException { return new MemoryResolver(loc); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -