View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.tika.parser.pkg;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  import org.apache.tika.exception.TikaException;
27  import org.apache.tika.metadata.Metadata;
28  import org.apache.tika.mime.MediaType;
29  import org.apache.tika.parser.ParseContext;
30  import org.apache.tika.parser.Parser;
31  import org.xml.sax.ContentHandler;
32  import org.xml.sax.SAXException;
33  
34  /**
35   * Parser for various packaging and compression formats. Package entries will
36   * be written to the XHTML event stream as <div class="package-entry">
37   * elements that contain the (optional) entry name as a <h1> element
38   * and the full structured body content of the parsed entry.
39   */
40  public class PackageParser implements Parser {
41  
42      private static final Set<MediaType> SUPPORTED_TYPES =
43          Collections.unmodifiableSet(new HashSet<MediaType>(Arrays.asList(
44                  MediaType.application("x-archive"),
45                  MediaType.application("x-bzip"),
46                  MediaType.application("x-bzip2"),
47                  MediaType.application("x-cpio"),
48                  MediaType.application("x-gtar"),
49                  MediaType.application("x-gzip"),
50                  MediaType.application("x-tar"),
51                  MediaType.application("zip"))));
52  
53      public Set<MediaType> getSupportedTypes(ParseContext context) {
54          return SUPPORTED_TYPES;
55      }
56  
57      public void parse(
58              InputStream stream, ContentHandler handler,
59              Metadata metadata, ParseContext context)
60              throws IOException, SAXException, TikaException {
61          new PackageExtractor(handler, metadata, context).parse(stream);
62      }
63  
64      /**
65       * @deprecated This method will be removed in Apache Tika 1.0.
66       */
67      public void parse(
68              InputStream stream, ContentHandler handler, Metadata metadata)
69              throws IOException, SAXException, TikaException {
70          parse(stream, handler, metadata, new ParseContext());
71      }
72  
73  }