001package io.ebeaninternal.dbmigration.migrationreader; 002 003 004import io.ebeaninternal.dbmigration.migration.Migration; 005 006import javax.xml.bind.JAXBContext; 007import javax.xml.bind.JAXBException; 008import javax.xml.bind.Marshaller; 009import java.io.File; 010import java.io.FileWriter; 011import java.io.IOException; 012 013/** 014 * Simple writer for output of the Migration/ChangeSet as an XML document. 015 */ 016public class MigrationXmlWriter { 017 018 private final String comment; 019 020 public MigrationXmlWriter(String comment) { 021 this.comment = comment; 022 } 023 024 /** 025 * Write a Migration as a standalone XML document to a file. 026 */ 027 public void write(Migration migration, File file) { 028 029 try (FileWriter writer = new FileWriter(file)) { 030 031 writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"); 032 if (comment != null) { 033 writer.write("<!-- "); 034 writer.write(comment); 035 writer.write(" -->\n"); 036 } 037 038 JAXBContext jaxbContext = JAXBContext.newInstance(Migration.class); 039 Marshaller marshaller = jaxbContext.createMarshaller(); 040 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 041 marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); 042 043 marshaller.marshal(migration, writer); 044 045 } catch (IOException | JAXBException e) { 046 throw new RuntimeException(e); 047 } 048 } 049 050}