com.ccg.net
Class MimeType

java.lang.Object
  extended by com.ccg.net.MimeType

public class MimeType
extends Object

Representation of a MIME content type.

This object contains information to a particular MIME type. These objects are often combined into a MimeTypes collection so one can look up the proper mime type for a file with a certain extension (or vice versa).

NOTE: The current implementation is case insensitive for a preference for lower case (I should probably verify this is a reasonable thing).

Since:
1.0
Version:
$Revision: 1.1 $
Author:
$Author: pkb $
See Also:
MimeTypes

Field Summary
static MimeType UNKNOWN
          Constant for the "unknown/unknown" mime type (when we don't know the mime type).
 
Constructor Summary
MimeType(String type, String subtype, String[] fileExt)
          Constructs a MimeType object with a type, subtype and optional set of file extensions typically associated with a mime type.
 
Method Summary
 boolean equals(Object o)
          Determine if this type matches another type.
static MimeType fromMimeTypesString(String s)
          Creates a MimeType object from a ASCII line of text one might find in a /etc/mime.types files on a Unix System.
 String[] getFileExt()
          Get the typical file extension(s) associated with the mime type.
 String getSubType()
          Get the secondary Mime type (for example: "html" for "text/html").
 String getType()
          Get the primary Mime type (for example: "text" for "text/html").
 int hashCode()
          Generate a hash code such that two objects which are equal produce the same hash code.
 boolean isApplicationType()
          Is mime-type one of the "application/*" types.
 boolean isExtSupported(String s)
          Determine if the file extension in the string passed is associated with this mime type.
 boolean isImageType()
          Is mime-type one of the "image/*" types.
 boolean isTextType()
          Is mime-type one of the "text/*" types.
 boolean isVideoType()
          Is mime-type one of the "video/*" types.
 String toMimeTypesString()
          Return the mime type in a string form as one might find in "/etc/mime.types".
 String toString()
          Returns the standard "mime" looking string of the mime type (like: "text/html").
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

UNKNOWN

public static MimeType UNKNOWN
Constant for the "unknown/unknown" mime type (when we don't know the mime type).

Since:
1.0
Constructor Detail

MimeType

public MimeType(String type,
                String subtype,
                String[] fileExt)
Constructs a MimeType object with a type, subtype and optional set of file extensions typically associated with a mime type.

Parameters:
type - The primary type (like "text" in "text/html") - must not be null.
subtype - The secondary type (like "html" in "text/html") - must not be null.
fileExt - The array of file extensions associated with the mime type (like { "html", "htm" } for the "text/html" type). This may be null if you don't need to associate any file extension types, HOWEVER, if you don't pass null - then each entry must NOT be null.
Since:
1.0
Method Detail

equals

public boolean equals(Object o)
Determine if this type matches another type.

Overrides:
equals in class Object
Parameters:
o - Object (or null) to compare to this type.
Returns:
true if object is a MimeType object whose type and sub type match this object.
Since:
1.0

hashCode

public int hashCode()
Generate a hash code such that two objects which are equal produce the same hash code.

Overrides:
hashCode in class Object
Returns:
Hash code for object.
Since:
1.0

getType

public String getType()
Get the primary Mime type (for example: "text" for "text/html").

Returns:
Primary type as string value assigned.

getSubType

public String getSubType()
Get the secondary Mime type (for example: "html" for "text/html").

Returns:
Secondary type (sub type) as string value.

getFileExt

public String[] getFileExt()
Get the typical file extension(s) associated with the mime type.

If the mime type has a set of typical file extensions for files containg this type of content, this method will return the extensions (as an array of strings).

For example, for the "text/html" mime type, this method would return an array with two entries: { "html", "htm" }.

If there isn't an associated type, then a zero length array is returned (we never return null).

You are free to modify the contents of the returned array (we give you a clone of our current copy so you can't muck up our immutable status).

Returns:
Array of file extensions associated with the mime type. Never returns null, but may return a zero length array.

toString

public String toString()
Returns the standard "mime" looking string of the mime type (like: "text/html").

Overrides:
toString in class Object
Returns:
String representation of the mime type.
Since:
1.0

toMimeTypesString

public String toMimeTypesString()
Return the mime type in a string form as one might find in "/etc/mime.types".

On Unix systems, mime types are often store in a configuration file (like "/etc/mime.types" on a RedHat 7.1 system). Each entry on the line has the form:

 TYPE/SUBTYPE\t[\t[\t]][EXT0 [EXT1 ...]
 

For example, the "image/tiff" entry looks like:

 image/tiff                      tiff tif
 

This method formats the mime type in the above form.

Returns:
String representation of the mime type in the above mentioned form.
Since:
1.0
See Also:
fromMimeTypesString(java.lang.String)

fromMimeTypesString

public static MimeType fromMimeTypesString(String s)
Creates a MimeType object from a ASCII line of text one might find in a /etc/mime.types files on a Unix System.

The /etc/mime.types file on Unix systems typically has a form similar to:

 TYPE/SUBTYPE           [EXT0[ EXT1[...]]]
 

The following shows example of a couple of these lines:

 video/vnd.vivo
 text/html                      html htm
 

This static method takes one of these types of lines as input and returns a corresponding MimeType object.

Parameters:
line - Line of text to parse (must not be null).
Returns:
A MimeType object which represents the parsed line.
Throws:
NullPointerException - If you pass null.
IllegalArgumentException - If string passed is not in the form "TYPE/SUBTYPE [EXT0 [EXT1 [...]"
Since:
1.0
See Also:
toMimeTypesString()

isApplicationType

public final boolean isApplicationType()
Is mime-type one of the "application/*" types.

Returns:
true if mime type is "application".
Since:
1.0

isTextType

public final boolean isTextType()
Is mime-type one of the "text/*" types.

Returns:
true if mime type is "text".
Since:
1.0

isImageType

public final boolean isImageType()
Is mime-type one of the "image/*" types.

Returns:
true if mime type is "image".
Since:
1.0

isVideoType

public final boolean isVideoType()
Is mime-type one of the "video/*" types.

Returns:
true if mime type is "video".
Since:
1.0

isExtSupported

public boolean isExtSupported(String s)
Determine if the file extension in the string passed is associated with this mime type.

This method is used to determine whether or not the file extension in the string passed is associated with this mime type or not.

Parameters:
s - The string to look for the file extension in (or just the file extension). We only look at the characters following the last period in the string (or the entire string if no period is present). The compare is case insensitive. If you pass null, we return false.
Returns:
true if this mime type is associated with the file extension in the string you passed, false if not.
Since:
1.0


Copyright 1998-1998-2006 null. All Rights Reserved.