Search in sources :

Example 1 with SettingsSerializable

use of org.gradle.gradleplugin.foundation.settings.SettingsSerializable in project gradle by gradle.

the class DOM4JSerializer method exportToFile.

/**
     * Call this to save the JDOMSerializable to a file. This handles confirming overwriting an existing file as well as ensuring the extension is correct based on the passed in fileFilter.
     */
public static void exportToFile(String rootElementTag, ExportInteraction exportInteraction, ExtensionFileFilter fileFilter, SettingsSerializable... serializables) {
    File file = promptForFile(exportInteraction, fileFilter);
    if (file == null) {
        //the user canceled.
        return;
    }
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        LOGGER.error("Could not write to file: " + file.getAbsolutePath(), e);
        exportInteraction.reportError("Could not write to file: " + file.getAbsolutePath());
        return;
    }
    try {
        XMLWriter xmlWriter = new XMLWriter(fileOutputStream, OutputFormat.createPrettyPrint());
        Document document = DocumentHelper.createDocument();
        Element rootElement = document.addElement(rootElementTag);
        DOM4JSettingsNode settingsNode = new DOM4JSettingsNode(rootElement);
        for (int index = 0; index < serializables.length; index++) {
            SettingsSerializable serializable = serializables[index];
            try {
                //don't let a single serializer stop the entire thing from being written in.
                serializable.serializeOut(settingsNode);
            } catch (Exception e) {
                LOGGER.error("serializing", e);
            }
        }
        xmlWriter.write(document);
    } catch (Throwable t) {
        LOGGER.error("Failed to save", t);
        exportInteraction.reportError("Internal error. Failed to save.");
    } finally {
        closeQuietly(fileOutputStream);
    }
}
Also used : Element(org.dom4j.Element) SettingsSerializable(org.gradle.gradleplugin.foundation.settings.SettingsSerializable) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter) DOM4JSettingsNode(org.gradle.gradleplugin.foundation.settings.DOM4JSettingsNode)

Aggregations

Document (org.dom4j.Document)1 Element (org.dom4j.Element)1 XMLWriter (org.dom4j.io.XMLWriter)1 DOM4JSettingsNode (org.gradle.gradleplugin.foundation.settings.DOM4JSettingsNode)1 SettingsSerializable (org.gradle.gradleplugin.foundation.settings.SettingsSerializable)1