Package org.apache.tika.detect
Class NameDetector
java.lang.Object
org.apache.tika.detect.NameDetector
- All Implemented Interfaces:
Serializable
,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:
-
Constructor Summary
ConstructorDescriptionNameDetector
(Map<Pattern, MediaType> patterns) Creates a new content type detector based on the given name patterns. -
Method Summary
Modifier and TypeMethodDescriptiondetect
(InputStream input, Metadata metadata) Detects the content type of an input document based on the document name given in the input metadata.
-
Constructor Details
-
NameDetector
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 Details
-
detect
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.
- Potential URL query (?...) and fragment identifier (#...) parts are removed from the end of the resource name.
- Potential leading path elements (up to the last slash or backslash) are removed from the beginning of the resource name.
- Potential URL encodings (%nn, in UTF-8) are decoded.
- 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.
-