Get Tika parsing up and running in 5 minutes

This page is a quick start guide showing how to add a new parser to Apache Tika. Following the simple steps listed below your new parser can be running in only 5 minutes.

Getting Started

The Getting Started document describes how to build Apache Tika from sources and how to start using Tika in an application. Pay close attention and follow the instructions in the "Getting and building the sources" section.

Add your MIME-Type

Tika loads the core, standard MIME-Types from the file "org/apache/tika/mime/tika-mimetypes.xml", which comes from tika-core/src/main/resources/org/apache/tika/mime/tika-mimetypes.xml . If your new MIME-Type is a standard one which is missing from Tika, submit a patch for this file!

If your MIME-Type needs adding, create a new file "org/apache/tika/mime/custom-mimetypes.xml" in your codebase. You should add to it something like this:

 <?xml version="1.0" encoding="UTF-8"?>
 <mime-info>
   <mime-type type="application/hello">
          <glob pattern="*.hi"/>
   </mime-type>
 </mime-info>

Create your Parser class

Now, you need to create your new parser. This is a class that must implement the Parser interface offered by Tika. Instead of implementing the Parser interface directly, it is recommended that you extend the abstract class AbstractParser if possible. AbstractParser handles translating between API changes for you.

A very simple Tika Parser looks like this:

/*
 * 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.
 * 
 * @Author: Arturo Beltran
 */
package org.apache.tika.parser.hello;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Set;

import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.mime.MediaType;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.AbstractParser;
import org.apache.tika.sax.XHTMLContentHandler;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;

public class HelloParser extends AbstractParser {

        private static final Set<MediaType> SUPPORTED_TYPES = Collections.singleton(MediaType.application("hello"));
        public static final String HELLO_MIME_TYPE = "application/hello";
        
        public Set<MediaType> getSupportedTypes(ParseContext context) {
                return SUPPORTED_TYPES;
        }

        public void parse(
                        InputStream stream, ContentHandler handler,
                        Metadata metadata, ParseContext context)
                        throws IOException, SAXException, TikaException {

                metadata.set(Metadata.CONTENT_TYPE, HELLO_MIME_TYPE);
                metadata.set("Hello", "World");

                XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
                xhtml.startDocument();
                xhtml.endDocument();
        }
}

Pay special attention to the definition of the SUPPORTED_TYPES static class field in the parser class that defines what MIME-Types it supports. If your MIME-Types aren't standard ones, ensure you listed them in a "custom-mimetypes.xml" file so that Tika knows about them (see above).

Is in the "parse" method where you will do all your work. This is, extract the information of the resource and then set the metadata.

List the new parser

Finally, you should explicitly tell the AutoDetectParser to include your new parser. This step is only needed if you want to use the AutoDetectParser functionality. If you figure out the correct parser in a different way, it isn't needed.

List your new parser in: tika-parsers/src/main/resources/META-INF/services/org.apache.tika.parser.Parser