Search in sources :

Example 1 with XmlProtoNode

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

the class AndroidManifestTest method configSplit_noExtraElementsFromModuleSplit.

@Test
public void configSplit_noExtraElementsFromModuleSplit() throws Exception {
    XmlNode.Builder xmlNodeBuilder = XmlNode.newBuilder();
    TextFormat.merge(TestData.openReader("testdata/manifest/manifest1.textpb"), xmlNodeBuilder);
    AndroidManifest androidManifest = AndroidManifest.create(xmlNodeBuilder.build());
    XmlNode.Builder expectedXmlNodeBuilder = XmlNode.newBuilder();
    TextFormat.merge(TestData.openReader("testdata/manifest/config_split_manifest1.textpb"), expectedXmlNodeBuilder);
    AndroidManifest configManifest = AndroidManifest.createForConfigSplit(androidManifest.getPackageName(), androidManifest.getVersionCode(), "testModule.config.hdpi", "testModule", Optional.empty());
    XmlProtoNode generatedManifest = configManifest.getManifestRoot();
    assertThat(generatedManifest.getProto()).ignoringRepeatedFieldOrder().isEqualTo(expectedXmlNodeBuilder.build());
}
Also used : XmlNode(com.android.aapt.Resources.XmlNode) XmlProtoNode(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode) Test(org.junit.Test)

Example 2 with XmlProtoNode

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

the class WearApkLocator method extractWearApkName.

/**
 * Parses the XML description file for the name of the wear APK.
 *
 * <p>According to
 * https://developer.android.com/training/wearables/apps/packaging#PackageManually, it is the
 * value inside the tag <rawPathResId>.
 */
private static Optional<String> extractWearApkName(ModuleEntry wearApkDescriptionXmlEntry) {
    XmlProtoNode root;
    try (InputStream content = wearApkDescriptionXmlEntry.getContent().openStream()) {
        root = new XmlProtoNode(XmlNode.parseFrom(content));
    } catch (InvalidProtocolBufferException e) {
        throw InvalidBundleException.builder().withCause(e).withUserMessage("The wear APK description file '%s' could not be parsed.", wearApkDescriptionXmlEntry.getPath()).build();
    } catch (IOException e) {
        throw new UncheckedIOException(String.format("An unexpected error occurred while reading APK description file '%s'.", wearApkDescriptionXmlEntry.getPath()), e);
    }
    // If the Wear APK is unbundled, there is nothing to find.
    if (root.getElement().getOptionalChildElement("unbundled").isPresent()) {
        return Optional.empty();
    }
    // If 'unbundled' is not present, then 'rawPathResId' must be.
    Optional<XmlProtoElement> rawPathResId = root.getElement().getOptionalChildElement("rawPathResId");
    if (!rawPathResId.isPresent()) {
        throw InvalidBundleException.builder().withUserMessage("The wear APK description file '%s' does not contain 'unbundled' or 'rawPathResId'.", wearApkDescriptionXmlEntry.getPath()).build();
    }
    return Optional.of(rawPathResId.get().getChildText().get().getText());
}
Also used : InputStream(java.io.InputStream) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) XmlProtoElement(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElement) XmlProtoNode(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode)

Example 3 with XmlProtoNode

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

the class XmlProtoToXmlConverterTest method testNoNamespaceDeclaration_doesNotCrash.

@Test
public void testNoNamespaceDeclaration_doesNotCrash() {
    XmlProtoNode proto = XmlProtoNode.createElementNode(XmlProtoElementBuilder.create("root").addAttribute(XmlProtoAttributeBuilder.create("http://uri", "key").setValueAsString("value")).addAttribute(XmlProtoAttributeBuilder.create("http://uri", "key2").setValueAsString("value2")).addAttribute(XmlProtoAttributeBuilder.create("http://other-uri", "other-key").setValueAsString("other-value")).build());
    Document document = XmlProtoToXmlConverter.convert(proto);
    String xmlString = XmlUtils.documentToString(document);
    assertThat(xmlString).isEqualTo(String.format("<root xmlns:_unknown0_=\"http://uri\" " + "_unknown0_:key=\"value\" " + "_unknown0_:key2=\"value2\" " + "xmlns:_unknown1_=\"http://other-uri\" " + "_unknown1_:other-key=\"other-value\"/>%n"));
}
Also used : Document(org.w3c.dom.Document) XmlProtoNode(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode) Test(org.junit.Test)

Example 4 with XmlProtoNode

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

the class XmlProtoToXmlConverterTest method testNameAndNamespaceOfAndroidAttributeRemoved.

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

Example 5 with XmlProtoNode

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

the class XmlProtoToXmlConverterTest method testNoNamespaceDeclarationWithNestedElementsWithCommonUris.

@Test
public void testNoNamespaceDeclarationWithNestedElementsWithCommonUris() {
    XmlProtoNode proto = XmlProtoNode.createElementNode(XmlProtoElementBuilder.create("root").addChildElement(XmlProtoElementBuilder.create("http://schemas.android.com/apk/res/android", "android-element").addChildElement(XmlProtoElementBuilder.create("http://schemas.android.com/apk/res/android", "android-element2"))).build());
    Document document = XmlProtoToXmlConverter.convert(proto);
    String xmlString = XmlUtils.documentToString(document);
    assertThat(xmlString).isEqualTo(String.format("<root>%n" + "  <android:android-element " + "xmlns:android=\"http://schemas.android.com/apk/res/android\">%n" + "    <android:android-element2/>%n" + "  </android:android-element>%n" + "</root>%n"));
}
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