An object that encapsualtes the parameter list of a MimeType
as defined in RFC 2045 and 2046.
Method from java.awt.datatransfer.MimeTypeParameterList Detail: |
public Object clone() {
MimeTypeParameterList newObj = null;
try {
newObj = (MimeTypeParameterList)super.clone();
} catch (CloneNotSupportedException cannotHappen) {
}
newObj.parameters = (Hashtable)parameters.clone();
return newObj;
}
|
public boolean equals(Object thatObject) {
//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;
}
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. |
public String get(String name) {
return (String)parameters.get(name.trim().toLowerCase());
}
Retrieve the value associated with the given name, or null if there
is no current association. |
public Enumeration getNames() {
return parameters.keys();
}
Retrieve an enumeration of all the names in this list. |
public int hashCode() {
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 boolean isEmpty() {
return parameters.isEmpty();
}
Determine whether or not this list is empty. |
protected void parse(String rawdata) throws MimeTypeParseException {
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.");
}
}
}
}
A routine for parsing the parameter list out of a String. |
public void remove(String name) {
parameters.remove(name.trim().toLowerCase());
}
Remove any value associated with the given name. |
public void set(String name,
String value) {
parameters.put(name.trim().toLowerCase(), value);
}
Set the value to be associated with the given name, replacing
any previous association. |
public int size() {
return parameters.size();
}
return the number of name-value pairs in this list. |
public String toString() {
// Heuristic: 8 characters per field
StringBuilder buffer = new StringBuilder(parameters.size() * 16);
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();
}
|