- /*
- * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
- * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
- */
-
- package javax.activation;
-
- import java.io.*;
- import com.sun.activation.registries.MimeTypeFile;
-
- /**
- * This class extends FileTypeMap and provides data typing of files
- * via their file extension. It uses the <code>.mime.types</code> format. <p>
- *
- * <b>MIME types file search order:</b><p>
- * The MimetypesFileTypeMap looks in various places in the user's
- * system for MIME types file entries. When requests are made
- * to search for MIME types in the MimetypesFileTypeMap, it searches
- * MIME types files in the following order:
- * <p>
- * <ol>
- * <li> Programmatically added entries to the MimetypesFileTypeMap instance.
- * <li> The file <code>.mime.types</code> in the user's home directory.
- * <li> The file <<i>java.home</i>><code>/lib/mime.types</code>.
- * <li> The file or resource named <code>META-INF/mime.types</code>.
- * <li> The file or resource named <code>META-INF/mimetypes.default</code>
- * (usually found only in the <code>activation.jar</code> file).
- * </ol>
- * <p>
- * <b>MIME types file format:</b><p>
- *
- * <code>
- * # comments begin with a '#'<br>
- * # the format is <mime type> <space separated file extensions><br>
- * # for example:<br>
- * text/plain txt text TXT<br>
- * # this would map file.txt, file.text, and file.TXT to<br>
- * # the mime type "text/plain"<br>
- * </code>
- *
- * @author Bart Calder
- * @author Bill Shannon
- */
- public class MimetypesFileTypeMap extends FileTypeMap {
- /*
- * We manage up to 5 databases, searched in order.
- * The default database is shared between all instances
- * of this class.
- * XXX - Can we safely share more databases between instances?
- */
- private static MimeTypeFile defDB = null;
- private MimeTypeFile[] DB = new MimeTypeFile[5];
- private static final int PROG = 0; // programmatically added entries
- private static final int HOME = 1; // the user's home directory
- private static final int SYS = 2; // the system file
- private static final int JAR = 3; // the app's entry in the jar file
- private static final int DEF = 4; // the default entry in our jar file
-
- private static String defaultType = "application/octet-stream";
-
- /**
- * The default constructor.
- */
- public MimetypesFileTypeMap() {
- try {
- String user_home = System.getProperty("user.home");
-
- if (user_home != null) {
- String path = user_home + File.separator + ".mime.types";
- DB[HOME] = loadFile(path);
- }
- } catch (SecurityException ex) {}
-
- try {
- // check system's home
- String system_mimetypes = System.getProperty("java.home") +
- File.separator + "lib" + File.separator + "mime.types";
- DB[SYS] = loadFile(system_mimetypes);
- } catch (SecurityException ex) {}
-
- // load from the app's jar file
- DB[JAR] = loadResource("/META-INF/mime.types");
-
- synchronized (MimetypesFileTypeMap.class) {
- // see if another instance has created this yet.
- if (defDB == null)
- defDB = loadResource("/META-INF/mimetypes.default");
- }
-
- DB[DEF] = defDB;
- }
-
- /**
- * Load the named resource.
- */
- private MimeTypeFile loadResource(String name) {
- try {
- InputStream clis = this.getClass().getResourceAsStream(name);
- if (clis != null)
- return new MimeTypeFile(clis);
- } catch (IOException e) {
- // e.printStackTrace();
- }
- return null;
- }
-
- /**
- * Load the named file.
- */
- private MimeTypeFile loadFile(String name) {
- MimeTypeFile mtf = null;
-
- try {
- mtf = new MimeTypeFile(name);
- } catch (IOException e) {
- // e.printStackTrace();
- }
- return mtf;
- }
-
- /**
- * Construct a MimetypesFileTypeMap with programmatic entries
- * added from the named file.
- *
- * @param mimeTypeFileName the file name
- */
- public MimetypesFileTypeMap(String mimeTypeFileName) throws IOException {
- this();
- DB[PROG] = new MimeTypeFile(mimeTypeFileName);
- }
-
- /**
- * Construct a MimetypesFileTypeMap with programmatic entries
- * added from the InputStream.
- *
- * @param is the input stream to read from
- */
- public MimetypesFileTypeMap(InputStream is) {
- this();
- try {
- DB[PROG] = new MimeTypeFile(is);
- } catch (IOException ex) {
- // XXX - really should throw it
- }
- }
-
- /**
- * Prepend the MIME type values to the registry.
- *
- * @param mime_types A .mime.types formatted string of entries.
- */
- public synchronized void addMimeTypes(String mime_types) {
- // check to see if we have created the registry
- if (DB[PROG] == null)
- DB[PROG] = new MimeTypeFile(); // make one
-
- DB[PROG].appendToRegistry(mime_types);
- }
-
- /**
- * Return the MIME type of the file object.
- * The implementation in this class calls
- * <code>getContentType(f.getName())</code>.
- *
- * @param f the file
- * @return the file's MIME type
- */
- public String getContentType(File f) {
- return this.getContentType(f.getName());
- }
-
- /**
- * Return the MIME type based on the specified file name.
- * The MIME type entries are searched as described above under
- * <i>MIME types file search order</i>.
- * If no entry is found, the type "application/octet-stream" is returned.
- *
- * @param filename the file name
- * @return the file's MIME type
- */
- public synchronized String getContentType(String filename) {
- int dot_pos = filename.lastIndexOf("."); // period index
-
- if (dot_pos < 0)
- return defaultType;
-
- String file_ext = filename.substring(dot_pos + 1);
- if (file_ext.length() == 0)
- return defaultType;
-
- for (int i = 0; i < DB.length; i++) {
- if (DB[i] == null)
- continue;
- String result = DB[i].getMIMETypeString(file_ext);
- if (result != null)
- return result;
- }
- return defaultType;
- }
-
- /**
- * for debugging...
- *
- public static void main(String[] argv) throws Exception {
- MimetypesFileTypeMap map = new MimetypesFileTypeMap();
- System.out.println("File " + argv[0] + " has MIME type " +
- map.getContentType(argv[0]));
- System.exit(0);
- }
- */
- }