Search in sources :

Example 1 with NamespaceDetails

use of liquibase.parser.NamespaceDetails in project liquibase by liquibase.

the class XMLChangeLogSerializer method createNode.

public Element createNode(LiquibaseSerializable object) {
    String namespace = object.getSerializedObjectNamespace();
    String nodeName = object.getSerializedObjectName();
    NamespaceDetails details = NamespaceDetailsFactory.getInstance().getNamespaceDetails(this, namespace);
    if ((details != null) && !"".equals(details.getShortName(namespace))) {
        nodeName = details.getShortName(namespace) + ":" + nodeName;
    }
    Element node = currentChangeLogFileDOM.createElementNS(namespace, nodeName);
    try {
        for (String field : object.getSerializableFields()) {
            setValueOnNode(node, object.getSerializableFieldNamespace(field), field, object.getSerializableFieldValue(field), object.getSerializableFieldType(field), namespace);
        }
    } catch (UnexpectedLiquibaseException e) {
        if (object instanceof ChangeSet && e.getMessage().startsWith(INVALID_STRING_ENCODING_MESSAGE)) {
            throw new UnexpectedLiquibaseException(e.getMessage() + " in changeSet " + ((ChangeSet) object).toString(false) + ". To resolve, remove the invalid character on the database and try again");
        }
        throw e;
    }
    return node;
}
Also used : NamespaceDetails(liquibase.parser.NamespaceDetails) Element(org.w3c.dom.Element) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) ChangeSet(liquibase.changelog.ChangeSet)

Example 2 with NamespaceDetails

use of liquibase.parser.NamespaceDetails 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

NamespaceDetails (liquibase.parser.NamespaceDetails)2 Element (org.w3c.dom.Element)2 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 ChangeSet (liquibase.changelog.ChangeSet)1 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)1 DefaultXmlWriter (liquibase.util.xml.DefaultXmlWriter)1 Document (org.w3c.dom.Document)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1