/*
 * @(#)MultiRecognizer.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;

/**
 * This content type detector serves as a
 * container for a collection of content type detectors.
 *
 * @author  Werner Randelshofer, Hausmatt 10, CH-6405 Immensee, Switzerland.
 * @version 1.1 2002-02-12 Handle content type unknown and octet streams as unknown
 * data types.
 * <br>1.0  1999-10-19
 */
public class MultiRecognizer
implements Recognizer {
    private Vector detectors = new Vector();
    private int minimalLength;
    
    public void add(Recognizer detector) {
        minimalLength = Math.max(minimalLength,detector.getMinimalDeterminableDataLength());
        detectors.addElement(detector);
    }
    /**
     * Return the number of bytes needed to
     * determine the content type.
     */
    public int getMinimalDeterminableDataLength() {
        return minimalLength;
    }
    
    /**
     * Return the base MIME Type of this data. This method is
     * expected to ALWAYS return a valid (non-null) MIME Type.
     *
     * @param  dataSnippet the first few bytes of the data file.
     * @return MIME Type or 'application/octet-stream' if content type
     * can not be determined.
     */
    public String getContentType(byte[] dataSnippet) {
        Enumeration e = detectors.elements();
        String contentType = Recognizer.CONTENT_TYPE_OCTET_STREAM;
        while (e.hasMoreElements()
        && (contentType.equals(Recognizer.CONTENT_TYPE_UNKNOWN)
        ||contentType.equals(Recognizer.CONTENT_TYPE_OCTET_STREAM))) {
            contentType =
            ((Recognizer)e.nextElement()).getContentType(dataSnippet);
        }
        return contentType;
    }
}
