public abstract class AbstractRDFParser<T extends AbstractRDFParser<T>> extends Object implements RDFParser, Cloneable
This abstract class keeps the properties in protected fields like
sourceFile using Optional. Some basic checking like
checkIsAbsolute(IRI) is performed.
This class and its subclasses are Cloneable, immutable and
(therefore) thread-safe - each call to option methods like
contentType(String) or source(IRI) will return a cloned,
mutated copy.
By default, parsing is done by the abstract method
parseSynchronusly() - which is executed in a cloned snapshot - hence
multiple parse() calls are thread-safe. The default parse()
uses a thread pool in threadGroup - but implementations can override
parse() (e.g. because it has its own threading model or use
asynchronous remote execution).
RDFParser.ParseResult| Modifier and Type | Field and Description |
|---|---|
static ThreadGroup |
threadGroup |
| Constructor and Description |
|---|
AbstractRDFParser() |
| Modifier and Type | Method and Description |
|---|---|
protected T |
asT() |
T |
base(IRI base) |
T |
base(String base) |
protected void |
checkBaseRequired()
Check if base is required.
|
protected void |
checkContentType()
Subclasses can override this method to check compatibility with the
contentType setting.
|
protected void |
checkIsAbsolute(IRI iri)
Check if an iri is absolute.
|
protected void |
checkSource()
Check that one and only one source is present and valid.
|
protected void |
checkTarget()
Subclasses can override this method to check the target is valid.
|
T |
clone() |
T |
contentType(RDFSyntax rdfSyntax) |
T |
contentType(String contentType) |
protected RDF |
createRDFTermFactory()
Create a new
RDF for a parse session. |
Optional<IRI> |
getBase()
Get the set base
IRI, if present. |
Optional<String> |
getContentType()
Get the set content-type String, if any.
|
Optional<RDFSyntax> |
getContentTypeSyntax()
Get the set content-type
RDFSyntax, if any. |
Optional<RDF> |
getRdfTermFactory()
Get the set
RDF, if any. |
Optional<Path> |
getSourceFile()
Get the set source
Path. |
Optional<InputStream> |
getSourceInputStream()
Get the set source
InputStream. |
Optional<IRI> |
getSourceIri()
Get the set source
Path. |
Consumer<Quad> |
getTarget()
Get the target to consume parsed Quads.
|
Optional<Dataset> |
getTargetDataset()
Get the target dataset as set by
target(Dataset). |
Optional<Graph> |
getTargetGraph()
Get the target graph as set by
target(Graph). |
protected static Optional<RDFSyntax> |
guessRDFSyntax(Path path)
Guess RDFSyntax from a local file's extension.
|
Future<RDFParser.ParseResult> |
parse() |
protected abstract void |
parseSynchronusly()
|
protected T |
prepareForParsing()
Prepare a clone of this RDFParser which have been checked and completed.
|
T |
rdfTermFactory(RDF rdfTermFactory) |
protected void |
resetSource()
Reset all source* fields to Optional.empty()
|
protected void |
resetTarget()
Reset all optional target* fields to
Optional.empty(). |
T |
source(InputStream inputStream) |
T |
source(IRI iri) |
T |
source(Path file) |
T |
source(String iri) |
T |
target(Consumer<Quad> consumer) |
T |
target(Dataset dataset) |
T |
target(Graph graph) |
public static final ThreadGroup threadGroup
public Optional<RDF> getRdfTermFactory()
RDF, if any.RDF to use, or Optional.empty() if it has not
been setpublic Optional<RDFSyntax> getContentTypeSyntax()
RDFSyntax, if any.
If this is Optional.isPresent(), then getContentType()
contains the value of RDFSyntax.mediaType().
RDFSyntax of the content type, or
Optional.empty() if it has not been setpublic final Optional<String> getContentType()
If this is Optional.isPresent() and is recognized by
RDFSyntax.byMediaType(String), then the corresponding
RDFSyntax is set on getContentType(), otherwise that is
Optional.empty().
text/turtle,
or Optional.empty() if it has not been setpublic Consumer<Quad> getTarget()
From the call to parseSynchronusly(), this will be a
non-null value (as a target is a required setting).
Quads, or null if it
has not yet been set.public Optional<Dataset> getTargetDataset()
target(Dataset).
The return value is Optional.isPresent() if and only if
target(Dataset) has been set, meaning that the implementation
may choose to append parsed quads to the Dataset directly instead
of relying on the generated getTarget() consumer.
If this value is present, then getTargetGraph() MUST be
Optional.empty().
Optional.empty() if another kind
of target has been set.public Optional<Graph> getTargetGraph()
target(Graph).
The return value is Optional.isPresent() if and only if
target(Graph) has been set, meaning that the implementation may
choose to append parsed triples to the Graph directly instead of
relying on the generated getTarget() consumer.
If this value is present, then getTargetDataset() MUST be
Optional.empty().
Optional.empty() if another kind of
target has been set.public Optional<IRI> getBase()
IRI, if present.IRI, or Optional.empty() if it has not
been setpublic Optional<InputStream> getSourceInputStream()
InputStream.
If this is Optional.isPresent(), then getSourceFile()
and getSourceIri() are Optional.empty().
InputStream, or Optional.empty() if it
has not been setpublic Optional<Path> getSourceFile()
Path.
If this is Optional.isPresent(), then
getSourceInputStream() and getSourceIri() are
Optional.empty().
Path, or Optional.empty() if it has
not been setpublic Optional<IRI> getSourceIri()
Path.
If this is Optional.isPresent(), then
getSourceInputStream() and getSourceInputStream() are
Optional.empty().
IRI, or Optional.empty() if it has not
been setprotected T asT()
public T rdfTermFactory(RDF rdfTermFactory)
rdfTermFactory in interface RDFParserpublic T contentType(RDFSyntax rdfSyntax) throws IllegalArgumentException
contentType in interface RDFParserIllegalArgumentExceptionpublic T contentType(String contentType) throws IllegalArgumentException
contentType in interface RDFParserIllegalArgumentExceptionpublic T base(String base) throws IllegalArgumentException
base in interface RDFParserIllegalArgumentExceptionpublic T source(InputStream inputStream)
public T source(String iri) throws IllegalArgumentException
source in interface RDFParserIllegalArgumentExceptionprotected void checkIsAbsolute(IRI iri) throws IllegalArgumentException
Used by source(String) and base(String).
iri - IRI to checkIllegalArgumentException - If the IRI is not absoluteprotected void checkSource()
throws IOException
Used by parse().
Subclasses might override this method, e.g. to support other source combinations, or to check if the sourceIri is resolvable.
IOException - If a source file can't be readprotected void checkBaseRequired()
throws IllegalStateException
IllegalStateException - if base is required, but not set.protected void resetSource()
Subclasses should override this and call super.resetSource()
if they need to reset any additional source* fields.
protected void resetTarget()
Optional.empty().
Note that the consumer set for getTarget() is
note reset.
Subclasses should override this and call super.resetTarget()
if they need to reset any additional target* fields.
protected abstract void parseSynchronusly()
throws IOException,
RDFParseException
sourceInputStream, sourceFile or
sourceIri.
One of the source fields MUST be present, as checked by
checkSource().
checkBaseRequired() is called to verify if getBase() is
required.
IOException - If the source could not be readRDFParseException - If the source could not be parsed (e.g. a .ttl file was not
valid Turtle)protected T prepareForParsing() throws IOException, IllegalStateException
The returned clone will always have getTarget() and
getRdfTermFactory() present.
If the getSourceFile() is present, but the getBase() is
not present, the base will be set to the file:/// IRI for
the Path's real path (e.g. resolving any symbolic links).
IOException - If the source was not accessible (e.g. a file was not found)IllegalStateException - If the parser was not in a compatible setting (e.g.
contentType was an invalid string)protected void checkTarget()
The default implementation throws an IllegalStateException if the target has not been set.
protected void checkContentType()
throws IllegalStateException
IllegalStateException - if the getContentType() or
getContentTypeSyntax() is not compatible or invalidprotected static Optional<RDFSyntax> guessRDFSyntax(Path path)
This method can be used by subclasses if getContentType() is not
present and getSourceFile() is set.
path - Path which extension should be checkedRDFSyntax which has a matching
RDFSyntax.fileExtension(), otherwise
Optional.empty().protected RDF createRDFTermFactory()
RDF for a parse session.
This is called by parse() to set rdfTermFactory(RDF) if
it is Optional.empty().
As parsed blank nodes might be made with
RDF.createBlankNode(String), each call to this method SHOULD
return a new RDF instance.
RDFpublic Future<RDFParser.ParseResult> parse() throws IOException, IllegalStateException
parse in interface RDFParserIOExceptionIllegalStateExceptionCopyright © 2015–2017 The Apache Software Foundation. All rights reserved.