use of com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNamespace in project bundletool by google.
the class XmlProtoToXmlConverter method createXmlNode.
private Node createXmlNode(XmlProtoNode protoNode, Document xmlFactory) {
if (protoNode.isElement()) {
XmlProtoElement protoElement = protoNode.getElement();
// Create the element.
Element xmlElement;
String namespaceUri = protoElement.getNamespaceUri();
if (namespaceUri.isEmpty()) {
xmlElement = xmlFactory.createElement(protoElement.getName());
} else {
String prefix = getPrefixForNamespace(namespaceUri);
xmlElement = xmlFactory.createElementNS(namespaceUri, prefix + ":" + protoElement.getName());
}
// Add the namespaces.
ImmutableList<XmlProtoNamespace> namespaces = protoElement.getNamespaceDeclarations().collect(toImmutableList());
for (XmlProtoNamespace namespace : namespaces) {
String prefix = namespace.getPrefix();
Deque<String> prefixes = namespaceUriToPrefix.computeIfAbsent(namespace.getUri(), k -> new ArrayDeque<>());
prefixes.addLast(prefix);
xmlElement.setAttributeNS(/* namespaceUri= */
XMLNS_NAMESPACE_URI, /* qualifiedName= */
prefix.isEmpty() ? "xmlns" : "xmlns:" + prefix, /* value= */
namespace.getUri());
}
// Add the attributes.
for (XmlProtoAttribute protoAttribute : protoElement.getAttributes().collect(toList())) {
String attrNamespaceUri = protoAttribute.getNamespaceUri();
if (attrNamespaceUri.isEmpty()) {
xmlElement.setAttribute(getAttributeTagName(protoAttribute), protoAttribute.getDebugString());
} else {
String prefix = getPrefixForNamespace(attrNamespaceUri);
xmlElement.setAttributeNS(attrNamespaceUri, prefix + ":" + getAttributeTagName(protoAttribute), protoAttribute.getDebugString());
}
}
// Recursively add children.
for (XmlProtoNode child : protoElement.getChildren().collect(toImmutableList())) {
xmlElement.appendChild(createXmlNode(child, xmlFactory));
}
// Remove the namespace declarations that are now out of scope.
namespaces.forEach(namespace -> namespaceUriToPrefix.get(namespace.getUri()).removeLast());
return xmlElement;
} else {
return xmlFactory.createTextNode(protoNode.getText());
}
}
Aggregations