FileDocCategorySizeDatePackage
MimeTypeParameterList.javaAPI DocJava SE 5 API14160Fri Aug 26 14:56:48 BST 2005java.awt.datatransfer

MimeTypeParameterList

public class MimeTypeParameterList extends Object implements Cloneable
An object that encapsualtes the parameter list of a MimeType as defined in RFC 2045 and 2046.
version
1.15, 12/19/03
author
jeff.dunn@eng.sun.com

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 rawdata)

        parameters = new Hashtable();
        
        //    now parse rawdata
        parse(rawdata);
    
Methods Summary
public java.lang.Objectclone()

return
a clone of this object

	 MimeTypeParameterList newObj = null;
	 try {
	     newObj = (MimeTypeParameterList)super.clone();
	 } catch (CloneNotSupportedException cannotHappen) {
	 }
	 newObj.parameters = (Hashtable)parameters.clone();
	 return newObj;
     
public booleanequals(java.lang.Object thatObject)
Two parameter lists are considered equal if they have exactly the same set of parameter names and associated values. The order of the parameters is not considered.

	//System.out.println("MimeTypeParameterList.equals("+this+","+thatObject+")");
	if (!(thatObject instanceof MimeTypeParameterList)) {
	    return false;
	}
	MimeTypeParameterList that = (MimeTypeParameterList)thatObject;
	if (this.size() != that.size()) {
	    return false;
	}
	String name = null;
	String thisValue = null;
	String thatValue = null;
	Set entries = parameters.entrySet();
	Iterator iterator = entries.iterator();
	Map.Entry entry = null;
	while (iterator.hasNext()) {
	    entry = (Map.Entry)iterator.next();
	    name = (String)entry.getKey();
	    thisValue = (String)entry.getValue();
	    thatValue = (String)that.parameters.get(name);
	    if ((thisValue == null) || (thatValue == null)) {
		// both null -> equal, only one null -> not equal
		if (thisValue != thatValue) {
		    return false;
		}
	    } else if (!thisValue.equals(thatValue)) {
	        return false;
	    }
	} // while iterator

	return true;
    
public java.lang.Stringget(java.lang.String name)
Retrieve the value associated with the given name, or null if there is no current association.

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

        return parameters.keys();
    
public inthashCode()

        int code = Integer.MAX_VALUE/45; // "random" value for empty lists
        String paramName = null;
        Enumeration enum_ = this.getNames();

        while (enum_.hasMoreElements()) {
            paramName = (String)enum_.nextElement();
            code += paramName.hashCode();
            code += this.get(paramName).hashCode();
        }

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

        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 rawdata)
A routine for parsing the parameter list out of a String.

        int length = rawdata.length();
        if(length > 0) {
            int currentIndex = skipWhiteSpace(rawdata, 0);
            int lastIndex = 0;
            
            if(currentIndex < length) {
                char currentChar = rawdata.charAt(currentIndex);
                while ((currentIndex < length) && (currentChar == ';")) {
                    String name;
                    String value;
                    boolean foundit;
                    
                    //    eat the ';'
                    ++currentIndex;
                    
                    //    now parse the parameter name
                    
                    //    skip whitespace
                    currentIndex = skipWhiteSpace(rawdata, currentIndex);
                    
                    if(currentIndex < length) {
                        //    find the end of the token char run
                        lastIndex = currentIndex;
                        currentChar = rawdata.charAt(currentIndex);
                        while((currentIndex < length) && isTokenChar(currentChar)) {
                            ++currentIndex;
                            currentChar = rawdata.charAt(currentIndex);
                        }
                        name = rawdata.substring(lastIndex, currentIndex).toLowerCase();
                        
                        //    now parse the '=' that separates the name from the value
                        
                        //    skip whitespace
                        currentIndex = skipWhiteSpace(rawdata, currentIndex);
                        
                        if((currentIndex < length) && (rawdata.charAt(currentIndex) == '="))  {
                            //    eat it and parse the parameter value
                            ++currentIndex;
                            
                            //    skip whitespace
                            currentIndex = skipWhiteSpace(rawdata, currentIndex);
                            
                            if(currentIndex < length) {
                                //    now find out whether or not we have a quoted value
                                currentChar = rawdata.charAt(currentIndex);
                                if(currentChar == '"") {
                                    //    yup it's quoted so eat it and capture the quoted string
                                    ++currentIndex;
                                    lastIndex = currentIndex;
                                    
                                    if(currentIndex < length) {
                                        //    find the next unescqped quote
                                        foundit = false;
                                        while((currentIndex < length) && !foundit) {
                                            currentChar = rawdata.charAt(currentIndex);
                                            if(currentChar == '\\") {
                                                //    found an escape sequence so pass this and the next character
                                                currentIndex += 2;
                                            } else if(currentChar == '"") {
                                                //    foundit!
                                                foundit = true;
                                            } else {
                                                ++currentIndex;
                                            }
                                        }
                                        if(currentChar == '"") {
                                            value = unquote(rawdata.substring(lastIndex, currentIndex));
                                            //    eat the quote
                                            ++currentIndex;
                                        } else {
                                            throw new MimeTypeParseException("Encountered unterminated quoted parameter value.");
                                        }
                                    } else {
                                        throw new MimeTypeParseException("Encountered unterminated quoted parameter value.");
                                    }
                                } else if(isTokenChar(currentChar)) {
                                    //    nope it's an ordinary token so it ends with a non-token char
                                    lastIndex = currentIndex;
                                    foundit = false;
                                    while((currentIndex < length) && !foundit) {
                                        currentChar = rawdata.charAt(currentIndex);
                                        
                                        if(isTokenChar(currentChar)) {
                                            ++currentIndex;
                                        } else {
                                            foundit = true;
                                        }
                                    }
                                    value = rawdata.substring(lastIndex, currentIndex);
                                } else {
                                    //    it ain't a value
                                    throw new MimeTypeParseException("Unexpected character encountered at index " + currentIndex);
                                }
                                
                                //    now put the data into the hashtable
                                parameters.put(name, value);
                            } else {
                                throw new MimeTypeParseException("Couldn't find a value for parameter named " + name);
                            }
                        } else {
                            throw new MimeTypeParseException("Couldn't find the '=' that separates a parameter name from its value.");
                        }
                    } else {
                        throw new MimeTypeParseException("Couldn't find parameter name");
                    }
                    
                    //    setup the next iteration
                    currentIndex = skipWhiteSpace(rawdata, currentIndex);
                    if(currentIndex < length) {
                        currentChar = rawdata.charAt(currentIndex);
                    }
                }
                if(currentIndex < 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.

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

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

        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();
        if (i < length) {
            char c =  rawdata.charAt(i);
            while ((i < length) && Character.isWhitespace(c)) {
                ++i;
                c = rawdata.charAt(i);
            }
        }
        
        return i;
    
public java.lang.StringtoString()

        StringBuffer buffer = new StringBuffer();
        buffer.ensureCapacity(parameters.size() * 16);    //    heuristic: 8 characters per field
        
        Enumeration keys = parameters.keys();
        while(keys.hasMoreElements())
        {
            buffer.append("; ");
            
            String key = (String)keys.nextElement();
            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();