001 /*
002 * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.activation;
027
028 import java.io.File;
029
030 /**
031 * The FileTypeMap is an abstract class that provides a data typing
032 * interface for files. Implementations of this class will
033 * implement the getContentType methods which will derive a content
034 * type from a file name or a File object. FileTypeMaps could use any
035 * scheme to determine the data type, from examining the file extension
036 * of a file (like the MimetypesFileTypeMap) to opening the file and
037 * trying to derive its type from the contents of the file. The
038 * FileDataSource class uses the default FileTypeMap (a MimetypesFileTypeMap
039 * unless changed) to determine the content type of files.
040 *
041 * @see javax.activation.FileTypeMap
042 * @see javax.activation.FileDataSource
043 * @see javax.activation.MimetypesFileTypeMap
044 *
045 * @since 1.6
046 */
047
048 public abstract class FileTypeMap {
049
050 private static FileTypeMap defaultMap = null;
051
052 /**
053 * The default constructor.
054 */
055 public FileTypeMap() {
056 super ();
057 }
058
059 /**
060 * Return the type of the file object. This method should
061 * always return a valid MIME type.
062 *
063 * @param file A file to be typed.
064 * @return The content type.
065 */
066 abstract public String getContentType(File file);
067
068 /**
069 * Return the type of the file passed in. This method should
070 * always return a valid MIME type.
071 *
072 * @param filename the pathname of the file.
073 * @return The content type.
074 */
075 abstract public String getContentType(String filename);
076
077 /**
078 * Sets the default FileTypeMap for the system. This instance
079 * will be returned to callers of getDefaultFileTypeMap.
080 *
081 * @param map The FileTypeMap.
082 * @exception SecurityException if the caller doesn't have permission
083 * to change the default
084 */
085 public static void setDefaultFileTypeMap(FileTypeMap map) {
086 SecurityManager security = System.getSecurityManager();
087 if (security != null) {
088 try {
089 // if it's ok with the SecurityManager, it's ok with me...
090 security.checkSetFactory();
091 } catch (SecurityException ex) {
092 // otherwise, we also allow it if this code and the
093 // factory come from the same class loader (e.g.,
094 // the JAF classes were loaded with the applet classes).
095 if (FileTypeMap.class.getClassLoader() != map
096 .getClass().getClassLoader())
097 throw ex;
098 }
099 }
100 defaultMap = map;
101 }
102
103 /**
104 * Return the default FileTypeMap for the system.
105 * If setDefaultFileTypeMap was called, return
106 * that instance, otherwise return an instance of
107 * <code>MimetypesFileTypeMap</code>.
108 *
109 * @return The default FileTypeMap
110 * @see javax.activation.FileTypeMap#setDefaultFileTypeMap
111 */
112 public static FileTypeMap getDefaultFileTypeMap() {
113 // XXX - probably should be synchronized
114 if (defaultMap == null)
115 defaultMap = new MimetypesFileTypeMap();
116 return defaultMap;
117 }
118 }
|