You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commonsrdf.apache.org by st...@apache.org on 2016/09/28 13:52:02 UTC
[10/36] incubator-commonsrdf git commit: Add guessRDFSyntax()
convenience method
Add guessRDFSyntax() convenience method
Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/478b20e2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/478b20e2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/478b20e2
Branch: refs/heads/master
Commit: 478b20e27b2e24494d20d6edbf7a80c3604a7879
Parents: 8dfc3f4
Author: Stian Soiland-Reyes <st...@apache.org>
Authored: Sun Apr 3 00:53:48 2016 +0100
Committer: Stian Soiland-Reyes <st...@apache.org>
Committed: Sun Apr 3 01:07:39 2016 +0100
----------------------------------------------------------------------
.../rdf/simple/AbstractRDFParserBuilder.java | 41 ++++++++++++++++++++
1 file changed, 41 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/478b20e2/simple/src/main/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilder.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilder.java b/simple/src/main/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilder.java
index 925a365..b31251d 100644
--- a/simple/src/main/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilder.java
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/AbstractRDFParserBuilder.java
@@ -361,10 +361,51 @@ public abstract class AbstractRDFParserBuilder implements RDFParserBuilder, Clon
URI baseUri = c.sourceFile.get().toRealPath().toUri();
c.base = Optional.of(internalRdfTermFactory.createIRI(baseUri.toString()));
}
+
return c;
}
/**
+ * Guess RDFSyntax from a local file's extension.
+ * <p>
+ * This method can be used by subclasses if {@link #getContentType()} is not
+ * present and {@link #getSourceFile()} is set.
+ *
+ * @param path Path which extension should be checked
+ * @return The {@link RDFSyntax} which has a matching {@link RDFSyntax#fileExtension},
+ * otherwise {@link Optional#empty()}.
+ */
+ protected static Optional<RDFSyntax> guessRDFSyntax(Path path) {
+ return fileExtension(path).flatMap(RDFSyntax::byFileExtension);
+ }
+
+ /**
+ * Return the file extension of a Path - if any.
+ * <p>
+ * The returned file extension includes the leading <code>.</code>
+ * <p>
+ * Note that this only returns the last extension, e.g. the
+ * file extension for <code>archive.tar.gz</code> would be <code>.gz</code>
+ *
+ * @param path Path which filename might contain an extension
+ * @return File extension (including the leading <code>.</code>,
+ * or {@link Optional#empty()} if the path has no extension
+ */
+ private static Optional<String> fileExtension(Path path) {
+ Path fileName = path.getFileName();
+ if (fileName == null) {
+ return Optional.empty();
+ }
+ String filenameStr = fileName.toString();
+ int last = filenameStr.lastIndexOf(".");
+ if (last > -1) {
+ return Optional.of(filenameStr.substring(last));
+ }
+ return Optional.empty();
+ }
+
+
+ /**
* Create a new {@link RDFTermFactory} for a parse session.
* <p>
* This is called by {@link #parse()} to set