public static void addAllDefinitions()

in src/main/java/org/apache/xmlbeans/impl/schema/StscTranslator.java [41:222]


    public static void addAllDefinitions(SchemaToProcess[] schemasAndChameleons) {
        // Build all redefine objects
        List<RedefinitionHolder> redefinitions = new ArrayList<>();
        for (SchemaToProcess schemasAndChameleon : schemasAndChameleons) {
            List<SchemaToProcess> redefines = schemasAndChameleon.getRedefines();
            if (redefines != null) {
                List<Redefine> redefineObjects = schemasAndChameleon.getRedefineObjects();
                Iterator<SchemaToProcess> it = redefines.iterator();
                Iterator<Redefine> ito = redefineObjects.iterator();
                while (it.hasNext()) {
                    assert ito.hasNext() :
                        "The array of redefines and redefine objects have to have the same length";
                    redefinitions.add(new RedefinitionHolder(it.next(), ito.next()));
                }
            }
        }
        RedefinitionMaster globalRedefinitions = new RedefinitionMaster(redefinitions.toArray(new RedefinitionHolder[0]));

        StscState state = StscState.get();
        for (SchemaToProcess schemasAndChameleon : schemasAndChameleons) {
            Schema schema = schemasAndChameleon.getSchema();
            String givenTargetNamespace = schemasAndChameleon.getChameleonNamespace();

            // quick check for a few unsupported features

            if (schema.sizeOfNotationArray() > 0) {
                state.warning("Schema <notation> is not yet supported for this release.", XmlErrorCodes.UNSUPPORTED_FEATURE, schema.getNotationArray(0));
            }

            // figure namespace (taking into account chameleons)
            String targetNamespace = schema.getTargetNamespace();
            boolean chameleon = false;
            if (givenTargetNamespace != null && targetNamespace == null) {
                targetNamespace = givenTargetNamespace;
                chameleon = true;
            }
            if (targetNamespace == null) {
                targetNamespace = "";
            }

            //SchemaContainer container = null;
            if (targetNamespace.length() > 0 || !isEmptySchema(schema)) {
                state.registerContribution(targetNamespace, schema.documentProperties().getSourceName());
                state.addNewContainer(targetNamespace);
                //container = state.getContainer(targetNamespace);
            }

            List<Annotated> redefChain = new ArrayList<>();
            TopLevelComplexType[] complexTypes = schema.getComplexTypeArray();
            for (TopLevelComplexType complexType : complexTypes) {
                TopLevelComplexType type = complexType;
                TopLevelComplexType redef;
                // 1. Traverse the list of redefining Schemas putting all redefinitions
                // of this type in a List
                RedefinitionHolder[] rhArray = globalRedefinitions.getComplexTypeRedefinitions(
                    type.getName(), schemasAndChameleon);
                for (RedefinitionHolder redefinitionHolder : rhArray) {
                    // In error cases, some redefinitions were nulled out in the list
                    // which is why we need to perform this check
                    if (redefinitionHolder != null) {
                        redef = redefinitionHolder.redefineComplexType(type.getName());
                        assert redef != null; // This was already checked
                        redefChain.add(type);
                        type = redef;
                    }
                }

                SchemaTypeImpl t = translateGlobalComplexType(type, targetNamespace, chameleon, !redefChain.isEmpty());
                state.addGlobalType(t, null);
                SchemaTypeImpl r;
                // 2. Traverse the List built in step 1 in reverse and add all the
                // types in it to the list of redefined types
                for (int k = redefChain.size() - 1; k >= 0; k--) {
                    redef = (TopLevelComplexType) redefChain.remove(k);
                    r = translateGlobalComplexType(redef, targetNamespace, chameleon, k > 0);
                    state.addGlobalType(r, t);
                    t = r;
                }
            }

            TopLevelSimpleType[] simpleTypes = schema.getSimpleTypeArray();
            for (TopLevelSimpleType simpleType : simpleTypes) {
                TopLevelSimpleType type = simpleType;
                TopLevelSimpleType redef;
                RedefinitionHolder[] rhArray = globalRedefinitions.getSimpleTypeRedefinitions(
                    type.getName(), schemasAndChameleon);
                for (RedefinitionHolder redefinitionHolder : rhArray) {
                    // In error cases, some redefinitions were nulled out in the list
                    // which is why we need to perform this check
                    if (redefinitionHolder != null) {
                        redef = redefinitionHolder.redefineSimpleType(type.getName());
                        assert redef != null; // This was already checked
                        redefChain.add(type);
                        type = redef;
                    }
                }

                SchemaTypeImpl t = translateGlobalSimpleType(type, targetNamespace, chameleon, !redefChain.isEmpty());
                state.addGlobalType(t, null);
                SchemaTypeImpl r;
                for (int k = redefChain.size() - 1; k >= 0; k--) {
                    redef = (TopLevelSimpleType) redefChain.remove(k);
                    r = translateGlobalSimpleType(redef, targetNamespace, chameleon, k > 0);
                    state.addGlobalType(r, t);
                    t = r;
                }
            }

            TopLevelElement[] elements = schema.getElementArray();
            for (TopLevelElement element : elements) {
                state.addDocumentType(translateDocumentType(element, targetNamespace, chameleon), QNameHelper.forLNS(element.getName(), targetNamespace));
            }

            TopLevelAttribute[] attributes = schema.getAttributeArray();
            for (TopLevelAttribute attribute : attributes) {
                state.addAttributeType(translateAttributeType(attribute, targetNamespace, chameleon), QNameHelper.forLNS(attribute.getName(), targetNamespace));
            }

            NamedGroup[] modelgroups = schema.getGroupArray();
            for (NamedGroup group : modelgroups) {
                NamedGroup redef;
                RedefinitionHolder[] rhArray = globalRedefinitions.getModelGroupRedefinitions(
                    group.getName(), schemasAndChameleon);
                for (RedefinitionHolder redefinitionHolder : rhArray) {
                    // In error cases, some redefinitions were nulled out in the list
                    // which is why we need to perform this check
                    if (redefinitionHolder != null) {
                        redef = redefinitionHolder.redefineModelGroup(group.getName());
                        assert redef != null; // This was already checked
                        redefChain.add(group);
                        group = redef;
                    }
                }

                SchemaModelGroupImpl g = translateModelGroup(group, targetNamespace, chameleon, !redefChain.isEmpty());
                state.addModelGroup(g, null);
                SchemaModelGroupImpl r;
                for (int k = redefChain.size() - 1; k >= 0; k--) {
                    redef = (NamedGroup) redefChain.remove(k);
                    r = translateModelGroup(redef, targetNamespace, chameleon, k > 0);
                    state.addModelGroup(r, g);
                    g = r;
                }
            }

            NamedAttributeGroup[] attrgroups = schema.getAttributeGroupArray();
            for (NamedAttributeGroup group : attrgroups) {
                NamedAttributeGroup redef;
                RedefinitionHolder[] rhArray = globalRedefinitions.getAttributeGroupRedefinitions(
                    group.getName(), schemasAndChameleon);
                for (RedefinitionHolder redefinitionHolder : rhArray) {
                    // In error cases, some redefinitions were nulled out in the list
                    // which is why we need to perform this check
                    if (redefinitionHolder != null) {
                        redef = redefinitionHolder.redefineAttributeGroup(group.getName());
                        assert redef != null; // This was already checked
                        redefChain.add(group);
                        group = redef;
                    }
                }

                SchemaAttributeGroupImpl g = translateAttributeGroup(group, targetNamespace, chameleon, !redefChain.isEmpty());
                state.addAttributeGroup(g, null);
                SchemaAttributeGroupImpl r;
                for (int k = redefChain.size() - 1; k >= 0; k--) {
                    redef = (NamedAttributeGroup) redefChain.remove(k);
                    r = translateAttributeGroup(redef, targetNamespace, chameleon, k > 0);
                    state.addAttributeGroup(r, g);
                    g = r;
                }
            }

            AnnotationDocument.Annotation[] annotations = schema.getAnnotationArray();
            for (AnnotationDocument.Annotation annotation : annotations) {
                state.addAnnotation(SchemaAnnotationImpl.getAnnotation(state.getContainer(targetNamespace), schema, annotation), targetNamespace);
            }
        }

        for (Object redefinition : redefinitions) {
            ((RedefinitionHolder) redefinition).complainAboutMissingDefinitions();
        }
    }