Search in sources :

Example 41 with SAXParseException

use of org.xml.sax.SAXParseException in project robovm by robovm.

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(tests.api.org.xml.sax.support.MockHandler) MockResolver(tests.api.org.xml.sax.support.MockResolver) Document(org.w3c.dom.Document) MethodLogger(tests.api.org.xml.sax.support.MethodLogger) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Example 42 with SAXParseException

use of org.xml.sax.SAXParseException in project robovm by robovm.

the class DocumentBuilderTest method testSetErrorHandler.

public void testSetErrorHandler() {
    // Ordinary case
    InputStream source = new ByteArrayInputStream("</a>".getBytes());
    MethodLogger logger = new MethodLogger();
    ErrorHandler handler = new MockHandler(logger);
    try {
        db = dbf.newDocumentBuilder();
        db.setErrorHandler(handler);
        db.parse(source);
    } catch (SAXParseException e) {
    // Expected, ErrorHandler does not mask exception
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
    assertEquals("error", logger.getMethod());
    assertTrue(logger.getArgs()[0] instanceof SAXParseException);
    // null case
    source = new ByteArrayInputStream("</a>".getBytes());
    try {
        db = dbf.newDocumentBuilder();
        db.setErrorHandler(null);
        db.parse(source);
    } catch (SAXParseException e) {
    // Expected
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SAXParseException(org.xml.sax.SAXParseException) MockHandler(tests.api.org.xml.sax.support.MockHandler) MethodLogger(tests.api.org.xml.sax.support.MethodLogger) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Example 43 with SAXParseException

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

the class DocumentBuilderTest method testSetErrorHandler.

public void testSetErrorHandler() {
    // Ordinary case
    InputStream source = new ByteArrayInputStream("</a>".getBytes());
    MethodLogger logger = new MethodLogger();
    ErrorHandler handler = new MockHandler(logger);
    try {
        db = dbf.newDocumentBuilder();
        db.setErrorHandler(handler);
        db.parse(source);
    } catch (SAXParseException e) {
    // Expected, ErrorHandler does not mask exception
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
    assertEquals("error", logger.getMethod());
    assertTrue(logger.getArgs()[0] instanceof SAXParseException);
    // null case
    source = new ByteArrayInputStream("</a>".getBytes());
    try {
        db = dbf.newDocumentBuilder();
        db.setErrorHandler(null);
        db.parse(source);
    } catch (SAXParseException e) {
    // Expected
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SAXParseException(org.xml.sax.SAXParseException) MockHandler(org.apache.harmony.tests.org.xml.sax.support.MockHandler) 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 44 with SAXParseException

use of org.xml.sax.SAXParseException 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 45 with SAXParseException

use of org.xml.sax.SAXParseException in project gocd by gocd.

the class XmlSaxParserContext method parse_with.

@JRubyMethod
public IRubyObject parse_with(ThreadContext context, IRubyObject handlerRuby) {
    Ruby ruby = context.getRuntime();
    if (!invoke(context, handlerRuby, "respond_to?", ruby.newSymbol("document")).isTrue()) {
        String msg = "argument must respond_to document";
        throw ruby.newArgumentError(msg);
    }
    handler = new NokogiriHandler(ruby, handlerRuby);
    preParse(context, handlerRuby, handler);
    setContentHandler(handler);
    setErrorHandler(handler);
    try {
        setProperty("http://xml.org/sax/properties/lexical-handler", handler);
    } catch (Exception ex) {
        throw ruby.newRuntimeError("Problem while creating XML SAX Parser: " + ex.toString());
    }
    try {
        try {
            do_parse();
        } catch (SAXParseException spe) {
            // A bad document (<foo><bar></foo>) should call the
            // error handler instead of raising a SAX exception.
            // However, an EMPTY document should raise a
            // RuntimeError.  This is a bit kludgy, but AFAIK SAX
            // doesn't distinguish between empty and bad whereas
            // Nokogiri does.
            String message = spe.getMessage();
            if ("Premature end of file.".matches(message) && stringDataSize < 1) {
                throw ruby.newRuntimeError("couldn't parse document: " + message);
            } else {
                handler.error(spe);
            }
        }
    } catch (SAXException se) {
        throw RaiseException.createNativeRaiseException(ruby, se);
    } catch (IOException ioe) {
        throw ruby.newIOErrorFromException(ioe);
    }
    postParse(context, handlerRuby, handler);
    return ruby.getNil();
}
Also used : SAXParseException(org.xml.sax.SAXParseException) NokogiriHandler(nokogiri.internals.NokogiriHandler) IOException(java.io.IOException) Ruby(org.jruby.Ruby) RaiseException(org.jruby.exceptions.RaiseException) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

SAXParseException (org.xml.sax.SAXParseException)365 SAXException (org.xml.sax.SAXException)170 IOException (java.io.IOException)131 DocumentBuilder (javax.xml.parsers.DocumentBuilder)73 InputSource (org.xml.sax.InputSource)69 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)68 Document (org.w3c.dom.Document)64 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)56 ErrorHandler (org.xml.sax.ErrorHandler)52 InputStream (java.io.InputStream)36 File (java.io.File)34 ArrayList (java.util.ArrayList)33 FileInputStream (java.io.FileInputStream)31 FileNotFoundException (java.io.FileNotFoundException)31 Element (org.w3c.dom.Element)30 StringReader (java.io.StringReader)21 NodeList (org.w3c.dom.NodeList)21 URL (java.net.URL)20 Test (org.junit.jupiter.api.Test)19 Node (org.w3c.dom.Node)19