Search in sources :

Example 31 with InputSource

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

the class DocumentBuilderTest method testParseInputSource.

/**
     * javax.xml.parsers.DocumentBuilder#parse(java.io.InputStream)
     * Case 1: Try to parse correct xml document.
     * Case 2: Try to call parse() with null argument.
     * Case 3: Try to parse a non-existent file.
     * Case 4: Try to parse incorrect xml file.
     */
public void testParseInputSource() {
    InputStream stream = getClass().getResourceAsStream("/simple.xml");
    InputSource is = new InputSource(stream);
    // case 1: Trivial use.
    try {
        Document d = db.parse(is);
        assertNotNull(d);
        // TBD getXmlEncoding() IS NOT SUPPORTED
        // assertEquals("ISO-8859-1", d.getXmlEncoding());
        assertEquals(2, d.getChildNodes().getLength());
        assertEquals("#comment", d.getChildNodes().item(0).getNodeName());
        assertEquals("breakfast_menu", d.getChildNodes().item(1).getNodeName());
    } catch (IOException ioe) {
        fail("Unexpected IOException " + ioe.toString());
    } catch (SAXException sax) {
        fail("Unexpected SAXException " + sax.toString());
    }
    // case 2: Try to call parse with null argument
    try {
        db.parse((InputSource) null);
        fail("Expected IllegalArgumentException was not thrown");
    } catch (IllegalArgumentException iae) {
    // expected
    } catch (IOException ioe) {
        fail("Unexpected IOException " + ioe.toString());
    } catch (SAXException sax) {
        fail("Unexpected SAXException " + sax.toString());
    }
    // case 3: Try to parse a non-existent file
    try {
        db.parse(new InputSource(new FileInputStream("_")));
        fail("Expected IOException was not thrown");
    } catch (IOException ioe) {
    // expected
    } catch (SAXException sax) {
        fail("Unexpected SAXException " + sax.toString());
    }
    // case 4: Try to parse incorrect xml file
    try {
        is = new InputSource(getClass().getResourceAsStream("/wrong.xml"));
        db.parse(is);
        fail("Expected SAXException was not thrown");
    } catch (IOException ioe) {
        fail("Unexpected IOException " + ioe.toString());
    } catch (SAXException sax) {
    // expected
    }
}
Also used : InputSource(org.xml.sax.InputSource) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException)

Example 32 with InputSource

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

the class DocumentBuilderTest method testReset.

public void testReset() {
    // Make sure EntityResolver gets reset
    InputStream source = new ByteArrayInputStream("<a>&foo;</a>".getBytes());
    InputStream entity = new ByteArrayInputStream("bar".getBytes());
    MockResolver resolver = new MockResolver();
    resolver.addEntity("foo", "foo", new InputSource(entity));
    Document d;
    try {
        db = dbf.newDocumentBuilder();
        db.setEntityResolver(resolver);
        db.reset();
        d = db.parse(source);
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
    Element root = (Element) d.getElementsByTagName("a").item(0);
    assertEquals("foo", ((EntityReference) root.getFirstChild()).getNodeName());
    // Make sure ErrorHandler gets reset
    source = new ByteArrayInputStream("</a>".getBytes());
    MethodLogger logger = new MethodLogger();
    ErrorHandler handler = new MockHandler(logger);
    try {
        db = dbf.newDocumentBuilder();
        db.setErrorHandler(handler);
        db.reset();
        d = db.parse(source);
    } catch (SAXParseException e) {
    // Expected
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
    assertEquals(0, logger.size());
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) MockHandler(org.apache.harmony.tests.org.xml.sax.support.MockHandler) MockResolver(org.apache.harmony.tests.org.xml.sax.support.MockResolver) Document(org.w3c.dom.Document) MethodLogger(org.apache.harmony.tests.org.xml.sax.support.MethodLogger) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Example 33 with InputSource

use of org.xml.sax.InputSource in project CoreNLP by stanfordnlp.

the class XMLUtils method readDocumentFromString.

// end class SAXErrorHandler
public static Document readDocumentFromString(String s) throws Exception {
    InputSource in = new InputSource(new StringReader(s));
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(false);
    return factory.newDocumentBuilder().parse(in);
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory)

Example 34 with InputSource

use of org.xml.sax.InputSource in project liquibase by liquibase.

the class LiquibaseEntityResolver method tryResolveFromResourceAccessor.

private InputSource tryResolveFromResourceAccessor(String systemId) {
    String path = FilenameUtils.concat(basePath, systemId);
    log.debug("Attempting to load " + systemId + " from resourceAccessor as " + path);
    try {
        InputStream resourceAsStream = StreamUtil.singleInputStream(path, resourceAccessor);
        if (resourceAsStream == null) {
            log.debug("Could not load " + systemId + " from resourceAccessor as " + path);
            return null;
        }
        return new InputSource(resourceAsStream);
    } catch (Exception ex) {
        return null;
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 35 with InputSource

use of org.xml.sax.InputSource in project liquibase by liquibase.

the class LiquibaseEntityResolver method resolveEntity.

@Override
public InputSource resolveEntity(String name, String publicId, String baseURI, String systemId) throws SAXException, IOException {
    log.debug("Resolving XML entity name='" + name + "', publicId='" + publicId + "', baseURI='" + baseURI + "', systemId='" + systemId + "'");
    if (systemId == null) {
        log.debug("Unable to resolve XML entity locally. Will load from network.");
        return null;
    }
    InputSource resolved = null;
    if (systemId.toLowerCase().endsWith(".xsd")) {
        if (systemId.startsWith("http://www.liquibase.org/xml/ns/migrator/")) {
            systemId = systemId.replace("http://www.liquibase.org/xml/ns/migrator/", "http://www.liquibase.org/xml/ns/dbchangelog/");
        }
        resolved = tryResolveLiquibaseSchema(systemId, publicId);
    }
    if (resolved == null && resourceAccessor != null && basePath != null) {
        resolved = tryResolveFromResourceAccessor(systemId);
    }
    if (resolved == null) {
        log.debug("Unable to resolve XML entity locally. Will load from network.");
    }
    return resolved;
}
Also used : InputSource(org.xml.sax.InputSource)

Aggregations

InputSource (org.xml.sax.InputSource)1126 StringReader (java.io.StringReader)403 IOException (java.io.IOException)304 Document (org.w3c.dom.Document)282 SAXException (org.xml.sax.SAXException)281 DocumentBuilder (javax.xml.parsers.DocumentBuilder)263 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)214 XMLReader (org.xml.sax.XMLReader)194 Test (org.junit.Test)160 InputStream (java.io.InputStream)158 NodeList (org.w3c.dom.NodeList)146 Element (org.w3c.dom.Element)144 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)143 ByteArrayInputStream (java.io.ByteArrayInputStream)103 SAXParser (javax.xml.parsers.SAXParser)103 SAXSource (javax.xml.transform.sax.SAXSource)95 SAXParserFactory (javax.xml.parsers.SAXParserFactory)90 File (java.io.File)82 Node (org.w3c.dom.Node)82 URL (java.net.URL)65