You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ni...@apache.org on 2014/03/07 06:57:57 UTC

svn commit: r1575175 - in /tika/site: publish/1.5/detection.html src/site/apt/1.5/detection.apt

Author: nick
Date: Fri Mar  7 05:57:57 2014
New Revision: 1575175

URL: http://svn.apache.org/r1575175
Log:
Fix the Container detection documentation to match how it really works (service loader + DefaultDetector), mention custom mimetypes, and provide examples of how to call detection

Modified:
    tika/site/publish/1.5/detection.html
    tika/site/src/site/apt/1.5/detection.apt

Modified: tika/site/publish/1.5/detection.html
URL: http://svn.apache.org/viewvc/tika/site/publish/1.5/detection.html?rev=1575175&r1=1575174&r2=1575175&view=diff
==============================================================================
--- tika/site/publish/1.5/detection.html (original)
+++ tika/site/publish/1.5/detection.html Fri Mar  7 05:57:57 2014
@@ -84,8 +84,21 @@
                 width="387" height="100"/></a>
       </div>
       <div id="content">
-        <!-- Licensed to the Apache Software Foundation (ASF) under one or more --><!-- contributor license agreements.  See the NOTICE file distributed with --><!-- this work for additional information regarding copyright ownership. --><!-- The ASF licenses this file to You under the Apache License, Version 2.0 --><!-- (the "License"); you may not use this file except in compliance with --><!-- the License.  You may obtain a copy of the License at --><!--  --><!-- http://www.apache.org/licenses/LICENSE-2.0 --><!--  --><!-- Unless required by applicable law or agreed to in writing, software --><!-- distributed under the License is distributed on an "AS IS" BASIS, --><!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. --><!-- See the License for the specific language governing permissions and --><!-- limitations under the License. --><div class="section"><h2>Content Detection<a name="Content_Detection"></a></h2><p>This page gives you information on how conte
 nt and language detection works with Apache Tika, and how to tune the behaviour of Tika.</p><ul><li><a href="#Content_Detection">Content Detection</a><ul><li><a href="#The_Detector_Interface">The Detector Interface</a></li><li><a href="#Mime_Magic_Detction">Mime Magic Detction</a></li><li><a href="#Resource_Name_Based_Detection">Resource Name Based Detection</a></li><li><a href="#Known_Content_Type_Detection">Known Content Type &quot;Detection</a></li><li><a href="#The_default_Mime_Types_Detector">The default Mime Types Detector</a></li><li><a href="#Container_Aware_Detection">Container Aware Detection</a></li><li><a href="#Language_Detection">Language Detection</a></li></ul></li></ul><div class="section"><h3><a name="The_Detector_Interface">The Detector Interface</a></h3><p>The <a href="./api/org/apache/tika/detect/Detector.html">org.apache.tika.detect.Detector</a> interface is the basis for most of the content type detection in Apache Tika. All the different ways of detecting cont
 ent all implement the same common method:</p><div><pre>MediaType detect(java.io.InputStream input,
-                 Metadata metadata) throws java.io.IOException</pre></div><p>The <tt>detect</tt> method takes the stream to inspect, and a <tt>Metadata</tt> object that holds any additional information on the content. The detector will return a <a href="./api/org/apache/tika/mime/MediaType.html">MediaType</a> object describing its best guess as to the type of the file.</p><p>In general, only two keys on the Metadata object are used by Detectors. These are <tt>Metadata.RESOURCE_NAME_KEY</tt> which should hold the name of the file (where known), and <tt>Metadata.CONTENT_TYPE</tt> which should hold the advertised content type of the file (eg from a webserver or a content repository).</p></div><div class="section"><h3><a name="Mime_Magic_Detction">Mime Magic Detction</a></h3><p>By looking for special (&quot;magic&quot;) patterns of bytes near the start of the file, it is often possible to detect the type of the file. For some file types, this is a simple process. For others, typically c
 ontainer based formats, the magic detection may not be enough. (More detail on detecting container formats below)</p><p>Tika is able to make use of a a mime magic info file, in the <a class="externalLink" href="http://www.freedesktop.org/standards/shared-mime-info">Freedesktop MIME-info</a> format to peform mime magic detection.</p><p>This is provided within Tika by <a href="./api/org/apache/tika/detect/MagicDetector.html">org.apache.tika.detect.MagicDetector</a>. It is most commonly access via <a href="./api/org/apache/tika/mime/MimeTypes.html">org.apache.tika.mime.MimeTypes</a>, normally sourced from the <tt>tika-mimetypes.xml</tt> file.</p></div><div class="section"><h3><a name="Resource_Name_Based_Detection">Resource Name Based Detection</a></h3><p>Where the name of the file is known, it is sometimes possible to guess the file type from the name or extension. Within the <tt>tika-mimetypes.xml</tt> file is a list of patterns which are used to identify the type from the filename.<
 /p><p>However, because files may be renamed, this method of detection is quick but not always as accurate.</p><p>This is provided within Tika by <a href="./api/org/apache/tika/detect/NameDetector.html">org.apache.tika.detect.NameDetector</a>.</p></div><div class="section"><h3><a name="Known_Content_Type_Detection">Known Content Type &quot;Detection</a></h3><p>Sometimes, the mime type for a file is already known, such as when downloading from a webserver, or when retrieving from a content store. This information can be used by detectors, such as <a href="./api/org/apache/tika/mime/MimeTypes.html">org.apache.tika.mime.MimeTypes</a>,</p></div><div class="section"><h3><a name="The_default_Mime_Types_Detector">The default Mime Types Detector</a></h3><p>By default, the mime type detection in Tika is provided by <a href="./api/org/apache/tika/mime/MimeTypes.html">org.apache.tika.mime.MimeTypes</a>. This detector makes use of <tt>tika-mimetypes.xml</tt> to power magic based and filename bas
 ed detection.</p><p>Firstly, magic based detection is used on the start of the file. If the file is an XML file, then the start of the XML is processed to look for root elements. Next, if available, the filename (from <tt>Metadata.RESOURCE_NAME_KEY</tt>) is then used to improve the detail of the detection, such as when magic detects a text file, and the filename hints it's really a CSV. Finally, if available, the supplied content type (from <tt>Metadata.CONTENT_TYPE</tt>) is used to further refine the type.</p></div><div class="section"><h3><a name="Container_Aware_Detection">Container Aware Detection</a></h3><p>Several common file formats are actually held within a common container format. One example is the PowerPoint .ppt and Word .doc formats, which are both held within an OLE2 container. Another is Apple iWork formats, which are actually a series of XML files within a Zip file.</p><p>Using magic detection, it is easy to spot that a given file is an OLE2 document, or a Zip file.
  Using magic detection alone, it is very difficult (and often impossible) to tell what kind of file lives inside the container.</p><p>For some use cases, speed is important, so having a quick way to know the container type is sufficient. For other cases however, you don't mind spending a bit of time (and memory!) processing the container to get a more accurate answer on its contents. For these cases, a container aware detector should be used.</p><p>Tika provides a wrapping detector in the parsers bundle, of <a href="./api/org/apache/tika/detect/ContainerAwareDetector.html">org.apache.tika.detect.ContainerAwareDetector</a>. This detector will check for certain known containers, and if found, will open them and detect the appropriate type based on the contents. If the file isn't a known container, it will fall back to another detector for the answer (most commonly the default <tt>MimeTypes</tt> detector)</p><p>Because this detector needs to read the whole file to process the container
 , it must be used with a <a href="./api/org/apache/tika/io/TikaInputStream.html">org.apache.tika.io.TikaInputStream</a>. If called with a regular <tt>InputStream</tt>, then all work will be done by the fallback detector.</p><p>For more information on container formats and Tika, see <a class="externalLink" href="http://wiki.apache.org/tika/MetadataDiscussion"></a></p></div><div class="section"><h3><a name="Language_Detection">Language Detection</a></h3><p>Tika is able to help identify the language of a piece of text, which is useful when extracting text from document formats which do not include language information in their metadata.</p><p>The language detection is provided by <a href="./api/org/apache/tika/language/LanguageIdentifier.html">org.apache.tika.language.LanguageIdentifier</a></p></div></div>
