Search in sources :

Example 1 with DefaultXmlWriter

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);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NamespaceDetails(liquibase.parser.NamespaceDetails) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) DefaultXmlWriter(liquibase.util.xml.DefaultXmlWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) HashMap(java.util.HashMap) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 NamespaceDetails (liquibase.parser.NamespaceDetails)1 DefaultXmlWriter (liquibase.util.xml.DefaultXmlWriter)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1