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.Set; 22 23 import org.apache.tika.exception.TikaException; 24 import org.apache.tika.metadata.Metadata; 25 import org.apache.tika.mime.MediaType; 26 import org.xml.sax.ContentHandler; 27 import org.xml.sax.SAXException; 28 29 /** 30 * Base class for parser implementations that want to delegate parts of the 31 * task of parsing an input document to another parser. The delegate parser 32 * is looked up from the parsing context using the {@link Parser} class as 33 * the key. 34 * 35 * @since Apache Tika 0.4, major changes in Tika 0.5 36 */ 37 public class DelegatingParser implements Parser { 38 39 /** 40 * Returns the parser instance to which parsing tasks should be delegated. 41 * The default implementation looks up the delegate parser from the given 42 * parse context, and uses an {@link EmptyParser} instance as a fallback. 43 * Subclasses can override this method to implement alternative delegation 44 * strategies. 45 * 46 * @since Apache Tika 0.7 47 * @param context parse context 48 * @return delegate parser 49 */ 50 protected Parser getDelegateParser(ParseContext context) { 51 return context.get(Parser.class, EmptyParser.INSTANCE); 52 } 53 54 public Set<MediaType> getSupportedTypes(ParseContext context) { 55 return getDelegateParser(context).getSupportedTypes(context); 56 } 57 58 /** 59 * Looks up the delegate parser from the parsing context and 60 * delegates the parse operation to it. If a delegate parser is not 61 * found, then an empty XHTML document is returned. 62 * <p> 63 * Subclasses should override this method to parse the top level 64 * structure of the given document stream. Parsed sub-streams can 65 * be passed to this base class method to be parsed by the configured 66 * delegate parser. 67 */ 68 public void parse( 69 InputStream stream, ContentHandler handler, 70 Metadata metadata, ParseContext context) 71 throws SAXException, IOException, TikaException { 72 getDelegateParser(context).parse(stream, handler, metadata, context); 73 } 74 75 /** 76 * @deprecated This method will be removed in Apache Tika 1.0. 77 */ 78 public void parse( 79 InputStream stream, ContentHandler handler, Metadata metadata) 80 throws IOException, SAXException, TikaException { 81 parse(stream, handler, metadata, new ParseContext()); 82 } 83 84 }