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);
}
}
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();
}
}
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));
}
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);
}
}
}
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;
}
Aggregations