Search in sources :

Example 1 with XPathResult

use of com.android.tools.build.bundletool.xml.XPathResolver.XPathResult 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

ZipPath (com.android.tools.build.bundletool.model.ZipPath)1 XmlProtoNode (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode)1 XPathResult (com.android.tools.build.bundletool.xml.XPathResolver.XPathResult)1 XmlNamespaceContext (com.android.tools.build.bundletool.xml.XmlNamespaceContext)1 XPath (javax.xml.xpath.XPath)1 XPathExpression (javax.xml.xpath.XPathExpression)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 Document (org.w3c.dom.Document)1