commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [3/4] incubator-commonsrdf git commit: Add guessRDFSyntax() convenience method
Date Sun, 03 Apr 2016 01:33:01 GMT
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/parser-writer-interface
Commit: 478b20e27b2e24494d20d6edbf7a80c3604a7879
Parents: 8dfc3f4
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Sun Apr 3 00:53:48 2016 +0100
Committer: Stian Soiland-Reyes <stain@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 


Mime
View raw message