use of liquibase.util.xml.DefaultXmlWriter in project liquibase by liquibase.
the class XMLChangeLogSerializer method write.
@Override
public <T extends ChangeLogChild> void write(List<T> children, OutputStream out) throws IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder documentBuilder;
try {
documentBuilder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
}
documentBuilder.setEntityResolver(resolver);
Document doc = documentBuilder.newDocument();
doc.setXmlVersion(XML_VERSION);
Element changeLogElement = doc.createElementNS(LiquibaseSerializable.STANDARD_CHANGELOG_NAMESPACE, "databaseChangeLog");
changeLogElement.setAttribute("xmlns", LiquibaseSerializable.STANDARD_CHANGELOG_NAMESPACE);
changeLogElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
Map<String, String> shortNameByNamespace = new HashMap<>();
Map<String, String> urlByNamespace = new HashMap<>();
for (NamespaceDetails details : NamespaceDetailsFactory.getInstance().getNamespaceDetails()) {
for (String namespace : details.getNamespaces()) {
if (details.getPriority() > 0 && details.supports(this, namespace)) {
String shortName = details.getShortName(namespace);
String url = details.getSchemaUrl(namespace);
if (shortName != null) {
shortNameByNamespace.put(namespace, shortName);
}
if (url != null) {
urlByNamespace.put(namespace, url);
}
}
}
}
for (Map.Entry<String, String> entry : shortNameByNamespace.entrySet()) {
if (!"".equals(entry.getValue())) {
changeLogElement.setAttribute("xmlns:" + entry.getValue(), entry.getKey());
}
}
StringBuilder schemaLocationAttribute = new StringBuilder();
for (Map.Entry<String, String> entry : urlByNamespace.entrySet()) {
if (!"".equals(entry.getValue())) {
schemaLocationAttribute.append(entry.getKey()).append(" ").append(entry.getValue()).append(" ");
}
}
changeLogElement.setAttribute("xsi:schemaLocation", schemaLocationAttribute.toString().trim());
doc.appendChild(changeLogElement);
setCurrentChangeLogFileDOM(doc);
for (T child : children) {
doc.getDocumentElement().appendChild(createNode(child));
}
new DefaultXmlWriter().write(doc, out);
}
Aggregations