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;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import org.apache.tika.exception.TikaException;
28  import org.apache.tika.io.TaggedInputStream;
29  import org.apache.tika.metadata.Metadata;
30  import org.apache.tika.mime.MediaType;
31  import org.apache.tika.sax.TaggedContentHandler;
32  import org.xml.sax.ContentHandler;
33  import org.xml.sax.SAXException;
34  
35  /**
36   * Composite parser that delegates parsing tasks to a component parser
37   * based on the declared content type of the incoming document. A fallback
38   * parser is defined for cases where a parser for the given content type is
39   * not available.
40   */
41  public class CompositeParser implements Parser {
42  
43      /**
44       * Set of component parsers, keyed by the supported media types.
45       */
46      private Map<String, Parser> parsers = new HashMap<String, Parser>();
47  
48      /**
49       * The fallback parser, used when no better parser is available.
50       */
51      private Parser fallback = new EmptyParser();
52  
53      /**
54       * Returns the component parsers.
55       *
56       * @return component parsers, keyed by media type
57       */
58      public Map<String, Parser> getParsers() {
59          return parsers;
60      }
61  
62      /**
63       * Sets the component parsers.
64       *
65       * @param parsers component parsers, keyed by media type
66       */
67      public void setParsers(Map<String, Parser> parsers) {
68          this.parsers = parsers;
69      }
70  
71      /**
72       * Returns the fallback parser.
73       *
74       * @return fallback parser
75       */
76      public Parser getFallback() {
77          return fallback;
78      }
79  
80      /**
81       * Sets the fallback parser.
82       *
83       * @param fallback fallback parser
84       */
85      public void setFallback(Parser fallback) {
86          this.fallback = fallback;
87      }
88  
89      /**
90       * Returns the parser that best matches the given metadata. By default
91       * looks for a parser that matches the content type metadata property,
92       * and uses the fallback parser if a better match is not found.
93       * <p>
94       * Subclasses can override this method to provide more accurate
95       * parser resolution.
96       *
97       * @param metadata document metadata
98       * @return matching parser
99       */
100     protected Parser getParser(Metadata metadata) {
101         Parser parser = parsers.get(metadata.get(Metadata.CONTENT_TYPE));
102         if (parser == null) {
103             parser = fallback;
104         }
105         return parser;
106     }
107 
108     public Set<MediaType> getSupportedTypes(ParseContext context) {
109         Set<MediaType> supportedTypes = new HashSet<MediaType>();
110         for (String type : parsers.keySet()) {
111             supportedTypes.add(MediaType.parse(type));
112         }
113         return Collections.unmodifiableSet(supportedTypes);
114     }
115 
116     /**
117      * Delegates the call to the matching component parser.
118      * <p>
119      * Potential {@link RuntimeException}s, {@link IOException}s and
120      * {@link SAXException}s unrelated to the given input stream and content
121      * handler are automatically wrapped into {@link TikaException}s to better
122      * honor the {@link Parser} contract.
123      */
124     public void parse(
125             InputStream stream, ContentHandler handler,
126             Metadata metadata, ParseContext context)
127             throws IOException, SAXException, TikaException {
128         Parser parser = getParser(metadata);
129         TaggedInputStream taggedStream = new TaggedInputStream(stream);
130         TaggedContentHandler taggedHandler = new TaggedContentHandler(handler);
131         try {
132             parser.parse(taggedStream, taggedHandler, metadata, context);
133         } catch (RuntimeException e) {
134             throw new TikaException(
135                     "Unexpected RuntimeException from " + parser, e);
136         } catch (IOException e) {
137             taggedStream.throwIfCauseOf(e);
138             throw new TikaException(
139                     "TIKA-198: Illegal IOException from " + parser, e);
140         } catch (SAXException e) {
141             taggedHandler.throwIfCauseOf(e);
142             throw new TikaException(
143                     "TIKA-237: Illegal SAXException from " + parser, e);
144         }
145     }
146 
147     /**
148      * @deprecated This method will be removed in Apache Tika 1.0.
149      */
150     public void parse(
151             InputStream stream, ContentHandler handler, Metadata metadata)
152             throws IOException, SAXException, TikaException {
153         parse(stream, handler, metadata, new ParseContext());
154     }
155 
156 }