Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface SerializerFactory {

/**
* Creates a new RDF serializer for the specified format and model.
*
*
* @param format The {@link RDFFormat} to use for serialization.
* @param model The {@link Model} to be serialized.
* @param config The {@link IOOptions} configuration to use for
Expand All @@ -27,4 +27,15 @@ public interface SerializerFactory {
* model.
*/
RDFSerializer createSerializer(RDFFormat format, Model model, IOOptions config);
}

/**
* Creates a new RDF serializer for the specified format and model
* using the default configuration for that format.
*
* @param format The {@link RDFFormat} to use for serialization.
* @param model The {@link Model} to be serialized.
* @return A new instance of an RDF serializer for the specified format and
* model with default configuration.
*/
RDFSerializer createSerializer(RDFFormat format, Model model);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@
import fr.inria.corese.core.next.api.base.io.RDFFormat;
import fr.inria.corese.core.next.api.io.IOOptions;
import fr.inria.corese.core.next.api.io.serialization.RDFSerializer;
import fr.inria.corese.core.next.impl.exception.SerializationException;
import fr.inria.corese.core.next.impl.io.common.JSONLDOptions;
import fr.inria.corese.core.next.impl.io.serialization.canonical.RDFC10Canonicalizer;
import fr.inria.corese.core.next.impl.io.serialization.canonical.RDFC10SerializerOptions;
import fr.inria.corese.core.next.impl.io.serialization.canonical.RDFC10Serializer;
import fr.inria.corese.core.next.impl.io.serialization.nquads.NQuadsSerializerOptions;
import fr.inria.corese.core.next.impl.io.serialization.canonical.RDFC10SerializerOptions;
import fr.inria.corese.core.next.impl.io.serialization.jsonld.JSONLDSerializer;
import fr.inria.corese.core.next.impl.io.serialization.nquads.NQuadsSerializer;
import fr.inria.corese.core.next.impl.io.serialization.ntriples.NTriplesSerializerOptions;
import fr.inria.corese.core.next.impl.io.serialization.nquads.NQuadsSerializerOptions;
import fr.inria.corese.core.next.impl.io.serialization.ntriples.NTriplesSerializer;
import fr.inria.corese.core.next.impl.io.serialization.rdfxml.RDFXMLSerializerOption;
import fr.inria.corese.core.next.impl.io.serialization.ntriples.NTriplesSerializerOptions;
import fr.inria.corese.core.next.impl.io.serialization.rdfxml.RDFXMLSerializer;
import fr.inria.corese.core.next.impl.io.serialization.trig.TriGSerializerOptions;
import fr.inria.corese.core.next.impl.io.serialization.rdfxml.RDFXMLSerializerOption;
import fr.inria.corese.core.next.impl.io.serialization.trig.TriGSerializer;
import fr.inria.corese.core.next.impl.io.serialization.turtle.TurtleSerializerOptions;
import fr.inria.corese.core.next.impl.io.serialization.trig.TriGSerializerOptions;
import fr.inria.corese.core.next.impl.io.serialization.turtle.TurtleSerializer;
import fr.inria.corese.core.next.impl.io.serialization.jsonld.JSONLDSerializer;
import fr.inria.corese.core.next.impl.io.serialization.turtle.TurtleSerializerOptions;
import fr.inria.corese.core.next.impl.temp.CoreseAdaptedValueFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
* Default implementation of {@link fr.inria.corese.core.next.api.io.serialization.SerializerFactory}.
Expand All @@ -46,9 +46,8 @@
*/
public class SerializerFactory implements fr.inria.corese.core.next.api.io.serialization.SerializerFactory {

private static final Logger logger = LoggerFactory.getLogger(SerializerFactory.class);

private final Map<RDFFormat, BiFunction<Model, IOOptions, RDFSerializer>> registry;
private final Map<RDFFormat, Function<Model, RDFSerializer>> defaultRegistry;
private final ValueFactory coreseValueFactory;

/**
Expand All @@ -64,103 +63,107 @@ public SerializerFactory() {
this.coreseValueFactory = new CoreseAdaptedValueFactory();

Map<RDFFormat, BiFunction<Model, IOOptions, RDFSerializer>> tempRegistry = new HashMap<>();
Map<RDFFormat, Function<Model, RDFSerializer>> tempDefaultRegistry = new HashMap<>();

tempRegistry.put(RDFFormat.TURTLE, (model, genericConfig) -> {
if (genericConfig instanceof TurtleSerializerOptions specificConfig) {
return new TurtleSerializer(model, specificConfig);
} else {
logger.warn("Provided config for TURTLE is not TurtleConfig (was {}). Using default TurtleConfig.",
genericConfig.getClass().getSimpleName());
return new TurtleSerializer(model, TurtleSerializerOptions.defaultConfig());
}
throw new SerializationException(
"Invalid configuration type. Expected TurtleSerializerOptions, got: " +
genericConfig.getClass().getSimpleName(),
RDFFormat.TURTLE.getName()
);
});
tempDefaultRegistry.put(RDFFormat.TURTLE, TurtleSerializer::new);

tempRegistry.put(RDFFormat.NTRIPLES, (model, genericConfig) -> {
if (genericConfig instanceof NTriplesSerializerOptions specificConfig) {
return new NTriplesSerializer(model, specificConfig);
} else {
logger.warn(
"Provided config for NTRIPLES is not NTriplesConfig (was {}). Using default NTriplesConfig.",
genericConfig.getClass().getSimpleName());
return new NTriplesSerializer(model, NTriplesSerializerOptions.defaultConfig());
}
throw new SerializationException(
"Invalid configuration type. Expected NTriplesSerializerOptions, got: " +
genericConfig.getClass().getSimpleName(),
RDFFormat.NTRIPLES.getName()
);
});
tempDefaultRegistry.put(RDFFormat.NTRIPLES, NTriplesSerializer::new);

tempRegistry.put(RDFFormat.NQUADS, (model, genericConfig) -> {
if (genericConfig instanceof NQuadsSerializerOptions specificConfig) {
return new NQuadsSerializer(model, specificConfig);
} else {
logger.warn("Provided config for NQUADS is not NQuadsConfig (was {}). Using default NQuadsConfig.",
genericConfig.getClass().getSimpleName());
return new NQuadsSerializer(model, NQuadsSerializerOptions.defaultConfig());
}
throw new SerializationException(
"Invalid configuration type. Expected NQuadsSerializerOptions, got: " +
genericConfig.getClass().getSimpleName(),
RDFFormat.NQUADS.getName()
);
});
tempDefaultRegistry.put(RDFFormat.NQUADS, NQuadsSerializer::new);

tempRegistry.put(RDFFormat.TRIG, (model, genericConfig) -> {
if (genericConfig instanceof TriGSerializerOptions specificConfig) {
return new TriGSerializer(model, specificConfig);
} else {
logger.warn("Provided config for TRIG is not TriGConfig (was {}). Using default TriGConfig.",
genericConfig.getClass().getSimpleName());
return new TriGSerializer(model, TriGSerializerOptions.defaultConfig());
}
throw new SerializationException(
"Invalid configuration type. Expected TriGSerializerOptions, got: " +
genericConfig.getClass().getSimpleName(),
RDFFormat.TRIG.getName()
);
});
tempDefaultRegistry.put(RDFFormat.TRIG, TriGSerializer::new);

tempRegistry.put(RDFFormat.RDFXML, (model, genericConfig) -> {
if (genericConfig instanceof RDFXMLSerializerOption specificConfig) {
return new RDFXMLSerializer(model, specificConfig);
} else {
logger.warn("Provided config for RDFXML is not RDFXmlConfig (was {}). Using default RDFXmlConfig.",
genericConfig.getClass().getSimpleName());
return new RDFXMLSerializer(model, RDFXMLSerializerOption.defaultConfig());
}
throw new SerializationException(
"Invalid configuration type. Expected RDFXMLSerializerOption, got: " +
genericConfig.getClass().getSimpleName(),
RDFFormat.RDFXML.getName()
);
});
tempDefaultRegistry.put(RDFFormat.RDFXML, RDFXMLSerializer::new);

tempRegistry.put(RDFFormat.JSONLD, (model, genericConfig) -> {
if (genericConfig instanceof JSONLDOptions specificConfig) {
return new JSONLDSerializer(model, specificConfig);
} else {
logger.warn(
"Provided config for JSONLD is not TitaniumJSONLDProcessorOption (was {}). Using default TitaniumJSONLDProcessorOption.",
genericConfig.getClass().getSimpleName());
return new JSONLDSerializer(model, new JSONLDOptions.Builder().build());
}
throw new SerializationException(
"Invalid configuration type. Expected JSONLDOptions, got: " +
genericConfig.getClass().getSimpleName(),
RDFFormat.JSONLD.getName()
);
});
tempDefaultRegistry.put(RDFFormat.JSONLD, JSONLDSerializer::new);

tempRegistry.put(RDFFormat.RDFC_1_0, (model, genericConfig) -> {
if (genericConfig instanceof RDFC10SerializerOptions specificConfig) {
RDFC10Canonicalizer canonicalizer = new RDFC10Canonicalizer(
RDFC10Canonicalizer rdfc10Canonicalizer = new RDFC10Canonicalizer(
specificConfig.getHashAlgorithm(),
specificConfig.getPermutationLimit(),
coreseValueFactory
);
return new RDFC10Serializer(model, specificConfig, canonicalizer);
} else {
logger.warn("Provided config for RDFC_1_0 is not CanonicalOption (was {}). Using default CanonicalOption.",
genericConfig != null ? genericConfig.getClass().getSimpleName() : "null");
RDFC10SerializerOptions defaultConfig = RDFC10SerializerOptions.defaultConfig();
RDFC10Canonicalizer canonicalizer = new RDFC10Canonicalizer(
defaultConfig.getHashAlgorithm(),
defaultConfig.getPermutationLimit(),
coreseValueFactory
);
return new RDFC10Serializer(model, defaultConfig, canonicalizer);
return new RDFC10Serializer(model, specificConfig, rdfc10Canonicalizer);
}
throw new SerializationException(
"Invalid configuration type. Expected RDFC10SerializerOptions, got: " +
(genericConfig != null ? genericConfig.getClass().getSimpleName() : "null"),
RDFFormat.RDFC_1_0.getName()
);
});


tempRegistry.put(RDFFormat.JSONLD, (model, genericConfig) -> {
if (genericConfig instanceof JSONLDOptions specificConfig) {
return new JSONLDSerializer(model, specificConfig);
} else {
logger.warn(
"Provided config for JSONLD is not TitaniumJSONLDProcessorOption (was {}). Using default TitaniumJSONLDProcessorOption.",
genericConfig.getClass().getSimpleName());
return new JSONLDSerializer(model, new JSONLDOptions.Builder().build());
}
tempDefaultRegistry.put(RDFFormat.RDFC_1_0, model -> {
RDFC10SerializerOptions defaultConfig = RDFC10SerializerOptions.defaultConfig();
RDFC10Canonicalizer rdfc10Canonicalizer = new RDFC10Canonicalizer(
defaultConfig.getHashAlgorithm(),
defaultConfig.getPermutationLimit(),
coreseValueFactory
);
return new RDFC10Serializer(model, defaultConfig, rdfc10Canonicalizer);
});

this.registry = Collections.unmodifiableMap(tempRegistry);
this.defaultRegistry = Collections.unmodifiableMap(tempDefaultRegistry);
}

/**
Expand All @@ -176,8 +179,8 @@ public SerializerFactory() {
* format.
* @throws NullPointerException if any of the arguments (format, model,
* config) are null.
* @throws IllegalArgumentException if the provided format is not supported by
* this factory.
* @throws SerializationException if the provided format is not supported by
* this factory or if the configuration type is invalid.
*/
@Override
public RDFSerializer createSerializer(RDFFormat format, Model model, IOOptions config) {
Expand All @@ -189,9 +192,43 @@ public RDFSerializer createSerializer(RDFFormat format, Model model, IOOptions c
BiFunction<Model, IOOptions, RDFSerializer> constructor = registry.get(format);

if (constructor == null) {
throw new IllegalArgumentException("Unsupported RDFFormat: " + format.getName());
throw new SerializationException(
"Unsupported RDF format: " + format.getName(),
format.getName()
);
}

return constructor.apply(model, config);
}

/**
* Creates an {@link RDFSerializer} instance for the specified format and model
* using the default configuration for that format.
*
* @param format the {@link RDFFormat} for which to create the serializer. Must
* not be null.
* @param model the {@link Model} to be serialized. Must not be null.
* @return a new instance of {@link RDFSerializer} configured for the specified
* format with default configuration.
* @throws NullPointerException if any of the arguments (format, model) are null.
* @throws SerializationException if the provided format is not supported by
* this factory.
*/
@Override
public RDFSerializer createSerializer(RDFFormat format, Model model) {

Objects.requireNonNull(format, "RDFFormat cannot be null");
Objects.requireNonNull(model, "Model cannot be null");

Function<Model, RDFSerializer> constructor = defaultRegistry.get(format);

if (constructor == null) {
throw new SerializationException(
"Unsupported RDF format: " + format.getName(),
format.getName()
);
}

return constructor.apply(model);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import fr.inria.corese.core.next.api.ValueFactory;
import fr.inria.corese.core.next.impl.common.BasicIRI;
import fr.inria.corese.core.next.impl.common.literal.XSD;
import fr.inria.corese.core.next.impl.exception.IncorrectFormatException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -21,7 +20,8 @@
import java.time.temporal.TemporalAccessor;
import java.util.Date;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public abstract class ValueFactoryTest {

Expand All @@ -33,10 +33,8 @@ public abstract class ValueFactoryTest {
@Test
public void testCreateIRI() {
String correctIRI = "http://example.org";
String incorrectIRI = "test";

assertNotNull(this.valueFactory.createIRI(correctIRI));
// assertThrows(IncorrectFormatException.class, () -> this.valueFactory.createIRI(incorrectIRI));
}

@Test
Expand All @@ -48,7 +46,7 @@ public void testCreateBNode() {
assertNotNull(nodesCorese123);
assertNotNull(nodesCoreseRandom);
assertNotNull(nodesCoreseRandom.getID());
assertEquals(nodesCorese123.getID(), "corese123");
assertEquals("corese123", nodesCorese123.getID());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ void testGetNonExistentPrefix() {
@Test
@DisplayName("Should throw IllegalArgumentException if prefix is null")
void testSetPrefixNullPrefix() {
assertThrows(IllegalArgumentException.class, () -> {
handler.setPrefix(null, "http://example.org/");
});
assertThrows(IllegalArgumentException.class, () -> handler.setPrefix(null, "http://example.org/"));
}

/**
Expand All @@ -73,9 +71,7 @@ void testSetPrefixNullPrefix() {
@Test
@DisplayName("Should throw IllegalArgumentException if namespace is null")
void testSetPrefixNullNamespace() {
assertThrows(IllegalArgumentException.class, () -> {
handler.setPrefix("ex", null);
});
assertThrows(IllegalArgumentException.class, () -> handler.setPrefix("ex", null));
}


Expand Down Expand Up @@ -220,9 +216,7 @@ void testExpandPrefixUnknownPrefix() {
@Test
@DisplayName("Should throw IllegalArgumentException if expansion input is null")
void testExpandPrefixNullInput() {
assertThrows(IllegalArgumentException.class, () -> {
handler.expandPrefix(null);
});
assertThrows(IllegalArgumentException.class, () -> handler.expandPrefix(null));
}

/**
Expand All @@ -243,9 +237,7 @@ void testCompressIRI() {
@Test
@DisplayName("Should throw IllegalArgumentException if compression input is null")
void testCompressIRINullInput() {
assertThrows(IllegalArgumentException.class, () -> {
handler.compressIRI(null);
});
assertThrows(IllegalArgumentException.class, () -> handler.compressIRI(null));
}

/**
Expand Down Expand Up @@ -312,9 +304,7 @@ void testCopyFrom() {
@Test
@DisplayName("Should throw IllegalArgumentException if copy source is null")
void testCopyFromNull() {
assertThrows(IllegalArgumentException.class, () -> {
handler.copyFrom(null);
});
assertThrows(IllegalArgumentException.class, () -> handler.copyFrom(null));
}

/**
Expand Down
Loading