Skip to content
This repository was archived by the owner on Feb 15, 2024. It is now read-only.

Commit 9034559

Browse files
author
Matthias Böckmann
committed
Support multiple RDF formats
1 parent 7019b1d commit 9034559

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

  • dataformat-jsonld/src/main/java/io/adminshell/aas/v3/dataformat/jsonld

dataformat-jsonld/src/main/java/io/adminshell/aas/v3/dataformat/jsonld/Parser.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.apache.jena.datatypes.DatatypeFormatException;
77
import org.apache.jena.query.*;
88
import org.apache.jena.rdf.model.*;
9+
import org.apache.jena.riot.Lang;
910
import org.apache.jena.riot.RDFDataMgr;
1011
import org.apache.jena.riot.RDFLanguages;
1112
import org.apache.jena.riot.RiotException;
@@ -969,17 +970,25 @@ <T> T parseMessage(String message, Class<T> targetClass) throws IOException {
969970
private Model readMessage(String message) throws IOException {
970971

971972
Model targetModel = ModelFactory.createDefaultModel();
972-
973-
//Read incoming message to the same model
974-
try {
975-
RDFDataMgr.read(targetModel, new ByteArrayInputStream(message.getBytes()), RDFLanguages.JSONLD);
973+
List<Lang> supportedLanguages = new ArrayList<>(
974+
Arrays.asList(
975+
RDFLanguages.JSONLD, //JSON-LD first
976+
RDFLanguages.TURTLE, //N-TRIPLE is a subset of Turtle
977+
RDFLanguages.RDFXML
978+
));
979+
980+
boolean successfullyParsed = false;
981+
for (Lang lang : supportedLanguages) {
982+
try {
983+
RDFDataMgr.read(targetModel, new ByteArrayInputStream(message.getBytes()), lang);
984+
successfullyParsed = true; //Only set, if no exception occurred
985+
} catch (RiotException ignored) {
986+
}
976987
}
977-
catch (RiotException e)
978-
{
979-
throw new IOException("The message is no valid JSON-LD and therefore could not be parsed.", e);
988+
if (successfullyParsed) {
989+
return targetModel;
980990
}
981-
982-
return targetModel;
991+
throw new IOException("Could not parse string as any supported RDF format (JSON-LD, Turtle/N-Triple, RDF-XML).");
983992
}
984993

985994

0 commit comments

Comments
 (0)