FileDocCategorySizeDatePackage
MimeType.javaAPI DocJava SE 5 API18589Fri Aug 26 14:57:42 BST 2005javax.print

MimeType

public class MimeType extends Object implements Serializable, Cloneable
Class MimeType encapsulates a Multipurpose Internet Mail Extensions (MIME) media type as defined in RFC 2045 and RFC 2046. A MIME type object is part of a {@link DocFlavor DocFlavor} object and specifies the format of the print data.

Class MimeType is similar to the like-named class in package {@link java.awt.datatransfer java.awt.datatransfer}. Class java.awt.datatransfer.MimeType is not used in the Jini Print Service API for two reasons:

  1. Since not all Java profiles include the AWT, the Jini Print Service should not depend on an AWT class.

  2. The implementation of class java.awt.datatransfer.MimeType does not guarantee that equivalent MIME types will have the same serialized representation. Thus, since the Jini Lookup Service (JLUS) matches service attributes based on equality of serialized representations, JLUS searches involving MIME types encapsulated in class java.awt.datatransfer.MimeType may incorrectly fail to match.

Class MimeType's serialized representation is based on the following canonical form of a MIME type string. Thus, two MIME types that are not identical but that are equivalent (that have the same canonical form) will be considered equal by the JLUS's matching algorithm.

  • The media type, media subtype, and parameters are retained, but all comments and whitespace characters are discarded.
  • The media type, media subtype, and parameter names are converted to lowercase.
  • The parameter values retain their original case, except a charset parameter value for a text media type is converted to lowercase.
  • Quote characters surrounding parameter values are removed.
  • Quoting backslash characters inside parameter values are removed.
  • The parameters are arranged in ascending order of parameter name.

author
Alan Kaminsky

Fields Summary
private static final long
serialVersionUID
private String[]
myPieces
Array of strings that hold pieces of this MIME type's canonical form. If the MIME type has n parameters, n >= 0, then the strings in the array are:
Index 0 -- Media type.
Index 1 -- Media subtype.
Index 2i+2 -- Name of parameter i, i=0,1,...,n-1.
Index 2i+3 -- Value of parameter i, i=0,1,...,n-1.
Parameters are arranged in ascending order of parameter name.
private transient String
myStringValue
String value for this MIME type. Computed when needed and cached.
private transient ParameterMapEntrySet
myEntrySet
Parameter map entry set. Computed when needed and cached.
private transient ParameterMap
myParameterMap
Parameter map. Computed when needed and cached.
private static final int
TOKEN_LEXEME
private static final int
QUOTED_STRING_LEXEME
private static final int
TSPECIAL_LEXEME
private static final int
EOF_LEXEME
private static final int
ILLEGAL_LEXEME
Constructors Summary
public MimeType(String s)
Construct a new MIME type object from the given string. The given string is converted into canonical form and stored internally.

param
s MIME media type string.
exception
NullPointerException (unchecked exception) Thrown if s is null.
exception
IllegalArgumentException (unchecked exception) Thrown if s does not obey the syntax for a MIME media type string.

	parse (s);
    
