GibsonParser.java
11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.fontengine.font.Font
* com.adobe.fontengine.font.FontLoadingException
* com.adobe.fontengine.fontmanagement.FontLoader
* com.adobe.internal.io.ByteReader
* com.adobe.internal.io.RandomAccessFileByteReader
* com.adobe.internal.pdftoolkit.core.exceptions.PDFException
* com.adobe.internal.pdftoolkit.core.fontset.PDFFontSet
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferenceCardinality
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.tika.exception.TikaException
* org.apache.tika.io.IOExceptionWithCause
* org.apache.tika.io.TemporaryResources
* org.apache.tika.io.TikaInputStream
* org.apache.tika.metadata.Metadata
* org.apache.tika.metadata.PagedText
* org.apache.tika.metadata.Property
* org.apache.tika.mime.MediaType
* org.apache.tika.parser.AbstractParser
* org.apache.tika.parser.ParseContext
* org.apache.tika.sax.XHTMLContentHandler
*/
package com.adobe.internal.pdf.tika;
import com.adobe.fontengine.font.Font;
import com.adobe.fontengine.font.FontLoadingException;
import com.adobe.fontengine.fontmanagement.FontLoader;
import com.adobe.internal.io.ByteReader;
import com.adobe.internal.io.RandomAccessFileByteReader;
import com.adobe.internal.pdf.tika.PDFFontSetDefault;
import com.adobe.internal.pdf.tika.PDFFontSetInterface;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFException;
import com.adobe.internal.pdftoolkit.core.fontset.PDFFontSet;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.document.PDFOpenOptions;
import com.adobe.internal.pdftoolkit.pdf.interactive.annotation.PDFAnnotation;
import com.adobe.internal.pdftoolkit.pdf.interactive.annotation.PDFAnnotationIterator;
import com.adobe.internal.pdftoolkit.pdf.interactive.annotation.PDFAnnotationMarkup;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPage;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPageTree;
import com.adobe.internal.pdftoolkit.services.readingorder.ReadingOrderTextExtractor;
import com.adobe.internal.pdftoolkit.services.textextraction.ParagraphIterator;
import com.adobe.internal.pdftoolkit.services.textextraction.Word;
import com.adobe.internal.pdftoolkit.services.xmp.DocumentMetadata;
import com.adobe.internal.pdftoolkit.services.xmp.XMPService;
import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.tika.exception.TikaException;
import org.apache.tika.io.IOExceptionWithCause;
import org.apache.tika.io.TemporaryResources;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.metadata.PagedText;
import org.apache.tika.metadata.Property;
import org.apache.tika.mime.MediaType;
import org.apache.tika.parser.AbstractParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.sax.XHTMLContentHandler;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class GibsonParser
extends AbstractParser {
private static final long serialVersionUID = 1776775061856503811L;
private PDFFontSet pdfFontSet = null;
@Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.MANDATORY_UNARY)
private PDFFontSetInterface fontSet;
private static final MediaType PDF = MediaType.application((String)"pdf");
private boolean extractAnnotationText = true;
public Set<MediaType> getSupportedTypes(ParseContext context) {
return Collections.singleton(PDF);
}
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
TemporaryResources tmp = new TemporaryResources();
try {
TikaInputStream tis = TikaInputStream.get((InputStream)stream, (TemporaryResources)tmp);
RandomAccessFile file = new RandomAccessFile(tis.getFile(), "r");
tmp.addResource((Closeable)file);
RandomAccessFileByteReader reader = new RandomAccessFileByteReader(file);
tmp.addResource(new Closeable((ByteReader)reader){
final /* synthetic */ ByteReader val$reader;
public void close() throws IOException {
this.val$reader.close();
}
});
final PDFDocument document = PDFDocument.newInstance((ByteReader)reader, PDFOpenOptions.newInstance());
tmp.addResource(new Closeable(){
public void close() throws IOException {
try {
document.close();
}
catch (PDFException e) {
throw new IOExceptionWithCause((Throwable)e);
}
}
});
metadata.set("Content-Type", PDF.toString());
this.extractMetadata(document, metadata);
if (this.fontSet != null) {
byte[] fontByteArray;
this.pdfFontSet = this.fontSet.getPdfFontSet();
FontLoader loader = new FontLoader();
if (this.pdfFontSet == null) {
Font[] fonts = null;
File[] directories = this.fontSet.getFontDirectories();
if (directories != null && directories.length > 0) {
for (int i = 0; i < directories.length; ++i) {
if (!directories[i].exists() || (fonts = loader.load(directories[i], true, new ArrayList())) == null || fonts.length <= 0) continue;
int j = 0;
while (i < fonts.length) {
this.pdfFontSet.addFont(fonts[j]);
++i;
}
}
}
}
if (this.pdfFontSet == null && (fontByteArray = this.fontSet.getFontSetStream()) != null) {
ByteArrayInputStream inputStream = new ByteArrayInputStream(fontByteArray);
try {
loader.load((InputStream)inputStream, inputStream.available(), false);
}
catch (FontLoadingException e) {
throw new IOExceptionWithCause("Unable to load font from provided font stream", (Throwable)e);
}
}
}
if (this.pdfFontSet == null) {
this.fontSet = new PDFFontSetDefault();
this.pdfFontSet = this.fontSet.getPdfFontSet();
}
ReadingOrderTextExtractor extractor = ReadingOrderTextExtractor.newInstance(document, this.pdfFontSet);
XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
xhtml.startDocument();
Iterator<PDFPage> pageIter = document.requirePages().iterator();
while (pageIter.hasNext()) {
PDFPage page = pageIter.next();
xhtml.startElement("div", "class", "page");
xhtml.startElement("p");
ParagraphIterator iterator = extractor.getParagraphIterator(page, page.getIndex());
while (iterator.hasNext()) {
xhtml.startElement("p");
List<List<Word>> lines = iterator.next();
for (List<Word> line : lines) {
for (Word word : line) {
xhtml.characters(word.toString());
}
xhtml.characters("\n");
}
xhtml.endElement("p");
}
xhtml.endElement("p");
if (this.extractAnnotationText) {
xhtml.startElement("div", "class", "annotation");
PDFAnnotationIterator annotsIterator = page.getAnnotationsIterator();
while (annotsIterator.hasNext()) {
String subject;
String contents;
PDFAnnotation annot = annotsIterator.next();
if (!(annot instanceof PDFAnnotationMarkup)) continue;
PDFAnnotationMarkup markupAnnot = (PDFAnnotationMarkup)annot;
String title = markupAnnot.getTitle();
if (title != null) {
xhtml.startElement("p", "class", "annotationTitle");
xhtml.characters(title);
xhtml.endElement("p");
}
if ((subject = markupAnnot.getSubject()) != null) {
xhtml.startElement("p", "class", "annotationSubject");
xhtml.characters(subject);
xhtml.endElement("p");
}
if ((contents = markupAnnot.getContents()) == null) continue;
xhtml.startElement("p", "class", "annotationContents");
xhtml.characters(contents);
xhtml.endElement("p");
}
xhtml.endElement("div");
}
xhtml.endElement("div");
}
xhtml.endDocument();
}
catch (PDFException e) {
throw new TikaException("PDF parse error", (Throwable)e);
}
finally {
tmp.close();
}
}
private void extractMetadata(PDFDocument document, Metadata metadata) {
try {
DocumentMetadata info = XMPService.getDocumentMetadata(document);
metadata.set(PagedText.N_PAGES, document.requirePages().getCount());
metadata.set(Metadata.CREATION_DATE, info.getCreationDate());
metadata.set(Metadata.LAST_MODIFIED, info.getModificationDate());
this.addMetadata(metadata, "title", info.getTitle());
this.addMetadata(metadata, "Author", info.getAuthor());
this.addMetadata(metadata, "creator", info.getCreator());
this.addMetadata(metadata, "Keywords", info.getKeywordsAsString());
this.addMetadata(metadata, "producer", info.getProducer());
this.addMetadata(metadata, "subject", info.getSubject());
this.addMetadata(metadata, "trapped", info.getTrapped());
List<String> handledMetadata = Arrays.asList("Author", "Creator", "CreationDate", "ModDate", "Keywords", "Producer", "Subject", "Title", "Trapped");
List customPropertyNames = info.getCustomPropertyNames();
if (customPropertyNames != null && customPropertyNames.size() > 0) {
for (String name : info.getCustomPropertyNames()) {
if (handledMetadata.contains(name)) continue;
this.addMetadata(metadata, name, info.getCustomProperty(name));
}
}
}
catch (PDFException e) {
// empty catch block
}
}
private void addMetadata(Metadata metadata, String name, String value) {
if (value != null) {
metadata.add(name, value);
}
}
}