0

I have a org.apache.jena.riot.RiotException: Bad character in IRI (space) exception when reading an OWL/RDL model in Jena.

I know that it's because I have a space in an IRI declaration in an XML namespace declaration (which is not allowed), but as I am using Jena in an open Source tool I developed to create the graph corresponding to an ontology, I would like to know the number of the line in the (XML) model where the error occur. Is it possible in Jena when catching this exception?

Hervé Girod
  • 465
  • 3
  • 12
  • 1
    "an IRI declaration" - that's a XML namepsace declaration? These are handled by the XML parser (normally the JDK XML parser) so information is hard to access. But when the first usage occurs, Jena sends a warning. You can parse with `RDFParser.source(...).errorHandler(errHandler).toModel()` and provide your own `ErrorHandler` to capture the line/column of the warning by your application. (Turtle parsing is not layered on another system. It will show an error at the PREFIX declaration.) – AndyS Jan 20 '23 at 23:00
  • Hello, Thanks, in my particular case it shows the error in line -1, but the mechanism work! – Hervé Girod Jan 22 '23 at 13:54
  • The answer assumes that by IRI declaration you're talking about an XML namespace declaration. – AndyS Jan 22 '23 at 18:11
  • Yes it was the case, I will update the question to be more clear about that :) – Hervé Girod Jan 22 '23 at 18:44

1 Answers1

2

You can supply an error handler using

RDFParser.source("filename").errorHandler(...).toModel();

It will actually be warning from the parser (the error is generated later when the line number isn't available)

To change it to an error:

  Map<String, Object> properties = new HashMap<>();
  properties.put("WARN_BAD_NAMESPACE_URI", "EM_ERROR");
  Context cxt = new Context().set(SysRIOT.sysRdfReaderProperties, properties);
  RDFParser.source(filename)
           .context(cxt)
           .errorHandler(....)
           .toModel();
AndyS
  • 16,345
  • 17
  • 21