KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > activation > FileTypeMap


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)FileTypeMap.java 1.8 05/11/16
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.activation;
29
30 import java.io.File JavaDoc;
31
32 /**
33  * The FileTypeMap is an abstract class that provides a data typing
34  * interface for files. Implementations of this class will
35  * implement the getContentType methods which will derive a content
36  * type from a file name or a File object. FileTypeMaps could use any
37  * scheme to determine the data type, from examining the file extension
38  * of a file (like the MimetypesFileTypeMap) to opening the file and
39  * trying to derive its type from the contents of the file. The
40  * FileDataSource class uses the default FileTypeMap (a MimetypesFileTypeMap
41  * unless changed) to determine the content type of files.
42  *
43  * @see javax.activation.FileTypeMap
44  * @see javax.activation.FileDataSource
45  * @see javax.activation.MimetypesFileTypeMap
46  */

47
48 public abstract class FileTypeMap {
49
50     private static FileTypeMap JavaDoc defaultMap = null;
51
52     /**
53      * The default constructor.
54      */

55     public FileTypeMap() {
56     super();
57     }
58
59     /**
60      * Return the type of the file object. This method should
61      * always return a valid MIME type.
62      *
63      * @param file A file to be typed.
64      * @return The content type.
65      */

66     abstract public String JavaDoc getContentType(File JavaDoc file);
67
68     /**
69      * Return the type of the file passed in. This method should
70      * always return a valid MIME type.
71      *
72      * @param filename the pathname of the file.
73      * @return The content type.
74      */

75     abstract public String JavaDoc getContentType(String JavaDoc filename);
76
77     /**
78      * Sets the default FileTypeMap for the system. This instance
79      * will be returned to callers of getDefaultFileTypeMap.
80      *
81      * @param map The FileTypeMap.
82      * @exception SecurityException if the caller doesn't have permission
83      * to change the default
84      */

85     public static void setDefaultFileTypeMap(FileTypeMap JavaDoc map) {
86     SecurityManager JavaDoc security = System.getSecurityManager();
87     if (security != null) {
88         try {
89         // if it's ok with the SecurityManager, it's ok with me...
90
security.checkSetFactory();
91         } catch (SecurityException JavaDoc ex) {
92         // otherwise, we also allow it if this code and the
93
// factory come from the same class loader (e.g.,
94
// the JAF classes were loaded with the applet classes).
95
if (FileTypeMap JavaDoc.class.getClassLoader() !=
96             map.getClass().getClassLoader())
97             throw ex;
98         }
99     }
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 JavaDoc getDefaultFileTypeMap() {
113     // XXX - probably should be synchronized
114
if (defaultMap == null)
115         defaultMap = new MimetypesFileTypeMap JavaDoc();
116     return defaultMap;
117     }
118 }
119
Popular Tags