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);
}
}
Aggregations