Search in sources :

Example 1 with ParserException

use of tinker.net.dongliu.apk.parser.exception.ParserException in project tinker by Tencent.

the class AndroidParser method parse.

private void parse() throws ParserException {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    Document document;
    try {
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        // Block any external content resolving actions since we don't need them and a report
        // says these actions may cause security problems.
        builder.setEntityResolver(new EntityResolver() {

            @Override
            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                return new InputSource();
            }
        });
        document = builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        Node manifestNode = document.getElementsByTagName("manifest").item(0);
        NodeList nodes = manifestNode.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            String nodeName = node.getNodeName();
            if (nodeName.equals("application")) {
                NodeList children = node.getChildNodes();
                for (int j = 0; j < children.getLength(); j++) {
                    Node child = children.item(j);
                    String childName = child.getNodeName();
                    switch(childName) {
                        case "service":
                            services.add(getAndroidComponent(child, TYPE_SERVICE));
                            break;
                        case "activity":
                            activities.add(getAndroidComponent(child, TYPE_ACTIVITY));
                            break;
                        case "receiver":
                            receivers.add(getAndroidComponent(child, TYPE_BROADCAST_RECEIVER));
                            break;
                        case "provider":
                            providers.add(getAndroidComponent(child, TYPE_CONTENT_PROVIDER));
                            break;
                        case "meta-data":
                            NamedNodeMap attributes = child.getAttributes();
                            metaDatas.put(getAttribute(attributes, "android:name"), getAttribute(attributes, "android:value"));
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new ParserException("Error parsing AndroidManifest.xml", e);
    }
}
Also used : ParserException(tinker.net.dongliu.apk.parser.exception.ParserException) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.w3c.dom.Document) ParseException(java.text.ParseException) IOException(java.io.IOException) ParserException(tinker.net.dongliu.apk.parser.exception.ParserException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Document (org.w3c.dom.Document)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1 EntityResolver (org.xml.sax.EntityResolver)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1 ParserException (tinker.net.dongliu.apk.parser.exception.ParserException)1