org.norther.tammi.acorn.mime
Class MimeTypeMap

java.lang.Object
  extended by org.norther.tammi.acorn.mime.MimeTypeMap

public class MimeTypeMap
extends Object

MimeTypeMap maintains a set of mappers defining mappings between MIME types and the corresponding file name extensions. The mappings are defined as lines formed by a MIME type name followed by a list of extensions separated by a whitespace. The definitions can be listed in MIME type files located in user's home directory, Java home directory or the current class jar. In addition, this class maintains static default mappings and constructors support application specific mappings.

Author:
Ilkka Priha

Field Summary
static String MIMETYPE_RESOURCE
          The name for MIME type mapper resources.
 
Constructor Summary
MimeTypeMap()
          Constructs a new MIME type map with default mappers.
MimeTypeMap(File file)
          Contructs a MIME type map read from a file.
MimeTypeMap(InputStream input)
          Contructs a MIME type map read from a stream.
MimeTypeMap(String path)
          Contructs a MIME type map read from a file path.
 
Method Summary
 String getContentType(File file)
          Gets the MIME content type for a file as a string.
 String getContentType(String path)
          Gets the MIME content type for a named file as a string.
 String getContentType(String ext, String def)
          Gets the MIME content type for a file name extension as a string.
 String[] getContentTypes(String ext)
          Gets the MIME content types for a file name extension.
 String getDefaultExtension(MimeType mime)
          Gets the default file name extension for a MIME type.
 String getDefaultExtension(String type)
          Gets the default file name extension for a MIME type.
 String[] getExtensions(String type)
          Gets the file name extensions for a MIME type.
 MimeType getMimeType(File file)
          Gets the MIME content type for a file.
 MimeType getMimeType(String name)
          Gets the MIME content type for a named file.
 MimeType getMimeType(String ext, String def)
          Gets the MIME content type for a file name extension.
protected static MimeTypeMapper loadPath(String path)
          Loads mappings from a file path.
protected static MimeTypeMapper loadResource(String name)
          Loads mappings from a resource.
protected  void setCommonContentType(String spec)
          Sets a base MIME content type mapping to extensions.
 void setContentType(String spec)
          Sets a MIME content type mapping to extensions.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MIMETYPE_RESOURCE

public static final String MIMETYPE_RESOURCE
The name for MIME type mapper resources.

See Also:
Constant Field Values
Constructor Detail

MimeTypeMap

public MimeTypeMap()
Constructs a new MIME type map with default mappers.


MimeTypeMap

public MimeTypeMap(InputStream input)
            throws IOException
Contructs a MIME type map read from a stream.

Parameters:
input - an input stream.
Throws:
IOException - for an incorrect stream.

MimeTypeMap

public MimeTypeMap(File file)
            throws IOException
Contructs a MIME type map read from a file.

Parameters:
file - an input file.
Throws:
IOException - for an incorrect input file.

MimeTypeMap

public MimeTypeMap(String path)
            throws IOException
Contructs a MIME type map read from a file path.

Parameters:
path - an input file path.
Throws:
IOException - for an incorrect input file.
Method Detail

loadPath

protected static MimeTypeMapper loadPath(String path)
                                  throws IOException
Loads mappings from a file path.

Parameters:
path - a file path.
Returns:
the mappings.
Throws:
IOException - for an incorrect file.

loadResource

protected static MimeTypeMapper loadResource(String name)
Loads mappings from a resource.

Parameters:
name - a resource name.
Returns:
the mappings.

setContentType

public void setContentType(String spec)
Sets a MIME content type mapping to extensions.

Parameters:
spec - a MIME type extension specification to set.

getContentType

public String getContentType(File file)
Gets the MIME content type for a file as a string.

Parameters:
file - the file.
Returns:
the MIME type string.

getContentType

public String getContentType(String path)
Gets the MIME content type for a named file as a string.

Parameters:
path - the file path.
Returns:
the MIME type string.

getContentType

public String getContentType(String ext,
                             String def)
Gets the MIME content type for a file name extension as a string.

Parameters:
ext - the file name extension.
def - the default type if none is found.
Returns:
the MIME type string.

getContentTypes

public String[] getContentTypes(String ext)
Gets the MIME content types for a file name extension.

Parameters:
ext - the file name extension.
Returns:
an array of MIME type strings or null.

getMimeType

public MimeType getMimeType(File file)
Gets the MIME content type for a file.

Parameters:
file - the file.
Returns:
the MIME type.

getMimeType

public MimeType getMimeType(String name)
Gets the MIME content type for a named file.

Parameters:
name - the name of the file.
Returns:
the MIME type.

getMimeType

public MimeType getMimeType(String ext,
                            String def)
Gets the MIME content type for a file name extension.

Parameters:
ext - the file name extension.
def - the default type if none is found.
Returns:
the MIME type.

getDefaultExtension

public String getDefaultExtension(String type)
Gets the default file name extension for a MIME type. Note that the mappers are called in the reverse order.

Parameters:
type - the MIME type as a string.
Returns:
the file name extension or null.

getDefaultExtension

public String getDefaultExtension(MimeType mime)
Gets the default file name extension for a MIME type. Note that the mappers are called in the reverse order.

Parameters:
mime - the MIME type.
Returns:
the file name extension or null.

getExtensions

public String[] getExtensions(String type)
Gets the file name extensions for a MIME type.

Parameters:
type - the MIME type as a string.
Returns:
an array of file name extensions or null.

setCommonContentType

protected void setCommonContentType(String spec)
Sets a base MIME content type mapping to extensions.

Parameters:
spec - a MIME type extension specification to set.


Copyright © 2004 The Norther Organization. All rights reserved.