+ Method + FileUtil.setMIMEType(String extension, String mimeType) + resurrected to register file extension for specified MIME type. + It is persisted in userdir contrary to previous implementation. + Added method + FileUtil.getMIMETypeExtensions(String mimeType) + to get list of file extensions associated with specified MIME type. +
+"text/plain"
; never null
*/
public String getMIMEType() {
- return FileUtil.getMIMETypeOrDefault(this);
+ String mimeType = FileUtil.getMIMEType(this);
+ return mimeType == null ? "content/unknown" : mimeType; //NOI18N
}
/** Get the size of the file.
diff --git a/openide.filesystems/src/org/openide/filesystems/FileUtil.java b/openide.filesystems/src/org/openide/filesystems/FileUtil.java
--- a/openide.filesystems/src/org/openide/filesystems/FileUtil.java
+++ b/openide.filesystems/src/org/openide/filesystems/FileUtil.java
@@ -52,13 +52,13 @@
import java.net.URI;
import java.net.URL;
import java.net.URLStreamHandler;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Dictionary;
+import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
-import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -73,6 +73,7 @@
import javax.swing.Icon;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
+import org.netbeans.modules.openide.filesystems.declmime.MIMEResolverImpl;
import org.openide.filesystems.FileSystem.AtomicAction;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle;
@@ -108,16 +109,6 @@
transientAttributes.add("SystemFileSystem.icon"); // NOI18N
transientAttributes.add("SystemFileSystem.icon32"); // NOI18N
transientAttributes.add("position"); // NOI18N
- }
-
- /* mapping of file extensions to content-types */
- private static Dictionarynull
if the FileObject is unrecognized
+ * @return the MIME type for the FileObject, or {@code null} if the FileObject is unrecognized.
+ * It may return {@code content/unknown} instead of {@code null}.
*/
public static String getMIMEType(FileObject fo) {
- String retVal = MIMESupport.findMIMEType(fo, null);
-
- if (retVal == null) {
- retVal = getMIMEType(fo.getExt());
- }
-
- return retVal;
+ return MIMESupport.findMIMEType(fo);
}
/** Resolves MIME type. Registered resolvers are invoked and used to achieve this goal.
- * Resolvers must subclass MIMEResolver. If resolvers don't recognize MIME type then
- * MIME type is obtained for a well-known extension.
+ * Resolvers must subclass MIMEResolver.
* @param fo whose MIME type should be recognized
* @param withinMIMETypes an array of MIME types. Only resolvers whose
* {@link MIMEResolver#getMIMETypes} contain one or more of the requested
* MIME types will be asked if they recognize the file. It is possible for
* the resulting MIME type to not be a member of this list.
* @return the MIME type for the FileObject, or null
if
- * the FileObject is unrecognized. It is possible for the resulting MIME type
- * to not be a member of given list.
+ * the FileObject is unrecognized. It may return {@code content/unknown} instead of {@code null}.
+ * It is possible for the resulting MIME type to not be a member of given list.
* @since 7.13
*/
public static String getMIMEType(FileObject fo, String... withinMIMETypes) {
Parameters.notNull("withinMIMETypes", withinMIMETypes); //NOI18N
- String retVal = MIMESupport.findMIMEType(fo, null, withinMIMETypes);
-
- if (retVal == null) {
- retVal = getMIMEType(fo.getExt());
- }
-
- return retVal;
+ return MIMESupport.findMIMEType(fo, withinMIMETypes);
}
- /** Finds mime type by calling getMIMEType, but
- * instead of returning null it fallbacks to default type
- * either text/plain or content/unknown (even for folders)
+ /** Registers specified extension to be recognized as specified MIME type.
+ * If MIME type parameter is null, it cancels previous registration.
+ * Note that you may register a case-sensitive extension if that is
+ * relevant (for example {@literal *.C} for C++) but if you register
+ * a lowercase extension it will by default apply to uppercase extensions
+ * too on Windows.
+ * @param extension the file extension to be registered
+ * @param mimeType the MIME type to be registered for the extension or {@code null} to deregister
+ * @see #getMIMEType(FileObject)
+ * @see #getMIMETypeExtensions(String)
*/
- static String getMIMETypeOrDefault(FileObject fo) {
- String def = getMIMEType(fo.getExt());
- String t = MIMESupport.findMIMEType(fo, def);
-
- if (t == null) {
- // #42965: never allowed
- t = "content/unknown"; // NOI18N
+ public static void setMIMEType(String extension, String mimeType) {
+ Parameters.notEmpty("extension", extension); //NOI18N
+ final Map