Skip to content

Commit

Permalink
COMMONSRDF-49: Make AbstractRDFParser serializable
Browse files Browse the repository at this point in the history
Merge commit 'refs/pull/43/head' of https://github.com/apache/commons-rdf

Contributed by @ajs6f

This closes #43
  • Loading branch information
stain committed Feb 14, 2018
2 parents 6123c73 + 8506422 commit 092c465
Showing 1 changed file with 50 additions and 52 deletions.
Expand Up @@ -71,7 +71,7 @@ public abstract class AbstractRDFParser<T extends AbstractRDFParser<T>> implemen
* been set
*/
public Optional<RDF> getRdfTermFactory() {
return rdfTermFactory;
return Optional.ofNullable(rdfTermFactory);
}

/**
Expand All @@ -84,7 +84,7 @@ public Optional<RDF> getRdfTermFactory() {
* {@link Optional#empty()} if it has not been set
*/
public Optional<RDFSyntax> getContentTypeSyntax() {
return contentTypeSyntax;
return Optional.ofNullable(contentTypeSyntax);
}

/**
Expand All @@ -99,7 +99,7 @@ public Optional<RDFSyntax> getContentTypeSyntax() {
* or {@link Optional#empty()} if it has not been set
*/
public final Optional<String> getContentType() {
return contentType;
return Optional.ofNullable(contentType);
}

/**
Expand Down Expand Up @@ -131,7 +131,7 @@ public Consumer<Quad> getTarget() {
* of target has been set.
*/
public Optional<Dataset> getTargetDataset() {
return targetDataset;
return Optional.ofNullable(targetDataset);
}

/**
Expand All @@ -149,7 +149,7 @@ public Optional<Dataset> getTargetDataset() {
* target has been set.
*/
public Optional<Graph> getTargetGraph() {
return targetGraph;
return Optional.ofNullable(targetGraph);
}

/**
Expand All @@ -159,7 +159,7 @@ public Optional<Graph> getTargetGraph() {
* been set
*/
public Optional<IRI> getBase() {
return base;
return Optional.ofNullable(base);
}

/**
Expand All @@ -172,7 +172,7 @@ public Optional<IRI> getBase() {
* has not been set
*/
public Optional<InputStream> getSourceInputStream() {
return sourceInputStream;
return Optional.ofNullable(sourceInputStream);
}

/**
Expand All @@ -186,7 +186,7 @@ public Optional<InputStream> getSourceInputStream() {
* not been set
*/
public Optional<Path> getSourceFile() {
return sourceFile;
return Optional.ofNullable(sourceFile);
}

/**
Expand All @@ -200,19 +200,19 @@ public Optional<Path> getSourceFile() {
* been set
*/
public Optional<IRI> getSourceIri() {
return sourceIri;
return Optional.ofNullable(sourceIri);
}

private Optional<RDF> rdfTermFactory = Optional.empty();
private Optional<RDFSyntax> contentTypeSyntax = Optional.empty();
private Optional<String> contentType = Optional.empty();
private Optional<IRI> base = Optional.empty();
private Optional<InputStream> sourceInputStream = Optional.empty();
private Optional<Path> sourceFile = Optional.empty();
private Optional<IRI> sourceIri = Optional.empty();
private RDF rdfTermFactory = null;
private RDFSyntax contentTypeSyntax = null;
private String contentType = null;
private IRI base = null;
private InputStream sourceInputStream = null;
private Path sourceFile = null;
private IRI sourceIri = null;
private Consumer<Quad> target;
private Optional<Dataset> targetDataset;
private Optional<Graph> targetGraph;
private Dataset targetDataset;
private Graph targetGraph;