+        <!-- Licensed to the Apache Software Foundation (ASF) under one or more --><!-- contributor license agreements.  See the NOTICE file distributed with --><!-- this work for additional information regarding copyright ownership. --><!-- The ASF licenses this file to You under the Apache License, Version 2.0 --><!-- (the "License"); you may not use this file except in compliance with --><!-- the License.  You may obtain a copy of the License at --><!--  --><!-- http://www.apache.org/licenses/LICENSE-2.0 --><!--  --><!-- Unless required by applicable law or agreed to in writing, software --><!-- distributed under the License is distributed on an "AS IS" BASIS, --><!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. --><!-- See the License for the specific language governing permissions and --><!-- limitations under the License. --><div class="section"><h2>Content Detection<a name="Content_Detection"></a></h2><p>This page gives you information on how conte
 nt and language detection works with Apache Tika, and how to tune the behaviour of Tika.</p><ul><li><a href="#Content_Detection">Content Detection</a><ul><li><a href="#The_Detector_Interface">The Detector Interface</a></li><li><a href="#Mime_Magic_Detction">Mime Magic Detction</a></li><li><a href="#Resource_Name_Based_Detection">Resource Name Based Detection</a></li><li><a href="#Known_Content_Type_Detection">Known Content Type &quot;Detection</a></li><li><a href="#The_default_Mime_Types_Detector">The default Mime Types Detector</a></li><li><a href="#Container_Aware_Detection">Container Aware Detection</a></li><li><a href="#The_default_Tika_Detector">The default Tika Detector</a></li><li><a href="#Ways_of_triggering_Detection">Ways of triggering Detection</a></li><li><a href="#Language_Detection">Language Detection</a></li></ul></li></ul><div class="section"><h3><a name="The_Detector_Interface">The Detector Interface</a></h3><p>The <a href="./api/org/apache/tika/detect/Detector.html
 ">org.apache.tika.detect.Detector</a> interface is the basis for most of the content type detection in Apache Tika. All the different ways of detecting content all implement the same common method:</p><div><pre>MediaType detect(java.io.InputStream input,
