use of org.mule.runtime.app.declaration.api.ElementDeclaration in project mule by mulesoft.
the class DeclarationBasedElementModelFactory method create.
public <T> Optional<DslElementModel<T>> create(ElementDeclaration declaration) {
setupCurrentExtensionContext(declaration.getDeclaringExtension());
final Function<NamedObject, Boolean> equalsName = (named) -> named.getName().equals(declaration.getName());
if (declaration instanceof TopLevelParameterDeclaration) {
return createFromType((TopLevelParameterDeclaration) declaration);
}
Reference<DslElementModel> elementModel = new Reference<>();
new ExtensionWalker() {
@Override
protected void onConfiguration(ConfigurationModel model) {
if (equalsName.apply(model) && declaration instanceof ConfigurationElementDeclaration) {
elementModel.set(createConfigurationElement(model, (ConfigurationElementDeclaration) declaration));
stop();
}
}
@Override
protected void onOperation(HasOperationModels owner, OperationModel model) {
if (equalsName.apply(model) && declaration instanceof OperationElementDeclaration) {
elementModel.set(createComponentElement(model, (OperationElementDeclaration) declaration));
stop();
}
}
@Override
protected void onConstruct(HasConstructModels owner, ConstructModel model) {
if (equalsName.apply(model) && declaration instanceof ConstructElementDeclaration) {
elementModel.set(createComponentElement(model, (ConstructElementDeclaration) declaration));
stop();
}
}
@Override
protected void onSource(HasSourceModels owner, SourceModel model) {
if (equalsName.apply(model) && declaration instanceof SourceElementDeclaration) {
elementModel.set(createComponentElement(model, (SourceElementDeclaration) declaration));
stop();
}
}
}.walk(currentExtension);
if (LOGGER.isDebugEnabled() && elementModel.get() == null) {
LOGGER.debug(format("No model found with name [%s] of type [%s] for extension [%s]", declaration.getName(), declaration.getClass().getName(), declaration.getDeclaringExtension()));
}
return Optional.ofNullable(elementModel.get());
}
use of org.mule.runtime.app.declaration.api.ElementDeclaration in project mule by mulesoft.
the class ApplicationModel method convertArtifactDeclarationToComponentModel.
private void convertArtifactDeclarationToComponentModel(Set<ExtensionModel> extensionModels, ArtifactDeclaration artifactDeclaration) {
if (artifactDeclaration != null && !extensionModels.isEmpty()) {
ExtensionModel muleModel = MuleExtensionModelProvider.getExtensionModel();
if (!extensionModels.contains(muleModel)) {
extensionModels = new HashSet<>(extensionModels);
extensionModels.add(muleModel);
}
DslElementModelFactory elementFactory = DslElementModelFactory.getDefault(DslResolvingContext.getDefault(extensionModels));
ComponentModel rootComponent = new ComponentModel.Builder().setIdentifier(ComponentIdentifier.builder().namespace(CORE_PREFIX).name(CORE_PREFIX).build()).build();
AtomicBoolean atLeastOneComponentAdded = new AtomicBoolean(false);
artifactDeclaration.getGlobalElements().stream().map(e -> elementFactory.create((ElementDeclaration) e)).filter(Optional::isPresent).map(e -> e.get().getConfiguration()).forEach(config -> config.ifPresent(c -> {
atLeastOneComponentAdded.set(true);
ComponentModel componentModel = convertComponentConfiguration(c, true);
componentModel.setParent(rootComponent);
rootComponent.getInnerComponents().add(componentModel);
}));
if (atLeastOneComponentAdded.get()) {
this.muleComponentModels.add(rootComponent);
}
}
}
use of org.mule.runtime.app.declaration.api.ElementDeclaration in project mule by mulesoft.
the class DefaultArtifactDeclarationXmlSerializer method serializeArtifact.
private String serializeArtifact(ArtifactDeclaration artifact) {
checkArgument(artifact != null, "The artifact to serialize cannot be null");
try {
Document doc = createAppDocument(artifact);
XmlDslElementModelConverter toXmlConverter = XmlDslElementModelConverter.getDefault(doc);
DslElementModelFactory modelResolver = DslElementModelFactory.getDefault(context);
artifact.getGlobalElements().forEach(declaration -> appendChildElement(toXmlConverter, doc.getDocumentElement(), modelResolver, (ElementDeclaration) declaration));
List<String> cDataElements = getCDataElements(doc.getDocumentElement());
// write the content into xml file
TransformerFactory transformerFactory = XMLSecureFactories.createDefault().getTransformerFactory();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, join(cDataElements, " "));
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
DOMSource source = new DOMSource(doc);
StringWriter writer = new StringWriter();
transformer.transform(source, new StreamResult(writer));
return writer.getBuffer().toString();
} catch (Exception e) {
throw new MuleRuntimeException(createStaticMessage("Failed to serialize the declaration for the artifact [" + artifact.getName() + "]: " + e.getMessage()), e);
}
}
Aggregations