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