+                 Metadata metadata) throws java.io.IOException</pre></div><p>The <tt>detect</tt> method takes the stream to inspect, and a <tt>Metadata</tt> object that holds any additional information on the content. The detector will return a <a href="./api/org/apache/tika/mime/MediaType.html">MediaType</a> object describing its best guess as to the type of the file.</p><p>In general, only two keys on the Metadata object are used by Detectors. These are <tt>Metadata.RESOURCE_NAME_KEY</tt> which should hold the name of the file (where known), and <tt>Metadata.CONTENT_TYPE</tt> which should hold the advertised content type of the file (eg from a webserver or a content repository).</p></div><div class="section"><h3><a name="Mime_Magic_Detction">Mime Magic Detction</a></h3><p>By looking for special (&quot;magic&quot;) patterns of bytes near the start of the file, it is often possible to detect the type of the file. For some file types, this is a simple process. For others, typically c
 ontainer based formats, the magic detection may not be enough. (More detail on detecting container formats below)</p><p>Tika is able to make use of a a mime magic info file, in the <a class="externalLink" href="http://www.freedesktop.org/standards/shared-mime-info">Freedesktop MIME-info</a> format to peform mime magic detection.</p><p>This is provided within Tika by <a href="./api/org/apache/tika/detect/MagicDetector.html">org.apache.tika.detect.MagicDetector</a>. It is most commonly access via <a href="./api/org/apache/tika/mime/MimeTypes.html">org.apache.tika.mime.MimeTypes</a>, normally sourced from the <tt>tika-mimetypes.xml</tt> and <tt>custom-mimetypes.xml</tt> files. For more information on defining your own custom mimetypes, see <a href="./parser_guide.html#Add_your_MIME-Type">the new parser guide</a>.</p></div><div class="section"><h3><a name="Resource_Name_Based_Detection">Resource Name Based Detection</a></h3><p>Where the name of the file is known, it is sometimes possibl
 e to guess the file type from the name or extension. Within the <tt>tika-mimetypes.xml</tt> file is a list of patterns which are used to identify the type from the filename.</p><p>However, because files may be renamed, this method of detection is quick but not always as accurate.</p><p>This is provided within Tika by <a href="./api/org/apache/tika/detect/NameDetector.html">org.apache.tika.detect.NameDetector</a>.</p></div><div class="section"><h3><a name="Known_Content_Type_Detection">Known Content Type &quot;Detection</a></h3><p>Sometimes, the mime type for a file is already known, such as when downloading from a webserver, or when retrieving from a content store. This information can be used by detectors, such as <a href="./api/org/apache/tika/mime/MimeTypes.html">org.apache.tika.mime.MimeTypes</a>,</p></div><div class="section"><h3><a name="The_default_Mime_Types_Detector">The default Mime Types Detector</a></h3><p>By default, the mime type detection in Tika is provided by <a hre
 f="./api/org/apache/tika/mime/MimeTypes.html">org.apache.tika.mime.MimeTypes</a>. This detector makes use of <tt>tika-mimetypes.xml</tt> to power magic based and filename based detection.</p><p>Firstly, magic based detection is used on the start of the file. If the file is an XML file, then the start of the XML is processed to look for root elements. Next, if available, the filename (from <tt>Metadata.RESOURCE_NAME_KEY</tt>) is then used to improve the detail of the detection, such as when magic detects a text file, and the filename hints it's really a CSV. Finally, if available, the supplied content type (from <tt>Metadata.CONTENT_TYPE</tt>) is used to further refine the type.</p></div><div class="section"><h3><a name="Container_Aware_Detection">Container Aware Detection</a></h3><p>Several common file formats are actually held within a common container format. One example is the PowerPoint .ppt and Word .doc formats, which are both held within an OLE2 container. Another is Apple iW
 ork formats, which are actually a series of XML files within a Zip file.</p><p>Using magic detection, it is easy to spot that a given file is an OLE2 document, or a Zip file. Using magic detection alone, it is very difficult (and often impossible) to tell what kind of file lives inside the container.</p><p>For some use cases, speed is important, so having a quick way to know the container type is sufficient. For other cases however, you don't mind spending a bit of time (and memory!) processing the container to get a more accurate answer on its contents. For these cases, the additional container aware detectors contained in the <tt>Tika Parsers</tt> jar should be used.</p><p>Tika provides a wrapping detector in the form of <a href="./api/org/apache/tika/detect/DefaultDetector.html">org.apache.tika.detect.DefaultDetector</a>. This uses the service loader to discover all available detectors, including any available container aware ones, and tries them in turn. For container aware dete
 ction, include the <tt>Tika Parsers</tt> jar and its dependencies in your project, then use DefaultDetector along with a <tt>TikaInputStream</tt>.</p><p>Because these container detectors needs to read the whole file to open and inspect the container, they must be used with a <a href="./api/org/apache/tika/io/TikaInputStream.html">org.apache.tika.io.TikaInputStream</a>. If called with a regular <tt>InputStream</tt>, then all work will be done by the default Mime Magic detection only.</p></div><div class="section"><h3><a name="The_default_Tika_Detector">The default Tika Detector</a></h3><p>Just as with Parsers, Tika provides a special detector <a href="./api/org/apache/tika/detect/DefaultDetector.html">org.apache.tika.detect.DefaultDetector</a> which auto-detects (based on service files) the available detectors at runtime, and tries these in turn to identify the file type.</p><p>If only <tt>Tika Core</tt> is available, the Default Detector will work only with Mime Magic and Resource N
 ame detection. However, if <tt>Tika Parsers</tt> (and its dependencies!) are available, additional detectors which known about containers (such as zip and ole2) will be used as appropriate, provided that detection is being performed with a <a href="./api/org/apache/tika/io/TikaInputStream.html">org.apache.tika.io.TikaInputStream</a>. Custom detectors can also be used as desired, they simply need to be listed in a service file much as is done for <a href="./parser_guide.html#List_the_new_parser">custom parsers</a>.</p></div><div class="section"><h3><a name="Ways_of_triggering_Detection">Ways of triggering Detection</a></h3><p>The simplest way to detect is through the <a href="./api/org/apache/tika/Tika.html">Tika Facade class</a>, which provides methods to detect based on <a href="./api/org/apache/tika/Tika.html#adetectjava.io.File">File</a>, <a href="./api/org/apache/tika/Tika.html#adetectjava.io.InputStream">InputStream</a>, <a href="./api/org/apache/tika/Tika.html#adetectjava.io.I
 nputStream_java.lang.String">InputStream and Filename</a>, <a href="./api/org/apache/tika/Tika.html#adetectjava.lang.String">Filename</a> or a few others. It works best with a File or <a href="./api/org/apache/tika/io/TikaInputStream.html">TikaInputStream</a>.</p><p>Alternately, detection can be performed on a specific Detector, or using <tt>DefaultDetector</tt> to have all available Detectors used. A typical pattern would be something like:</p><div><pre>TikaConfig tika = new TikaConfig();
