From: <jrb...@us...> - 2010-12-22 17:32:25
|
Revision: 1183 http://cishell.svn.sourceforge.net/cishell/?rev=1183&view=rev Author: jrbibers Date: 2010-12-22 17:32:19 +0000 (Wed, 22 Dec 2010) Log Message: ----------- Updating FileUtilities.extractExtension(String format). Previously it attempted to derive the extension from the MIME type assuming it began with "file:text/". For a type like "file:application/pajeknet" it would return an empty string. We attempt to handle this better now with a Map from MIME types to extensions (excluding the case "file:text/foo" -> "foo" as we default to this). Reviewed by Patrick. Modified Paths: -------------- trunk/core/org.cishell.utilities/src/org/cishell/utilities/FileUtilities.java Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/FileUtilities.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/FileUtilities.java 2010-12-21 22:59:56 UTC (rev 1182) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/FileUtilities.java 2010-12-22 17:32:19 UTC (rev 1183) @@ -24,10 +24,28 @@ import org.osgi.service.log.LogService; +import com.google.common.collect.ImmutableMap; + public class FileUtilities { public static final int READ_TEXT_FILE_BUFFER_SIZE = 1024; public static final String DEFAULT_STREAM_TO_FILE_NAME = "stream_"; + /** Don't bother with "file:text/foo" -> "foo", we can find those automatically. */ + public static final ImmutableMap<String, String> MIME_TYPE_TO_FILE_EXTENSION = + new ImmutableMap.Builder<String, String>() + .put("file:application/pajekmat", "mat") + .put("file:application/pajeknet", "net") + .put("file:application/parvis", "stf") // TODO: This is a guess. + .put("file:text/bibtex", "bib") + .put("file:text/compartmentalmodel", "mdl") + .put("file:text/coord", "nwb") + .put("file:text/grace", "grace.dat") + .put("file:text/intsim", "int") + .put("file:text/plain", "txt") + .put("file:text/plot", "plot.dat") + .put("file:text/referbib", "refer") + .build(); + /* * Return a File pointing to the directory specified in * temporaryDirectoryPath, creating the directory if it doesn't @@ -395,16 +413,28 @@ return tempFile; } + /* TODO: We should really have explicit piece of metadata that says what + * the extension is, as this method is not guaranteed to yield the + * correct extension. + */ public static String extractExtension(String format) { String extension = ""; - /* TODO: We should really have explicit piece of metadata that says what - * the extension is, as this method is not guaranteed to yield the - * correct extension. - */ - if (format.startsWith("file:text/")) { - extension = "." + format.substring("file:text/".length()); - } else if (format.startsWith("file-ext:")) { - extension = "." + format.substring("file-ext:".length()); + + if (format.startsWith("file-ext:")) { + extension = format.substring("file-ext:".length()); + } else if (format.startsWith("file:")) { + if (MIME_TYPE_TO_FILE_EXTENSION.containsKey(format)) { + // Unless explicitly set.. + extension = MIME_TYPE_TO_FILE_EXTENSION.get(format); + } else { + if (format.contains("/")) { + // Whatever follows "/" + extension = format.substring(format.indexOf("/") + 1); + } else { + // Whatever follows "file:" + extension = format.substring("file:".length()); + } + } } extension = extension.replace('+', '.'); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |