1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.activation;
  6. import java.io.File;
  7. /**
  8. * The FileTypeMap is an abstract class that provides a data typing
  9. * interface for files. Implementations of this class will
  10. * implement the getContentType methods which will derive a content
  11. * type from a file name or a File object. FileTypeMaps could use any
  12. * scheme to determine the data type, from examining the file extension
  13. * of a file (like the MimetypesFileTypeMap) to opening the file and
  14. * trying to derive its type from the contents of the file. The
  15. * FileDataSource class uses the default FileTypeMap (a MimetypesFileTypeMap
  16. * unless changed) to determine the content type of files.
  17. *
  18. * @see javax.activation.FileTypeMap
  19. * @see javax.activation.FileDataSource
  20. * @see javax.activation.MimetypesFileTypeMap
  21. */
  22. public abstract class FileTypeMap {
  23. private static FileTypeMap defaultMap = null;
  24. /**
  25. * The default constructor.
  26. */
  27. public FileTypeMap() {
  28. super();
  29. }
  30. /**
  31. * Return the type of the file object. This method should
  32. * always return a valid MIME type.
  33. *
  34. * @param f A file to be typed.
  35. * @return The content type.
  36. */
  37. abstract public String getContentType(File file);
  38. /**
  39. * Return the type of the file passed in. This method should
  40. * always return a valid MIME type.
  41. *
  42. * @param filename the pathname of the file.
  43. * @return The content type.
  44. */
  45. abstract public String getContentType(String filename);
  46. /**
  47. * Sets the default FileTypeMap for the system. This instance
  48. * will be returned to callers of getDefaultFileTypeMap.
  49. *
  50. * @param map The FileTypeMap.
  51. * @exception SecurityException if the caller doesn't have permission
  52. * to change the default
  53. */
  54. public static void setDefaultFileTypeMap(FileTypeMap map) {
  55. SecurityManager security = System.getSecurityManager();
  56. if (security != null) {
  57. try {
  58. // if it's ok with the SecurityManager, it's ok with me...
  59. security.checkSetFactory();
  60. } catch (SecurityException ex) {
  61. // otherwise, we also allow it if this code and the
  62. // factory come from the same class loader (e.g.,
  63. // the JAF classes were loaded with the applet classes).
  64. if (FileTypeMap.class.getClassLoader() !=
  65. map.getClass().getClassLoader())
  66. throw ex;
  67. }
  68. }
  69. defaultMap = map;
  70. }
  71. /**
  72. * Return the default FileTypeMap for the system.
  73. * If setDefaultFileTypeMap was called, return
  74. * that instance, otherwise return an instance of
  75. * <code>MimetypesFileTypeMap</code>.
  76. *
  77. * @return The default FileTypeMap
  78. * @see javax.activation.FileTypeMap#setDefaultFileTypeMap
  79. */
  80. public static FileTypeMap getDefaultFileTypeMap() {
  81. // XXX - probably should be synchronized
  82. if (defaultMap == null)
  83. defaultMap = new MimetypesFileTypeMap();
  84. return defaultMap;
  85. }
  86. }