Search in sources :

Example 41 with RDFParseException

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

the class TestTurtleParser method testParseBooleanLiteralWhitespaceComma.

@Test
public void testParseBooleanLiteralWhitespaceComma() throws Exception {
    String data = "<urn:a> <urn:b> true , false .";
    Reader r = new StringReader(data);
    try {
        parser.parse(r, baseURI);
        assertTrue(statementCollector.getStatements().size() == 2);
    } catch (RDFParseException e) {
        fail("parse error on correct data: " + e.getMessage());
    }
}
Also used : StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) Test(org.junit.Test)

Example 42 with RDFParseException

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

the class TestTurtleParser method testParseBooleanLiteralSemicolumn.

@Test
public void testParseBooleanLiteralSemicolumn() throws Exception {
    String data = "<urn:a> <urn:b> true; <urn:c> false .";
    Reader r = new StringReader(data);
    try {
        parser.parse(r, baseURI);
        assertTrue(statementCollector.getStatements().size() == 2);
    } catch (RDFParseException e) {
        fail("parse error on correct data: " + e.getMessage());
    }
}
Also used : StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) Test(org.junit.Test)

Example 43 with RDFParseException

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

the class TestTurtleParser method testParseIllegalURIFatal.

@Test
public void testParseIllegalURIFatal() throws Exception {
    String data = " <urn:foo_bar\\r> <urn:foo> <urn:bar> ; <urn:foo2> <urn:bar2> . <urn:foobar> <urn:food> <urn:barf> . ";
    try {
        parser.parse(new StringReader(data), baseURI);
        fail("default config should result in fatal error / parse exception");
    } catch (RDFParseException e) {
    // expected
    }
}
Also used : StringReader(java.io.StringReader) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) Test(org.junit.Test)

Example 44 with RDFParseException

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

the class RDFXMLParser method parse.

private void parse(InputSource inputSource) throws IOException, RDFParseException, RDFHandlerException {
    clear();
    try {
        documentURI = inputSource.getSystemId();
        saxFilter.setParseStandAloneDocuments(getParserConfig().get(XMLParserSettings.PARSE_STANDALONE_DOCUMENTS));
        // saxFilter.clear();
        saxFilter.setDocumentURI(documentURI);
        XMLReader xmlReader;
        if (getParserConfig().isSet(XMLParserSettings.CUSTOM_XML_READER)) {
            xmlReader = getParserConfig().get(XMLParserSettings.CUSTOM_XML_READER);
        } else {
            xmlReader = XMLReaderFactory.createXMLReader();
        }
        xmlReader.setContentHandler(saxFilter);
        xmlReader.setErrorHandler(this);
        // not explicitly set
        for (RioSetting<Boolean> aSetting : getCompulsoryXmlFeatureSettings()) {
            try {
                xmlReader.setFeature(aSetting.getKey(), getParserConfig().get(aSetting));
            } catch (SAXNotRecognizedException e) {
                reportWarning(String.format("%s is not a recognized SAX feature.", aSetting.getKey()));
            } catch (SAXNotSupportedException e) {
                reportWarning(String.format("%s is not a supported SAX feature.", aSetting.getKey()));
            }
        }
        // not explicitly set
        for (RioSetting<?> aSetting : getCompulsoryXmlPropertySettings()) {
            try {
                xmlReader.setProperty(aSetting.getKey(), getParserConfig().get(aSetting));
            } catch (SAXNotRecognizedException e) {
                reportWarning(String.format("%s is not a recognized SAX property.", aSetting.getKey()));
            } catch (SAXNotSupportedException e) {
                reportWarning(String.format("%s is not a supported SAX property.", aSetting.getKey()));
            }
        }
        // the parser config
        for (RioSetting<Boolean> aSetting : getOptionalXmlFeatureSettings()) {
            try {
                if (getParserConfig().isSet(aSetting)) {
                    xmlReader.setFeature(aSetting.getKey(), getParserConfig().get(aSetting));
                }
            } catch (SAXNotRecognizedException e) {
                reportWarning(String.format("%s is not a recognized SAX feature.", aSetting.getKey()));
            } catch (SAXNotSupportedException e) {
                reportWarning(String.format("%s is not a supported SAX feature.", aSetting.getKey()));
            }
        }
        // the parser config
        for (RioSetting<?> aSetting : getOptionalXmlPropertySettings()) {
            try {
                if (getParserConfig().isSet(aSetting)) {
                    xmlReader.setProperty(aSetting.getKey(), getParserConfig().get(aSetting));
                }
            } catch (SAXNotRecognizedException e) {
                reportWarning(String.format("%s is not a recognized SAX property.", aSetting.getKey()));
            } catch (SAXNotSupportedException e) {
                reportWarning(String.format("%s is not a supported SAX property.", aSetting.getKey()));
            }
        }
        xmlReader.parse(inputSource);
    } 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 {
        // Clean up
        saxFilter.clear();
        xmlLang = null;
        elementStack.clear();
        usedIDs.clear();
        clear();
    }
}
Also used : SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) SAXParseException(org.xml.sax.SAXParseException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) 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 45 with RDFParseException

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

the class SAXFilter method endElement.

@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
    try {
        if (rdfParser.getParserConfig().get(XMLParserSettings.FAIL_ON_MISMATCHED_TAGS) && !parseLiteralMode) {
            // Verify that the end tag matches the start tag.
            ElementInfo elInfo;
            if (deferredElement != null) {
                elInfo = deferredElement;
            } else {
                elInfo = peekStack();
            }
            if (!qName.equals(elInfo.qName)) {
                rdfParser.reportError("expected end tag </'" + elInfo.qName + ">, found </" + qName + ">", XMLParserSettings.FAIL_ON_MISMATCHED_TAGS);
            }
        }
        if (!inRDFContext) {
            elInfoStack.pop();
            charBuf.setLength(0);
            return;
        }
        if (deferredElement == null && rdfContextStackHeight == 0) {
            // This end tag removes the element that signaled the start
            // of the RDF context (i.e. <rdf:RDF>) from the stack.
            inRDFContext = false;
            elInfoStack.pop();
            charBuf.setLength(0);
            return;
        }
        if (parseLiteralMode && xmlLiteralStackHeight > 0) {
            appendEndTag(qName);
            xmlLiteralStackHeight--;
            return;
        }
        // Check for any deferred start elements
        if (deferredElement != null) {
            // Start element still deferred, this is an empty element
            rdfParser.setBaseURI(deferredElement.baseURI.toString());
            rdfParser.setXMLLang(deferredElement.xmlLang);
            rdfParser.emptyElement(deferredElement.namespaceURI, deferredElement.localName, deferredElement.qName, deferredElement.atts);
            deferredElement = null;
        } else {
            if (parseLiteralMode) {
                // Insert any used namespace prefixes from the XML literal's
                // context that are not defined in the XML literal itself.
                insertUsedContextPrefixes();
                rdfParser.text(charBuf.toString());
                parseLiteralMode = false;
            } else {
                String s = charBuf.toString();
                // ignore whitespace-only nodes
                if (s.trim().length() > 0) {
                    rdfParser.text(s);
                }
            }
            charBuf.setLength(0);
            // Handle the end tag
            elInfoStack.pop();
            rdfContextStackHeight--;
            rdfParser.endElement(namespaceURI, localName, qName);
        }
    } 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)

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