Search in sources :

Example 1 with Attribute

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

the class SchemaBuilder method createAttribute.

private Attribute createAttribute(final String name, String description, final MetadataType type, Object defaultValue, boolean required, final ExpressionSupport expressionSupport) {
    final Attribute attribute = new Attribute();
    attribute.setUse(required ? USE_REQUIRED : USE_OPTIONAL);
    attribute.setAnnotation(createDocAnnotation(description));
    if ((defaultValue instanceof String && isNotBlank(defaultValue.toString())) || defaultValue instanceof Enum) {
        attribute.setDefault(defaultValue.toString());
    }
    type.accept(new MetadataTypeVisitor() {

        @Override
        public void visitString(StringType stringType) {
            Optional<EnumAnnotation> enumAnnotation = stringType.getAnnotation(EnumAnnotation.class);
            if (enumAnnotation.isPresent()) {
                visitEnum(stringType);
            } else {
                defaultVisit(stringType);
            }
        }

        private void visitEnum(StringType enumType) {
            attribute.setName(name);
            String typeName = getTypeId(enumType);
            if (OperationTransactionalAction.class.getName().equals(typeName)) {
                attribute.setType(MULE_OPERATION_TRANSACTIONAL_ACTION_TYPE);
            } else if (TransactionType.class.getName().equals(typeName)) {
                attribute.setType(MULE_TRANSACTION_TYPE);
                attribute.setDefault(LOCAL.name());
            } else {
                attribute.setType(new QName(schema.getTargetNamespace(), sanitizeName(typeName) + ENUM_TYPE_SUFFIX));
                registeredEnums.add(enumType);
            }
        }

        @Override
        protected void defaultVisit(MetadataType metadataType) {
            attribute.setName(name);
            attribute.setType(SchemaTypeConversion.convertType(type, expressionSupport));
        }
    });
    return attribute;
}
Also used : TransactionType(org.mule.runtime.api.tx.TransactionType) Optional(java.util.Optional) Attribute(org.mule.runtime.module.extension.internal.capability.xml.schema.model.Attribute) StringType(org.mule.metadata.api.model.StringType) EnumAnnotation(org.mule.metadata.api.annotation.EnumAnnotation) QName(javax.xml.namespace.QName) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor)

Example 2 with Attribute

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

the class ExecutableTypeSchemaDelegate method createExecutableType.

protected ExtensionType createExecutableType(String name, QName base, DslElementSyntax dslSyntax, boolean hasImplicitConfig) {
    TopLevelComplexType complexType = new TopLevelComplexType();
    complexType.setName(name);
    ComplexContent complexContent = new ComplexContent();
    complexType.setComplexContent(complexContent);
    final ExtensionType complexContentExtension = new ExtensionType();
    complexContentExtension.setBase(base);
    complexContent.setExtension(complexContentExtension);
    if (dslSyntax.requiresConfig()) {
        Attribute configAttr = builder.createAttribute(CONFIG_ATTRIBUTE_NAME, CONFIG_ATTRIBUTE_DESCRIPTION, hasImplicitConfig, SUBSTITUTABLE_NAME);
        complexContentExtension.getAttributeOrAttributeGroup().add(configAttr);
    }
    this.builder.getSchema().getSimpleTypeOrComplexTypeOrGroup().add(complexType);
    return complexContentExtension;
}
Also used : TopLevelComplexType(org.mule.runtime.module.extension.internal.capability.xml.schema.model.TopLevelComplexType) Attribute(org.mule.runtime.module.extension.internal.capability.xml.schema.model.Attribute) ExtensionType(org.mule.runtime.module.extension.internal.capability.xml.schema.model.ExtensionType) ComplexContent(org.mule.runtime.module.extension.internal.capability.xml.schema.model.ComplexContent)

Example 3 with Attribute

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

the class MapSchemaDelegate method generateMapComplexType.

