1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaf_classes/javax/activation/FileTypeMap.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,118 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package javax.activation; 1.30 + 1.31 +import java.io.File; 1.32 + 1.33 +/** 1.34 + * The FileTypeMap is an abstract class that provides a data typing 1.35 + * interface for files. Implementations of this class will 1.36 + * implement the getContentType methods which will derive a content 1.37 + * type from a file name or a File object. FileTypeMaps could use any 1.38 + * scheme to determine the data type, from examining the file extension 1.39 + * of a file (like the MimetypesFileTypeMap) to opening the file and 1.40 + * trying to derive its type from the contents of the file. The 1.41 + * FileDataSource class uses the default FileTypeMap (a MimetypesFileTypeMap 1.42 + * unless changed) to determine the content type of files. 1.43 + * 1.44 + * @see javax.activation.FileTypeMap 1.45 + * @see javax.activation.FileDataSource 1.46 + * @see javax.activation.MimetypesFileTypeMap 1.47 + * 1.48 + * @since 1.6 1.49 + */ 1.50 + 1.51 +public abstract class FileTypeMap { 1.52 + 1.53 + private static FileTypeMap defaultMap = null; 1.54 + 1.55 + /** 1.56 + * The default constructor. 1.57 + */ 1.58 + public FileTypeMap() { 1.59 + super(); 1.60 + } 1.61 + 1.62 + /** 1.63 + * Return the type of the file object. This method should 1.64 + * always return a valid MIME type. 1.65 + * 1.66 + * @param file A file to be typed. 1.67 + * @return The content type. 1.68 + */ 1.69 + abstract public String getContentType(File file); 1.70 + 1.71 + /** 1.72 + * Return the type of the file passed in. This method should 1.73 + * always return a valid MIME type. 1.74 + * 1.75 + * @param filename the pathname of the file. 1.76 + * @return The content type. 1.77 + */ 1.78 + abstract public String getContentType(String filename); 1.79 + 1.80 + /** 1.81 + * Sets the default FileTypeMap for the system. This instance 1.82 + * will be returned to callers of getDefaultFileTypeMap. 1.83 + * 1.84 + * @param map The FileTypeMap. 1.85 + * @exception SecurityException if the caller doesn't have permission 1.86 + * to change the default 1.87 + */ 1.88 + public static void setDefaultFileTypeMap(FileTypeMap map) { 1.89 + SecurityManager security = System.getSecurityManager(); 1.90 + if (security != null) { 1.91 + try { 1.92 + // if it's ok with the SecurityManager, it's ok with me... 1.93 + security.checkSetFactory(); 1.94 + } catch (SecurityException ex) { 1.95 + // otherwise, we also allow it if this code and the 1.96 + // factory come from the same class loader (e.g., 1.97 + // the JAF classes were loaded with the applet classes). 1.98 + if (FileTypeMap.class.getClassLoader() != 1.99 + map.getClass().getClassLoader()) 1.100 + throw ex; 1.101 + } 1.102 + } 1.103 + defaultMap = map; 1.104 + } 1.105 + 1.106 + /** 1.107 + * Return the default FileTypeMap for the system. 1.108 + * If setDefaultFileTypeMap was called, return 1.109 + * that instance, otherwise return an instance of 1.110 + * <code>MimetypesFileTypeMap</code>. 1.111 + * 1.112 + * @return The default FileTypeMap 1.113 + * @see javax.activation.FileTypeMap#setDefaultFileTypeMap 1.114 + */ 1.115 + public static FileTypeMap getDefaultFileTypeMap() { 1.116 + // XXX - probably should be synchronized 1.117 + if (defaultMap == null) 1.118 + defaultMap = new MimetypesFileTypeMap(); 1.119 + return defaultMap; 1.120 + } 1.121 +}