FileDocCategorySizeDatePackage
MimeTypeParameterList.javaAPI DocGlassfish v2 API10456Mon May 14 15:29:52 BST 2007javax.activation

MimeTypeParameterList

public class MimeTypeParameterList extends Object
A parameter list of a MimeType as defined in RFC 2045 and 2046. The Primary type of the object must already be stripped off.
see
javax.activation.MimeType

Fields Summary
private Hashtable
parameters
private static final String
TSPECIALS
A string that holds all the special chars.
Constructors Summary
public MimeTypeParameterList()
Default constructor.



           
      
        parameters = new Hashtable();
    
public MimeTypeParameterList(String parameterList)
Constructs a new MimeTypeParameterList with the passed in data.

param
parameterList an RFC 2045, 2046 compliant parameter list.

        parameters = new Hashtable();

        //    now parse rawdata
        parse(parameterList);
    
Methods Summary
public java.lang.Stringget(java.lang.String name)
Retrieve the value associated with the given name, or null if there is no current association.

param
name the parameter name
return
the parameter's value

        return (String)parameters.get(name.trim().toLowerCase(Locale.ENGLISH));
    
public java.util.EnumerationgetNames()
Retrieve an enumeration of all the names in this list.

return
an enumeration of all parameter names

        return parameters.keys();
    
public booleanisEmpty()
Determine whether or not this list is empty.

return
true if there are no parameters

        return parameters.isEmpty();
    
private static booleanisTokenChar(char c)
Determine whether or not a given character belongs to a legal token.

        return ((c > 040) && (c < 0177)) && (TSPECIALS.indexOf(c) < 0);
    
protected voidparse(java.lang.String parameterList)
A routine for parsing the parameter list out of a String.

param
parameterList an RFC 2045, 2046 compliant parameter list.

	if (parameterList == null)
	    return;

        int length = parameterList.length();
        if (length <= 0)
	    return;

	int i;
	char c;
	for (i = skipWhiteSpace(parameterList, 0);
		i < length && (c = parameterList.charAt(i)) == ';";
		i = skipWhiteSpace(parameterList, i)) {
	    int lastIndex;
	    String name;
	    String value;

	    //    eat the ';'
	    i++;

	    //    now parse the parameter name

	    //    skip whitespace
	    i = skipWhiteSpace(parameterList, i);

	    // tolerate trailing semicolon, even though it violates the spec
	    if (i >= length)
		return;

	    //    find the end of the token char run
	    lastIndex = i;
	    while ((i < length) && isTokenChar(parameterList.charAt(i)))
		i++;

	    name = parameterList.substring(lastIndex, i).
						toLowerCase(Locale.ENGLISH);

	    //    now parse the '=' that separates the name from the value
	    i = skipWhiteSpace(parameterList, i);

	    if (i >= length || parameterList.charAt(i) != '=")
		throw new MimeTypeParseException(
		    "Couldn't find the '=' that separates a " +
		    "parameter name from its value.");

	    //    eat it and parse the parameter value
	    i++;
	    i = skipWhiteSpace(parameterList, i);

	    if (i >= length)
		throw new MimeTypeParseException(
			"Couldn't find a value for parameter named " + name);

	    //    now find out whether or not we have a quoted value
	    c = parameterList.charAt(i);
	    if (c == '"") {
		//    yup it's quoted so eat it and capture the quoted string
		i++;
		if (i >= length)
		    throw new MimeTypeParseException(
			    "Encountered unterminated quoted parameter value.");

		lastIndex = i;

		//    find the next unescaped quote
		while (i < length) {
		    c = parameterList.charAt(i);
		    if (c == '"")
			break;
		    if (c == '\\") {
			//    found an escape sequence
			//    so skip this and the
			//    next character
			i++;
		    }
		    i++;
		}
		if (c != '"")
		    throw new MimeTypeParseException(
			"Encountered unterminated quoted parameter value.");

		value = unquote(parameterList.substring(lastIndex, i));
		//    eat the quote
		i++;
	    } else if (isTokenChar(c)) {
		//    nope it's an ordinary token so it
		//    ends with a non-token char
		lastIndex = i;
		while (i < length && isTokenChar(parameterList.charAt(i)))
		    i++;
		value = parameterList.substring(lastIndex, i);
	    } else {
		//    it ain't a value
		throw new MimeTypeParseException(
			"Unexpected character encountered at index " + i);
	    }

	    //    now put the data into the hashtable
	    parameters.put(name, value);
	}
	if (i < length) {
	    throw new MimeTypeParseException(
		"More characters encountered in input than expected.");
	}
    
private static java.lang.Stringquote(java.lang.String value)
A routine that knows how and when to quote and escape the given value.

        boolean needsQuotes = false;

        //    check to see if we actually have to quote this thing
        int length = value.length();
        for (int i = 0; (i < length) && !needsQuotes; i++) {
            needsQuotes = !isTokenChar(value.charAt(i));
        }

        if (needsQuotes) {
            StringBuffer buffer = new StringBuffer();
            buffer.ensureCapacity((int)(length * 1.5));

            //    add the initial quote
            buffer.append('"");

            //    add the properly escaped text
            for (int i = 0; i < length; ++i) {
                char c = value.charAt(i);
                if ((c == '\\") || (c == '""))
                    buffer.append('\\");
                buffer.append(c);
            }

            //    add the closing quote
            buffer.append('"");

            return buffer.toString();
        } else {
            return value;
        }
    
public voidremove(java.lang.String name)
Remove any value associated with the given name.

param
name the parameter name

        parameters.remove(name.trim().toLowerCase(Locale.ENGLISH));
    
public voidset(java.lang.String name, java.lang.String value)
Set the value to be associated with the given name, replacing any previous association.

param
name the parameter name
param
value the parameter's value

        parameters.put(name.trim().toLowerCase(Locale.ENGLISH), value);
    
public intsize()
Return the number of name-value pairs in this list.

return
the number of parameters

        return parameters.size();
    
private static intskipWhiteSpace(java.lang.String rawdata, int i)
return the index of the first non white space character in rawdata at or after index i.

        int length = rawdata.length();
	while ((i < length) && Character.isWhitespace(rawdata.charAt(i)))
	    i++;
        return i;
    
public java.lang.StringtoString()
Return a string representation of this object.

        StringBuffer buffer = new StringBuffer();
        buffer.ensureCapacity(parameters.size() * 16);
			//    heuristic: 8 characters per field

        Enumeration keys = parameters.keys();
        while (keys.hasMoreElements()) {
            String key = (String)keys.nextElement();
            buffer.append("; ");
            buffer.append(key);
            buffer.append('=");
	    buffer.append(quote((String)parameters.get(key)));
        }

        return buffer.toString();
    
private static java.lang.Stringunquote(java.lang.String value)
A routine that knows how to strip the quotes and escape sequences from the given value.

        int valueLength = value.length();
        StringBuffer buffer = new StringBuffer();
        buffer.ensureCapacity(valueLength);

        boolean escaped = false;
        for (int i = 0; i < valueLength; ++i) {
            char currentChar = value.charAt(i);
            if (!escaped && (currentChar != '\\")) {
                buffer.append(currentChar);
            } else if (escaped) {
                buffer.append(currentChar);
                escaped = false;
            } else {
                escaped = true;
            }
        }

        return buffer.toString();