FileDocCategorySizeDatePackage
MimeTypeFile.javaAPI DocGlassfish v2 API9265Mon May 14 15:29:52 BST 2007com.sun.activation.registries

MimeTypeFile

public class MimeTypeFile extends Object

Fields Summary
private String
fname
private Hashtable
type_hash
Constructors Summary
public MimeTypeFile(String new_fname)
The construtor that takes a filename as an argument.

param
new_fname The file name of the mime types file.


                            
         
	File mime_file = null;
	FileReader fr = null;

	fname = new_fname; // remember the file name

	mime_file = new File(fname); // get a file object

	fr = new FileReader(mime_file);

	try {
	    parse(new BufferedReader(fr));
	} finally {
	    try {
		fr.close(); // close it
	    } catch (IOException e) {
		// ignore it
	    }
	}
    
public MimeTypeFile(InputStream is)

	parse(new BufferedReader(new InputStreamReader(is, "iso-8859-1")));
    
public MimeTypeFile()
Creates an empty DB.

    
Methods Summary
public voidappendToRegistry(java.lang.String mime_types)
Appends string of entries to the types registry, must be valid .mime.types format. A mime.types entry is one of two forms: type/subtype ext1 ext2 ... or type=type/subtype desc="description of type" exts=ext1,ext2,... Example: # this is a test audio/basic au text/plain txt text type=application/postscript exts=ps,eps

	try {
	    parse(new BufferedReader(new StringReader(mime_types)));
	} catch (IOException ex) {
	    // can't happen
	}
    
public java.lang.StringgetMIMETypeString(java.lang.String file_ext)
Get the MIME type string corresponding to the file extension.

	MimeTypeEntry entry = this.getMimeTypeEntry(file_ext);

	if (entry != null)
	    return entry.getMIMEType();
	else
	    return null;
    
public com.sun.activation.registries.MimeTypeEntrygetMimeTypeEntry(java.lang.String file_ext)
get the MimeTypeEntry based on the file extension

	return (MimeTypeEntry)type_hash.get((Object)file_ext);
    
private voidparse(java.io.BufferedReader buf_reader)
Parse a stream of mime.types entries.

	String line = null, prev = null;

	while ((line = buf_reader.readLine()) != null) {
	    if (prev == null)
		prev = line;
	    else
		prev += line;
	    int end = prev.length();
	    if (prev.length() > 0 && prev.charAt(end - 1) == '\\") {
		prev = prev.substring(0, end - 1);
		continue;
	    }
	    this.parseEntry(prev);
	    prev = null;
	}
	if (prev != null)
	    this.parseEntry(prev);
    
private voidparseEntry(java.lang.String line)
Parse single mime.types entry.

	String mime_type = null;
	String file_ext = null;
	line = line.trim();

	if (line.length() == 0) // empty line...
	    return; // BAIL!

	// check to see if this is a comment line?
	if (line.charAt(0) == '#")
	    return; // then we are done!

	// is it a new format line or old format?
	if (line.indexOf('=") > 0) {
	    // new format
	    LineTokenizer lt = new LineTokenizer(line);
	    while (lt.hasMoreTokens()) {
		String name = lt.nextToken();
		String value = null;
		if (lt.hasMoreTokens() && lt.nextToken().equals("=") &&
							lt.hasMoreTokens())
		    value = lt.nextToken();
		if (value == null) {
		    if (LogSupport.isLoggable())
			LogSupport.log("Bad .mime.types entry: " + line);
		    return;
		}
		if (name.equals("type"))
		    mime_type = value;
		else if (name.equals("exts")) {
		    StringTokenizer st = new StringTokenizer(value, ",");
		    while (st.hasMoreTokens()) {
			file_ext = st.nextToken();
			MimeTypeEntry entry =
				new MimeTypeEntry(mime_type, file_ext);
			type_hash.put(file_ext, entry);
			if (LogSupport.isLoggable())
			    LogSupport.log("Added: " + entry.toString());
		    }
		}
	    }
	} else {
	    // old format
	    // count the tokens
	    StringTokenizer strtok = new StringTokenizer(line);
	    int num_tok = strtok.countTokens();

	    if (num_tok == 0) // empty line
		return;

	    mime_type = strtok.nextToken(); // get the MIME type

	    while (strtok.hasMoreTokens()) {
		MimeTypeEntry entry = null;

		file_ext = strtok.nextToken();
		entry = new MimeTypeEntry(mime_type, file_ext);
		type_hash.put(file_ext, entry);
		if (LogSupport.isLoggable())
		    LogSupport.log("Added: " + entry.toString());
	    }
	}