< prev index next >

src/java.activation/share/classes/javax/activation/MimetypesFileTypeMap.java

Print this page




  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.activation;
  27 
  28 import java.io.*;
  29 import java.net.*;
  30 import java.util.*;
  31 import com.sun.activation.registries.MimeTypeFile;
  32 import com.sun.activation.registries.LogSupport;
  33 
  34 /**
  35  * This class extends FileTypeMap and provides data typing of files
  36  * via their file extension. It uses the <code>.mime.types</code> format. <p>
  37  *
  38  * <b>MIME types file search order:</b><p>
  39  * The MimetypesFileTypeMap looks in various places in the user's
  40  * system for MIME types file entries. When requests are made
  41  * to search for MIME types in the MimetypesFileTypeMap, it searches
  42  * MIME types files in the following order:
  43  * <p>
  44  * <ol>
  45  * <li> Programmatically added entries to the MimetypesFileTypeMap instance.
  46  * <li> The file <code>.mime.types</code> in the user's home directory.
  47  * <li> The file &lt;<i>java.home</i>&gt;<code>/lib/mime.types</code>.
  48  * <li> The file or resources named <code>META-INF/mime.types</code>.
  49  * <li> The file or resource named <code>META-INF/mimetypes.default</code>
  50  * (usually found only in the <code>activation.jar</code> file).
  51  * </ol>
  52  * <p>
  53  * <b>MIME types file format:</b><p>
  54  *
  55  * <code>
  56  * # comments begin with a '#'<br>
  57  * # the format is &lt;mime type> &lt;space separated file extensions><br>
  58  * # for example:<br>
  59  * text/plain    txt text TXT<br>
  60  * # this would map file.txt, file.text, and file.TXT to<br>
  61  * # the mime type "text/plain"<br>
  62  * </code>
  63  *
  64  * @author Bart Calder
  65  * @author Bill Shannon
  66  *
  67  * @since 1.6
  68  */
  69 public class MimetypesFileTypeMap extends FileTypeMap {
  70     /*
  71      * We manage a collection of databases, searched in order.
  72      */
  73     private MimeTypeFile[] DB;
  74     private static final int PROG = 0;  // programmatically added entries
  75 
  76     private static String defaultType = "application/octet-stream";
  77 
  78     /**
  79      * The default constructor.
  80      */
  81     public MimetypesFileTypeMap() {
  82         Vector dbv = new Vector(5);     // usually 5 or less databases


 260             // XXX - really should throw it
 261         }
 262     }
 263 
 264     /**
 265      * Prepend the MIME type values to the registry.
 266      *
 267      * @param mime_types A .mime.types formatted string of entries.
 268      */
 269     public synchronized void addMimeTypes(String mime_types) {
 270         // check to see if we have created the registry
 271         if (DB[PROG] == null)
 272             DB[PROG] = new MimeTypeFile(); // make one
 273 
 274         DB[PROG].appendToRegistry(mime_types);
 275     }
 276 
 277     /**
 278      * Return the MIME type of the file object.
 279      * The implementation in this class calls
 280      * <code>getContentType(f.getName())</code>.
 281      *
 282      * @param f the file
 283      * @return  the file's MIME type
 284      */
 285     public String getContentType(File f) {
 286         return this.getContentType(f.getName());
 287     }
 288 
 289     /**
 290      * Return the MIME type based on the specified file name.
 291      * The MIME type entries are searched as described above under
 292      * <i>MIME types file search order</i>.
 293      * If no entry is found, the type "application/octet-stream" is returned.
 294      *
 295      * @param filename  the file name
 296      * @return          the file's MIME type
 297      */
 298     public synchronized String getContentType(String filename) {
 299         int dot_pos = filename.lastIndexOf("."); // period index
 300 




  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.activation;
  27 
  28 import java.io.*;
  29 import java.net.*;
  30 import java.util.*;
  31 import com.sun.activation.registries.MimeTypeFile;
  32 import com.sun.activation.registries.LogSupport;
  33 
  34 /**
  35  * This class extends FileTypeMap and provides data typing of files
  36  * via their file extension. It uses the {@code .mime.types} format. <p>
  37  *
  38  * <b>MIME types file search order:</b><p>
  39  * The MimetypesFileTypeMap looks in various places in the user's
  40  * system for MIME types file entries. When requests are made
  41  * to search for MIME types in the MimetypesFileTypeMap, it searches
  42  * MIME types files in the following order:

  43  * <ol>
  44  * <li> Programmatically added entries to the MimetypesFileTypeMap instance.
  45  * <li> The file {@code .mime.types} in the user's home directory.
  46  * <li> The file {@literal <}<i>java.home</i>{@literal >}{@code /lib/mime.types}.
  47  * <li> The file or resources named {@code META-INF/mime.types}.
  48  * <li> The file or resource named {@code META-INF/mimetypes.default}
  49  * (usually found only in the {@code activation.jar} file).
  50  * </ol>
  51  * <p>
  52  * <b>MIME types file format:</b>
  53  *
  54  * <pre>{@code
  55  * # comments begin with a '#'
  56  * # the format is <mime type> <space separated file extensions>
  57  * # for example:
  58  * text/plain    txt text TXT
  59  * # this would map file.txt, file.text, and file.TXT to
  60  * # the mime type "text/plain"
  61  * }</pre>
  62  *
  63  * @author Bart Calder
  64  * @author Bill Shannon
  65  *
  66  * @since 1.6
  67  */
  68 public class MimetypesFileTypeMap extends FileTypeMap {
  69     /*
  70      * We manage a collection of databases, searched in order.
  71      */
  72     private MimeTypeFile[] DB;
  73     private static final int PROG = 0;  // programmatically added entries
  74 
  75     private static String defaultType = "application/octet-stream";
  76 
  77     /**
  78      * The default constructor.
  79      */
  80     public MimetypesFileTypeMap() {
  81         Vector dbv = new Vector(5);     // usually 5 or less databases


 259             // XXX - really should throw it
 260         }
 261     }
 262 
 263     /**
 264      * Prepend the MIME type values to the registry.
 265      *
 266      * @param mime_types A .mime.types formatted string of entries.
 267      */
 268     public synchronized void addMimeTypes(String mime_types) {
 269         // check to see if we have created the registry
 270         if (DB[PROG] == null)
 271             DB[PROG] = new MimeTypeFile(); // make one
 272 
 273         DB[PROG].appendToRegistry(mime_types);
 274     }
 275 
 276     /**
 277      * Return the MIME type of the file object.
 278      * The implementation in this class calls
 279      * {@code getContentType(f.getName())}.
 280      *
 281      * @param f the file
 282      * @return  the file's MIME type
 283      */
 284     public String getContentType(File f) {
 285         return this.getContentType(f.getName());
 286     }
 287 
 288     /**
 289      * Return the MIME type based on the specified file name.
 290      * The MIME type entries are searched as described above under
 291      * <i>MIME types file search order</i>.
 292      * If no entry is found, the type "application/octet-stream" is returned.
 293      *
 294      * @param filename  the file name
 295      * @return          the file's MIME type
 296      */
 297     public synchronized String getContentType(String filename) {
 298         int dot_pos = filename.lastIndexOf("."); // period index
 299 


< prev index next >