1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
81
82
83
84
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
108
109
110
111
112
113 private static class NoReclaimStyleContext extends StyleContext {
114
115
116 public void reclaim(AttributeSet a) {
117 }
118
119 }
120
121 }