ccs.mime
Class MimeRegistry

java.lang.Object
  extended by ccs.mime.MimeRegistry

public class MimeRegistry
extends java.lang.Object

This utility provides mappings between a list of MIME types, the (many) filename extensions which correspond to that type, and extra information about that MIME type.


Constructor Summary
MimeRegistry()
          Creates an empty MIME registry.
 
Method Summary
 void clear()
          Empty the MimeRegistry completely.
static MimeRegProvider defaultProvider()
          A default MimeRegistry-provider.
 MimeType[] getMimeTypes()
          Obtains the list of MIME types.
 MimeType infoForType(java.lang.String mimetype)
          Returns the available information for a given MIME type
 void loadDefault()
          loads the default set of MIME types into the registry.
 void marshalTo(java.io.DataOutputStream dest)
          Marshals the MIME registry onto the supplied stream.
 java.lang.String mimeTypeFor(java.lang.String path)
          Deprecated. This method is unreliable. Use the version with two parameters instead.
 java.lang.String mimeTypeFor(java.lang.String path, char separator)
          Utility function to return the MIME type of a given file based on its filename extension.
 java.lang.String mimeTypeFor(java.net.URLConnection conn)
          Utility function to work out the MIME type of a given URLConnection.
 void register(MimeType mt)
          Add a MimeType to the registry.
 MimeType typeForExtension(java.lang.String extn)
          Returns the MimeType corresponding to a given extension.
 MimeType typeForPath(java.lang.String path)
          Deprecated. This is unreliable. Use the two-parameter version instead, or use MUtil.getExtn directly.
 MimeType typeForPath(java.lang.String path, char separator)
          Wrapper to extract the extension from a path or filename.
 void unmarshalFrom(java.io.DataInputStream src)
          Unmarshals the MIME registry from the supplied stream.
 void unregister(MimeType mt)
          remove a MimeType from the registry.
 void xmlMarshal(CXTStream cxt)
          Marshals the MIME registry as XML.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MimeRegistry

public MimeRegistry()
Creates an empty MIME registry.

Method Detail

infoForType

public MimeType infoForType(java.lang.String mimetype)
Returns the available information for a given MIME type

Parameters:
mimetype - The type string ("text/html" or whatever).
Returns:
The MimeType object containing the information we have about this MIME type, or null if we don't recognise the type.

typeForExtension

public MimeType typeForExtension(java.lang.String extn)
Returns the MimeType corresponding to a given extension.

Parameters:
extn - The extension. The leading '.' is required.
Returns:
The MimeType object containing the information we have about this MIME type, or null if we don't recognise the type.

typeForPath

public MimeType typeForPath(java.lang.String path)
Deprecated. This is unreliable. Use the two-parameter version instead, or use MUtil.getExtn directly.

Wrapper to extract the extension from a path or filename.

Parameters:
path - The path or filename of the file.
Returns:
The MimeType for the file, if available.

typeForPath

public MimeType typeForPath(java.lang.String path,
                            char separator)
Wrapper to extract the extension from a path or filename.

Parameters:
path - The path or filename of the file.
separator - The separator char on the filesystem the path came from. '\\' for Win32, '/' for everything else.
Returns:
The MimeType for the file, if available.

clear

public void clear()
Empty the MimeRegistry completely.


register

public void register(MimeType mt)
Add a MimeType to the registry.

Parameters:
mt - The MimeType to add.
Throws:
LoseException - If the type is already registered, or claims an extension that is already owned by another type.

unregister

public void unregister(MimeType mt)
                throws LoseException
remove a MimeType from the registry.

Parameters:
mt - the MimeType to remove.
Throws:
LoseException - If the type is not registered here.

getMimeTypes

public MimeType[] getMimeTypes()
Obtains the list of MIME types. The list is sorted.

Returns:
The sorted list.

marshalTo

public void marshalTo(java.io.DataOutputStream dest)
               throws java.io.IOException
Marshals the MIME registry onto the supplied stream.

Parameters:
dest - The stream to marshal onto.
Throws:
java.io.IOException - If thrown by the stream.

unmarshalFrom

public void unmarshalFrom(java.io.DataInputStream src)
                   throws java.io.IOException
Unmarshals the MIME registry from the supplied stream.

Parameters:
src - The stream to unmarshal from.
Throws:
java.io.IOException - If thrown by the stream.

xmlMarshal

public void xmlMarshal(CXTStream cxt)
                throws java.io.IOException
Marshals the MIME registry as XML.

Parameters:
cxt - The stream to marshal to
Throws:
java.io.IOException - If thrown by the stream.

mimeTypeFor

public java.lang.String mimeTypeFor(java.net.URLConnection conn)
Utility function to work out the MIME type of a given URLConnection. It tries the following, in order:
  1. Asks the connection itself.
  2. If that returns null, it asks itself.
  3. If that returns null, it asks the system FileNameMap.
  4. If that returns null, it returns application/octet-stream.

Parameters:
conn - The URLConnection.
Returns:
The MIME type of the connection, as far as is available.

mimeTypeFor

public java.lang.String mimeTypeFor(java.lang.String path)
Deprecated. This method is unreliable. Use the version with two parameters instead.

Utility function to return the MIME type of a given file based on its filename extension. It tries the following, in order:
  1. It asks itself.
  2. If that returns null, it asks the system FileNameMap.
  3. If that returns null, it returns application/octet-stream.

Parameters:
path - The path or filename of the file.
Returns:
The MIME type of the connection, as far as is available. Note that this may be a superset of the types available via typeForPath

mimeTypeFor

public java.lang.String mimeTypeFor(java.lang.String path,
                                    char separator)
Utility function to return the MIME type of a given file based on its filename extension. It tries the following, in order:
  1. It asks itself.
  2. If that returns null, it asks the system FileNameMap.
  3. If that returns null, it returns application/octet-stream.

Parameters:
path - The path or filename of the file.
separator - The separator char of the filesystem the path comes from.
Returns:
The MIME type of the connection, as far as is available. Note that this may be a superset of the types available via typeForPath

loadDefault

public void loadDefault()
                 throws LoseException,
                        java.io.IOException
loads the default set of MIME types into the registry. Any previous entries are deleted.

Throws:
LoseException - If there is a conflict with any pre-existing types.
java.io.IOException - If there is an I/O failure during the load.

defaultProvider

public static MimeRegProvider defaultProvider()
A default MimeRegistry-provider. The provider provides a default MimeRegistry.