Search in sources :

Example 71 with EntityResolver

use of org.xml.sax.EntityResolver in project tinker by Tencent.

the class Configuration method readXmlConfig.

/**
 * read args from xml
 */
void readXmlConfig(File xmlConfigFile) throws IOException, ParserConfigurationException, SAXException {
    if (!xmlConfigFile.exists()) {
        return;
    }
    System.out.printf("reading config file, %s\n", xmlConfigFile.getAbsolutePath());
    BufferedInputStream input = null;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        input = new BufferedInputStream(new FileInputStream(xmlConfigFile));
        InputSource source = new InputSource(input);
        factory.setNamespaceAware(false);
        factory.setValidating(false);
        DocumentBuilder builder = factory.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 document = builder.parse(source);
        NodeList issues = document.getElementsByTagName(TAG_ISSUE);
        for (int i = 0, count = issues.getLength(); i < count; i++) {
            Node node = issues.item(i);
            Element element = (Element) node;
            String id = element.getAttribute(ATTR_ID);
            if (id.length() == 0) {
                System.err.println("Invalid config file: Missing required issue id attribute");
                continue;
            }
            if (id.equals(PROPERTY_ISSUE)) {
                readPropertyFromXml(node);
            } else if (id.equals(DEX_ISSUE)) {
                readDexPatternsFromXml(node);
            } else if (id.equals(SO_ISSUE)) {
                readLibPatternsFromXml(node);
            } else if (id.equals(RES_ISSUE)) {
                readResPatternsFromXml(node);
            } else if (id.equals(PACKAGE_CONFIG_ISSUE)) {
                readPackageConfigFromXml(node);
            } else if (id.equals(SIGN_ISSUE)) {
                if (mUseSignAPk) {
                    readSignFromXml(node);
                }
            } else if (id.equals(ARKHOT_ISSUE)) {
                readArkHotPropertyFromXml(node);
            } else {
                System.err.println("unknown issue " + id);
            }
        }
    } finally {
        IOHelper.closeQuietly(input);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) BufferedInputStream(java.io.BufferedInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Example 72 with EntityResolver

use of org.xml.sax.EntityResolver in project Mycat-Server by MyCATApache.

the class ConfigUtil method getDocument.

public static Document getDocument(final InputStream dtd, InputStream xml) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    factory.setNamespaceAware(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setEntityResolver(new EntityResolver() {

        @Override
        public InputSource resolveEntity(String publicId, String systemId) {
            return new InputSource(dtd);
        }
    });
    builder.setErrorHandler(new ErrorHandler() {

        @Override
        public void warning(SAXParseException e) {
        }

        @Override
        public void error(SAXParseException e) throws SAXException {
            throw e;
        }

        @Override
        public void fatalError(SAXParseException e) throws SAXException {
            throw e;
        }
    });
    return builder.parse(xml);
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) EntityResolver(org.xml.sax.EntityResolver) SAXException(org.xml.sax.SAXException)

Aggregations

EntityResolver (org.xml.sax.EntityResolver)72 InputSource (org.xml.sax.InputSource)39 SAXException (org.xml.sax.SAXException)34 IOException (java.io.IOException)29 DocumentBuilder (javax.xml.parsers.DocumentBuilder)22 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)22 Document (org.w3c.dom.Document)17 DefaultHandler (org.xml.sax.helpers.DefaultHandler)17 Test (org.junit.Test)16 StringReader (java.io.StringReader)13 SAXParseException (org.xml.sax.SAXParseException)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)10 NodeList (org.w3c.dom.NodeList)10 InputStream (java.io.InputStream)9 ArrayList (java.util.ArrayList)8 Node (org.w3c.dom.Node)8 ErrorHandler (org.xml.sax.ErrorHandler)8 Element (org.w3c.dom.Element)7 File (java.io.File)6