Skip to content

Commit ac4e3c1

Browse files
committed
more HSSFEventFactory changes
1 parent a1f6f27 commit ac4e3c1

File tree

2 files changed

+169
-26
lines changed

2 files changed

+169
-26
lines changed

poi/src/main/java/org/apache/poi/hssf/eventusermodel/HSSFEventFactory.java

Lines changed: 129 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public HSSFEventFactory() {
4747
/**
4848
* Processes a file into essentially record events.
4949
*
50-
* @param req an Instance of HSSFRequest which has your registered listeners
50+
* @param req an instance of HSSFRequest which has your registered listeners
5151
* @param fs a POIFS filesystem containing your workbook
5252
*
5353
* @throws IOException if the workbook contained errors
@@ -56,15 +56,45 @@ public void processWorkbookEvents(HSSFRequest req, POIFSFileSystem fs) throws IO
5656
processWorkbookEvents(req, fs.getRoot());
5757
}
5858

59+
/**
60+
* Processes a file into essentially record events.
61+
*
62+
* @param req an instance of HSSFRequest which has your registered listeners
63+
* @param fs a POIFS filesystem containing your workbook
64+
* @param password in char array format (can be null)
65+
*
66+
* @throws IOException if the workbook contained errors
67+
* @since 6.0.0
68+
*/
69+
public void processWorkbookEvents(HSSFRequest req, POIFSFileSystem fs,
70+
final char[] password) throws IOException {
71+
processWorkbookEvents(req, fs.getRoot(), password);
72+
}
73+
5974
/**
6075
* Processes a file into essentially record events.
6176
*
62-
* @param req an Instance of HSSFRequest which has your registered listeners
77+
* @param req an instance of HSSFRequest which has your registered listeners
6378
* @param dir a DirectoryNode containing your workbook
6479
*
6580
* @throws IOException if the workbook contained errors
6681
*/
6782
public void processWorkbookEvents(HSSFRequest req, DirectoryNode dir) throws IOException {
83+
processWorkbookEvents(req, dir, null);
84+
}
85+
86+
/**
87+
* Processes a file into essentially record events.
88+
*
89+
* @param req an instance of HSSFRequest which has your registered listeners
90+
* @param dir a DirectoryNode containing your workbook
91+
* @param password in char array format (can be null)
92+
*
93+
* @throws IOException if the workbook contained errors
94+
* @since 6.0.0
95+
*/
96+
public void processWorkbookEvents(HSSFRequest req, DirectoryNode dir,
97+
final char[] password) throws IOException {
6898
// some old documents have "WORKBOOK" or "BOOK"
6999
String name = null;
70100
if (dir.hasEntry(WORKBOOK)) {
@@ -79,29 +109,45 @@ public void processWorkbookEvents(HSSFRequest req, DirectoryNode dir) throws IOE
79109
}
80110

81111
try (InputStream in = dir.createDocumentInputStream(name)) {
82-
processEvents(req, in);
112+
processEvents(req, in, password);
83113
}
84114
}
85115

86-
/**
87-
* Processes a file into essentially record events.
88-
*
89-
* @param req an Instance of HSSFRequest which has your registered listeners
90-
* @param fs a POIFS filesystem containing your workbook
91-
* @return numeric user-specified result code.
92-
*
93-
* @throws HSSFUserException if the processing should be aborted
94-
* @throws IOException if the workbook contained errors
95-
*/
96-
public short abortableProcessWorkbookEvents(HSSFRequest req, POIFSFileSystem fs)
97-
throws IOException, HSSFUserException {
98-
return abortableProcessWorkbookEvents(req, fs.getRoot());
99-
}
116+
/**
117+
* Processes a file into essentially record events.
118+
*
119+
* @param req an instance of HSSFRequest which has your registered listeners
120+
* @param fs a POIFS filesystem containing your workbook
121+
* @return numeric user-specified result code.
122+
* @throws HSSFUserException if the processing should be aborted
123+
* @throws IOException if the workbook contained errors
124+
*/
125+
public short abortableProcessWorkbookEvents(HSSFRequest req, POIFSFileSystem fs)
126+
throws IOException, HSSFUserException {
127+
return abortableProcessWorkbookEvents(req, fs.getRoot());
128+
}
100129

101130
/**
102131
* Processes a file into essentially record events.
103132
*
104-
* @param req an Instance of HSSFRequest which has your registered listeners
133+
* @param req an instance of HSSFRequest which has your registered listeners
134+
* @param fs a POIFS filesystem containing your workbook
135+
* @param password in char array format (can be null)
136+
* @return numeric user-specified result code.
137+
* @throws HSSFUserException if the processing should be aborted
138+
* @throws IOException if the workbook contained errors
139+
* @since 6.0.0
140+
*/
141+
public short abortableProcessWorkbookEvents(HSSFRequest req, POIFSFileSystem fs,
142+
final char[] password)
143+
throws IOException, HSSFUserException {
144+
return abortableProcessWorkbookEvents(req, fs.getRoot(), password);
145+
}
146+
147+
/**
148+
* Processes a file into essentially record events.
149+
*
150+
* @param req an instance of HSSFRequest which has your registered listeners
105151
* @param dir a DirectoryNode containing your workbook
106152
* @return numeric user-specified result code.
107153
*
@@ -115,6 +161,26 @@ public short abortableProcessWorkbookEvents(HSSFRequest req, DirectoryNode dir)
115161
}
116162
}
117163

164+
/**
165+
* Processes a file into essentially record events.
166+
*
167+
* @param req an instance of HSSFRequest which has your registered listeners
168+
* @param dir a DirectoryNode containing your workbook
169+
* @param password in char array format (can be null)
170+
* @return numeric user-specified result code.
171+
*
172+
* @throws HSSFUserException if the processing should be aborted
173+
* @throws IOException if the workbook contained errors
174+
* @since 6.0.0
175+
*/
176+
public short abortableProcessWorkbookEvents(HSSFRequest req, DirectoryNode dir,
177+
final char[] password)
178+
throws IOException, HSSFUserException {
179+
try (InputStream in = dir.createDocumentInputStream("Workbook")) {
180+
return abortableProcessEvents(req, in, password);
181+
}
182+
}
183+
118184
/**
119185
* Processes a DocumentInputStream into essentially Record events.
120186
*
@@ -123,47 +189,85 @@ public short abortableProcessWorkbookEvents(HSSFRequest req, DirectoryNode dir)
123189
* user code or <code>HSSFUserException</code> will be passed back.
124190
*
125191
* @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String)
126-
* @param req an Instance of HSSFRequest which has your registered listeners
192+
* @param req an instance of HSSFRequest which has your registered listeners
127193
* @param in a DocumentInputStream obtained from POIFS's POIFSFileSystem object
128194
*/
129195
public void processEvents(HSSFRequest req, InputStream in) {
130196
try {
131-
genericProcessEvents(req, in);
197+
genericProcessEvents(req, in, null);
132198
} catch (HSSFUserException hue) {
133199
/*If an HSSFUserException user exception is thrown, ignore it.*/
134200
}
135201
}
136202

203+
/**
204+
* Processes a DocumentInputStream into essentially Record events.
205+
*
206+
* If an <code>AbortableHSSFListener</code> causes a halt to processing during this call
207+
* the method will return just as with <code>abortableProcessEvents</code>, but no
208+
* user code or <code>HSSFUserException</code> will be passed back.
209+
*
210+
* @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String)
211+
* @param req an instance of HSSFRequest which has your registered listeners
212+
* @param in a DocumentInputStream obtained from POIFS's POIFSFileSystem object
213+
* @param password in char array format (can be null)
214+
* @since 6.0.0
215+
*/
216+
public void processEvents(HSSFRequest req, InputStream in, final char[] password) {
217+
try {
218+
genericProcessEvents(req, in, password);
219+
} catch (HSSFUserException hue) {
220+
/*If an HSSFUserException user exception is thrown, ignore it.*/
221+
}
222+
}
137223

138224
/**
139225
* Processes a DocumentInputStream into essentially Record events.
140226
*
141227
* @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String)
142-
* @param req an Instance of HSSFRequest which has your registered listeners
228+
* @param req an instance of HSSFRequest which has your registered listeners
143229
* @param in a DocumentInputStream obtained from POIFS's POIFSFileSystem object
144230
* @return numeric user-specified result code.
145231
*
146232
* @throws HSSFUserException if the processing should be aborted
147233
*/
148234
public short abortableProcessEvents(HSSFRequest req, InputStream in)
149235
throws HSSFUserException {
150-
return genericProcessEvents(req, in);
236+
return genericProcessEvents(req, in, null);
237+
}
238+
239+
/**
240+
* Processes a DocumentInputStream into essentially Record events.
241+
*
242+
* @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String)
243+
* @param req an instance of HSSFRequest which has your registered listeners
244+
* @param in a DocumentInputStream obtained from POIFS's POIFSFileSystem object
245+
* @param password in char array format (can be null)
246+
* @return numeric user-specified result code.
247+
*
248+
* @throws HSSFUserException if the processing should be aborted
249+
* @since 6.0.0
250+
*/
251+
public short abortableProcessEvents(HSSFRequest req, InputStream in, final char[] password)
252+
throws HSSFUserException {
253+
return genericProcessEvents(req, in, password);
151254
}
152255

153256
/**
154257
* Processes a DocumentInputStream into essentially Record events.
155258
*
156259
* @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String)
157-
* @param req an Instance of HSSFRequest which has your registered listeners
260+
* @param req an instance of HSSFRequest which has your registered listeners
158261
* @param in a DocumentInputStream obtained from POIFS's POIFSFileSystem object
159262
* @return numeric user-specified result code.
160263
*/
161-
private short genericProcessEvents(HSSFRequest req, InputStream in)
264+
private short genericProcessEvents(HSSFRequest req, InputStream in, final char[] password)
162265
throws HSSFUserException {
163266
short userCode = 0;
164267

165268
// Create a new RecordStream and use that
166-
RecordFactoryInputStream recordStream = new RecordFactoryInputStream(in, false);
269+
RecordFactoryInputStream recordStream = new RecordFactoryInputStream(
270+
in, false, password);
167271

168272
// Process each record as they come in
169273
while(true) {

poi/src/test/java/org/apache/poi/hssf/eventusermodel/TestHSSFEventFactory.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private List<org.apache.poi.hssf.record.Record> openSample(
6464
try (InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleFileName);
6565
POIFSFileSystem fs = new POIFSFileSystem(is)) {
6666
HSSFEventFactory factory = new HSSFEventFactory();
67-
factory.processWorkbookEvents(req, fs);
67+
factory.processWorkbookEvents(req, fs, password.toCharArray());
6868
}
6969
return records;
7070
}
@@ -135,6 +135,45 @@ void testWithPasswordProtectedWorkbooksNoPass() {
135135

136136
@Test
137137
void testWithPasswordProtectedWorkbooks() throws Exception {
138+
final List<org.apache.poi.hssf.record.Record> records =
139+
openSample("xor-encryption-abc.xls", "abc");
140+
141+
// Check we got the sheet and the contents
142+
assertTrue(records.size() > 50);
143+
144+
// Has one sheet, with values 1,2,3 in column A rows 1-3
145+
boolean hasSheet = false, hasA1 = false, hasA2 = false, hasA3 = false;
146+
for (org.apache.poi.hssf.record.Record r : records) {
147+
if (r instanceof BoundSheetRecord) {
148+
BoundSheetRecord bsr = (BoundSheetRecord) r;
149+
assertEquals("Sheet1", bsr.getSheetname());
150+
hasSheet = true;
151+
}
152+
if (r instanceof NumberRecord) {
153+
NumberRecord nr = (NumberRecord) r;
154+
if (nr.getColumn() == 0 && nr.getRow() == 0) {
155+
assertEquals(1, (int) nr.getValue());
156+
hasA1 = true;
157+
}
158+
if (nr.getColumn() == 0 && nr.getRow() == 1) {
159+
assertEquals(2, (int) nr.getValue());
160+
hasA2 = true;
161+
}
162+
if (nr.getColumn() == 0 && nr.getRow() == 2) {
163+
assertEquals(3, (int) nr.getValue());
164+
hasA3 = true;
165+
}
166+
}
167+
}
168+
169+
assertTrue(hasSheet, "Sheet record not found");
170+
assertTrue(hasA1, "Numeric record for A1 not found");
171+
assertTrue(hasA2, "Numeric record for A2 not found");
172+
assertTrue(hasA3, "Numeric record for A3 not found");
173+
}
174+
175+
@Test
176+
void testWithPasswordProtectedWorkbooksBiff8EncryptionKey() throws Exception {
138177
// With the password, is properly processed
139178
Biff8EncryptionKey.setCurrentUserPassword("abc");
140179
try {

0 commit comments

Comments
 (0)