Search in sources :

Example 46 with RDFParseException

use of org.eclipse.rdf4j.rio.RDFParseException in project rdf4j by eclipse.

the class SAXFilter method startElement.

@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes attributes) throws SAXException {
    try {
        if (deferredElement != null) {
            // The next call could set parseLiteralMode to true!
            reportDeferredStartElement();
        }
        if (parseLiteralMode) {
            appendStartTag(qName, attributes);
            xmlLiteralStackHeight++;
        } else {
            ElementInfo parent = peekStack();
            ElementInfo elInfo = new ElementInfo(parent, qName, namespaceURI, localName);
            elInfo.setNamespaceMappings(newNamespaceMappings);
            newNamespaceMappings.clear();
            if (!inRDFContext && parseStandAloneDocuments && (!localName.equals("RDF") || !namespaceURI.equals(RDF.NAMESPACE))) {
                // Stand-alone document that does not start with an rdf:RDF root
                // element. Assume this root element is omitted.
                inRDFContext = true;
            }
            if (!inRDFContext) {
                // Check for presence of xml:base and xlm:lang attributes.
                for (int i = 0; i < attributes.getLength(); i++) {
                    String attQName = attributes.getQName(i);
                    if ("xml:base".equals(attQName)) {
                        elInfo.setBaseURI(attributes.getValue(i));
                    } else if ("xml:lang".equals(attQName)) {
                        elInfo.xmlLang = attributes.getValue(i);
                    }
                }
                elInfoStack.push(elInfo);
                // Check if we are entering RDF context now.
                if (localName.equals("RDF") && namespaceURI.equals(RDF.NAMESPACE)) {
                    inRDFContext = true;
                    rdfContextStackHeight = 0;
                }
            } else {
                // We're parsing RDF elements.
                checkAndCopyAttributes(attributes, elInfo);
                // Don't report the new element to the RDF parser just yet.
                deferredElement = elInfo;
            }
            charBuf.setLength(0);
        }
    } catch (RDFParseException e) {
        throw new SAXException(e);
    } catch (RDFHandlerException e) {
        throw new SAXException(e);
    }
}
Also used : RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) SAXException(org.xml.sax.SAXException)

Example 47 with RDFParseException

use of org.eclipse.rdf4j.rio.RDFParseException in project rdf4j by eclipse.

the class TriXParser method parse.

private void parse(InputSource inputStreamOrReader) throws IOException, RDFParseException, RDFHandlerException {
    clear();
    try {
        if (rdfHandler != null) {
            rdfHandler.startRDF();
        }
        XMLReader xmlReader;
        if (getParserConfig().isSet(XMLParserSettings.CUSTOM_XML_READER)) {
            xmlReader = getParserConfig().get(XMLParserSettings.CUSTOM_XML_READER);
        } else {
            xmlReader = XMLReaderFactory.createXMLReader();
        }
        xmlReader.setErrorHandler(this);
        saxParser = new SimpleSAXParser(xmlReader);
        saxParser.setPreserveWhitespace(true);
        saxParser.setListener(new TriXSAXHandler());
        saxParser.parse(inputStreamOrReader);
    } catch (SAXParseException e) {
        Exception wrappedExc = e.getException();
        if (wrappedExc == null) {
            reportFatalError(e, e.getLineNumber(), e.getColumnNumber());
        } else {
            reportFatalError(wrappedExc, e.getLineNumber(), e.getColumnNumber());
        }
    } catch (SAXException e) {
        Exception wrappedExc = e.getException();
        if (wrappedExc == null) {
            reportFatalError(e);
        } else if (wrappedExc instanceof RDFParseException) {
            throw (RDFParseException) wrappedExc;
        } else if (wrappedExc instanceof RDFHandlerException) {
            throw (RDFHandlerException) wrappedExc;
        } else {
            reportFatalError(wrappedExc);
        }
    } finally {
        clear();
    }
    if (rdfHandler != null) {
        rdfHandler.endRDF();
    }
}
Also used : SimpleSAXParser(org.eclipse.rdf4j.common.xml.SimpleSAXParser) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) SAXParseException(org.xml.sax.SAXParseException) XMLReader(org.xml.sax.XMLReader) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 48 with RDFParseException

use of org.eclipse.rdf4j.rio.RDFParseException in project rdf4j by eclipse.

the class TriXParserTest method testFatalErrorPrologContent.

