+ To speed up MIME type recognition it is added an extra parameter
+ to method FileUtil.getMIMEType(FileObject, String...).
+ We can supply one or more MIME types which we are only interested in.
+ Module writers have to override default constructor and call
+ super(String...)
, e.g.:
+
+ public MyResolver() { + super("text/plain", "text/sh"); + } ++
"jar"
, "zip"
, etc.
* @return the MIME type for the extension, or null
if the extension is unrecognized
- * @deprecated use {@link #getMIMEType(FileObject) getMIMEType(FileObject)} as MIME cannot
- * be generally detected by file object extension.
+ * @deprecated use {@link #getMIMEType(FileObject)} or {@link #getMIMEType(FileObject, String[])}
+ * as MIME cannot be generally detected by file object extension.
*/
@Deprecated
public static String getMIMEType(String ext) {
@@ -1118,6 +1118,27 @@
*/
public static String getMIMEType(FileObject fo) {
String retVal = MIMESupport.findMIMEType(fo, null);
+
+ if (retVal == null) {
+ retVal = getMIMEType(fo.getExt());
+ }
+
+ return retVal;
+ }
+
+ /** 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.
+ * @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
+ */
+ 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());
diff --git a/openide.filesystems/src/org/openide/filesystems/MIMEResolver.java b/openide.filesystems/src/org/openide/filesystems/MIMEResolver.java
--- a/openide.filesystems/src/org/openide/filesystems/MIMEResolver.java
+++ b/openide.filesystems/src/org/openide/filesystems/MIMEResolver.java
@@ -40,6 +40,8 @@
*/
package org.openide.filesystems;
+import org.openide.util.Parameters;
+
/**
* This class is intended as superclass for individual resolvers.
* All registered subclasses of MIMEResolver are looked up and asked one by one
@@ -57,6 +59,35 @@
* @author rmatous
*/
public abstract class MIMEResolver {
+
+ private String[] resolvableMIMETypes = null;
+
+ /** Creates a new MIMEResolver.
+ * @param mimeTypes an array of MIME types which can be resolved by this resolver.
+ * It should contain all MIME types which {@link #findMIMEType} can return.
+ * If something is missing, this resolver can be ignored, when searching for that
+ * missing MIME type (see {@link FileUtil#getMIMEType(FileObject, String...)}).
+ */
+ public MIMEResolver(String... mimeTypes) {
+ Parameters.notNull("mimeTypes", mimeTypes); //NOI18N
+ if(mimeTypes.length == 0) {
+ throw new IllegalArgumentException("The mimeTypes parameter cannot be empty array."); //NOI18N
+ }
+ for (String mimeType : mimeTypes) {
+ if(mimeType == null || mimeType.length() == 0) {
+ throw new IllegalArgumentException("The item in mimeTypes parameter cannot be null nor empty String."); //NOI18N
+ }
+ }
+ resolvableMIMETypes = mimeTypes;
+
+ //the MIMEResolver(String...) constructor does not check for a null or
+ //empty array, nor for nulls in the array.
+ }
+
+ /** Creates a new MIMEResolver. */
+ public MIMEResolver() {
+ }
+
/**
* Resolves FileObject and returns recognized MIME type
* @param fo is FileObject which should be resolved (This FileObject is not
@@ -64,4 +95,11 @@
* @return recognized MIME type or null if not recognized
*/
public abstract String findMIMEType(FileObject fo);
+
+ /** Returns an array of MIME types which can be resolved by this resolver.
+ * @return a non-empty array of MIME types
+ */
+ String[] getMIMETypes() {
+ return resolvableMIMETypes;
+ }
}
diff --git a/openide.filesystems/src/org/openide/filesystems/MIMESupport.java b/openide.filesystems/src/org/openide/filesystems/MIMESupport.java
--- a/openide.filesystems/src/org/openide/filesystems/MIMESupport.java
+++ b/openide.filesystems/src/org/openide/filesystems/MIMESupport.java
@@ -61,6 +61,7 @@
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.util.Union2;
+import org.openide.util.WeakSet;
/**
* This class is intended to enhance MIME resolving. This class offers
@@ -97,8 +98,9 @@
/** Asks all registered subclasses of MIMEResolver to resolve FileObject passed as parameter.
* @param fo is FileObject, whose MIME should be resolved
* @param def the default value to return or null
+ * @param withinMIMETypes an array of MIME types which only should be considered
* @return MIME type or null if not resolved*/
- static String findMIMEType(FileObject fo, String def) {
+ static String findMIMEType(FileObject fo, String def, String... withinMIMETypes) {
if (!fo.isValid() || fo.isFolder()) {
return null;
}
@@ -121,7 +123,7 @@
lastCfo = EMPTY;
}
- return cfo.getMIMEType(def);
+ return cfo.getMIMEType(def, withinMIMETypes);
} finally {
synchronized (lock) {
lastFo = new WeakReference