-
Notifications
You must be signed in to change notification settings - Fork 219
/
MultipleParser.java
176 lines (151 loc) · 6.14 KB
/
MultipleParser.java
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
package iped.parsers.misc;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.apache.tika.config.Field;
import org.apache.tika.exception.TikaException;
import org.apache.tika.io.TemporaryResources;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.metadata.TikaCoreProperties;
import org.apache.tika.mime.MediaType;
import org.apache.tika.parser.AbstractParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.EmbeddedContentHandler;
import org.apache.tika.utils.ParserUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import iped.io.IStreamSource;
import iped.parsers.standard.StandardParser;
import iped.parsers.util.ItemInfo;
import iped.utils.IOUtil;
/**
* Runs a list of parsers sequentially on a file.
*
* @author Nassif
*
*/
public class MultipleParser extends AbstractParser {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final Logger logger = LoggerFactory.getLogger(MultipleParser.class);
private List<Parser> parsers = new ArrayList<>();
private Set<MediaType> supportedTypes = new HashSet<>();
@Field
private boolean stopAfterSomeParserWorks = false;
@Field
private String parserName = MultipleParser.class.getSimpleName();
public String getParserName() {
return parserName;
}
@Override
public Set<MediaType> getSupportedTypes(ParseContext context) {
return supportedTypes;
}
public void addParser(Parser parser) {
parsers.add(parser);
}
public void addSupportedTypes(Set<MediaType> mimes) {
supportedTypes.addAll(mimes);
}
@Field
public void setParsers(String value) throws InstantiationException, IllegalAccessException, ClassNotFoundException,
IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
String[] parsers = value.split(";");
for (String p : parsers) {
if (!(p = p.trim()).isEmpty()) {
this.parsers.add((Parser) Class.forName(p).getDeclaredConstructor().newInstance());
}
}
}
@Override
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context)
throws IOException, SAXException, TikaException {
ItemInfo itemInfo = context.get(ItemInfo.class);
IStreamSource source = context.get(IStreamSource.class);
TemporaryResources tmp = new TemporaryResources();
TikaException tikaException = null;
EmbeddedContentHandler embeddedHandler = new EmbeddedContentHandler(handler);
embeddedHandler.startDocument();
try {
TikaInputStream tis = TikaInputStream.get(stream, tmp);
boolean firstTis = true;
Path tempPath = null;
for (Parser parser : parsers) {
if (tis != null && (tis.hasFile() || source == null)) {
tempPath = tis.getPath();
}
if (tis == null) {
if (tempPath != null) {
tis = TikaInputStream.get(tempPath);
} else {
tis = TikaInputStream.get(source.getSeekableInputStream());
}
firstTis = false;
}
Metadata newMetadata = getNewMetadata(metadata);
try {
parser.parse(tis, embeddedHandler, newMetadata, context);
if (stopAfterSomeParserWorks) {
break;
}
// if this parser works, clear exception to not throw it
tikaException = null;
} catch (Throwable e) {
ParserUtils.recordParserFailure(parser, e, newMetadata);
String filepath = itemInfo != null ? itemInfo.getPath() : "file";
logger.warn("Exception from {} on {}: {}", parser.getClass().getName(), filepath, e.toString());
if (tikaException == null) {
tikaException = new TikaException("Exception from " + parser.getClass().getName());
}
tikaException.addSuppressed(e);
} finally {
ParserUtils.recordParserDetails(parser, newMetadata);
// merge even if parser fails, some meta could be extracted
mergeMetadata(metadata, newMetadata);
if (!firstTis) {
IOUtil.closeQuietly(tis);
}
tis = null;
}
}
} finally {
embeddedHandler.endDocument();
tmp.close();
if (tikaException != null) {
throw tikaException;
}
}
}
private Metadata getNewMetadata(Metadata metadata) {
Metadata newMetadata = new Metadata();
newMetadata.set(TikaCoreProperties.RESOURCE_NAME_KEY, metadata.get(TikaCoreProperties.RESOURCE_NAME_KEY));
newMetadata.set(Metadata.CONTENT_LENGTH, metadata.get(Metadata.CONTENT_LENGTH));
newMetadata.set(Metadata.CONTENT_TYPE, metadata.get(Metadata.CONTENT_TYPE));
newMetadata.set(StandardParser.INDEXER_CONTENT_TYPE,
metadata.get(StandardParser.INDEXER_CONTENT_TYPE));
return metadata;
}
private void mergeMetadata(Metadata metadata, Metadata newMetadata) {
for (String name : newMetadata.names()) {
Set<String> oldValues = new TreeSet<>(Arrays.asList(metadata.getValues(name)));
for (String newVal : newMetadata.getValues(name)) {
if (!oldValues.contains(newVal)) {
metadata.add(name, newVal);
}
}
}
}
}