Search in sources :

Example 6 with XmlProtoNode

use of com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode in project bundletool by google.

the class XmlProtoToXmlConverterTest method testNameOfNamespacedAttributeRemoved_doesNotCrash.

@Test
public void testNameOfNamespacedAttributeRemoved_doesNotCrash() {
    XmlProtoNode proto = XmlProtoNode.createElementNode(XmlProtoElementBuilder.create("root").addNamespaceDeclaration("a", "http://uri").addAttribute(XmlProtoAttributeBuilder.create("http://uri", "").setValueAsString("hello")).build());
    Document document = XmlProtoToXmlConverter.convert(proto);
    String xmlString = XmlUtils.documentToString(document);
    assertThat(xmlString).isEqualTo(String.format("<root xmlns:a=\"http://uri\" a:_unknown_=\"hello\"/>%n"));
}
Also used : Document(org.w3c.dom.Document) XmlProtoNode(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode) Test(org.junit.Test)

Example 7 with XmlProtoNode

use of com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode in project bundletool by google.

the class XmlProtoToXmlConverterTest method testNameOfAttributeRemoved_doesNotCrash.

@Test
public void testNameOfAttributeRemoved_doesNotCrash() {
    XmlProtoNode proto = XmlProtoNode.createElementNode(XmlProtoElementBuilder.create("manifest").addAttribute(XmlProtoAttributeBuilder.create("").setValueAsString("hello")).build());
    Document document = XmlProtoToXmlConverter.convert(proto);
    String xmlString = XmlUtils.documentToString(document);
    assertThat(xmlString).isEqualTo(String.format("<manifest _unknown_=\"hello\"/>%n"));
}
Also used : Document(org.w3c.dom.Document) XmlProtoNode(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode) Test(org.junit.Test)

Example 8 with XmlProtoNode

use of com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode 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)

Example 9 with XmlProtoNode

use of com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode in project bundletool by google.

the class XmlProtoToXmlConverterTest method testNamespaceScopes.

@Test
public void testNamespaceScopes() {
    XmlProtoNode proto = XmlProtoNode.createElementNode(XmlProtoElementBuilder.create("root").addChildElement(XmlProtoElementBuilder.create("child1").addNamespaceDeclaration("a", "http://uri").addAttribute(newAttribute("http://uri", "test1").setValueAsBoolean(true))).addChildElement(XmlProtoElementBuilder.create("child2").addNamespaceDeclaration("b", "http://uri").addAttribute(newAttribute("http://uri", "test2").setValueAsBoolean(true))).build());
    Document document = XmlProtoToXmlConverter.convert(proto);
    String xmlString = XmlUtils.documentToString(document);
    assertThat(xmlString).isEqualTo(LINE_BREAK_JOINER.join("<root>", "  <child1 xmlns:a=\"http://uri\" a:test1=\"true\"/>", "  <child2 xmlns:b=\"http://uri\" b:test2=\"true\"/>", "</root>" + LINE_BREAK));
}
Also used : Document(org.w3c.dom.Document) XmlProtoNode(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode) Test(org.junit.Test)

Example 10 with XmlProtoNode

use of com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode in project bundletool by google.

the class XmlProtoToXmlConverterTest method testRedundantNamespaces_usesLastOneFound.

@Test
public void testRedundantNamespaces_usesLastOneFound() {
    XmlProtoNode proto = XmlProtoNode.createElementNode(XmlProtoElementBuilder.create("root").addNamespaceDeclaration("a", "http://uri").addChildElement(XmlProtoElementBuilder.create("child1").addNamespaceDeclaration("b", "http://uri").addAttribute(newAttribute("http://uri", "test1").setValueAsBoolean(true))).addChildElement(XmlProtoElementBuilder.create("child2").addAttribute(newAttribute("http://uri", "test2").setValueAsBoolean(true))).build());
    Document document = XmlProtoToXmlConverter.convert(proto);
    String xmlString = XmlUtils.documentToString(document);
    assertThat(xmlString).isEqualTo(LINE_BREAK_JOINER.join("<root xmlns:a=\"http://uri\">", "  <child1 xmlns:b=\"http://uri\" b:test1=\"true\"/>", "  <child2 a:test2=\"true\"/>", "</root>" + LINE_BREAK));
}
Also used : Document(org.w3c.dom.Document) XmlProtoNode(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode) Test(org.junit.Test)

Aggregations

XmlProtoNode (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode)18 Test (org.junit.Test)12 Document (org.w3c.dom.Document)12 XmlNode (com.android.aapt.Resources.XmlNode)3 XmlProtoElement (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElement)3 XmlProtoNodeBuilder (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNodeBuilder)3 ZipPath (com.android.tools.build.bundletool.model.ZipPath)1 XmlProtoAttribute (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoAttribute)1 XmlProtoNamespace (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNamespace)1 XPathResult (com.android.tools.build.bundletool.xml.XPathResolver.XPathResult)1 XmlNamespaceContext (com.android.tools.build.bundletool.xml.XmlNamespaceContext)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 XPath (javax.xml.xpath.XPath)1 XPathExpression (javax.xml.xpath.XPathExpression)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 Element (org.w3c.dom.Element)1