Search in sources :

Example 11 with LocalComplexType

use of org.mule.runtime.module.extension.internal.capability.xml.schema.model.LocalComplexType in project mule by mulesoft.

the class CollectionSchemaDelegate method generateCollectionComplexType.

private LocalComplexType generateCollectionComplexType(DslElementSyntax collectionDsl, final ArrayType metadataType) {
    final LocalComplexType collectionComplexType = new LocalComplexType();
    final ExplicitGroup sequence = new ExplicitGroup();
    final MetadataType genericType = metadataType.getType();
    DslElementSyntax itemDsl = collectionDsl.getGeneric(genericType).orElseThrow(() -> new IllegalArgumentException(format("Missing item's DSL information for collection [%s]", collectionDsl.getAttributeName())));
    genericType.accept(new MetadataTypeVisitor() {

        /**
         * For a Collection with an {@link ObjectType} as generic. The generated {@link ComplexType} declares a sequence of either a
         * {@code ref} or a {@code choice}.
         * <p/>
         * It creates an element {@code ref} to the concrete element whose {@code type} is the {@link ComplexType} associated to the
         * {@code objectType}
         * <p/>
         * In the case of having a {@link DslElementSyntax#isWrapped wrapped} {@link ObjectType}, then a {@link ExplicitGroup
         * Choice} group that can receive a {@code ref} to any subtype that this wrapped type might have, be it either a top-level
         * element for the mule schema, or if it can only be declared as child of this element.
         *
         * If the collections's value is a map, then a value attribute is created for the value map.
         *
         * @param objectType the item's type
         */
        @Override
        public void visitObject(ObjectType objectType) {
            if (isMap(objectType)) {
                defaultVisit(objectType);
                return;
            }
            DslElementSyntax typeDsl = builder.getDslResolver().resolve(objectType).orElseThrow(() -> new IllegalArgumentException(format("The given type [%s] cannot be represented as a collection item", getId(objectType))));
            if (typeDsl.isWrapped()) {
                ExplicitGroup choice = builder.createTypeRefChoiceLocalOrGlobal(typeDsl, objectType, ZERO, UNBOUNDED);
                sequence.getParticle().add(objectFactory.createChoice(choice));
            } else {
                TopLevelElement collectionItemElement = builder.createTypeRef(typeDsl, objectType, false);
                collectionItemElement.setMaxOccurs(UNBOUNDED);
                sequence.getParticle().add(objectFactory.createElement(collectionItemElement));
            }
        }

        /**
         * For a Collection with any other type as generic.
         * The generated {@link ComplexType} declares a sequence of child elements with an inline declaration of the type
         *
         * @param metadataType the item's type
         */
        @Override
        protected void defaultVisit(MetadataType metadataType) {
            final LocalComplexType complexType = new LocalComplexType();
            complexType.getAttributeOrAttributeGroup().add(builder.createValueAttribute(genericType));
            TopLevelElement collectionItemElement = builder.createTopLevelElement(itemDsl.getElementName(), ZERO, UNBOUNDED);
            collectionItemElement.setComplexType(complexType);
            sequence.getParticle().add(objectFactory.createElement(collectionItemElement));
        }
    });
    collectionComplexType.setSequence(sequence);
    return collectionComplexType;
}
Also used : ObjectType(org.mule.metadata.api.model.ObjectType) TopLevelElement(org.mule.runtime.module.extension.internal.capability.xml.schema.model.TopLevelElement) DslElementSyntax(org.mule.runtime.extension.api.dsl.syntax.DslElementSyntax) MetadataType(org.mule.metadata.api.model.MetadataType) LocalComplexType(org.mule.runtime.module.extension.internal.capability.xml.schema.model.LocalComplexType) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) ExplicitGroup(org.mule.runtime.module.extension.internal.capability.xml.schema.model.ExplicitGroup)

Aggregations

LocalComplexType (org.mule.runtime.module.extension.internal.capability.xml.schema.model.LocalComplexType)11 TopLevelElement (org.mule.runtime.module.extension.internal.capability.xml.schema.model.TopLevelElement)9 ExplicitGroup (org.mule.runtime.module.extension.internal.capability.xml.schema.model.ExplicitGroup)7 DslElementSyntax (org.mule.runtime.extension.api.dsl.syntax.DslElementSyntax)5 ComplexContent (org.mule.runtime.module.extension.internal.capability.xml.schema.model.ComplexContent)3 ExtensionType (org.mule.runtime.module.extension.internal.capability.xml.schema.model.ExtensionType)3 BigInteger (java.math.BigInteger)2 MetadataType (org.mule.metadata.api.model.MetadataType)2 ObjectType (org.mule.metadata.api.model.ObjectType)2 MetadataTypeVisitor (org.mule.metadata.api.visitor.MetadataTypeVisitor)2 Element (org.mule.runtime.module.extension.internal.capability.xml.schema.model.Element)2 Optional (java.util.Optional)1 QName (javax.xml.namespace.QName)1 ArrayType (org.mule.metadata.api.model.ArrayType)1 NestedChainModel (org.mule.runtime.api.meta.model.nested.NestedChainModel)1 ParameterModel (org.mule.runtime.api.meta.model.parameter.ParameterModel)1 Attribute (org.mule.runtime.module.extension.internal.capability.xml.schema.model.Attribute)1