+ Add method FileUtil.checkMIMEType(FileObject, String...)
+ for checking whether the file has one of specified MIME types.
+
+ Deprecate method FileUtil.getMIMEType(FileObject, String...)
.
+
+ * Note: Checking MIME type "content/unknown" (or null) may be more + * expensive than checking specific MIME types, because all resolvers need + * to be queried. + *
+ * + * @param fo The FileObject to check. + * @param mimeTypes Array of MIME types. It can also contain null or value + * "content/unknown", which means that the method will return value + * "content/unknown" if MIME type of {@code fo} is unrecognized. + * @return The actual MIME type if MIME type of {@code fo} is equal to one + * of values in {@code mimeTypes}; or value "content/unknown" if the MIME + * type of {@code fo} is unrecognized and {@code mimeTypes} contains null or + * value "content/unknown". Null otherwise (i.e. MIME type of the file is + * not contained in {@code mimeTypes}) + */ + public static String checkMIMEType(FileObject fo, String... mimeTypes) { + + // If we want to check that the MIME type is unrecognized, we need + // to query all resolvers. + boolean checkUnrecognized = false; + + String theMime = getMIMEType(fo, mimeTypes); + for (String oneOfTypes : mimeTypes) { + if (oneOfTypes == null || oneOfTypes.equals(MIME_CONTENT_UNKNOWN)) { + checkUnrecognized = true; + } else if (oneOfTypes.equals(theMime)) { + return theMime; + } + } + if (checkUnrecognized) { + String checkAllResult = getMIMEType(fo); + if (checkAllResult == null + || checkAllResult.equals(MIME_CONTENT_UNKNOWN)) { + return MIME_CONTENT_UNKNOWN; + } + } + return null; + } + /** 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 diff --git a/openide.filesystems/test/unit/src/org/openide/filesystems/FileUtilTest.java b/openide.filesystems/test/unit/src/org/openide/filesystems/FileUtilTest.java --- a/openide.filesystems/test/unit/src/org/openide/filesystems/FileUtilTest.java +++ b/openide.filesystems/test/unit/src/org/openide/filesystems/FileUtilTest.java @@ -86,6 +86,7 @@ // These tests have to be run in correct order, see bug 231316. List