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