Search in sources :

Example 1 with XmlProtoNamespace

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());
    }
}
Also used : Element(org.w3c.dom.Element) XmlProtoElement(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElement) XmlProtoNamespace(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNamespace) XmlProtoAttribute(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoAttribute) XmlProtoElement(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElement) XmlProtoNode(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode)

Aggregations

XmlProtoAttribute (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoAttribute)1 XmlProtoElement (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElement)1 XmlProtoNamespace (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNamespace)1 XmlProtoNode (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode)1 Element (org.w3c.dom.Element)1