org.apache.fulcrum.mimetype.util
Class MimeTypeMapper

java.lang.Object
  extended by org.apache.fulcrum.mimetype.util.MimeTypeMapper

public class MimeTypeMapper
extends Object

This class defines 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.

Version:
$Id: MimeTypeMapper.java 826489 2009-10-18 18:54:59Z tv $
Author:
Ilkka Priha, Daniel Rall

Field Summary
protected  HashMap extensionMimeTypes
           
 
Constructor Summary
MimeTypeMapper()
          Constructs an empty MIME type mapper.
MimeTypeMapper(File file)
          Constructs a mapper reading from a file.
MimeTypeMapper(InputStream input)
          Constructs a mapper reading from a stream.
MimeTypeMapper(String path)
          Constructs a mapper reading from a file path.
 
Method Summary
 String getContentType(String ext)
          Gets a MIME content type corresponding to a specified file name extension.
 String getExtension(String type)
          Gets a file name extension corresponding to a specified MIME content type.
protected  void parse(BufferedReader reader)
          Parses MIME type extensions.
protected  int parseMimeTypeExtension(String spec, Map mimeTypes, Map extensions)
          Parses a MIME type extension.
 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

extensionMimeTypes

protected HashMap extensionMimeTypes
Constructor Detail

MimeTypeMapper

public MimeTypeMapper()
Constructs an empty MIME type mapper.


MimeTypeMapper

public MimeTypeMapper(InputStream input)
               throws IOException
Constructs a mapper reading from a stream.

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

MimeTypeMapper

public MimeTypeMapper(File file)
               throws IOException
Constructs a mapper reading from a file.

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

MimeTypeMapper

public MimeTypeMapper(String path)
               throws IOException
Constructs a mapper reading from a file path.

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

setContentType

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

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

getContentType

public String getContentType(String ext)
Gets a MIME content type corresponding to a specified file name extension. If a mapping is initially not found, tries a second lookup using the provided extension in lower case.

Parameters:
ext - The file name extension to resolve.
Returns:
The MIME type, or null if not found.

getExtension

public String getExtension(String type)
Gets a file name extension corresponding to a specified MIME content type.

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

parse

protected void parse(BufferedReader reader)
              throws IOException
Parses MIME type extensions.

Parameters:
reader - a reader to parse.
Throws:
IOException - for an incorrect reader.

parseMimeTypeExtension

protected int parseMimeTypeExtension(String spec,
                                     Map mimeTypes,
                                     Map extensions)
Parses a MIME type extension.

Parameters:
spec - an extension specification to parse.
mimeTypes - a map of MIME types.
extensions - a map of extensions.
Returns:
the number of file name extensions parsed.


Copyright © 2004-2009 The Apache Software Foundation. All Rights Reserved.