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.epub;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.util.Collections;
22  import java.util.Set;
23  
24  import javax.xml.XMLConstants;
25  import javax.xml.parsers.ParserConfigurationException;
26  import javax.xml.parsers.SAXParser;
27  import javax.xml.parsers.SAXParserFactory;
28  
29  import org.apache.tika.exception.TikaException;
30  import org.apache.tika.io.CloseShieldInputStream;
31  import org.apache.tika.metadata.Metadata;
32  import org.apache.tika.mime.MediaType;
33  import org.apache.tika.parser.ParseContext;
34  import org.apache.tika.parser.Parser;
35  import org.apache.tika.sax.OfflineContentHandler;
36  import org.apache.tika.sax.XHTMLContentHandler;
37  import org.xml.sax.ContentHandler;
38  import org.xml.sax.SAXException;
39  import org.xml.sax.SAXNotRecognizedException;
40  
41  /**
42   * Parser for EPUB OPS <code>*.html</code> files.
43   *
44   * For the time being, assume XHTML (TODO: DTBook)
45   */
46  public class EpubContentParser implements Parser {
47  
48      public Set<MediaType> getSupportedTypes(ParseContext context) {
49          return Collections.emptySet(); // not a top-level parser
50      }
51  
52      public void parse(
53              InputStream stream, ContentHandler handler,
54              Metadata metadata, ParseContext context)
55              throws IOException, SAXException, TikaException {
56          final XHTMLContentHandler xhtml =
57              new XHTMLContentHandler(handler,metadata);
58  
59          try {
60              SAXParserFactory factory = SAXParserFactory.newInstance();
61              factory.setValidating(false);
62              factory.setNamespaceAware(true);
63              try {
64                  factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
65              } catch (SAXNotRecognizedException e) {
66                  // TIKA-329: Some XML parsers do not support the secure-processing
67                  // feature, even though it's required by JAXP in Java 5. Ignoring
68                  // the exception is fine here, deployments without this feature
69                  // are inherently vulnerable to XML denial-of-service attacks.
70              }
71              SAXParser parser = factory.newSAXParser();
72              parser.parse(
73                      new CloseShieldInputStream(stream),
74                      new OfflineContentHandler(xhtml));
75          } catch (ParserConfigurationException e) {
76              throw new TikaException("XML parser configuration error", e);
77          }
78      }
79  
80      /**
81       * @deprecated This method will be removed in Apache Tika 1.0.
82       */
83      public void parse(
84              InputStream stream, ContentHandler handler, Metadata metadata)
85              throws IOException, SAXException, TikaException {
86          parse(stream, handler, metadata, new ParseContext());
87      }
88  
89  }