Search in sources :

Example 61 with SAXParseException

use of org.xml.sax.SAXParseException in project jena by apache.

the class TestErrorMsg method check.

/**
	 * @param filename
	 *            Read this file
	 * @param regex
	 *            Error msg must match this.
	 *
	private void check(String filename, String regex)
		throws IOException, MalformedPatternException, SAXException {
		check(filename, regex, null);
	}
	*/
private void check(String filename, String regexPresent, String regexAbsent) throws IOException {
    final StringBuffer buf = new StringBuffer();
    ARP arp = new ARP();
    arp.getHandlers().setErrorHandler(new ErrorHandler() {

        @Override
        public void warning(SAXParseException exception) {
            buf.append(exception.getMessage());
            buf.append("\n");
        }

        @Override
        public void error(SAXParseException e) {
            warning(e);
        }

        @Override
        public void fatalError(SAXParseException e) {
            warning(e);
        }
    });
    try (InputStream in = new FileInputStream("testing/arp/error-msgs/" + filename + ".rdf")) {
        arp.load(in, "file:///" + filename);
    } catch (SAXException e) {
    }
    String contents = buf.toString();
    if (regexPresent != null)
        assertTrue("Should find /" + regexPresent + "/", Pattern.compile(regexPresent, Pattern.DOTALL).matcher(contents).find());
    if (regexAbsent != null)
        assertTrue("Should not find /" + regexAbsent + "/", !Pattern.compile(regexAbsent, Pattern.DOTALL).matcher(contents).find());
    contents = null;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) SAXParseException(org.xml.sax.SAXParseException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) ARP(org.apache.jena.rdfxml.xmlinput.ARP) SAXException(org.xml.sax.SAXException)

Example 62 with SAXParseException

use of org.xml.sax.SAXParseException in project jena by apache.

the class MoreTests method testInterrupt.

public void testInterrupt() throws SAXException, IOException {
    ARP a = new ARP();
    try (InputStream in = new FileInputStream("testing/wg/miscellaneous/consistent001.rdf")) {
        a.getHandlers().setStatementHandler(new StatementHandler() {

            int countDown = 10;

            @Override
            public void statement(AResource subj, AResource pred, AResource obj) {
                if (countDown-- == 0)
                    Thread.currentThread().interrupt();
            }

            @Override
            public void statement(AResource subj, AResource pred, ALiteral lit) {
            }
        });
        a.getHandlers().setErrorHandler(new ErrorHandler() {

            @Override
            public void error(SAXParseException exception) throws SAXException {
                throw new RuntimeException("Unexpected error", exception);
            }

            @Override
            public void fatalError(SAXParseException exception) throws SAXException {
                throw exception;
            }

            @Override
            public void warning(SAXParseException exception) throws SAXException {
                throw new RuntimeException("Unexpected warning", exception);
            }
        });
        try {
            a.load(in);
            fail("Thread was not interrupted.");
        } catch (InterruptedIOException | SAXParseException e) {
        }
    }
// System.err.println("Finished "+Thread.interrupted());
}
Also used : RDFErrorHandler(org.apache.jena.rdf.model.RDFErrorHandler) ErrorHandler(org.xml.sax.ErrorHandler) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException)

Example 63 with SAXParseException

use of org.xml.sax.SAXParseException in project jmeter by apache.

the class XMLSchemaAssertion method setSchemaResult.

/**
     * set Schema result
     * 
     * @param result
     * @param xmlStr
     * @param xsdFileName
     */
private void setSchemaResult(AssertionResult result, String xmlStr, String xsdFileName) {
    try {
        DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance();
        parserFactory.setValidating(true);
        parserFactory.setNamespaceAware(true);
        parserFactory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
        parserFactory.setAttribute(JAXP_SCHEMA_SOURCE, xsdFileName);
        // create a parser:
        DocumentBuilder parser = parserFactory.newDocumentBuilder();
        parser.setErrorHandler(new SAXErrorHandler(result));
        parser.parse(new InputSource(new StringReader(xmlStr)));
    // if everything went fine then xml schema validation is valid
    } catch (SAXParseException e) {
        // Only set message if error not yet flagged
        if (!result.isError() && !result.isFailure()) {
            result.setError(true);
            result.setFailureMessage(errorDetails(e));
        }
    } catch (SAXException e) {
        if (log.isWarnEnabled()) {
            log.warn(e.toString());
        }
        result.setResultForFailure(e.getMessage());
    } catch (IOException e) {
        log.warn("IO error", e);
        result.setResultForFailure(e.getMessage());
    } catch (ParserConfigurationException e) {
        log.warn("Problem with Parser Config", e);
        result.setResultForFailure(e.getMessage());
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 64 with SAXParseException

use of org.xml.sax.SAXParseException in project jena by apache.

the class XMLHandler method generalError.

void generalError(int id, Exception e) throws SAXParseException {
    ARPLocation where = new ARPLocation(locator);
    // System.err.println(e.getMessage());
    warning(null, id, new ParseException(id, where, e));
}
Also used : SAXParseException(org.xml.sax.SAXParseException)

Example 65 with SAXParseException

use of org.xml.sax.SAXParseException in project jena by apache.

the class ParseException method formatMessage.

/**
     * Calls e.getMessage() and also accesses line and column information for
     * SAXParseException's.
     * 
     * @return e.getMessage() possibly prepended by error location information.
     * @param e
     *            The exception to describe.
     */
public static String formatMessage(Exception e) {
    String msg = e.getMessage();
    if (msg == null)
        msg = e.toString();
    if (!(e instanceof SAXParseException))
        return msg;
    SAXParseException sax = (SAXParseException) e;
    String file = sax.getSystemId();
    if (file == null)
        file = sax.getPublicId();
    String rslt = file == null ? "" : file;
    if (sax.getLineNumber() == -1)
        return (file != null ? (file + ": ") : "") + msg;
    if (sax.getColumnNumber() == -1) {
        return rslt + "(line " + sax.getLineNumber() + "): " + msg;
    }
    return rslt + "(line " + sax.getLineNumber() + " column " + sax.getColumnNumber() + "): " + msg;
}
Also used : SAXParseException(org.xml.sax.SAXParseException)

Aggregations

SAXParseException (org.xml.sax.SAXParseException)206 SAXException (org.xml.sax.SAXException)109 IOException (java.io.IOException)75 DocumentBuilder (javax.xml.parsers.DocumentBuilder)53 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)47 Document (org.w3c.dom.Document)47 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)42 InputSource (org.xml.sax.InputSource)39 ErrorHandler (org.xml.sax.ErrorHandler)34 Test (org.junit.Test)28 Element (org.w3c.dom.Element)24 InputStream (java.io.InputStream)20 ArrayList (java.util.ArrayList)18 NodeList (org.w3c.dom.NodeList)18 FileInputStream (java.io.FileInputStream)17 FileNotFoundException (java.io.FileNotFoundException)17 File (java.io.File)16 Node (org.w3c.dom.Node)13 StringReader (java.io.StringReader)12 URL (java.net.URL)11