use of org.mule.runtime.extension.api.dsl.syntax.DslElementSyntax 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;
}
use of org.mule.runtime.extension.api.dsl.syntax.DslElementSyntax in project mule by mulesoft.
the class SourceSchemaDelegate method registerSourceType.
private void registerSourceType(String name, SourceModel sourceModel, DslElementSyntax dslSyntax, boolean hasImplicitConfig) {
final ExtensionType sourceType = createExecutableType(name, MULE_ABSTRACT_MESSAGE_SOURCE_TYPE, dslSyntax, hasImplicitConfig);
initialiseSequence(sourceType);
ExplicitGroup sequence = sourceType.getSequence();
builder.addInfrastructureParameters(sourceType, sourceModel, sequence);
List<ParameterModel> visitedParameters = new LinkedList<>();
sourceModel.getParameterGroupModels().forEach(group -> {
registerParameterGroup(sourceType, group);
visitedParameters.addAll(group.getParameterModels());
});
sourceModel.getSuccessCallback().map(ParameterizedModel::getParameterGroupModels).ifPresent(groups -> groups.forEach(group -> registerCallbackParameters(sourceType, visitedParameters, group)));
sourceModel.getErrorCallback().map(ParameterizedModel::getParameterGroupModels).ifPresent(groups -> groups.forEach(group -> registerCallbackParameters(sourceType, visitedParameters, group)));
}
use of org.mule.runtime.extension.api.dsl.syntax.DslElementSyntax in project mule by mulesoft.
the class ExtensionDefinitionParser method parseMapParameters.
/**
* Registers a definition for a {@link ParameterModel} which represents an open {@link ObjectType}
*
* @param key the key that the parsed value should have on the parsed parameter's map
* @param name the parameter's name
* @param dictionaryType the parameter's open {@link ObjectType}
* @param defaultValue the parameter's default value
* @param expressionSupport the parameter's {@link ExpressionSupport}
* @param required whether the parameter is required
*/
protected void parseMapParameters(String key, String name, ObjectType dictionaryType, Object defaultValue, ExpressionSupport expressionSupport, boolean required, DslElementSyntax paramDsl, Set<ModelProperty> modelProperties) {
parseAttributeParameter(key, name, dictionaryType, defaultValue, expressionSupport, required, modelProperties);
Class<? extends Map> mapType = getType(dictionaryType);
if (ConcurrentMap.class.equals(mapType)) {
mapType = ConcurrentHashMap.class;
} else if (Map.class.equals(mapType)) {
mapType = LinkedHashMap.class;
}
final MetadataType valueType = dictionaryType.getOpenRestriction().orElse(typeLoader.load(Object.class));
final Class<?> valueClass = getType(valueType);
final MetadataType keyType = typeLoader.load(String.class);
final Class<?> keyClass = String.class;
final String mapElementName = paramDsl.getElementName();
addParameter(getChildKey(key), fromChildMapConfiguration(String.class, valueClass).withWrapperIdentifier(mapElementName).withDefaultValue(defaultValue));
addDefinition(baseDefinitionBuilder.withIdentifier(mapElementName).withTypeDefinition(fromType(mapType)).build());
Optional<DslElementSyntax> mapValueChildDsl = paramDsl.getGeneric(valueType);
if (!mapValueChildDsl.isPresent()) {
return;
}
DslElementSyntax valueDsl = mapValueChildDsl.get();
valueType.accept(new MetadataTypeVisitor() {
@Override
protected void defaultVisit(MetadataType metadataType) {
String parameterName = paramDsl.getAttributeName();
addDefinition(baseDefinitionBuilder.withIdentifier(valueDsl.getElementName()).withTypeDefinition(fromMapEntryType(keyClass, valueClass)).withKeyTypeConverter(value -> resolverOf(parameterName, keyType, value, null, expressionSupport, true, emptySet(), false)).withTypeConverter(value -> resolverOf(parameterName, valueType, value, null, expressionSupport, true, emptySet(), false)).build());
}
@Override
public void visitObject(ObjectType objectType) {
defaultVisit(objectType);
Optional<DslElementSyntax> containedElement = valueDsl.getContainedElement(VALUE_ATTRIBUTE_NAME);
if (isMap(objectType) || !containedElement.isPresent()) {
return;
}
DslElementSyntax valueChild = containedElement.get();
if ((valueChild.supportsTopLevelDeclaration() || (valueChild.supportsChildDeclaration() && !valueChild.isWrapped())) && !parsingContext.isRegistered(valueChild.getElementName(), valueChild.getPrefix())) {
try {
parsingContext.registerObjectType(valueChild.getElementName(), valueChild.getPrefix(), objectType);
new ObjectTypeParameterParser(baseDefinitionBuilder, objectType, getContextClassLoader(), dslResolver, parsingContext).parse().forEach(definition -> addDefinition(definition));
} catch (ConfigurationException e) {
throw new MuleRuntimeException(createStaticMessage("Could not create parser for map complex type"), e);
}
}
}
@Override
public void visitArrayType(ArrayType arrayType) {
defaultVisit(arrayType);
Optional<DslElementSyntax> valueListGenericDsl = valueDsl.getGeneric(arrayType.getType());
if (valueDsl.supportsChildDeclaration() && valueListGenericDsl.isPresent()) {
arrayType.getType().accept(new BasicTypeMetadataVisitor() {
@Override
protected void visitBasicType(MetadataType metadataType) {
String parameterName = paramDsl.getAttributeName();
addDefinition(baseDefinitionBuilder.withIdentifier(valueListGenericDsl.get().getElementName()).withTypeDefinition(fromType(getType(metadataType))).withTypeConverter(value -> resolverOf(parameterName, metadataType, value, getDefaultValue(metadataType), getExpressionSupport(metadataType), false, emptySet())).build());
}
@Override
protected void defaultVisit(MetadataType metadataType) {
addDefinition(baseDefinitionBuilder.withIdentifier(valueListGenericDsl.get().getElementName()).withTypeDefinition(fromType(ValueResolver.class)).withObjectFactoryType(TopLevelParameterObjectFactory.class).withConstructorParameterDefinition(fromFixedValue(arrayType.getType()).build()).withConstructorParameterDefinition(fromFixedValue(getContextClassLoader()).build()).build());
}
});
}
}
});
}
use of org.mule.runtime.extension.api.dsl.syntax.DslElementSyntax in project mule by mulesoft.
the class DefaultXmlArtifactDeclarationLoader method declareComponentModel.
private Optional<ComponentElementDeclarer> declareComponentModel(final ConfigLine line, ComponentModel model, Function<String, ComponentElementDeclarer> declarerBuilder) {
final DslElementSyntax elementDsl = resolvers.get(getNamespace(line)).resolve(model);
if (elementDsl.getElementName().equals(line.getIdentifier())) {
ComponentElementDeclarer declarer = declarerBuilder.apply(model.getName());
if (line.getConfigAttributes().get(CONFIG_ATTRIBUTE_NAME) != null) {
declarer.withConfig(line.getConfigAttributes().get(CONFIG_ATTRIBUTE_NAME).getValue());
}
declareParameterizedComponent(model, elementDsl, declarer, line.getConfigAttributes(), line.getChildren());
declareComposableModel(model, elementDsl, line, declarer);
return Optional.of(declarer);
}
return Optional.empty();
}
use of org.mule.runtime.extension.api.dsl.syntax.DslElementSyntax in project mule by mulesoft.
the class DefaultXmlArtifactDeclarationLoader method declareRoute.
private Optional<RouteElementDeclaration> declareRoute(ComposableModel model, DslElementSyntax elementDsl, ConfigLine child, ElementDeclarer extensionElementsDeclarer) {
return model.getNestedComponents().stream().filter(nestedModel -> elementDsl.getContainedElement(nestedModel.getName()).map(nestedDsl -> child.getIdentifier().equals(nestedDsl.getElementName())).orElse(false)).filter(nestedModel -> nestedModel instanceof NestedRouteModel).findFirst().map(nestedModel -> {
RouteElementDeclarer routeDeclarer = extensionElementsDeclarer.newRoute(nestedModel.getName());
DslElementSyntax routeDsl = elementDsl.getContainedElement(nestedModel.getName()).get();
declareParameterizedComponent((ParameterizedModel) nestedModel, routeDsl, routeDeclarer, child.getConfigAttributes(), child.getChildren());
declareComposableModel((ComposableModel) nestedModel, elementDsl, child, routeDeclarer);
return routeDeclarer.getDeclaration();
});
}
Aggregations