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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.sl.usermodel;
import static org.apache.poi.extractor.ExtractorFactory.OOXML_PACKAGE;
import static org.apache.poi.poifs.crypt.EncryptionInfo.ENCRYPTION_INFO_ENTRY;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
import org.apache.poi.EmptyFileException;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.poifs.crypt.Decryptor;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.FileMagic;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public final class SlideShowFactory {
private static class Singleton {
private static final SlideShowFactory INSTANCE = new SlideShowFactory();
}
private interface ProviderMethod {
SlideShow<?,?> create(SlideShowProvider<?,?> prov) throws IOException;
}
private final List<SlideShowProvider<?,?>> provider = new ArrayList<>();
private SlideShowFactory() {
ClassLoader cl = SlideShowFactory.class.getClassLoader();
ServiceLoader.load(SlideShowProvider.class, cl).forEach(provider::add);
}
/**
* Create a new empty SlideShow, either XSLF or HSLF depending
* on the parameter
*
* @param XSLF If an XSLFSlideShow or a HSLFSlideShow should be created
*
* @return The created SlideShow
*
* @throws IOException if an error occurs while creating the objects
*/
public static SlideShow<?,?> create(boolean XSLF) throws IOException {
return wp(XSLF ? FileMagic.OOXML : FileMagic.OLE2, SlideShowProvider::create);
}
/**
* Creates a HSLFSlideShow from the given POIFSFileSystem<p>
*
* Note that in order to properly release resources the
* SlideShow should be closed after use.
*
* @param fs The {@link POIFSFileSystem} to read the document from
*
* @return The created SlideShow
*
* @throws IOException if an error occurs while reading the data
*/
public static SlideShow<?,?> create(POIFSFileSystem fs) throws IOException {
return create(fs, null);
}
/**
* Creates a SlideShow from the given POIFSFileSystem, which may
* be password protected
*
* @param fs The {@link POIFSFileSystem} to read the document from
* @param password The password that should be used or null if no password is necessary.
*
* @return The created SlideShow
*
* @throws IOException if an error occurs while reading the data
*/
private static SlideShow<?,?> create(final POIFSFileSystem fs, String password) throws IOException {
return create(fs.getRoot(), password);
}
/**
* Creates a SlideShow from the given DirectoryNode.
*
* @param root The {@link DirectoryNode} to start reading the document from
*
* @return The created SlideShow
*
* @throws IOException if an error occurs while reading the data
*/
public static SlideShow<?,?> create(final DirectoryNode root) throws IOException {
return create(root, null);
}
/**
* Creates a SlideShow from the given DirectoryNode, which may
* be password protected
*
* @param root The {@link DirectoryNode} to start reading the document from
* @param password The password that should be used or null if no password is necessary.
*
* @return The created SlideShow
*
* @throws IOException if an error occurs while reading the data
*/
public static SlideShow<?,?> create(final DirectoryNode root, String password) throws IOException {
// Encrypted OOXML files go inside OLE2 containers, is this one?
if (root.hasEntry(Decryptor.DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE)) {
return wp(FileMagic.OOXML, w -> w.create(root, password));
} else {
return wp(FileMagic.OLE2, w -> w.create(root, password));
}
}
/**
* Creates the appropriate HSLFSlideShow / XSLFSlideShow from
* the given InputStream.
*
* <p>Your input stream MUST either support mark/reset, or
* be wrapped as a {@link BufferedInputStream}!
* Note that using an {@link InputStream} has a higher memory footprint
* than using a {@link File}.</p>
*
* <p>Note that in order to properly release resources the
* SlideShow should be closed after use. Note also that loading
* from an InputStream requires more memory than loading
* from a File, so prefer {@link #create(File)} where possible.
*
* @param inp The {@link InputStream} to read data from.
*
* @return The created SlideShow
*
* @throws IOException if an error occurs while reading the data
* @throws EncryptedDocumentException If the SlideShow<?,?> given is password protected
*/
public static SlideShow<?,?> create(InputStream inp) throws IOException, EncryptedDocumentException {
return create(inp, null);
}
/**
* Creates the appropriate HSLFSlideShow / XSLFSlideShow from
* the given InputStream, which may be password protected.
*
* <p>Your input stream MUST either support mark/reset, or
* be wrapped as a {@link BufferedInputStream}!
* Note that using an {@link InputStream} has a higher memory footprint
* than using a {@link File}.</p>
*
* <p>Note that in order to properly release resources the
* SlideShow should be closed after use. Note also that loading
* from an InputStream requires more memory than loading
* from a File, so prefer {@link #create(File)} where possible.</p>
*
* @param inp The {@link InputStream} to read data from.
* @param password The password that should be used or null if no password is necessary.
*
* @return The created SlideShow
*
* @throws IOException if an error occurs while reading the data
* @throws EncryptedDocumentException If the wrong password is given for a protected file
*/
public static SlideShow<?,?> create(InputStream inp, String password) throws IOException, EncryptedDocumentException {
InputStream is = FileMagic.prepareToCheckMagic(inp);
byte[] emptyFileCheck = new byte[1];
is.mark(emptyFileCheck.length);
if (is.read(emptyFileCheck) < emptyFileCheck.length) {
throw new EmptyFileException();
}
is.reset();
final FileMagic fm = FileMagic.valueOf(is);
if (FileMagic.OOXML == fm) {
return wp(fm, w -> w.create(is));
}
if (FileMagic.OLE2 != fm) {
throw new IOException("Can't open SlideShow - unsupported file type: "+fm);
}
POIFSFileSystem poifs = new POIFSFileSystem(is);
boolean isOOXML = poifs.getRoot().hasEntry(ENCRYPTION_INFO_ENTRY);
return wp(isOOXML ? FileMagic.OOXML : fm, w -> w.create(poifs.getRoot(), password));
}
/**
* Creates the appropriate HSLFSlideShow / XSLFSlideShow from
* the given File, which must exist and be readable.
* <p>Note that in order to properly release resources the
* SlideShow should be closed after use.
*
* @param file The file to read data from.
*
* @return The created SlideShow
*
* @throws IOException if an error occurs while reading the data
* @throws EncryptedDocumentException If the SlideShow given is password protected
*/
public static SlideShow<?,?> create(File file) throws IOException, EncryptedDocumentException {
return create(file, null);
}
/**
* Creates the appropriate HSLFSlideShow / XSLFSlideShow from
* the given File, which must exist and be readable, and
* may be password protected
* <p>Note that in order to properly release resources the
* SlideShow should be closed after use.
*
* @param file The file to read data from.
* @param password The password that should be used or null if no password is necessary.
*
* @return The created SlideShow
*
* @throws IOException if an error occurs while reading the data
* @throws EncryptedDocumentException If the wrong password is given for a protected file
*/
public static SlideShow<?,?> create(File file, String password) throws IOException, EncryptedDocumentException {
return create(file, password, false);
}
/**
* Creates the appropriate HSLFSlideShow / XSLFSlideShow from
* the given File, which must exist and be readable, and
* may be password protected
* <p>Note that in order to properly release resources the
* SlideShow should be closed after use.
*
* @param file The file to read data from.
* @param password The password that should be used or null if no password is necessary.
* @param readOnly If the SlideShow should be opened in read-only mode to avoid writing back
* changes when the document is closed.
*
* @return The created SlideShow
*
* @throws IOException if an error occurs while reading the data
* @throws EncryptedDocumentException If the wrong password is given for a protected file
*/
public static SlideShow<?,?> create(File file, String password, boolean readOnly) throws IOException, EncryptedDocumentException {
if (!file.exists()) {
throw new FileNotFoundException(file.toString());
}
if (file.length() == 0) {
throw new EmptyFileException();
}
FileMagic fm = FileMagic.valueOf(file);
if (fm == FileMagic.OOXML) {
return wp(fm, w -> w.create(file, password, readOnly));
} else if (fm == FileMagic.OLE2) {
final boolean ooxmlEnc;
try (POIFSFileSystem fs = new POIFSFileSystem(file, true)) {
DirectoryNode root = fs.getRoot();
ooxmlEnc = root.hasEntry(Decryptor.DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE);
}
return wp(ooxmlEnc ? FileMagic.OOXML : fm, w -> w.create(file, password, readOnly));
}
return null;
}
private static SlideShow<?,?> wp(FileMagic fm, SlideShowFactory.ProviderMethod fun) throws IOException {
for (SlideShowProvider<?,?> prov : SlideShowFactory.Singleton.INSTANCE.provider) {
if (prov.accepts(fm)) {
return fun.create(prov);
}
}
throw new IOException("Your InputStream was neither an OLE2 stream, nor an OOXML stream " +
"or you haven't provide the poi-ooxml*.jar in the classpath/modulepath - FileMagic: "+fm);
}
public static void addProvider(SlideShowProvider<?,?> provider){
Singleton.INSTANCE.provider.add(provider);
}
public static void removeProvider(Class<? extends SlideShowProvider> provider){
Singleton.INSTANCE.provider.removeIf(p -> p.getClass().getName().equals(provider.getName()));
}
}
|