Methods Summary
private static java.lang.StringaddQuotes(java.lang.String s)
Returns a version of the string surrounded by quotes and with interior quotes preceded by a backslash.

	int n = s.length();
	int i;
	char c;
	StringBuffer result = new StringBuffer (n+2);
	result.append ('\"");
	for (i = 0; i < n; ++ i) {
	    c = s.charAt (i);
	    if (c == '\"") {
		result.append ('\\");
	    }
	    result.append (c);
	}
	result.append ('\"");
	return result.toString();
    
public booleanequals(java.lang.Object obj)
Determine if this MIME type object is equal to the given object. The two are equal if the given object is not null, is an instance of class net.jini.print.data.MimeType, and has the same canonical form as this MIME type object (that is, has the same type, subtype, and parameters). Thus, if two MIME type objects are the same except for comments, they are considered equal. However, "text/plain" and "text/plain; charset=us-ascii" are not considered equal, even though they represent the same media type (because the default character set for plain text is US-ASCII).

param
obj Object to test.
return
True if this MIME type object equals obj, false otherwise.

	return(obj != null &&
	       obj instanceof MimeType &&
	       getStringValue().equals(((MimeType) obj).getStringValue()));
    
public java.lang.StringgetMediaSubtype()
Returns this MIME type object's media subtype.

	return myPieces[1];
    
public java.lang.StringgetMediaType()
Returns this MIME type object's media type.

	return myPieces[0];
    
public java.lang.StringgetMimeType()
Returns this MIME type object's MIME type string based on the canonical form. Each parameter value is enclosed in quotes.

	return getStringValue();
    
public java.util.MapgetParameterMap()
Returns an unmodifiable map view of the parameters in this MIME type object. Each entry in the parameter map view consists of a parameter name String (key) mapping to a parameter value String. If this MIME type object has no parameters, an empty map is returned.

return
Parameter map for this MIME type object.

	if (myParameterMap == null) {
	    myParameterMap = new ParameterMap();
	}
	return myParameterMap;
    
private java.lang.StringgetStringValue()
Returns this MIME type's string value in canonical form.

	if (myStringValue == null) {
	    StringBuffer result = new StringBuffer();
	    result.append (myPieces[0]);
	    result.append ('/");
	    result.append (myPieces[1]);
	    int n = myPieces.length;
	    for (int i = 2; i < n; i += 2) {
		result.append(';");
		result.append(' ");
		result.append(myPieces[i]);
		result.append('=");
		result.append(addQuotes (myPieces[i+1]));
	    }
	    myStringValue = result.toString();
	}
	return myStringValue;
    
public inthashCode()
Returns a hash code for this MIME type object.

	return getStringValue().hashCode();
    
private voidparse(java.lang.String s)
Parses the given string into canonical pieces and stores the pieces in {@link #myPieces myPieces}.

Special rules applied:

  • If the media type is text, the value of a charset parameter is converted to lowercase.

param
s MIME media type string.
exception
NullPointerException (unchecked exception) Thrown if s is null.
exception
IllegalArgumentException (unchecked exception) Thrown if s does not obey the syntax for a MIME media type string.

	// Initialize.
	if (s == null) {
	    throw new NullPointerException();
	}
	LexicalAnalyzer theLexer = new LexicalAnalyzer (s);
	int theLexemeType;
	Vector thePieces = new Vector();
	boolean mediaTypeIsText = false;
	boolean parameterNameIsCharset = false;

	// Parse media type.
	if (theLexer.getLexemeType() == TOKEN_LEXEME) {
	    String mt = toUnicodeLowerCase (theLexer.getLexeme());
	    thePieces.add (mt);
	    theLexer.nextLexeme();
	    mediaTypeIsText = mt.equals ("text");
	} else {
	    throw new IllegalArgumentException();
	}
	// Parse slash.
	if (theLexer.getLexemeType() == TSPECIAL_LEXEME &&
	      theLexer.getLexemeFirstCharacter() == '/") {
	    theLexer.nextLexeme();
	} else {
	    throw new IllegalArgumentException();
	}
	if (theLexer.getLexemeType() == TOKEN_LEXEME) {
	    thePieces.add (toUnicodeLowerCase (theLexer.getLexeme()));
	    theLexer.nextLexeme();
	} else {
	    throw new IllegalArgumentException();
	}
	// Parse zero or more parameters.
	while (theLexer.getLexemeType() == TSPECIAL_LEXEME &&
	       theLexer.getLexemeFirstCharacter() == ';") {
	    // Parse semicolon.
	    theLexer.nextLexeme();

	    // Parse parameter name.
	    if (theLexer.getLexemeType() == TOKEN_LEXEME) {
		String pn = toUnicodeLowerCase (theLexer.getLexeme());
		thePieces.add (pn);
		theLexer.nextLexeme();
		parameterNameIsCharset = pn.equals ("charset");
	    } else {
		throw new IllegalArgumentException();
	    }
	    
	    // Parse equals.
	    if (theLexer.getLexemeType() == TSPECIAL_LEXEME &&
		theLexer.getLexemeFirstCharacter() == '=") {
		theLexer.nextLexeme();
	    } else {
		throw new IllegalArgumentException();
	    }

	    // Parse parameter value.
	    if (theLexer.getLexemeType() == TOKEN_LEXEME) {
		String pv = theLexer.getLexeme();
		thePieces.add(mediaTypeIsText && parameterNameIsCharset ?
			      toUnicodeLowerCase (pv) :
			      pv);
		theLexer.nextLexeme();
	    } else if (theLexer.getLexemeType() == QUOTED_STRING_LEXEME) {
		String pv = removeBackslashes (theLexer.getLexeme());
		thePieces.add(mediaTypeIsText && parameterNameIsCharset ?
			      toUnicodeLowerCase (pv) :
			      pv);
		theLexer.nextLexeme();
	    } else {
		throw new IllegalArgumentException();
	    }
	}

	// Make sure we've consumed everything.
	if (theLexer.getLexemeType() != EOF_LEXEME) {
	    throw new IllegalArgumentException();
	}

	// Save the pieces. Parameters are not in ascending order yet.
	int n = thePieces.size();
	myPieces = (String[]) thePieces.toArray (new String [n]);

	// Sort the parameters into ascending order using an insertion sort.
	int i, j;
	String temp;
	for (i = 4; i < n; i += 2) {
	    j = 2;
	    while (j < i && myPieces[j].compareTo (myPieces[i]) <= 0) {
		j += 2;
	    }
	    while (j < i) {
		temp = myPieces[j];
		myPieces[j] = myPieces[i];
		myPieces[i] = temp;
		temp = myPieces[j+1];
		myPieces[j+1] = myPieces[i+1];
		myPieces[i+1] = temp;
		j += 2;
	    }
	}
    
private static java.lang.StringremoveBackslashes(java.lang.String s)
Returns a version of the given string with backslashes removed.

	int n = s.length();
	char[] result = new char [n];
	int i;
	int j = 0;
	char c;
	for (i = 0; i < n; ++ i) {
	    c = s.charAt (i);
	    if (c == '\\") {
		c = s.charAt (++ i);
	    }
	    result[j++] = c;
	}
	return new String (result, 0, j);
    
public java.lang.StringtoString()
Converts this MIME type object to a string.

return
MIME type string based on the canonical form. Each parameter value is enclosed in quotes.

	return getStringValue();
    
private static java.lang.StringtoUnicodeLowerCase(java.lang.String s)
Returns a lowercase version of the given string. The lowercase version is constructed by applying Character.toLowerCase() to each character of the given string, which maps characters to lowercase using the rules of Unicode. This mapping is the same regardless of locale, whereas the mapping of String.toLowerCase() may be different depending on the default locale.

	int n = s.length();
	char[] result = new char [n];
	for (int i = 0; i < n; ++ i) {
	    result[i] = Character.toLowerCase (s.charAt (i));
	}
	return new String (result);