Class NameDetector

  • All Implemented Interfaces:
    Serializable, Detector

    public class NameDetector
    extends Object
    implements Detector
    Content type detection based on the resource name. An instance of this class contains a set of regular expression patterns that are matched against the resource name potentially given as a part of the input metadata.

    If a pattern matches the given name, then the media type associated with that pattern is returned as the likely content type of the input document. Otherwise the returned type is application/octet-stream.

    See the detect(InputStream, Metadata) method for more details of the matching algorithm.

    Since:
    Apache Tika 0.3
    See Also:
    Serialized Form
    • Constructor Detail

      • NameDetector

        public NameDetector​(Map<Pattern,​MediaType> patterns)
        Creates a new content type detector based on the given name patterns. The given pattern map is not copied, so the caller may update the mappings even after this detector instance has been created. However, the map must not be concurrently modified while this instance is used for type detection.
        Parameters:
        patterns - map from name patterns to corresponding media types
    • Method Detail

      • detect

        public MediaType detect​(InputStream input,
                                Metadata metadata)
        Detects the content type of an input document based on the document name given in the input metadata. The RESOURCE_NAME_KEY attribute of the given input metadata is expected to contain the name (normally a file name or a URL) of the input document.

        If a resource name is given, then it is first processed as follows.

        1. Potential URL query (?...) and fragment identifier (#...) parts are removed from the end of the resource name.
        2. Potential leading path elements (up to the last slash or backslash) are removed from the beginning of the resource name.
        3. Potential URL encodings (%nn, in UTF-8) are decoded.
        4. Any leading and trailing whitespace is removed.

        The resulting name string (if any) is then matched in sequence against all the configured name patterns. If a match is found, then the (first) matching media type is returned.

        Specified by:
        detect in interface Detector
        Parameters:
        input - ignored
        metadata - input metadata, possibly with a RESOURCE_NAME_KEY value
        Returns:
        detected media type, or application/octet-stream