Search in sources :

Example 11 with XmlProtoNode

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

the class XmlProtoToXmlConverterTest method testNoNamespaceDeclarationWithCommonUris.

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

Example 12 with XmlProtoNode

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

the class XmlProtoToXmlConverterTest method testNameOfAndroidAttributeRemoved.

@Test
public void testNameOfAndroidAttributeRemoved() {
    XmlProtoNode proto = XmlProtoNode.createElementNode(XmlProtoElementBuilder.create("manifest").addNamespaceDeclaration("android", "http://schemas.android.com/apk/res/android").addAttribute(XmlProtoAttributeBuilder.createAndroidAttribute("", 0x0101021b).setValueAsDecimalInteger(123)).build());
    Document document = XmlProtoToXmlConverter.convert(proto);
    String xmlString = XmlUtils.documentToString(document);
    assertThat(xmlString).isEqualTo(String.format("<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\" " + "android:_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 13 with XmlProtoNode

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

the class XmlProtoToXmlConverterTest method testConversionManifest.

@Test
public void testConversionManifest() {
    XmlProtoElement manifestElement = XmlProtoElementBuilder.create("manifest").addNamespaceDeclaration("android", ANDROID_NS).addNamespaceDeclaration("dist", DIST_NS).addAttribute(newAttribute("package").setValueAsString("com.example.app")).addAttribute(newAttribute(ANDROID_NS, "versionCode").setValueAsDecimalInteger(123)).addAttribute(newAttribute(ANDROID_NS, "versionName").setValueAsString("1.2.3")).addChildElement(XmlProtoElementBuilder.create(DIST_NS, "module").addAttribute(newAttribute(DIST_NS, "onDemand").setValueAsBoolean(true)).addAttribute(newAttribute(DIST_NS, "title").setValueAsRefId(0x7f0b0001, "string/title_module"))).addChildElement(XmlProtoElementBuilder.create("application").addAttribute(newAttribute(ANDROID_NS, "name").setValueAsString(".MyApp")).addAttribute(newAttribute(ANDROID_NS, "icon").setValueAsRefId(0x7f010005, "mipmap/ic_launcher")).addAttribute(newAttribute(ANDROID_NS, "supportsRtl").setValueAsBoolean(true)).addChildElement(XmlProtoElementBuilder.create("meta-data").addAttribute(newAttribute(ANDROID_NS, "name").setValueAsString("com.google.android.gms.version")).addAttribute(newAttribute(ANDROID_NS, "value").setValueAsRefId(0x7f0b0002, "integer/google_play_services_version"))).addChildElement(XmlProtoElementBuilder.create("activity").addAttribute(newAttribute(ANDROID_NS, "exported").setValueAsBoolean(true)).addAttribute(newAttribute(ANDROID_NS, "name").setValueAsString("com.example.app.MyActivity")))).build();
    XmlProtoNode manifestProto = XmlProtoNode.createElementNode(manifestElement);
    Document manifestXml = XmlProtoToXmlConverter.convert(manifestProto);
    assertThat(XmlUtils.documentToString(manifestXml)).isEqualTo(LINE_BREAK_JOINER.join("<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\" " + "xmlns:dist=\"http://schemas.android.com/apk/distribution\" " + "android:versionCode=\"123\" " + "android:versionName=\"1.2.3\" " + "package=\"com.example.app\">", "  <dist:module dist:onDemand=\"true\" dist:title=\"@string/title_module\"/>", "  <application android:icon=\"@mipmap/ic_launcher\" " + "android:name=\".MyApp\" " + "android:supportsRtl=\"true\">", "    <meta-data android:name=\"com.google.android.gms.version\" " + "android:value=\"@integer/google_play_services_version\"/>", "    <activity android:exported=\"true\" " + "android:name=\"com.example.app.MyActivity\"/>", "  </application>", "</manifest>" + LINE_BREAK));
}
Also used : Document(org.w3c.dom.Document) XmlProtoElement(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElement) XmlProtoNode(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode) Test(org.junit.Test)

Example 14 with XmlProtoNode

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

the class XmlProtoToXmlConverterTest method testCommonUriPrefixAlreadyInUse.

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

Example 15 with XmlProtoNode

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

the class DumpManager method printManifest.

void printManifest(BundleModuleName moduleName, Optional<String> xPathExpression) {
    // Extract the manifest from the bundle.
    ZipPath manifestPath = ZipPath.create(moduleName.getName()).resolve(SpecialModuleEntry.ANDROID_MANIFEST.getPath());
    XmlProtoNode manifestProto = new XmlProtoNode(extractAndParse(bundlePath, manifestPath, XmlNode::parseFrom));
    // Convert the proto to real XML.
    Document document = XmlProtoToXmlConverter.convert(manifestProto);
    // Select the output.
    String output;
    if (xPathExpression.isPresent()) {
        try {
            XPath xPath = XPathFactory.newInstance().newXPath();
            xPath.setNamespaceContext(new XmlNamespaceContext(manifestProto));
            XPathExpression compiledXPathExpression = xPath.compile(xPathExpression.get());
            XPathResult xPathResult = XPathResolver.resolve(document, compiledXPathExpression);
            output = xPathResult.toString();
        } catch (XPathExpressionException e) {
            throw InvalidCommandException.builder().withInternalMessage("Error in the XPath expression: " + xPathExpression).withCause(e).build();
        }
    } else {
        output = XmlUtils.documentToString(document);
    }
    // Print the output.
    printStream.println(output.trim());
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XmlNamespaceContext(com.android.tools.build.bundletool.xml.XmlNamespaceContext) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ZipPath(com.android.tools.build.bundletool.model.ZipPath) XPathResult(com.android.tools.build.bundletool.xml.XPathResolver.XPathResult) Document(org.w3c.dom.Document) XmlProtoNode(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode)

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