Search in sources :

Example 1 with XmlDslElementModelConverter

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);
    }
}
Also used : DslElementModelFactory(org.mule.runtime.config.api.dsl.model.DslElementModelFactory) DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Document(org.w3c.dom.Document) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) StringWriter(java.io.StringWriter) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) XmlDslElementModelConverter(org.mule.runtime.config.api.dsl.model.XmlDslElementModelConverter) ElementDeclaration(org.mule.runtime.app.declaration.api.ElementDeclaration)

Aggregations

StringWriter (java.io.StringWriter)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Transformer (javax.xml.transform.Transformer)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)1 ElementDeclaration (org.mule.runtime.app.declaration.api.ElementDeclaration)1 DslElementModelFactory (org.mule.runtime.config.api.dsl.model.DslElementModelFactory)1 XmlDslElementModelConverter (org.mule.runtime.config.api.dsl.model.XmlDslElementModelConverter)1 Document (org.w3c.dom.Document)1