Skip to content
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ dependencies {
implementation("fr.inria.lille.shexjava:shexjava-core:1.0") // ShEx validation engine
implementation("fr.inria.corese.org.semarglproject:semargl-rdfa:$semargl_version") // RDFa parser (Semargl)
implementation("fr.inria.corese.org.semarglproject:semargl-core:$semargl_version") // Semargl core RDF parser
implementation("com.github.jsonld-java:jsonld-java:0.13.4") // JSON-LD processing
implementation("com.github.jsonld-java:jsonld-java:0.13.6") // Legacy JSON-LD processing


// === HTTP and XML ===
implementation("org.glassfish.jersey.core:jersey-client:$jersey_version") // HTTP client (Jersey)
Expand Down Expand Up @@ -150,6 +151,7 @@ extraJavaModuleInfo {
automaticModule("commons-lang:commons-lang", "commons.lang") // Module for Commons Lang
automaticModule("fr.inria.lille.shexjava:shexjava-core", "shexjava.core") // Module for ShexJava core
automaticModule("org.eclipse.rdf4j:rdf4j-model", "rdf4j.model") // Module for RDF4J model

}


Expand Down
114 changes: 114 additions & 0 deletions src/main/java/fr/inria/corese/core/next/api/parser/RDFFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package fr.inria.corese.core.next.api.parser;

import java.nio.charset.Charset;
import java.util.*;
import fr.inria.corese.core.next.api.IRI;

public interface RDFFormat {

/**
* Gets the name of this file format.
*
* @return A human-readable format name, e.g. "PLAIN TEXT".
*/
String getName();


/**
* Gets the default MIME type for this file format.
*
* @return A MIME type string, e.g. "text/plain".
*/
String getDefaultMIMEType() ;


/**
* Checks if the specified MIME type matches the FileFormat's default MIME type. The MIME types are compared
* ignoring upper/lower-case differences.
*
* @param mimeType The MIME type to compare to the FileFormat's default MIME type.
* @return <var>true</var> if the specified MIME type matches the FileFormat's default MIME type.
*/
boolean hasDefaultMIMEType(String mimeType);

/**
* Gets the file format's MIME types.
*
* @return An unmodifiable list of MIME type strings, e.g. "text/plain".
*/
List<String> getMIMETypes();



/**
* Checks if specified MIME type matches one of the FileFormat's MIME types. The MIME types are compared ignoring
* upper/lower-case differences.
*
* @param mimeType The MIME type to compare to the FileFormat's MIME types.
* @return <var>true</var> if the specified MIME type matches one of the FileFormat's MIME types.
*/
boolean hasMIMEType(String mimeType);

/**
* Gets the default file name extension for this file format.
*
* @return A file name extension (excluding the dot), e.g. "txt", or <var>null</var> if there is no common file
* extension for the format.
*/
String getDefaultFileExtension();

/**
* Checks if the specified file name extension matches the FileFormat's default file name extension. The file name
* extension MIME types are compared ignoring upper/lower-case differences.
*
* @param extension The file extension to compare to the FileFormat's file extension.
* @return <var>true</var> if the file format has a default file name extension and if it matches the specified
* extension, <var>false</var> otherwise.
*/
boolean hasDefaultFileExtension(String extension);

/**
* Gets the file format's file extensions.
*
* @return An unmodifiable list of file extension strings, e.g. "txt".
*/
List<String> getFileExtensions();

/**
* Checks if the FileFormat's file extension is equal to the specified file extension. The file extensions are
* compared ignoring upper/lower-case differences.
*
* @param extension The file extension to compare to the FileFormat's file extension.
* @return <var>true</var> if the specified file extension is equal to the FileFormat's file extension.
*/
boolean hasFileExtension(String extension);

/**
* Get the (default) charset for this file format.
*
* @return the (default) charset for this file format, or null if this format does not have a default charset.
*/
Charset getCharset();

/**
* Checks if the FileFormat has a (default) charset.
*
* @return <var>true</var> if the FileFormat has a (default) charset.
*/
boolean hasCharset();

/**
* Return <var>true</var> if the RDFFormat supports the encoding of namespace/prefix information.
*/
boolean supportsNamespaces();

/**
* Return <var>true</var> if the RDFFormat supports the encoding of contexts/named graphs.
*/
boolean supportsContexts();

/**
* Return <var>true</var> if the RDFFormat supports the encoding of RDF-star triples natively.
*/
boolean supportsRDFStar();
}
255 changes: 255 additions & 0 deletions src/main/java/fr/inria/corese/core/next/api/parser/RDFFormats.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
package fr.inria.corese.core.next.api.parser;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;

public enum RDFFormats implements RDFFormat {

TURTLE("Turtle",
List.of("text/turtle"),
List.of("ttl"),
true,
false,
false),
N3("N3",
List.of("text/n3"),
List.of("n3"),
true,
false,
false),
RDF_XML("RDF/XML",
List.of("application/rdf+xml"),
List.of("rdf", "xml"),
true,
false,
false),
JSON_LD("JSON-LD",
List.of("application/ld+json"),
List.of("jsonld", "json"),
true,
true,
false),
N_TRIPLES("N-Triples",
List.of("application/n-triples"),
List.of("nt"),
false,
false,
false),
TRIG("TriG",
List.of("application/trig"),
List.of("trig"),
true,
true,
false),
NQUADS("N-Quads",
List.of("application/n-quads"),
List.of("nq"),
true,
true,
false);

private static final boolean DEFAULT_SUPPORTS_NAMESPACES = true;
private static final boolean DEFAULT_SUPPORTS_CONTEXTS = true;
private static final boolean DEFAULT_SUPPORTS_RDF_STAR = false;

/**
* The file format human-readable name.
*/
private final String name;

/**
* The file format's MIME types. The first item in the list is interpreted as the default MIME type for the format.
*/
private final List<String> mimeTypes;

/**
* The file format's (default) charset.
*/
private final Charset charset;

/**
* The file format's file extensions. The first item in the list is interpreted as the default file extension for
* the format.
*/
private final List<String> fileExtensions;

/**
* Flag indicating whether the RDFFormat can encode namespace information.
*/
private final boolean supportsNamespaces;

/**
* Flag indicating whether the RDFFormat can encode context information (ex: Graphs or quads).
*/
private final boolean supportsContexts;

/**
* Flag indicating whether the RDFFormat can encode RDF-star triples natively.
*/
private final boolean supportsRDFStar;

RDFFormats(String name,
List<String> mimeTypes,
Charset charset,
List<String> fileExtensions,
boolean supportsNamespaces,
boolean supportsContexts,
boolean supportsRDFStar) {
this.name = name;
this.mimeTypes = mimeTypes;
this.charset = charset;
this.fileExtensions = fileExtensions;
this.supportsNamespaces = supportsNamespaces;
this.supportsContexts = supportsContexts;
this.supportsRDFStar = supportsRDFStar;
}

RDFFormats(String name,
List<String> mimeTypes,
Charset charset,
List<String> fileExtensions) {
this(name, mimeTypes, charset, fileExtensions, DEFAULT_SUPPORTS_NAMESPACES, DEFAULT_SUPPORTS_CONTEXTS, DEFAULT_SUPPORTS_RDF_STAR);
}

RDFFormats(String name,
List<String> mimeTypes,
List<String> fileExtensions) {
this(name, mimeTypes, StandardCharsets.UTF_8, fileExtensions, DEFAULT_SUPPORTS_NAMESPACES, DEFAULT_SUPPORTS_CONTEXTS, DEFAULT_SUPPORTS_RDF_STAR);
}

RDFFormats(String name,
List<String> mimeTypes,
List<String> fileExtensions,
boolean supportsNamespaces,
boolean supportsContexts,
boolean supportsRDFStar) {
this(name, mimeTypes, StandardCharsets.UTF_8, fileExtensions, supportsNamespaces, supportsContexts, supportsRDFStar);
}

@Override
public String getName() {
return name;
}

@Override
public String getDefaultMIMEType() {
return mimeTypes.get(0);
}

@Override
public boolean hasDefaultMIMEType(String mimeType) {
return getDefaultMIMEType().equalsIgnoreCase(mimeType);
}

@Override
public List<String> getMIMETypes() {
return Collections.unmodifiableList(mimeTypes);
}

@Override
public boolean hasMIMEType(String mimeType) {
if (mimeType == null) {
return false;
}
String type = mimeType;
if (mimeType.indexOf(';') > 0) {
type = mimeType.substring(0, mimeType.indexOf(';'));
}
for (String mt : this.mimeTypes) {
if (mt.equalsIgnoreCase(mimeType)) {
return true;
}
if (mimeType != type && mt.equalsIgnoreCase(type)) {
return true;
}
}

return false;
}

@Override
public String getDefaultFileExtension() {
if (fileExtensions.isEmpty()) {
return null;
} else {
return fileExtensions.get(0);
}
}

@Override
public boolean hasDefaultFileExtension(String extension) {
String ext = getDefaultFileExtension();
return ext != null && ext.equalsIgnoreCase(extension);
}

@Override
public List<String> getFileExtensions() {
return Collections.unmodifiableList(fileExtensions);
}

@Override
public boolean hasFileExtension(String extension) {
for (String ext : fileExtensions) {
if (ext.equalsIgnoreCase(extension)) {
return true;
}
}

return false;
}

@Override
public Charset getCharset() {
return charset;
}

@Override
public boolean hasCharset() {
return charset != null;
}

@Override
public boolean supportsNamespaces() {
return supportsNamespaces;
}

@Override
public boolean supportsContexts() {
return supportsContexts;
}

@Override
public boolean supportsRDFStar() {
return supportsRDFStar;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder(64);

sb.append(name);

sb.append(" (mimeTypes=");
for (int i = 0; i < mimeTypes.size(); i++) {
if (i > 0) {
sb.append(", ");
}
sb.append(mimeTypes.get(i));
}

sb.append("; ext=");
for (int i = 0; i < fileExtensions.size(); i++) {
if (i > 0) {
sb.append(", ");
}
sb.append(fileExtensions.get(i));
}

sb.append(")");

return sb.toString();
}

}
Loading