Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T18011 JenaUtils.read support jsonld #214

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions cogni-libs/src/main/java/zone/cogni/libs/jena/utils/JenaUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -49,6 +50,14 @@

public class JenaUtils {
private static final Logger log = LoggerFactory.getLogger(JenaUtils.class);
private static final Map<String, String> extensionToLanguageMap = Collections.synchronizedMap(new HashMap<>());

static {
extensionToLanguageMap.put("nt", "N-TRIPLE");
extensionToLanguageMap.put("n3", "N3");
extensionToLanguageMap.put("ttl", "TURTLE");
extensionToLanguageMap.put("jsonld", "JSONLD");
}

private JenaUtils() {
}
Expand Down Expand Up @@ -237,26 +246,9 @@ private static RDFReader getReaderByRdfSyntax(Model model, String language) {

private static String getRdfSyntax(org.springframework.core.io.Resource resource) {
String extension = StringUtils.lowerCase(StringUtils.substringAfterLast(resource.getFilename(), "."));
if ("owl".equals(extension) || "rdf".equals(extension)) {
// .rdf is extension for application/rdf+xml and is default syntax in Jena
// .owl is here for legacy reasons, but it is often RDF XML because it is the Protégé default
return null;
}
if ("nt".equals(extension)) {
// .nt is extension for application/n-triples
return "N-TRIPLE";
}
if ("n3".equals(extension)) {
// .n3 is extension for text/n3
return "N3";
}
if ("ttl".equals(extension)) {
// .ttl is extension for text/turtle
return "TURTLE";
}

// any other extension falls back to RDF XML
return null;
// when return value is null, fall back to RDF/XML
return extensionToLanguageMap.getOrDefault(extension, null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's ok to do this as here the default return value is null
extensionToLanguageMap.get(extension)

}

public static void write(Model model, File file) {
Expand Down
33 changes: 13 additions & 20 deletions cogni-sem/src/main/java/zone/cogni/sem/jena/JenaUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
Expand All @@ -50,8 +51,17 @@
import java.util.function.Supplier;
import java.util.stream.Collectors;

@Deprecated
public class JenaUtils {
private static final Logger log = LoggerFactory.getLogger(JenaUtils.class);
private static final Map<String, String> extensionToLanguageMap = Collections.synchronizedMap(new HashMap<>());

static {
extensionToLanguageMap.put("nt", "N-TRIPLE");
extensionToLanguageMap.put("n3", "N3");
extensionToLanguageMap.put("ttl", "TURTLE");
extensionToLanguageMap.put("jsonld", "JSONLD");
}

private JenaUtils() {
}
Expand Down Expand Up @@ -253,28 +263,11 @@ private static RDFReader getReaderByRdfSyntax(Model model, String language) {
}
}

private static String getRdfSyntax(Resource resource) {
private static String getRdfSyntax(org.springframework.core.io.Resource resource) {
String extension = StringUtils.lowerCase(StringUtils.substringAfterLast(resource.getFilename(), "."));
if ("owl".equals(extension) || "rdf".equals(extension)) {
// .rdf is extension for application/rdf+xml and is default syntax in Jena
// .owl is here for legacy reasons, but it is often RDF XML because it is the Protégé default
return null;
}
if ("nt".equals(extension)) {
// .nt is extension for application/n-triples
return "N-TRIPLE";
}
if ("n3".equals(extension)) {
// .n3 is extension for text/n3
return "N3";
}
if ("ttl".equals(extension)) {
// .ttl is extension for text/turtle
return "TURTLE";
}

// any other extension falls back to RDF XML
return null;
// when return value is null, fall back to RDF/XML
return extensionToLanguageMap.getOrDefault(extension, null);
}

public static void write(Model model, File file) {
Expand Down
Loading