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.rtf;
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.swing.text.AttributeSet;
25  import javax.swing.text.BadLocationException;
26  import javax.swing.text.DefaultStyledDocument;
27  import javax.swing.text.Document;
28  import javax.swing.text.StyleContext;
29  import javax.swing.text.rtf.RTFEditorKit;
30  
31  import org.apache.tika.exception.TikaException;
32  import org.apache.tika.metadata.Metadata;
33  import org.apache.tika.mime.MediaType;
34  import org.apache.tika.parser.ParseContext;
35  import org.apache.tika.parser.Parser;
36  import org.apache.tika.sax.XHTMLContentHandler;
37  import org.xml.sax.ContentHandler;
38  import org.xml.sax.SAXException;
39  
40  /**
41   * RTF parser
42   */
43  public class RTFParser implements Parser {
44  
45      private static final Set<MediaType> SUPPORTED_TYPES =
46          Collections.singleton(MediaType.application("rtf"));
47  
48      public Set<MediaType> getSupportedTypes(ParseContext context) {
49          return SUPPORTED_TYPES;
50      }
51  
52      public void parse(
53              InputStream stream, ContentHandler handler,
54              Metadata metadata, ParseContext context)
55              throws IOException, SAXException, TikaException {
56          try {
57              Document sd = new CustomStyledDocument();
58              new RTFEditorKit().read(stream, sd, 0);
59  
60              XHTMLContentHandler xhtml =
61                  new XHTMLContentHandler(handler, metadata);
62              xhtml.startDocument();
63              xhtml.element("p", sd.getText(0, sd.getLength()));
64              xhtml.endDocument();
65          } catch (BadLocationException e) {
66              throw new TikaException("Error parsing an RTF document", e);
67          }
68      }
69  
70      /**
71       * @deprecated This method will be removed in Apache Tika 1.0.
72       */
73      public void parse(
74              InputStream stream, ContentHandler handler, Metadata metadata)
75              throws IOException, SAXException, TikaException {
76          parse(stream, handler, metadata, new ParseContext());
77      }
78  
79      /**
80       * Customized version of {@link DefaultStyledDocument}. Adds whitespace
81       * to places where words otherwise could have run together (see
82       * <a href="https://issues.apache.org/jira/browse/TIKA-392">TIKA-392</a>),
83       * and works around the problem of Swing expecting a GUI environment (see
84       * <a href="https://issues.apache.org/jira/browse/TIKA-282">TIKA-282</a>).
85       */
86      private static class CustomStyledDocument extends DefaultStyledDocument {
87  
88          public CustomStyledDocument() {
89              super(new NoReclaimStyleContext());
90          }
91  
92          @Override
93          public void insertString(
94                  int offs, String str, AttributeSet a)
95          throws BadLocationException {
96              if (offs > 0 && offs == getLength()) {
97                  super.insertString(offs, " ", a);
98                  super.insertString(getLength(), str, a);
99              } else {
100                 super.insertString(offs, str, a);
101             }
102         }
103 
104     }
105 
106     /**
107      * A workaround to
108      * <a href="https://issues.apache.org/jira/browse/TIKA-282">TIKA-282</a>:
109      * RTF parser expects a GUI environment. This class simply disables the
110      * troublesome SwingUtilities.isEventDispatchThread() call that's made in
111      * the {@link StyleContext#reclaim(AttributeSet)} method.
112      */
113     private static class NoReclaimStyleContext extends StyleContext {
114 
115         /** Ignored. */
116         public void reclaim(AttributeSet a) {
117         }
118 
119     }
120 
121 }