use of org.eclipse.rdf4j.rio.RDFParseException in project rdf4j by eclipse.
the class JSONLDInternalTripleCallback method triple.
private void triple(String s, String p, String value, String datatype, String language, String graph) {
if (s == null || p == null || value == null) {
// TODO: i don't know what to do here!!!!
return;
}
final Resource subject = createResource(s);
final IRI predicate = vf.createIRI(p);
final IRI datatypeURI = datatype == null ? null : vf.createIRI(datatype);
Value object;
try {
object = RDFParserHelper.createLiteral(value, language, datatypeURI, getParserConfig(), getParserErrorListener(), getValueFactory());
} catch (final RDFParseException e) {
throw new RuntimeException(e);
}
Statement result;
if (graph == null) {
result = vf.createStatement(subject, predicate, object);
} else {
result = vf.createStatement(subject, predicate, object, createResource(graph));
}
if (handler != null) {
try {
handler.handleStatement(result);
} catch (final RDFHandlerException e) {
throw new RuntimeException(e);
}
}
}
use of org.eclipse.rdf4j.rio.RDFParseException in project rdf4j by eclipse.
the class NQuadsParser method parseQuad.
private int parseQuad(int c) throws IOException, RDFParseException, RDFHandlerException {
boolean ignoredAnError = false;
try {
c = parseSubject(c);
c = skipWhitespace(c);
c = parsePredicate(c);
c = skipWhitespace(c);
c = parseObject(c);
c = skipWhitespace(c);
// Context is not required
if (c != '.') {
c = parseContext(c);
c = skipWhitespace(c);
}
if (c == -1) {
throwEOFException();
} else if (c != '.') {
reportFatalError("Expected '.', found: " + new String(Character.toChars(c)));
}
c = assertLineTerminates(c);
} 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;
}
}
c = skipLine(c);
if (!ignoredAnError) {
Statement st = createStatement(subject, predicate, object, context);
if (rdfHandler != null) {
rdfHandler.handleStatement(st);
}
}
subject = null;
predicate = null;
object = null;
context = null;
return c;
}
use of org.eclipse.rdf4j.rio.RDFParseException in project rdf4j by eclipse.
the class NTriplesParser method parseTriple.
private int parseTriple(int c) throws IOException, RDFParseException, RDFHandlerException {
boolean ignoredAnError = false;
try {
c = parseSubject(c);
c = skipWhitespace(c);
c = parsePredicate(c);
c = skipWhitespace(c);
c = parseObject(c);
c = skipWhitespace(c);
if (c == -1) {
throwEOFException();
} else if (c != '.') {
reportError("Expected '.', found: " + new String(Character.toChars(c)), NTriplesParserSettings.FAIL_ON_NTRIPLES_INVALID_LINES);
}
c = assertLineTerminates(c);
} 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;
}
}
c = skipLine(c);
if (!ignoredAnError) {
Statement st = createStatement(subject, predicate, object);
if (rdfHandler != null) {
rdfHandler.handleStatement(st);
}
}
subject = null;
predicate = null;
object = null;
return c;
}
use of org.eclipse.rdf4j.rio.RDFParseException in project rdf4j by eclipse.
the class RDFParserHelper method createLiteral.
/**
* Create a literal using the given parameters, including iterative verification and normalization by any
* {@link DatatypeHandler} or {@link LanguageHandler} implementations that are found in the
* {@link ParserConfig}.
*
* @param label
* The value for {@link Literal#getLabel()}, which may be iteratively normalized.
* @param lang
* If this is not null, and the datatype is either not null, or is equal to {@link RDF#LANGSTRING},
* then a language literal will be created.
* @param datatype
* If datatype is not null, and the datatype is not equal to {@link RDF#LANGSTRING} with a non-null
* lang, then a datatype literal will be created.
* @param parserConfig
* The source of parser settings, including the desired list of {@link DatatypeHandler} and
* {@link LanguageHandler}s to use for verification and normalization of datatype and language
* literals respectively.
* @param errListener
* The {@link ParseErrorListener} to use for signalling errors. This will be called if a setting is
* enabled by setting it to true in the {@link ParserConfig}, after which the error may trigger an
* {@link RDFParseException} if the setting is not present in
* {@link ParserConfig#getNonFatalErrors()}.
* @param valueFactory
* The {@link ValueFactory} to use for creating new {@link Literal}s using this method.
* @param lineNo
* Optional line number, should default to setting this as -1 if not known. Used for
* {@link ParseErrorListener#error(String, long, long)} and for
* {@link RDFParseException#RDFParseException(String, long, long)}.
* @param columnNo
* Optional column number, should default to setting this as -1 if not known. Used for
* {@link ParseErrorListener#error(String, long, long)} and for
* {@link RDFParseException#RDFParseException(String, long, long)}.
* @return A {@link Literal} created based on the given parameters.
* @throws RDFParseException
* If there was an error during the process that could not be recovered from, based on settings in
* the given parser config.
*/
public static final Literal createLiteral(String label, String lang, IRI datatype, ParserConfig parserConfig, ParseErrorListener errListener, ValueFactory valueFactory, long lineNo, long columnNo) throws RDFParseException {
if (label == null) {
throw new NullPointerException("Cannot create a literal using a null label");
}
Literal result = null;
String workingLabel = label;
Optional<String> workingLang = Optional.ofNullable(lang);
IRI workingDatatype = datatype;
// non-null lang
if (workingLang.isPresent() && (workingDatatype == null || RDF.LANGSTRING.equals(workingDatatype))) {
boolean recognisedLanguage = false;
for (LanguageHandler nextHandler : parserConfig.get(BasicParserSettings.LANGUAGE_HANDLERS)) {
if (nextHandler.isRecognizedLanguage(workingLang.get())) {
recognisedLanguage = true;
if (parserConfig.get(BasicParserSettings.VERIFY_LANGUAGE_TAGS)) {
try {
if (!nextHandler.verifyLanguage(workingLabel, workingLang.get())) {
reportError("'" + lang + "' is not a valid language tag ", lineNo, columnNo, BasicParserSettings.VERIFY_LANGUAGE_TAGS, parserConfig, errListener);
}
} catch (LiteralUtilException e) {
reportError("'" + label + " could not be verified by a language handler that recognised it. language was " + lang, lineNo, columnNo, BasicParserSettings.VERIFY_LANGUAGE_TAGS, parserConfig, errListener);
}
}
if (parserConfig.get(BasicParserSettings.NORMALIZE_LANGUAGE_TAGS)) {
try {
result = nextHandler.normalizeLanguage(workingLabel, workingLang.get(), valueFactory);
workingLabel = result.getLabel();
workingLang = result.getLanguage();
workingDatatype = result.getDatatype();
} catch (LiteralUtilException e) {
reportError("'" + label + "' did not have a valid value for language " + lang + ": " + e.getMessage() + " and could not be normalised", lineNo, columnNo, BasicParserSettings.NORMALIZE_LANGUAGE_TAGS, parserConfig, errListener);
}
}
}
}
if (!recognisedLanguage) {
reportError("'" + label + "' was not recognised as a language literal, and could not be verified, with language " + lang, lineNo, columnNo, BasicParserSettings.FAIL_ON_UNKNOWN_LANGUAGES, parserConfig, errListener);
}
} else if (workingDatatype != null) {
boolean recognisedDatatype = false;
for (DatatypeHandler nextHandler : parserConfig.get(BasicParserSettings.DATATYPE_HANDLERS)) {
if (nextHandler.isRecognizedDatatype(workingDatatype)) {
recognisedDatatype = true;
if (parserConfig.get(BasicParserSettings.VERIFY_DATATYPE_VALUES)) {
try {
if (!nextHandler.verifyDatatype(workingLabel, workingDatatype)) {
reportError("'" + label + "' is not a valid value for datatype " + datatype, lineNo, columnNo, BasicParserSettings.VERIFY_DATATYPE_VALUES, parserConfig, errListener);
}
} catch (LiteralUtilException e) {
reportError("'" + label + " could not be verified by a datatype handler that recognised it. datatype was " + datatype, lineNo, columnNo, BasicParserSettings.VERIFY_DATATYPE_VALUES, parserConfig, errListener);
}
}
if (parserConfig.get(BasicParserSettings.NORMALIZE_DATATYPE_VALUES)) {
try {
result = nextHandler.normalizeDatatype(workingLabel, workingDatatype, valueFactory);
workingLabel = result.getLabel();
workingLang = result.getLanguage();
workingDatatype = result.getDatatype();
} catch (LiteralUtilException e) {
reportError("'" + label + "' is not a valid value for datatype " + datatype + ": " + e.getMessage() + " and could not be normalised", lineNo, columnNo, BasicParserSettings.NORMALIZE_DATATYPE_VALUES, parserConfig, errListener);
}
}
}
}
if (!recognisedDatatype) {
reportError("'" + label + "' was not recognised, and could not be verified, with datatype " + datatype, lineNo, columnNo, BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES, parserConfig, errListener);
}
}
if (result == null) {
try {
// Backup for unnormalised language literal creation
if (workingLang.isPresent() && (workingDatatype == null || RDF.LANGSTRING.equals(workingDatatype))) {
result = valueFactory.createLiteral(workingLabel, workingLang.get().intern());
} else // Backup for unnormalised datatype literal creation
if (workingDatatype != null) {
result = valueFactory.createLiteral(workingLabel, workingDatatype);
} else {
result = valueFactory.createLiteral(workingLabel, XMLSchema.STRING);
}
} catch (Exception e) {
reportFatalError(e, lineNo, columnNo, errListener);
}
}
return result;
}
use of org.eclipse.rdf4j.rio.RDFParseException in project opentheso by miledrousset.
the class ReadRdfFile_rdf4j method readFile.
// TODO add test methods here.
// The methods must be annotated with annotation @Test. For example:
//
@Test
public void readFile() {
File file = new File("/Users/Miled/Desktop/Bureau2/test_unesco.rdf");
RDFParser rdfParser = Rio.createParser(RDFFormat.RDFXML);
Model model = new LinkedHashModel();
rdfParser.setRDFHandler(new StatementCollector(model));
Rio.write(model, System.out, RDFFormat.TURTLE);
try {
rdfParser.parse(new FileReader(file), "http://example.org");
/* for (Statement statement : model) {
// writeLine(statement.getObject().stringValue(), statement.getSubject().stringValue());
System.out.println("LocalName = " + statement.getPredicate().getLocalName());
System.out.println("objet = " + statement.getObject().stringValue());
System.out.println("predicat = " + statement.getPredicate());
System.out.println("URI = " + statement.getSubject());
System.out.println("");
// model.getNamespace(statement.getClass().getgetObject().stringValue());
} */
for (Statement statement2 : model) {
Literal literal = (SimpleLiteral) statement2;
System.out.println(literal.getLabel());
// System.out.println(literal.getLanguage());
System.out.println(literal.getDatatype());
}
} catch (IOException e) {
// handle IO problems (e.g. the file could not be read)
} catch (RDFParseException e) {
// handle unrecoverable parse error
} catch (RDFHandlerException e) {
// handle a problem encountered by the RDFHandler
}
}
Aggregations