/*
 * @(#)MultiFileTypeMap.java  1.1  2002-02-12
 *
 * Copyright (c) 1999 Werner Randelshofer, Immensee, Switzerland.
 * All rights reserved.
 *
 * You may not use, copy or modify this file, except in compliance with the
 * license agreement you entered into with Werner Randelshofer.
 * For details see accompanying license terms.
 */
package ch.randelshofer.activation;

import java.io.*;
import java.net.*;
import org.monte.media.iff.*;
import java.util.Vector;
import java.util.Enumeration;
import javax.activation.FileTypeMap;
/**
 * This file type map serves as a
 * container for a collection of file type maps.
 *
 * @author  Werner Randelshofer, Hausmatt 10, CH-6405 Immensee, Switzerland.
 * @version 1.1 2002-02-12 Return application/octet-stream' for unknown data types.
 * <br>1.0  1998-10-19
 */
public class MultiFileTypeMap
extends FileTypeMap {
    private Vector fileTypeMaps = new Vector();
    
    public void add(FileTypeMap fileTypeMap) {
        fileTypeMaps.addElement(fileTypeMap);
    }
    /**
     * Return the type of the file object. This method
     * should always return a valid MIME type.
     *
     * @param  file  A file to be typed.
     * @return The content type.
     */
    public String getContentType(File file) {
        String contentType = Recognizer.CONTENT_TYPE_OCTET_STREAM;
        Enumeration e = fileTypeMaps.elements();
        while (e.hasMoreElements()
        && (contentType.equals(Recognizer.CONTENT_TYPE_UNKNOWN)
        || contentType.equals(Recognizer.CONTENT_TYPE_OCTET_STREAM))
        ) {
            contentType =
            ((FileTypeMap)e.nextElement()).getContentType(file);
        }
        return contentType;
    }
    
    /**
     * Return the type of the file passed in. This method
     * should always return a valid MIME type.
     *
     * @param  filename  The pathname of the file.
     * @return The content type.
     */
    public String getContentType(String filename) {
        String contentType = Recognizer.CONTENT_TYPE_OCTET_STREAM;
        Enumeration e = fileTypeMaps.elements();
        while (e.hasMoreElements()
        && (contentType.equals(Recognizer.CONTENT_TYPE_UNKNOWN)
        || contentType.equals(Recognizer.CONTENT_TYPE_OCTET_STREAM))
        ) {
            contentType =
            ((FileTypeMap)e.nextElement()).getContentType(filename);
        }
        return contentType;
    }
}
