-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathTikaDocument.java
210 lines (168 loc) · 5.51 KB
/
TikaDocument.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
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
package org.icij.extract.document;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.metadata.Property;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Path;
import java.util.*;
import java.util.function.Supplier;
public class TikaDocument {
public static String CONTENT_ENCODING = "Content-Encoding";
public static String CONTENT_LANGUAGE = "Content-Language";
public static String CONTENT_LENGTH = "Content-Length";
public static String CONTENT_LOCATION = "Content-Location";
public static String CONTENT_DISPOSITION = "Content-Disposition";
public static String CONTENT_MD5 = "Content-MD5";
public static String CONTENT_TYPE = "Content-Type";
public static Property LAST_MODIFIED = Property.internalDate("Last-Modified");
public static String LOCATION = "Location";
private final Path path;
private Supplier<String> id;
private String foreignId = null;
private final Metadata metadata;
private Identifier identifier;
private List<EmbeddedTikaDocument> embeds = new LinkedList<>();
private Map<String, EmbeddedTikaDocument> lookup = new HashMap<>();
private Reader reader = null;
private ReaderGenerator readerGenerator = null;
/**
* Instantiate a document with a pre-generated ID. In this case, the ID generator is only used when adding
* embedded documents to this parent.
*
* @param id a pre-generated ID
* @param identifier an identifier generator
* @param path the path to the document
* @param metadata document metadata
*/
public TikaDocument(final String id, final Identifier identifier, final Path path, final Metadata metadata) {
Objects.requireNonNull(path, "The path must not be null.");
this.metadata = metadata;
this.path = path;
this.identifier = identifier;
this.id = ()-> id;
}
/**
* @see TikaDocument (String, Identifier, Path, Metadata)
*/
public TikaDocument(final String id, final Identifier identifier, final Path path) {
this(id, identifier, path, new Metadata());
}
/**
* Instantiate a document when the ID has not yet been generated.
*
* @param identifier for generating the ID
* @param path the path to the document
* @param metadata document metadata
*/
public TikaDocument(final Identifier identifier, final Path path, final Metadata metadata) {
Objects.requireNonNull(identifier, "The identifier generator must not be null.");
Objects.requireNonNull(path, "The path must not be null.");
this.metadata = metadata;
this.path = path;
this.identifier = identifier;
// Create a supplier that will cache the result of the generator after the first invocation.
this.id = ()-> {
final String id;
try {
id = this.generateId();
} catch (final Exception e) {
throw new RuntimeException("Unable to generate document ID.", e);
}
this.id = ()-> id;
return id;
};
}
/**
* @see TikaDocument (Identifier, Path, Metadata)
*/
public TikaDocument(final Identifier identifier, final Path path) {
this(identifier, path, new Metadata());
}
String generateId() throws Exception {
return identifier.generate(this);
}
public String getId() {
return id.get();
}
public String getHash() {
return identifier.retrieveHash(getMetadata());
}
Identifier getIdentifier() {
return identifier;
}
public Path getPath() {
return path;
}
public String getMetadata(final String fieldName) {
return metadata.get(fieldName);
}
public Metadata getMetadata() {
return metadata;
}
public EmbeddedTikaDocument addEmbed(final Metadata metadata) {
return addEmbed(new EmbeddedTikaDocument(this, metadata));
}
private EmbeddedTikaDocument addEmbed(final Identifier identifier, final Path path, final Metadata metadata) {
return addEmbed(new EmbeddedTikaDocument(this, identifier, path, metadata));
}
public EmbeddedTikaDocument addEmbed(final String key, final Identifier identifier, final Path path, final Metadata
metadata) {
return lookup.put(key, addEmbed(identifier, path, metadata));
}
private EmbeddedTikaDocument addEmbed(final EmbeddedTikaDocument embed) {
embeds.add(embed);
return embed;
}
public boolean removeEmbed(final EmbeddedTikaDocument embed) {
return embeds.remove(embed);
}
public List<EmbeddedTikaDocument> getEmbeds() {
return embeds;
}
public boolean hasEmbeds() {
return !embeds.isEmpty();
}
public EmbeddedTikaDocument getEmbed(final String key) {
return lookup.get(key);
}
public void setReader(final Reader reader) {
this.reader = reader;
}
public void setReader(final ReaderGenerator readerGenerator) {
this.readerGenerator = readerGenerator;
}
public void clearReader() {
this.reader = null;
this.readerGenerator = null;
}
public void setForeignId(final String foreignId) {
this.foreignId = foreignId;
}
public String getForeignId() {
return foreignId;
}
public synchronized Reader getReader() throws IOException {
if (null == reader && null != readerGenerator) {
reader = readerGenerator.generate();
}
return reader;
}
@Override
public boolean equals(final Object other) {
if (!(other instanceof TikaDocument)) {
return false;
}
// Only documents with the same ID are equal, as paths are not globally unique unless explicitly declared so,
// if, for example, the PathIdentifier is used.
final String id = getId();
return null != id && id.equals(((TikaDocument) other).getId());
}
@Override
public int hashCode() { return Objects.hash(id);}
@Override
public String toString() {
return path.toString();
}
@FunctionalInterface
public interface ReaderGenerator { Reader generate() throws IOException;}
}