use of org.mule.runtime.config.api.dsl.model.XmlDslElementModelConverter 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