Search in sources :

Example 41 with EntityResolver

use of org.xml.sax.EntityResolver in project j2objc by google.

the class XMLFilterImplTest method testGetSetEntityResolver.

public void testGetSetEntityResolver() {
    EntityResolver resolver = new MockResolver();
    parent.setEntityResolver(resolver);
    assertEquals(resolver, parent.getEntityResolver());
    parent.setEntityResolver(null);
    assertEquals(null, parent.getEntityResolver());
}
Also used : EntityResolver(org.xml.sax.EntityResolver) MockResolver(org.apache.harmony.tests.org.xml.sax.support.MockResolver)

Example 42 with EntityResolver

use of org.xml.sax.EntityResolver in project jangaroo-tools by CoreMedia.

the class ExmlValidator method setupSAXParser.

private SAXParser setupSAXParser() throws IOException {
    try {
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        ExmlSchemaResolver exmlSchemaResolver = new ExmlSchemaResolver();
        schemaFactory.setResourceResolver(exmlSchemaResolver);
        List<Source> schemas = new ArrayList<Source>();
        schemas.add(new StreamSource(getClass().getResourceAsStream(Exmlc.EXML_SCHEMA_LOCATION), "exml"));
        schemas.add(new StreamSource(getClass().getResourceAsStream(Exmlc.EXML_UNTYPED_SCHEMA_LOCATION), "untyped"));
        Collection<ExmlSchemaSource> exmlSchemaSources = exmlSchemaSourceByNamespace.values();
        for (ExmlSchemaSource exmlSchemaSource : exmlSchemaSources) {
            schemas.add(exmlSchemaSource.newStreamSource());
        }
        Schema exmlSchema = schemaFactory.newSchema(schemas.toArray(new Source[schemas.size()]));
        final SAXParserFactory saxFactory = SAXParserFactory.newInstance();
        saxFactory.setNamespaceAware(true);
        saxFactory.setSchema(exmlSchema);
        SAXParser saxParser = saxFactory.newSAXParser();
        saxParser.getXMLReader().setEntityResolver(new EntityResolver() {

            @Override
            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                // To change body of implemented methods use File | Settings | File Templates.
                return null;
            }
        });
        return saxParser;
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException("A default dom builder should be provided.", e);
    } catch (SAXParseException e) {
        // SAX parser error while parsing EXML schemas: log only, will cause error or warning, depending on configuration:
        logSAXParseException(null, e, true);
        return null;
    } catch (SAXException e) {
        throw new IllegalStateException("SAX parser does not support validation.", e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) ArrayList(java.util.ArrayList) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 43 with EntityResolver

use of org.xml.sax.EntityResolver in project tomcat by apache.

the class Digester method getXMLReader.

/**
 * Return the XMLReader to be used for parsing the input document.
 *
 * FIX ME: there is a bug in JAXP/XERCES that prevent the use of a
 * parser that contains a schema with a DTD.
 * @return the XML reader
 * @exception SAXException if no XMLReader can be instantiated
 */
public XMLReader getXMLReader() throws SAXException {
    if (reader == null) {
        reader = getParser().getXMLReader();
    }
    reader.setDTDHandler(this);
    reader.setContentHandler(this);
    EntityResolver entityResolver = getEntityResolver();
    if (entityResolver == null) {
        entityResolver = this;
    }
    // Wrap the resolver so we can perform ${...} property replacement
    if (entityResolver instanceof EntityResolver2) {
        entityResolver = new EntityResolver2Wrapper((EntityResolver2) entityResolver, source, classLoader);
    } else {
        entityResolver = new EntityResolverWrapper(entityResolver, source, classLoader);
    }
    reader.setEntityResolver(entityResolver);
    reader.setProperty("http://xml.org/sax/properties/lexical-handler", this);
    reader.setErrorHandler(this);
    return reader;
}
Also used : EntityResolver(org.xml.sax.EntityResolver) EntityResolver2(org.xml.sax.ext.EntityResolver2)

Example 44 with EntityResolver

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

the class JavaXmlUtil method getDocumentBuilder.

/**
 * get document builder
 *
 * @return DocumentBuilder
 */
private static DocumentBuilder getDocumentBuilder() {
    DocumentBuilder documentBuilder = null;
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    try {
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
        // Block any external content resolving actions since we don't need them and a report
        // says these actions may cause security problems.
        documentBuilder.setEntityResolver(new EntityResolver() {

            @Override
            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                return new InputSource();
            }
        });
    } catch (Exception e) {
        throw new JavaXmlUtilException(e);
    }
    return documentBuilder;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException)

Example 45 with EntityResolver

use of org.xml.sax.EntityResolver 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

EntityResolver (org.xml.sax.EntityResolver)85 InputSource (org.xml.sax.InputSource)41 SAXException (org.xml.sax.SAXException)37 IOException (java.io.IOException)32 DefaultHandler (org.xml.sax.helpers.DefaultHandler)25 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)23 DocumentBuilder (javax.xml.parsers.DocumentBuilder)22 Test (org.junit.jupiter.api.Test)22 Document (org.w3c.dom.Document)18 StringReader (java.io.StringReader)15 Field (java.lang.reflect.Field)14 SAXParseException (org.xml.sax.SAXParseException)14 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)11 NodeList (org.w3c.dom.NodeList)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 InputStream (java.io.InputStream)10 ArrayList (java.util.ArrayList)9 Node (org.w3c.dom.Node)9 ErrorHandler (org.xml.sax.ErrorHandler)9 Element (org.w3c.dom.Element)8