Search in sources :

Example 6 with XmlProtoElement

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

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

the class AndroidManifestTest method getPermissions_success.

@Test
public void getPermissions_success() {
    XmlProtoElement permission1 = new XmlProtoElement(xmlElement(PERMISSION_ELEMENT_NAME, xmlAttribute(ANDROID_NAMESPACE_URI, "name", NAME_RESOURCE_ID, "SEND_SMS")));
    XmlProtoElement permission2 = new XmlProtoElement(xmlElement(PERMISSION_ELEMENT_NAME, ImmutableList.of(xmlAttribute(ANDROID_NAMESPACE_URI, "name", NAME_RESOURCE_ID, "com.some.other.PERMISSION"), xmlResourceReferenceAttribute(ANDROID_NAMESPACE_URI, ICON_ATTRIBUTE_NAME, ICON_RESOURCE_ID, 12341234), xmlResourceReferenceAttribute(ANDROID_NAMESPACE_URI, LABEL_ATTRIBUTE_NAME, LABEL_RESOURCE_ID, 0x12345678), xmlResourceReferenceAttribute(ANDROID_NAMESPACE_URI, DESCRIPTION_ATTRIBUTE_NAME, DESCRIPTION_RESOURCE_ID, 0x87654321), xmlAttribute(ANDROID_NAMESPACE_URI, "protectionLevel", "normal|signature"), xmlAttribute(ANDROID_NAMESPACE_URI, "permissionGroup", "group1"))));
    AndroidManifest androidManifest = AndroidManifest.create(xmlNode(xmlElement("manifest", xmlNode(permission1.getProto()), xmlNode(permission2.getProto()))));
    assertThat(androidManifest.getPermissions()).containsExactly(permission1, permission2);
}
Also used : XmlProtoElement(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElement) Test(org.junit.Test)

Example 8 with XmlProtoElement

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

the class ManifestDeliveryElementTest method moduleConditions_missingMinSdkValue_throws.

@Test
public void moduleConditions_missingMinSdkValue_throws() {
    // Value attribute doesn't use distribution namespace.
    XmlProtoElement badCondition = XmlProtoElementBuilder.create(DISTRIBUTION_NAMESPACE_URI, "min-sdk").addAttribute(XmlProtoAttributeBuilder.create("value").setValueAsDecimalInteger(26)).build();
    Optional<ManifestDeliveryElement> manifestDeliveryElement = ManifestDeliveryElement.fromManifestRootNode(createAndroidManifestWithConditions(badCondition), /* isFastFollowAllowed= */
    false);
    assertThat(manifestDeliveryElement).isPresent();
    Throwable exception = assertThrows(InvalidBundleException.class, () -> manifestDeliveryElement.get().getModuleConditions());
    assertThat(exception).hasMessageThat().contains("Missing required 'dist:value' attribute in the 'min-sdk' condition element.");
}
Also used : XmlProtoElement(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElement) Test(org.junit.Test)

Example 9 with XmlProtoElement

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

the class ManifestDeliveryElementTest method moduleConditions_wrongAttributeInDeviceGroupElement_throws.

@Test
public void moduleConditions_wrongAttributeInDeviceGroupElement_throws() {
    XmlProtoElement badDeviceGroupCondition = XmlProtoElementBuilder.create(DISTRIBUTION_NAMESPACE_URI, "device-groups").addChildElement(XmlProtoElementBuilder.create(DISTRIBUTION_NAMESPACE_URI, "device-group").addAttribute(XmlProtoAttributeBuilder.create(DISTRIBUTION_NAMESPACE_URI, "groupName").setValueAsString("group1"))).build();
    Optional<ManifestDeliveryElement> element = ManifestDeliveryElement.fromManifestRootNode(createAndroidManifestWithConditions(badDeviceGroupCondition), /* isFastFollowAllowed= */
    false);
    assertThat(element).isPresent();
    InvalidBundleException exception = assertThrows(InvalidBundleException.class, () -> element.get().getModuleConditions());
    assertThat(exception).hasMessageThat().isEqualTo("'<dist:device-group>' element is expected to have 'dist:name' attribute but found" + " none.");
}
Also used : InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) XmlProtoElement(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElement) Test(org.junit.Test)

Example 10 with XmlProtoElement

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

the class AndroidManifestTest method getPermissionGroups_success.

@Test
public void getPermissionGroups_success() {
    XmlProtoElement permisisonGroup1 = new XmlProtoElement(xmlElement(PERMISSION_GROUP_ELEMENT_NAME, xmlAttribute(ANDROID_NAMESPACE_URI, "name", NAME_RESOURCE_ID, "group.name.1")));
    XmlProtoElement permisisonGroup2 = new XmlProtoElement(xmlElement(PERMISSION_GROUP_ELEMENT_NAME, ImmutableList.of(xmlAttribute(ANDROID_NAMESPACE_URI, "name", NAME_RESOURCE_ID, "group.name.2"), xmlResourceReferenceAttribute(ANDROID_NAMESPACE_URI, ICON_ATTRIBUTE_NAME, ICON_RESOURCE_ID, 12341234), xmlResourceReferenceAttribute(ANDROID_NAMESPACE_URI, LABEL_ATTRIBUTE_NAME, LABEL_RESOURCE_ID, 0x12345678), xmlResourceReferenceAttribute(ANDROID_NAMESPACE_URI, DESCRIPTION_ATTRIBUTE_NAME, DESCRIPTION_RESOURCE_ID, 0x87654321))));
    AndroidManifest androidManifest = AndroidManifest.create(xmlNode(xmlElement("manifest", xmlNode(permisisonGroup1.getProto()), xmlNode(permisisonGroup2.getProto()))));
    assertThat(androidManifest.getPermissionGroups()).containsExactly(permisisonGroup1, permisisonGroup2);
}
Also used : XmlProtoElement(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElement) Test(org.junit.Test)

Aggregations

XmlProtoElement (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElement)14 Test (org.junit.Test)7 XmlProtoNode (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode)3 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 AndroidManifest (com.android.tools.build.bundletool.model.AndroidManifest)2 BundleModuleName (com.android.tools.build.bundletool.model.BundleModuleName)2 BASE_MODULE_NAME (com.android.tools.build.bundletool.model.BundleModuleName.BASE_MODULE_NAME)2 ManifestEditor (com.android.tools.build.bundletool.model.ManifestEditor)2 CommandExecutionException (com.android.tools.build.bundletool.model.exceptions.CommandExecutionException)2 InvalidBundleException (com.android.tools.build.bundletool.model.exceptions.InvalidBundleException)2 XmlProtoAttributeBuilder (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoAttributeBuilder)2 XmlProtoElementBuilder (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElementBuilder)2 XmlProtoNodeBuilder (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNodeBuilder)2 AutoValue (com.google.auto.value.AutoValue)2 Predicates.not (com.google.common.base.Predicates.not)2 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)2