use of org.mule.runtime.api.meta.ExpressionSupport in project mule by mulesoft.
the class ExtensionDefinitionParser method parseCollectionParameter.
/**
* Registers a definition for a {@link ParameterModel} which represents an {@link ArrayType}
*
* @param key the key that the parsed value should have on the parsed parameter's map
* @param name the parameter's name
* @param arrayType the parameter's {@link ArrayType}
* @param defaultValue the parameter's default value
* @param expressionSupport the parameter's {@link ExpressionSupport}
* @param required whether the parameter is required
*/
protected void parseCollectionParameter(String key, String name, ArrayType arrayType, Object defaultValue, ExpressionSupport expressionSupport, boolean required, DslElementSyntax parameterDsl, Set<ModelProperty> modelProperties) {
parseAttributeParameter(key, name, arrayType, defaultValue, expressionSupport, required, modelProperties);
Class<?> collectionType = ExtensionMetadataTypeUtils.getType(arrayType).orElse(null);
if (Set.class.equals(collectionType)) {
collectionType = HashSet.class;
} else if (Collection.class.equals(collectionType) || Iterable.class.equals(collectionType) || collectionType == null) {
collectionType = List.class;
}
final String collectionElementName = parameterDsl.getElementName();
addParameter(getChildKey(key), fromChildConfiguration(collectionType).withWrapperIdentifier(collectionElementName));
addDefinition(baseDefinitionBuilder.withIdentifier(collectionElementName).withTypeDefinition(fromType(collectionType)).build());
Optional<DslElementSyntax> collectionItemDsl = parameterDsl.getGeneric(arrayType.getType());
if (parameterDsl.supportsChildDeclaration() && collectionItemDsl.isPresent()) {
String itemIdentifier = collectionItemDsl.get().getElementName();
String itemNamespace = collectionItemDsl.get().getPrefix();
arrayType.getType().accept(new BasicTypeMetadataVisitor() {
private void addBasicTypeDefinition(MetadataType metadataType) {
Builder itemDefinitionBuilder = baseDefinitionBuilder.withIdentifier(itemIdentifier).withNamespace(itemNamespace).withTypeDefinition(fromType(ExtensionMetadataTypeUtils.getType(metadataType).orElse(Object.class))).withTypeConverter(value -> resolverOf(name, metadataType, value, getDefaultValue(metadataType).orElse(null), getExpressionSupport(metadataType), false, emptySet()));
addDefinition(itemDefinitionBuilder.build());
}
@Override
protected void visitBasicType(MetadataType metadataType) {
addBasicTypeDefinition(metadataType);
}
@Override
public void visitDate(DateType dateType) {
addBasicTypeDefinition(dateType);
}
@Override
public void visitDateTime(DateTimeType dateTimeType) {
addBasicTypeDefinition(dateTimeType);
}
@Override
public void visitObject(ObjectType objectType) {
if (isMap(objectType)) {
return;
}
DslElementSyntax itemDsl = collectionItemDsl.get();
if ((itemDsl.supportsTopLevelDeclaration() || itemDsl.supportsChildDeclaration()) && !parsingContext.isRegistered(itemDsl.getElementName(), itemDsl.getPrefix())) {
try {
parsingContext.registerObjectType(itemDsl.getElementName(), itemDsl.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 collection complex type"), e);
}
}
}
});
}
}
use of org.mule.runtime.api.meta.ExpressionSupport in project mule by mulesoft.
the class ExtensionDefinitionParser method parseField.
private void parseField(ObjectType type, DslElementSyntax typeDsl, ObjectFieldType objectField, Map<String, ParameterRole> parametersRole) {
final MetadataType fieldType = objectField.getValue();
final String fieldName = objectField.getKey().getName().getLocalPart();
final boolean acceptsReferences = ExtensionMetadataTypeUtils.acceptsReferences(objectField);
final Object defaultValue = getDefaultValue(fieldType).orElse(null);
final ExpressionSupport expressionSupport = getExpressionSupport(objectField);
Optional<DslElementSyntax> fieldDsl = typeDsl.getContainedElement(fieldName);
if (!fieldDsl.isPresent() && !isFlattenedParameterGroup(objectField)) {
return;
}
Optional<String> keyName = getInfrastructureParameterName(fieldType);
if (keyName.isPresent()) {
parseObject(fieldName, keyName.get(), (ObjectType) fieldType, defaultValue, expressionSupport, false, acceptsReferences, fieldDsl.get(), emptySet());
return;
}
final boolean isContent = isContent(parametersRole.getOrDefault(fieldName, BEHAVIOUR));
fieldType.accept(new MetadataTypeVisitor() {
@Override
protected void defaultVisit(MetadataType metadataType) {
if (!parseAsContent(isContent, metadataType)) {
parseAttributeParameter(fieldName, fieldName, metadataType, defaultValue, expressionSupport, false, emptySet());
}
}
@Override
public void visitString(StringType stringType) {
if (fieldDsl.get().supportsChildDeclaration()) {
String elementName = fieldDsl.get().getElementName();
addParameter(fieldName, fromChildConfiguration(String.class).withWrapperIdentifier(elementName));
addDefinition(baseDefinitionBuilder.withIdentifier(elementName).withTypeDefinition(fromType(String.class)).withTypeConverter(value -> resolverOf(elementName, stringType, value, defaultValue, expressionSupport, false, emptySet(), acceptsReferences)).build());
} else {
defaultVisit(stringType);
}
}
@Override
public void visitObject(ObjectType objectType) {
if (objectType.isOpen()) {
if (!parseAsContent(isContent, objectType)) {
parseMapParameters(fieldName, fieldName, objectType, defaultValue, expressionSupport, false, fieldDsl.get(), emptySet());
}
return;
}
if (isFlattenedParameterGroup(objectField)) {
dslResolver.resolve(objectType).ifPresent(objectDsl -> objectType.getFields().forEach(field -> parseField(objectType, objectDsl, field, parametersRole)));
return;
}
if (parseAsContent(isContent, objectType)) {
return;
}
DslElementSyntax dsl = fieldDsl.get();
if (!parsingContext.isRegistered(dsl.getElementName(), dsl.getPrefix())) {
parsingContext.registerObjectType(dsl.getElementName(), dsl.getPrefix(), type);
parseObjectParameter(fieldName, fieldName, objectType, defaultValue, expressionSupport, false, acceptsReferences, dsl, emptySet());
} else {
parseObject(fieldName, fieldName, objectType, defaultValue, expressionSupport, false, acceptsReferences, dsl, emptySet());
}
}
@Override
public void visitArrayType(ArrayType arrayType) {
if (!parseAsContent(isContent, arrayType)) {
parseCollectionParameter(fieldName, fieldName, arrayType, defaultValue, expressionSupport, false, fieldDsl.get(), emptySet());
}
}
private boolean parseAsContent(boolean isContent, MetadataType type) {
if (isContent) {
parseFromTextExpression(fieldName, fieldDsl.get(), () -> value -> resolverOf(fieldName, type, value, defaultValue, expressionSupport, false, emptySet(), false));
return true;
}
return false;
}
});
}
use of org.mule.runtime.api.meta.ExpressionSupport in project mule by mulesoft.
the class ExtensionDefinitionParser method parseObjectParameter.
/**
* Registers a definition for a {@link ParameterModel} which represents an {@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 type an {@link ObjectType}
* @param defaultValue the parameter's default value
* @param expressionSupport the parameter's {@link ExpressionSupport}
* @param required whether the parameter is required or not
* @param modelProperties parameter's {@link ModelProperty}s
*/
protected void parseObjectParameter(String key, String name, ObjectType type, Object defaultValue, ExpressionSupport expressionSupport, boolean required, boolean acceptsReferences, DslElementSyntax elementDsl, Set<ModelProperty> modelProperties) {
parseObject(key, name, type, defaultValue, expressionSupport, required, acceptsReferences, elementDsl, modelProperties);
final String elementNamespace = elementDsl.getPrefix();
final String elementName = elementDsl.getElementName();
if (elementDsl.supportsChildDeclaration() && !elementDsl.isWrapped() && modelProperties.stream().noneMatch(m -> m.getName().equals(InfrastructureParameterModelProperty.NAME))) {
try {
new ObjectTypeParameterParser(baseDefinitionBuilder, elementName, elementNamespace, type, getContextClassLoader(), dslResolver, parsingContext).parse().forEach(this::addDefinition);
} catch (Exception e) {
throw new MuleRuntimeException(new ConfigurationException(e));
}
}
}
use of org.mule.runtime.api.meta.ExpressionSupport 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());
}
});
}
}
});
}
Aggregations