MimeMappublic class MimeMap extends Object implements SerializableClass representing the parsed mime mapping file of a mime element. |
Fields Summary |
---|
private static final String | MIME_TYPE | private static final String | MIME_EXTS | private String | id | private HashMap | mimeMappings |
Constructors Summary |
---|
MimeMap(String id)Constructor.
this.id = id;
|
Methods Summary |
---|
private void | addMappings(java.lang.String type, java.lang.String exts)
// Remove "type=" prefix
int index = type.indexOf(MIME_TYPE);
if (index == -1) {
// ignore
return;
}
type = type.substring(index + MIME_TYPE.length());
// Remove "exts=" prefix
index = exts.indexOf(MIME_EXTS);
if (index == -1) {
// ignore
return;
}
if (mimeMappings == null) {
mimeMappings = new HashMap();
}
exts = exts.substring(index + MIME_EXTS.length());
index = exts.indexOf(',");
String ext = null;
if (index != -1) {
// e.g., exts=aif,aiff,aifc
int fromIndex = 0;
while (index != -1) {
ext = exts.substring(fromIndex, index).trim();
if (ext.length() > 0) {
mimeMappings.put(ext, type);
}
fromIndex = index+1;
index = exts.indexOf(',", fromIndex);
}
ext = exts.substring(fromIndex);
} else {
// e.g., exts=gif
ext = exts;
}
if (ext != null) {
ext = ext.trim();
if (ext.length() > 0) {
mimeMappings.put(ext, type);
}
}
| java.util.Iterator | getExtensions()Returns an iterator over the mime extensions that were parsed
Iterator ret = null;
if (mimeMappings != null) {
ret = mimeMappings.keySet().iterator();
}
return ret;
| java.lang.String | getId()Gets the mime id of the mime element which this MimeMap represents.
return this.id;
| java.lang.String | getType(java.lang.String extension)Gets the mime type corresponding to the given extension
String ret = null;
if (mimeMappings != null) {
ret = (String) mimeMappings.get(extension);
}
return ret;
| void | load(java.lang.String file)Parses the mime mappings from the given file.
BufferedReader in = new BufferedReader(new FileReader(file));
try {
while (true) {
// Get next line
String line = in.readLine();
if (line == null)
return;
int len = line.length();
if (len > 0) {
// Ignore comments
char firstChar = line.charAt(0);
if ((firstChar != '#") && (firstChar != '!")) {
// Find start of key
int keyStart = 0;
while (keyStart < len
&& Character.isSpace(line.charAt(keyStart))) {
keyStart++;
}
// Blank lines are ignored
if (keyStart == len) {
continue;
}
int keyEnd = keyStart;
while (keyEnd<len
&& !Character.isSpace(line.charAt(keyEnd))) {
keyEnd++;
}
// Find start of value
int valueStart = keyEnd;
while (valueStart<len
&& Character.isSpace(line.charAt(valueStart))) {
valueStart++;
}
if (valueStart == len) {
// Ignore this MIME mapping
continue;
}
int valueEnd = valueStart;
while (valueEnd<len
&& !Character.isSpace(line.charAt(valueEnd))) {
valueEnd++;
}
String key = line.substring(keyStart, keyEnd);
String value = line.substring(valueStart, valueEnd);
addMappings(key, value);
}
}
}
} finally {
if (in != null) {
in.close();
}
}
|
|