|
6 | 6 | import org.apache.jena.datatypes.DatatypeFormatException; |
7 | 7 | import org.apache.jena.query.*; |
8 | 8 | import org.apache.jena.rdf.model.*; |
| 9 | +import org.apache.jena.riot.Lang; |
9 | 10 | import org.apache.jena.riot.RDFDataMgr; |
10 | 11 | import org.apache.jena.riot.RDFLanguages; |
11 | 12 | import org.apache.jena.riot.RiotException; |
@@ -969,17 +970,25 @@ <T> T parseMessage(String message, Class<T> targetClass) throws IOException { |
969 | 970 | private Model readMessage(String message) throws IOException { |
970 | 971 |
|
971 | 972 | 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 | + } |
976 | 987 | } |
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; |
980 | 990 | } |
981 | | - |
982 | | - return targetModel; |
| 991 | + throw new IOException("Could not parse string as any supported RDF format (JSON-LD, Turtle/N-Triple, RDF-XML)."); |
983 | 992 | } |
984 | 993 |
|
985 | 994 |
|
|
0 commit comments