use of com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode in project bundletool by google.
the class XmlProtoToXmlConverterTest method testNameOfNamespacedAttributeRemoved_doesNotCrash.
@Test
public void testNameOfNamespacedAttributeRemoved_doesNotCrash() {
XmlProtoNode proto = XmlProtoNode.createElementNode(XmlProtoElementBuilder.create("root").addNamespaceDeclaration("a", "http://uri").addAttribute(XmlProtoAttributeBuilder.create("http://uri", "").setValueAsString("hello")).build());
Document document = XmlProtoToXmlConverter.convert(proto);
String xmlString = XmlUtils.documentToString(document);
assertThat(xmlString).isEqualTo(String.format("<root xmlns:a=\"http://uri\" a:_unknown_=\"hello\"/>%n"));
}
use of com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode in project bundletool by google.
the class XmlProtoToXmlConverterTest method testNameOfAttributeRemoved_doesNotCrash.
@Test
public void testNameOfAttributeRemoved_doesNotCrash() {
XmlProtoNode proto = XmlProtoNode.createElementNode(XmlProtoElementBuilder.create("manifest").addAttribute(XmlProtoAttributeBuilder.create("").setValueAsString("hello")).build());
Document document = XmlProtoToXmlConverter.convert(proto);
String xmlString = XmlUtils.documentToString(document);
assertThat(xmlString).isEqualTo(String.format("<manifest _unknown_=\"hello\"/>%n"));
}
use of com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode 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.XmlProtoNode in project bundletool by google.
the class XmlProtoToXmlConverterTest method testNamespaceScopes.
@Test
public void testNamespaceScopes() {
XmlProtoNode proto = XmlProtoNode.createElementNode(XmlProtoElementBuilder.create("root").addChildElement(XmlProtoElementBuilder.create("child1").addNamespaceDeclaration("a", "http://uri").addAttribute(newAttribute("http://uri", "test1").setValueAsBoolean(true))).addChildElement(XmlProtoElementBuilder.create("child2").addNamespaceDeclaration("b", "http://uri").addAttribute(newAttribute("http://uri", "test2").setValueAsBoolean(true))).build());
Document document = XmlProtoToXmlConverter.convert(proto);
String xmlString = XmlUtils.documentToString(document);
assertThat(xmlString).isEqualTo(LINE_BREAK_JOINER.join("<root>", " <child1 xmlns:a=\"http://uri\" a:test1=\"true\"/>", " <child2 xmlns:b=\"http://uri\" b:test2=\"true\"/>", "</root>" + LINE_BREAK));
}
use of com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode in project bundletool by google.
the class XmlProtoToXmlConverterTest method testRedundantNamespaces_usesLastOneFound.
@Test
public void testRedundantNamespaces_usesLastOneFound() {
XmlProtoNode proto = XmlProtoNode.createElementNode(XmlProtoElementBuilder.create("root").addNamespaceDeclaration("a", "http://uri").addChildElement(XmlProtoElementBuilder.create("child1").addNamespaceDeclaration("b", "http://uri").addAttribute(newAttribute("http://uri", "test1").setValueAsBoolean(true))).addChildElement(XmlProtoElementBuilder.create("child2").addAttribute(newAttribute("http://uri", "test2").setValueAsBoolean(true))).build());
Document document = XmlProtoToXmlConverter.convert(proto);
String xmlString = XmlUtils.documentToString(document);
assertThat(xmlString).isEqualTo(LINE_BREAK_JOINER.join("<root xmlns:a=\"http://uri\">", " <child1 xmlns:b=\"http://uri\" b:test1=\"true\"/>", " <child2 a:test2=\"true\"/>", "</root>" + LINE_BREAK));
}
Aggregations