private LocalComplexType generateMapComplexType(DslElementSyntax mapDsl, final ObjectType metadataType) {
    final MetadataType valueType = metadataType.getOpenRestriction().get();
    final LocalComplexType entryComplexType = new LocalComplexType();
    final Attribute keyAttribute = builder.createAttribute(KEY_ATTRIBUTE_NAME, keyType, true, REQUIRED);
    entryComplexType.getAttributeOrAttributeGroup().add(keyAttribute);
    final LocalComplexType mapComplexType = new LocalComplexType();
    final ExplicitGroup mapEntrySequence = new ExplicitGroup();
    mapComplexType.setSequence(mapEntrySequence);
    DslElementSyntax entryValueDsl = mapDsl.getGeneric(valueType).orElseThrow(() -> new IllegalArgumentException("Illegal DslSyntax definition of the given DictionaryType. The DslElementSyntax for the entry is required"));
    final TopLevelElement mapEntryElement = new TopLevelElement();
    mapEntryElement.setName(entryValueDsl.getElementName());
    mapEntryElement.setMinOccurs(ZERO);
    mapEntryElement.setMaxOccurs(UNBOUNDED);
    valueType.accept(new MetadataTypeVisitor() {

        /**
         * For a Map with an {@link ObjectType} as value. The resulting {@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 map's value is another map, then a value attribute is created for the value map.
         *
         * @param objectType the item's type
         */
        @Override
        public void visitObject(ObjectType objectType) {
            Optional<DslElementSyntax> containedElement = entryValueDsl.getContainedElement(VALUE_ATTRIBUTE_NAME);
            if (isMap(objectType) || !containedElement.isPresent()) {
                defaultVisit(objectType);
                return;
            }
            final boolean shouldGenerateChildElement = containedElement.get().supportsChildDeclaration() || containedElement.get().isWrapped();
            entryComplexType.getAttributeOrAttributeGroup().add(builder.createAttribute(VALUE_ATTRIBUTE_NAME, valueType, !shouldGenerateChildElement, SUPPORTED));
            if (shouldGenerateChildElement) {
                DslElementSyntax typeDsl = builder.getDslResolver().resolve(objectType).orElseThrow(() -> new IllegalArgumentException(format("The given type [%s] cannot be represented as a child element in Map entries", getId(objectType))));
                if (typeDsl.isWrapped()) {
                    ExplicitGroup choice = builder.createTypeRefChoiceLocalOrGlobal(typeDsl, objectType, ZERO, UNBOUNDED);
                    entryComplexType.setChoice(choice);
                } else {
                    ExplicitGroup singleItemSequence = new ExplicitGroup();
                    singleItemSequence.setMaxOccurs("1");
                    TopLevelElement mapItemElement = builder.createTypeRef(typeDsl, objectType, false);
                    singleItemSequence.getParticle().add(objectFactory.createElement(mapItemElement));
                    entryComplexType.setSequence(singleItemSequence);
                }
            }
        }

        @Override
        public void visitArrayType(ArrayType arrayType) {
            entryComplexType.getAttributeOrAttributeGroup().add(builder.createAttribute(VALUE_ATTRIBUTE_NAME, valueType, false, SUPPORTED));
            entryComplexType.setSequence(new ExplicitGroup());
            LocalComplexType itemComplexType = new LocalComplexType();
            MetadataType itemType = arrayType.getType();
            itemComplexType.getAttributeOrAttributeGroup().add(builder.createAttribute(VALUE_ATTRIBUTE_NAME, itemType, true, REQUIRED));
            DslElementSyntax itemDsl = entryValueDsl.getGeneric(itemType).orElseThrow(() -> new IllegalArgumentException("Illegal DslSyntax definition of the given ArrayType. The DslElementSyntax for the item is required"));
            TopLevelElement itemElement = builder.createTopLevelElement(itemDsl.getElementName(), ZERO, UNBOUNDED, itemComplexType);
            entryComplexType.getSequence().getParticle().add(objectFactory.createElement(itemElement));
        }

        @Override
        protected void defaultVisit(MetadataType metadataType) {
            entryComplexType.getAttributeOrAttributeGroup().add(builder.createValueAttribute(valueType));
        }
    });
    mapEntryElement.setComplexType(entryComplexType);
    mapEntrySequence.getParticle().add(objectFactory.createElement(mapEntryElement));
    return mapComplexType;
}
Also used : ArrayType(org.mule.metadata.api.model.ArrayType) ObjectType(org.mule.metadata.api.model.ObjectType) Optional(java.util.Optional) Attribute(org.mule.runtime.module.extension.internal.capability.xml.schema.model.Attribute) 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)

Example 4 with Attribute

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

the class SchemaBuilder method createAttribute.

Attribute createAttribute(String name, String description, boolean optional, QName type) {
    Attribute attr = new Attribute();
    attr.setName(name);
    attr.setUse(optional ? USE_OPTIONAL : USE_REQUIRED);
    attr.setType(type);
    if (description != null) {
        attr.setAnnotation(createDocAnnotation(description));
    }
    return attr;
}
Also used : Attribute(org.mule.runtime.module.extension.internal.capability.xml.schema.model.Attribute)

Aggregations

Attribute (org.mule.runtime.module.extension.internal.capability.xml.schema.model.Attribute)4 Optional (java.util.Optional)2 MetadataType (org.mule.metadata.api.model.MetadataType)2 MetadataTypeVisitor (org.mule.metadata.api.visitor.MetadataTypeVisitor)2 QName (javax.xml.namespace.QName)1 EnumAnnotation (org.mule.metadata.api.annotation.EnumAnnotation)1 ArrayType (org.mule.metadata.api.model.ArrayType)1 ObjectType (org.mule.metadata.api.model.ObjectType)1 StringType (org.mule.metadata.api.model.StringType)1 TransactionType (org.mule.runtime.api.tx.TransactionType)1 DslElementSyntax (org.mule.runtime.extension.api.dsl.syntax.DslElementSyntax)1 ComplexContent (org.mule.runtime.module.extension.internal.capability.xml.schema.model.ComplexContent)1 ExplicitGroup (org.mule.runtime.module.extension.internal.capability.xml.schema.model.ExplicitGroup)1 ExtensionType (org.mule.runtime.module.extension.internal.capability.xml.schema.model.ExtensionType)1 LocalComplexType (org.mule.runtime.module.extension.internal.capability.xml.schema.model.LocalComplexType)1 TopLevelComplexType (org.mule.runtime.module.extension.internal.capability.xml.schema.model.TopLevelComplexType)1 TopLevelElement (org.mule.runtime.module.extension.internal.capability.xml.schema.model.TopLevelElement)1