Methods Summary |
---|
public java.lang.Object | clone()
MimeTypeParameterList newObj = null;
try {
newObj = (MimeTypeParameterList)super.clone();
} catch (CloneNotSupportedException cannotHappen) {
}
newObj.parameters = (Hashtable)parameters.clone();
return newObj;
|
public boolean | equals(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.String | get(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.Enumeration | getNames()Retrieve an enumeration of all the names in this list.
return parameters.keys();
|
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()Determine whether or not this list is empty.
return parameters.isEmpty();
|
private static boolean | isTokenChar(char c)Determine whether or not a given character belongs to a legal token.
return ((c > 040) && (c < 0177)) && (TSPECIALS.indexOf(c) < 0);
|
protected void | parse(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.String | quote(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 void | remove(java.lang.String name)Remove any value associated with the given name.
parameters.remove(name.trim().toLowerCase());
|
public void | set(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 int | size()return the number of name-value pairs in this list.
return parameters.size();
|
private static int | skipWhiteSpace(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.String | toString()
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.String | unquote(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();
|