@Test
public void testFatalErrorPrologContent() throws Exception {
    // Temporarily override System.err to verify that nothing is being
    // printed to it for this test
    PrintStream oldErr = System.err;
    ByteArrayOutputStream tempErr = new ByteArrayOutputStream();
    System.setErr(new PrintStream(tempErr));
    PrintStream oldOut = System.out;
    ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
    System.setOut(new PrintStream(tempOut));
    try (final InputStream in = this.getClass().getResourceAsStream("/org/eclipse/rdf4j/rio/trix/not-a-trix-file.trix")) {
        parser.parse(in, "");
    } catch (RDFParseException e) {
        assertEquals("Content is not allowed in prolog. [line 1, column 1]", e.getMessage());
    } finally {
        // Reset System Error output to ensure that we don't interfere with
        // other tests
        System.setErr(oldErr);
        // Reset System Out output to ensure that we don't interfere with
        // other tests
        System.setOut(oldOut);
    }
    // Verify nothing was printed to System.err during test
    assertEquals(0, tempErr.size());
    // Verify nothing was printed to System.out during test
    assertEquals(0, tempOut.size());
    assertEquals(0, el.getWarnings().size());
    assertEquals(0, el.getErrors().size());
    assertEquals(1, el.getFatalErrors().size());
    assertEquals("[Rio fatal] Content is not allowed in prolog. (1, 1)", el.getFatalErrors().get(0));
}
Also used : PrintStream(java.io.PrintStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) Test(org.junit.Test)

Example 49 with RDFParseException

use of org.eclipse.rdf4j.rio.RDFParseException in project timbuctoo by HuygensING.

the class Rdf4jRdfParser method importRdf.

@Override
public void importRdf(CachedLog input, String baseUri, String defaultGraph, RdfProcessor rdfProcessor) throws RdfProcessingFailedException {
    try {
        RDFFormat format = Rio.getParserFormatForMIMEType(input.getMimeType().toString()).orElseThrow(() -> new UnsupportedRDFormatException(input.getMimeType() + " is not a supported rdf type."));
        RDFParser rdfParser = Rio.createParser(format);
        rdfParser.setPreserveBNodeIDs(true);
        rdfParser.setRDFHandler(new TimRdfHandler(rdfProcessor, defaultGraph, input.getFile().getName()));
        rdfParser.parse(input.getReader(), baseUri);
    } catch (IOException | RDFParseException | UnsupportedRDFormatException e) {
        throw new RdfProcessingFailedException(e);
    } catch (RDFHandlerException e) {
        if (e.getCause() instanceof RdfProcessingFailedException) {
            throw (RdfProcessingFailedException) e.getCause();
        } else {
            throw new RdfProcessingFailedException(e);
        }
    }
}
Also used : UnsupportedRDFormatException(org.eclipse.rdf4j.rio.UnsupportedRDFormatException) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) TimRdfHandler(nl.knaw.huygens.timbuctoo.v5.rdfio.implementations.rdf4j.parsers.TimRdfHandler) IOException(java.io.IOException) RDFParser(org.eclipse.rdf4j.rio.RDFParser) RdfProcessingFailedException(nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.RdfProcessingFailedException) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 50 with RDFParseException

use of org.eclipse.rdf4j.rio.RDFParseException in project timbuctoo by HuygensING.

the class NquadsUdParser method parseQuad.

private int parseQuad(int character) throws IOException, RDFParseException, RDFHandlerException {
    boolean ignoredAnError = false;
    try {
        character = parseSubject(character);
        character = skipWhitespace(character);
        character = parsePredicate(character);
        character = skipWhitespace(character);
        character = parseObject(character);
        character = skipWhitespace(character);
        // Context is not required
        if (character != '.') {
            character = parseContext(character);
            character = skipWhitespace(character);
        }
        if (character == -1) {
            throwEOFException();
        } else if (character != '.') {
            reportFatalError("Expected '.', found: " + new String(Character.toChars(character)));
        }
        character = assertLineTerminates(character);
    } catch (RDFParseException rdfpe) {
        if (getParserConfig().isNonFatalError(NTriplesParserSettings.FAIL_ON_NTRIPLES_INVALID_LINES)) {
            reportError(rdfpe, NTriplesParserSettings.FAIL_ON_NTRIPLES_INVALID_LINES);
            ignoredAnError = true;
        } else {
            throw rdfpe;
        }
    }
    character = skipLine(character);
    if (!ignoredAnError) {
        Statement st = createStatement(subject, predicate, object, context);
        if (rdfHandler != null) {
            rdfHandler.handleStatement(st);
        }
    }
    subject = null;
    predicate = null;
    object = null;
    context = null;
    return character;
}
Also used : Statement(org.eclipse.rdf4j.model.Statement) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Aggregations

RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)50 Test (org.junit.Test)22 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)19 IOException (java.io.IOException)16 StringReader (java.io.StringReader)13 StatementCollector (org.eclipse.rdf4j.rio.helpers.StatementCollector)9 RDFLoader (org.eclipse.rdf4j.repository.util.RDFLoader)8 Model (org.eclipse.rdf4j.model.Model)7 RDFParser (org.eclipse.rdf4j.rio.RDFParser)7 Statement (org.eclipse.rdf4j.model.Statement)6 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)6 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)5 RDFFormat (org.eclipse.rdf4j.rio.RDFFormat)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 InputStream (java.io.InputStream)4 Reader (java.io.Reader)4 RDFInserter (org.eclipse.rdf4j.repository.util.RDFInserter)4 SAXException (org.xml.sax.SAXException)4 IRI (org.eclipse.rdf4j.model.IRI)3 Literal (org.eclipse.rdf4j.model.Literal)3