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