+
+for (File f : myListOfFiles) {
+   Metadata metadata = new Metadata();
+   metadata.set(Metadata.RESOURCE_NAME_KEY, f.toString());
+   String mimetype = tika.getDetector().detect(
+        TikaInputStream.get(f), metadata);
+   System.out.println(&quot;File &quot; + f + &quot; is &quot; + mimetype);
+}
+for (InputStream is : myListOfStreams) {
+   String mimetype = tika.getDetector().detect(
+        TikaInputStream.get(is), new Metadata());
+   System.out.println(&quot;File &quot; + f + &quot; is &quot; + mimetype);
+}</pre></div></div><div class="section"><h3><a name="Language_Detection">Language Detection</a></h3><p>Tika is able to help identify the language of a piece of text, which is useful when extracting text from document formats which do not include language information in their metadata.</p><p>The language detection is provided by <a href="./api/org/apache/tika/language/LanguageIdentifier.html">org.apache.tika.language.LanguageIdentifier</a></p></div></div>
       </div>
       <div id="sidebar">
         <div id="navigation">

Modified: tika/site/src/site/apt/1.5/detection.apt
URL: http://svn.apache.org/viewvc/tika/site/src/site/apt/1.5/detection.apt?rev=1575175&r1=1575174&r2=1575175&view=diff
==============================================================================
--- tika/site/src/site/apt/1.5/detection.apt (original)
+++ tika/site/src/site/apt/1.5/detection.apt Fri Mar  7 05:57:57 2014
@@ -65,7 +65,9 @@ MediaType detect(java.io.InputStream inp
   This is provided within Tika by
   {{{./api/org/apache/tika/detect/MagicDetector.html}org.apache.tika.detect.MagicDetector}}. It is most commonly access via
   {{{./api/org/apache/tika/mime/MimeTypes.html}org.apache.tika.mime.MimeTypes}},
-  normally sourced from the <<<tika-mimetypes.xml>>> file.
+  normally sourced from the <<<tika-mimetypes.xml>>> and <<<custom-mimetypes.xml>>>
+  files. For more information on defining your own custom mimetypes, see
+  {{{./parser_guide.html#Add_your_MIME-Type}the new parser guide}}.
    
 
 * {Resource Name Based Detection}
@@ -121,26 +123,74 @@ MediaType detect(java.io.InputStream inp
   For some use cases, speed is important, so having a quick way to know the
   container type is sufficient. For other cases however, you don't mind 
   spending a bit of time (and memory!) processing the container to get a 
-  more accurate answer on its contents. For these cases, a container
-  aware detector should be used.
+  more accurate answer on its contents. For these cases, the additional
+  container aware detectors contained in the <<<Tika Parsers>>> jar should
+  be used.
+
+  Tika provides a wrapping detector in the form of 
+  {{{./api/org/apache/tika/detect/DefaultDetector.html}org.apache.tika.detect.DefaultDetector}}.
+  This uses the service loader to discover all available detectors, including
+  any available container aware ones, and tries them in turn. For container
+  aware detection, include the <<<Tika Parsers>>> jar and its dependencies
+  in your project, then use DefaultDetector along with a <<<TikaInputStream>>>.
 
-  Tika provides a wrapping detector in the parsers bundle, of
-  {{{./api/org/apache/tika/detect/ContainerAwareDetector.html}org.apache.tika.detect.ContainerAwareDetector}}.
-  This detector will check for certain known containers, and if found,
-  will open them and detect the appropriate type based on the contents.
-  If the file isn't a known container, it will fall back to another
-  detector for the answer (most commonly the default 
-  <<<MimeTypes>>> detector)
-
-  Because this detector needs to read the whole file to process the
-  container, it must be used with a 
+  Because these container detectors needs to read the whole file to open and
+  inspect the container, they must be used with a 
   {{{./api/org/apache/tika/io/TikaInputStream.html}org.apache.tika.io.TikaInputStream}}.
   If called with a regular <<<InputStream>>>, then all work will be done
-  by the fallback detector.
+  by the default Mime Magic detection only.
+
 
-  For more information on container formats and Tika, see
-  {{{http://wiki.apache.org/tika/MetadataDiscussion}}}
+* {The default Tika Detector}
 
+  Just as with Parsers, Tika provides a special detector
+  {{{./api/org/apache/tika/detect/DefaultDetector.html}org.apache.tika.detect.DefaultDetector}}
+  which auto-detects (based on service files) the available detectors at 
+  runtime, and tries these in turn to identify the file type.
+
+  If only <<<Tika Core>>> is available, the Default Detector will work only
+  with Mime Magic and Resource Name detection. However, if <<<Tika Parsers>>>
+  (and its dependencies!) are available, additional detectors which known about
+  containers (such as zip and ole2) will be used as appropriate, provided that
+  detection is being performed with a
+  {{{./api/org/apache/tika/io/TikaInputStream.html}org.apache.tika.io.TikaInputStream}}.
+  Custom detectors can also be used as desired, they simply need to be listed
+  in a service file much as is done for
+  {{{./parser_guide.html#List_the_new_parser}custom parsers}}.
+
+
+* {Ways of triggering Detection}
+
+  The simplest way to detect is through the 
+  {{{./api/org/apache/tika/Tika.html}Tika Facade class}}, which provides methods to
+  detect based on
+  {{{./api/org/apache/tika/Tika.html#detect(java.io.File)}File}},
+  {{{./api/org/apache/tika/Tika.html#detect(java.io.InputStream)}InputStream}},
+  {{{./api/org/apache/tika/Tika.html#detect(java.io.InputStream, java.lang.String)}InputStream and Filename}},
+  {{{./api/org/apache/tika/Tika.html#detect(java.lang.String)}Filename}} or a few others.
+  It works best with a File or 
+  {{{./api/org/apache/tika/io/TikaInputStream.html}TikaInputStream}}.
+
+  Alternately, detection can be performed on a specific Detector, or using
+  <<<DefaultDetector>>> to have all available Detectors used. A typical pattern
+  would be something like:
+
+---
+TikaConfig tika = new TikaConfig();
+
+for (File f : myListOfFiles) {
+   Metadata metadata = new Metadata();
+   metadata.set(Metadata.RESOURCE_NAME_KEY, f.toString());
+   String mimetype = tika.getDetector().detect(
+        TikaInputStream.get(f), metadata);
+   System.out.println("File " + f + " is " + mimetype);
+}
+for (InputStream is : myListOfStreams) {
+   String mimetype = tika.getDetector().detect(
+        TikaInputStream.get(is), new Metadata());
+   System.out.println("File " + f + " is " + mimetype);
+}
+---
 
 * {Language Detection}