@SuppressWarnings("unchecked")
@Override
Expand All @@ -232,31 +232,31 @@ protected T asT() {
@Override
public T rdfTermFactory(final RDF rdfTermFactory) {
final AbstractRDFParser<T> c = clone();
c.rdfTermFactory = Optional.ofNullable(rdfTermFactory);
c.rdfTermFactory = rdfTermFactory;
return c.asT();
}

@Override
public T contentType(final RDFSyntax rdfSyntax) throws IllegalArgumentException {
final AbstractRDFParser<T> c = clone();
c.contentTypeSyntax = Optional.ofNullable(rdfSyntax);
c.contentType = c.contentTypeSyntax.map(syntax -> syntax.mediaType());
c.contentTypeSyntax = rdfSyntax;
c.contentType = c.getContentTypeSyntax().map(RDFSyntax::mediaType).orElse(null);
return c.asT();
}

@Override
public T contentType(final String contentType) throws IllegalArgumentException {
final AbstractRDFParser<T> c = clone();
c.contentType = Optional.ofNullable(contentType);
c.contentTypeSyntax = c.contentType.flatMap(RDFSyntax::byMediaType);
c.contentType = contentType;
c.contentTypeSyntax = c.getContentType().flatMap(RDFSyntax::byMediaType).orElse(null);
return c.asT();
}

@Override
public T base(final IRI base) {
final AbstractRDFParser<T> c = clone();
c.base = Optional.ofNullable(base);
c.base.ifPresent(i -> checkIsAbsolute(i));
c.base = base;
c.getBase().ifPresent(this::checkIsAbsolute);
return c.asT();
}

Expand All @@ -269,33 +269,33 @@ public T base(final String base) throws IllegalArgumentException {
public T source(final InputStream inputStream) {
final AbstractRDFParser<T> c = clone();
c.resetSource();
c.sourceInputStream = Optional.ofNullable(inputStream);
c.sourceInputStream = inputStream;
return c.asT();
}

@Override
public T source(final Path file) {
final AbstractRDFParser<T> c = clone();
c.resetSource();
c.sourceFile = Optional.ofNullable(file);
c.sourceFile = file;
return c.asT();
}

@Override
public T source(final IRI iri) {
final AbstractRDFParser<T> c = clone();
c.resetSource();
c.sourceIri = Optional.ofNullable(iri);
c.sourceIri.ifPresent(i -> checkIsAbsolute(i));
c.sourceIri = iri;
c.getSourceIri().ifPresent(this::checkIsAbsolute);
return c.asT();
}

@Override
public T source(final String iri) throws IllegalArgumentException {
final AbstractRDFParser<T> c = clone();
c.resetSource();
c.sourceIri = Optional.ofNullable(iri).map(internalRdfTermFactory::createIRI);
c.sourceIri.ifPresent(i -> checkIsAbsolute(i));
c.sourceIri = internalRdfTermFactory.createIRI(iri);
c.getSourceIri().ifPresent(this::checkIsAbsolute);
return source(internalRdfTermFactory.createIRI(iri));
}

Expand Down Expand Up @@ -327,19 +327,19 @@ protected void checkIsAbsolute(final IRI iri) throws IllegalArgumentException {
* If a source file can't be read
*/
protected void checkSource() throws IOException {
if (!sourceFile.isPresent() && !sourceInputStream.isPresent() && !sourceIri.isPresent()) {
if (!getSourceFile().isPresent() && !getSourceInputStream().isPresent() && !getSourceIri().isPresent()) {
throw new IllegalStateException("No source has been set");
}
if (sourceIri.isPresent() && sourceInputStream.isPresent()) {
if (getSourceIri().isPresent() && getSourceInputStream().isPresent()) {
throw new IllegalStateException("Both sourceIri and sourceInputStream have been set");
}
if (sourceIri.isPresent() && sourceFile.isPresent()) {
if (getSourceIri().isPresent() && getSourceFile().isPresent()) {
throw new IllegalStateException("Both sourceIri and sourceFile have been set");
}
if (sourceInputStream.isPresent() && sourceFile.isPresent()) {
if (getSourceInputStream().isPresent() && getSourceFile().isPresent()) {
throw new IllegalStateException("Both sourceInputStream and sourceFile have been set");
}
if (sourceFile.isPresent() && !sourceFile.filter(Files::isReadable).isPresent()) {
if (getSourceFile().isPresent() && !getSourceFile().filter(Files::isReadable).isPresent()) {
throw new IOException("Can't read file: " + sourceFile);
}
}
Expand All @@ -351,8 +351,8 @@ protected void checkSource() throws IOException {
* if base is required, but not set.
*/
protected void checkBaseRequired() throws IllegalStateException {
if (!base.isPresent() && sourceInputStream.isPresent()
&& !contentTypeSyntax.filter(t -> t == RDFSyntax.NQUADS || t == RDFSyntax.NTRIPLES).isPresent()) {
if (!getBase().isPresent() && getSourceInputStream().isPresent()
&& !getContentTypeSyntax().filter(t -> t == RDFSyntax.NQUADS || t == RDFSyntax.NTRIPLES).isPresent()) {
throw new IllegalStateException("base iri required for inputstream source");
}
}
Expand All @@ -365,9 +365,9 @@ protected void checkBaseRequired() throws IllegalStateException {
*
*/
protected void resetSource() {
sourceInputStream = Optional.empty();
sourceIri = Optional.empty();
sourceFile = Optional.empty();
sourceInputStream = null;
sourceIri = null;
sourceFile = null;
}

/**
Expand All @@ -381,8 +381,8 @@ protected void resetSource() {
*
*/
protected void resetTarget() {
targetDataset = Optional.empty();
targetGraph = Optional.empty();
targetDataset = null;
targetGraph = null;
}

/**
Expand Down Expand Up @@ -431,14 +431,12 @@ protected T prepareForParsing() throws IOException, IllegalStateException {
final AbstractRDFParser<T> c = clone();

// Use a fresh SimpleRDF for each parse
if (!c.rdfTermFactory.isPresent()) {
c.rdfTermFactory = Optional.of(createRDFTermFactory());
}
c.rdfTermFactory = c.getRdfTermFactory().orElse(createRDFTermFactory());
// sourceFile, but no base? Let's follow any symlinks and use
// the file:/// URI
if (c.sourceFile.isPresent() && !c.base.isPresent()) {
final URI baseUri = c.sourceFile.get().toRealPath().toUri();
c.base = Optional.of(internalRdfTermFactory.createIRI(baseUri.toString()));
if (c.getSourceFile().isPresent() && !c.getBase().isPresent()) {
final URI baseUri = c.getSourceFile().get().toRealPath().toUri();
c.base = internalRdfTermFactory.createIRI(baseUri.toString());
}

return c.asT();
Expand All @@ -454,7 +452,7 @@ protected void checkTarget() {
if (target == null) {
throw new IllegalStateException("target has not been set");
}
if (targetGraph.isPresent() && targetDataset.isPresent()) {
if (getTargetGraph().isPresent() && getTargetDataset().isPresent()) {
// This should not happen as each target(..) method resets the
// optionals
throw new IllegalStateException("targetGraph and targetDataset can't both be set");
Expand Down Expand Up @@ -553,7 +551,7 @@ public T target(final Dataset dataset) {
final
AbstractRDFParser<T> c = (AbstractRDFParser) RDFParser.super.target(dataset);
c.resetTarget();
c.targetDataset = Optional.of(dataset);
c.targetDataset = dataset;
return c.asT();
}

Expand All @@ -564,7 +562,7 @@ public T target(final Graph graph) {
// .clone()
AbstractRDFParser<T> c = (AbstractRDFParser) RDFParser.super.target(graph);
c.resetTarget();
c.targetGraph = Optional.of(graph);
c.targetGraph = graph;
return c.asT();
}

Expand Down

0 comments on commit 092c465

Please sign in to comment.