View | Details | Raw Unified | Return to bug 60707
Collapse All | Expand All

(-)a/src/ooxml/java/org/apache/poi/xssf/streaming/reader/StreamedCell.java (+497 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
package org.apache.poi.xssf.streaming.reader;
18
19
import java.util.Calendar;
20
import java.util.Date;
21
22
import org.apache.poi.ss.formula.FormulaParseException;
23
import org.apache.poi.ss.usermodel.Cell;
24
import org.apache.poi.ss.usermodel.CellStyle;
25
import org.apache.poi.ss.usermodel.CellType;
26
import org.apache.poi.ss.usermodel.Comment;
27
import org.apache.poi.ss.usermodel.Hyperlink;
28
import org.apache.poi.ss.usermodel.RichTextString;
29
import org.apache.poi.ss.usermodel.Row;
30
import org.apache.poi.ss.usermodel.Sheet;
31
import org.apache.poi.ss.util.CellAddress;
32
import org.apache.poi.ss.util.CellRangeAddress;
33
34
/**
35
 * Represents cell in a row
36
 * Value of cell is represented as a string.
37
 *
38
 */
39
public class StreamedCell  implements Cell{
40
	private String value;
41
	private int cellNumber;
42
	
43
	public StreamedCell(){
44
		
45
	}
46
47
	/**
48
	 * <pre>
49
	 * Return cell value
50
	 * </pre>
51
	 * Return the value of a cell in String format.
52
	 * Value will be same as how it is represented in excel.
53
	 * @return String
54
	 */
55
	public String getValue() {
56
		return value;
57
	}
58
59
	public void setValue(String value) {
60
		this.value = value;
61
	}
62
	
63
	public String toString(){
64
		return value;
65
	}
66
67
	/**
68
	 * <pre>
69
	 * Returns the cell number
70
	 * </pre>
71
	 *
72
	 * @return
73
	 */
74
    public int getCellNumber() {
75
        return cellNumber;
76
    }
77
78
    /**
79
     * <pre>
80
     * Not supported right now, as StreamedWorkbook
81
     * supports only reading.
82
     *</pre>
83
     * @param cellNumber
84
     */
85
    public void setCellNumber(int cellNumber) {
86
        this.cellNumber = cellNumber;
87
    }
88
89
    /**
90
     *  <pre>
91
     * Will be supported in future.
92
     *  </pre>
93
     */
94
    public int getColumnIndex() {
95
        // TODO Auto-generated method stub
96
        return 0;
97
    }
98
99
    /**
100
     *  <pre>
101
     * Will be supported in future.
102
     *  </pre>
103
     */
104
    public int getRowIndex() {
105
        // TODO Auto-generated method stub
106
        return 0;
107
    }
108
109
    /**
110
     *  <pre>
111
     * Will be supported in future.
112
     *  </pre>
113
     */
114
    public Sheet getSheet() {
115
        // TODO Auto-generated method stub
116
        return null;
117
    }
118
119
    /**
120
     *  <pre>
121
     * Will be supported in future.
122
     *  </pre>
123
     */
124
    public Row getRow() {
125
        // TODO Auto-generated method stub
126
        return null;
127
    }
128
129
    /**
130
     * <pre>
131
     * Not supported right now, as StreamedWorkbook
132
     * supports only reading.
133
     *</pre>
134
     * @param cellNumber
135
     */    
136
    public void setCellType(int cellType) {
137
        // TODO Auto-generated method stub
138
        
139
    }
140
141
    /**
142
     * <pre>
143
     * Not supported right now, as StreamedWorkbook
144
     * supports only reading.
145
     *</pre>
146
     * @param cellNumber
147
     */
148
    public void setCellType(CellType cellType) {
149
        // TODO Auto-generated method stub
150
        
151
    }
152
153
    /**
154
     *  <pre>
155
     * Will be supported in future.
156
     *  </pre>
157
     */
158
    public int getCellType() {
159
        // TODO Auto-generated method stub
160
        return 0;
161
    }
162
163
    /**
164
     *  <pre>
165
     * Will be supported in future.
166
     *  </pre>
167
     */
168
    public CellType getCellTypeEnum() {
169
        // TODO Auto-generated method stub
170
        return null;
171
    }
172
173
    /**
174
     *  <pre>
175
     * Will be supported in future.
176
     *  </pre>
177
     */
178
    public int getCachedFormulaResultType() {
179
        // TODO Auto-generated method stub
180
        return 0;
181
    }
182
183
    /**
184
     *  <pre>
185
     * Will be supported in future.
186
     *  </pre>
187
     */
188
    public CellType getCachedFormulaResultTypeEnum() {
189
        // TODO Auto-generated method stub
190
        return null;
191
    }
192
193
    /**
194
     * <pre>
195
     * Not supported right now, as StreamedWorkbook
196
     * supports only reading.
197
     *</pre>
198
     * @param cellNumber
199
     */
200
    public void setCellValue(double value) {
201
        // TODO Auto-generated method stub
202
        
203
    }
204
205
    /**
206
     * <pre>
207
     * Not supported right now, as StreamedWorkbook
208
     * supports only reading.
209
     *</pre>
210
     * @param cellNumber
211
     */
212
    public void setCellValue(Date value) {
213
        // TODO Auto-generated method stub
214
        
215
    }
216
217
    /**
218
     * <pre>
219
     * Not supported right now, as StreamedWorkbook
220
     * supports only reading.
221
     *</pre>
222
     * @param cellNumber
223
     */
224
    public void setCellValue(Calendar value) {
225
        // TODO Auto-generated method stub
226
        
227
    }
228
229
    /**
230
     * <pre>
231
     * Not supported right now, as StreamedWorkbook
232
     * supports only reading.
233
     *</pre>
234
     * @param cellNumber
235
     */
236
    public void setCellValue(RichTextString value) {
237
        // TODO Auto-generated method stub
238
        
239
    }
240
241
    /**
242
     * <pre>
243
     * Not supported right now, as StreamedWorkbook
244
     * supports only reading.
245
     *</pre>
246
     * @param cellNumber
247
     */
248
    public void setCellValue(String value) {
249
        // TODO Auto-generated method stub
250
        
251
    }
252
253
    /**
254
     * <pre>
255
     * Not supported right now, as StreamedWorkbook
256
     * supports only reading.
257
     *</pre>
258
     * @param cellNumber
259
     */
260
    public void setCellFormula(String formula) throws FormulaParseException {
261
        // TODO Auto-generated method stub
262
        
263
    }
264
265
    /**
266
     *  <pre>
267
     * Will be supported in future.
268
     *  </pre>
269
     */
270
    public String getCellFormula() {
271
        // TODO Auto-generated method stub
272
        return null;
273
    }
274
275
    /**
276
     *  <pre>
277
     * Will be supported in future.
278
     *  </pre>
279
     */
280
    public double getNumericCellValue() {
281
        // TODO Auto-generated method stub
282
        return 0;
283
    }
284
285
    /**
286
     *  <pre>
287
     * Will be supported in future.
288
     *  </pre>
289
     */
290
    public Date getDateCellValue() {
291
        // TODO Auto-generated method stub
292
        return null;
293
    }
294
295
    /**
296
     *  <pre>
297
     * Will be supported in future.
298
     *  </pre>
299
     */
300
    public RichTextString getRichStringCellValue() {
301
        // TODO Auto-generated method stub
302
        return null;
303
    }
304
305
    /**
306
     *  <pre>
307
     *  Returns the String value of cell content
308
     *  </pre>
309
     */
310
    public String getStringCellValue() {
311
        return value;
312
    }
313
314
    /**
315
     * <pre>
316
     * Not supported right now, as StreamedWorkbook
317
     * supports only reading.
318
     *</pre>
319
     * 
320
     */
321
    public void setCellValue(boolean value) {
322
        // TODO Auto-generated method stub
323
        
324
    }
325
326
    /**
327
     * <pre>
328
     * Not supported right now, as StreamedWorkbook
329
     * supports only reading.
330
     *</pre>
331
     * 
332
     */
333
    public void setCellErrorValue(byte value) {
334
        // TODO Auto-generated method stub
335
        
336
    }
337
338
    /**
339
     *  <pre>
340
     * Will be supported in future.
341
     *  </pre>
342
     */
343
    public boolean getBooleanCellValue() {
344
        // TODO Auto-generated method stub
345
        return false;
346
    }
347
348
    /**
349
     *  <pre>
350
     * Will be supported in future.
351
     *  </pre>
352
     */
353
    public byte getErrorCellValue() {
354
        // TODO Auto-generated method stub
355
        return 0;
356
    }
357
358
    /**
359
     * <pre>
360
     * Not supported right now, as StreamedWorkbook
361
     * supports only reading.
362
     *</pre>
363
     * 
364
     */
365
    public void setCellStyle(CellStyle style) {
366
        // TODO Auto-generated method stub
367
        
368
    }
369
370
    /**
371
     *  <pre>
372
     * Will be supported in future.
373
     *  </pre>
374
     */
375
    public CellStyle getCellStyle() {
376
        // TODO Auto-generated method stub
377
        return null;
378
    }
379
380
    /**
381
     * <pre>
382
     * Not supported right now, as StreamedWorkbook
383
     * supports only reading.
384
     *</pre>
385
     * 
386
     */
387
    public void setAsActiveCell() {
388
        // TODO Auto-generated method stub
389
        
390
    }
391
392
    /**
393
     *  <pre>
394
     * Will be supported in future.
395
     *  </pre>
396
     */
397
    public CellAddress getAddress() {
398
        // TODO Auto-generated method stub
399
        return null;
400
    }
401
    /**
402
     * <pre>
403
     * Not supported right now, as StreamedWorkbook
404
     * supports only reading.
405
     *</pre>
406
     * 
407
     */
408
    public void setCellComment(Comment comment) {
409
        // TODO Auto-generated method stub
410
        
411
    }
412
413
    /**
414
     *  <pre>
415
     * Will be supported in future.
416
     *  </pre>
417
     */
418
    public Comment getCellComment() {
419
        // TODO Auto-generated method stub
420
        return null;
421
    }
422
423
    /**
424
     * <pre>
425
     * Not supported right now, as StreamedWorkbook
426
     * supports only reading.
427
     *</pre>
428
     * 
429
     */
430
    public void removeCellComment() {
431
        // TODO Auto-generated method stub
432
        
433
    }
434
435
    /**
436
     *  <pre>
437
     * Will be supported in future.
438
     *  </pre>
439
     */
440
    public Hyperlink getHyperlink() {
441
        // TODO Auto-generated method stub
442
        return null;
443
    }
444
445
    /**
446
     * <pre>
447
     * Not supported right now, as StreamedWorkbook
448
     * supports only reading.
449
     *</pre>
450
     * 
451
     */
452
    public void setHyperlink(Hyperlink link) {
453
        // TODO Auto-generated method stub
454
        
455
    }
456
457
    /**
458
     * <pre>
459
     * Not supported right now, as StreamedWorkbook
460
     * supports only reading.
461
     *</pre>
462
     * 
463
     */
464
    public void removeHyperlink() {
465
        // TODO Auto-generated method stub
466
        
467
    }
468
469
    /**
470
     *  <pre>
471
     * Will be supported in future.
472
     *  </pre>
473
     */
474
    public CellRangeAddress getArrayFormulaRange() {
475
        // TODO Auto-generated method stub
476
        return null;
477
    }
478
479
    /**
480
     *  <pre>
481
     * Will be supported in future.
482
     *  </pre>
483
     */
484
    public boolean isPartOfArrayFormulaGroup() {
485
        // TODO Auto-generated method stub
486
        return false;
487
    }
488
489
	/*@Override
490
	protected void finalize() throws Throwable {
491
		super.finalize();
492
		value = null;
493
	}*/
494
	
495
	
496
	
497
}
(-)a/src/ooxml/java/org/apache/poi/xssf/streaming/reader/StreamedRow.java (+341 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
package org.apache.poi.xssf.streaming.reader;
18
19
import java.util.ArrayList;
20
import java.util.Iterator;
21
import java.util.List;
22
23
import org.apache.poi.ss.usermodel.Cell;
24
import org.apache.poi.ss.usermodel.CellStyle;
25
import org.apache.poi.ss.usermodel.CellType;
26
import org.apache.poi.ss.usermodel.Row;
27
import org.apache.poi.ss.usermodel.Sheet;
28
29
30
/**
31
 * Represents an excel row
32
 *
33
 */
34
public class StreamedRow implements Row{
35
	private List<StreamedCell> cells;
36
	private int rowNumber;
37
38
	public StreamedRow(int rowNumber) {
39
		this.rowNumber = rowNumber;
40
	}
41
42
	/**
43
	 * <pre>
44
	 * Used to get cells of a Row
45
	 * </pre>
46
	 * @return Iterator<Cell>
47
	 */
48
	public Iterator<StreamedCell> getCellIterator() {
49
		if (cells == null) {
50
			cells = new ArrayList<StreamedCell>();
51
		}
52
		return cells.iterator();
53
	}
54
	
55
	/**
56
	 * <pre>
57
	 * 	Returns the row number
58
	 * </pre>
59
	 * @return int
60
	 */
61
	public int getRowNum() {
62
		return rowNumber;
63
	}
64
65
	public void setRowNum(int rowNumber) {
66
		this.rowNumber = rowNumber;
67
	}
68
69
	public String toString() {
70
		StringBuffer sb = new StringBuffer(250);
71
		sb.append("Row Number:").append(rowNumber);
72
		sb.append(" --> ");
73
		if (cells != null) {
74
			for (StreamedCell cell : cells) {
75
				sb.append(cell.toString());
76
				sb.append(" | ");
77
			}
78
		}
79
80
		return sb.toString();
81
	}
82
83
/*	@Override
84
	protected void finalize() throws Throwable {
85
		super.finalize();
86
		if (cells != null) {
87
			cells.clear();
88
			cells = null;
89
		}
90
	}*/
91
92
	public List<StreamedCell> getCells() {
93
		if (cells == null) {
94
			cells = new ArrayList<StreamedCell>();
95
		}
96
		return cells;
97
	}
98
99
/**
100
 * <pre>
101
 * Will not be supported. User getCellIterator() instead
102
 * </pre>	
103
 */
104
public Iterator<Cell> iterator() {
105
    // TODO Auto-generated method stub
106
    return null;
107
}
108
109
/**
110
 * <pre>
111
 * Not supported right now, as StreamedWorkbook
112
 * supports only reading.
113
 * </pre>
114
 * 
115
 */
116
public Cell createCell(int column) {
117
    // TODO Auto-generated method stub
118
    return null;
119
}
120
121
/**
122
 * <pre>
123
 * Not supported right now, as StreamedWorkbook
124
 * supports only reading.
125
 * </pre>
126
 * 
127
 */
128
public Cell createCell(int column, int type) {
129
    // TODO Auto-generated method stub
130
    return null;
131
}
132
133
/**
134
 * <pre>
135
 * Not supported right now, as StreamedWorkbook
136
 * supports only reading.
137
 * </pre>
138
 * 
139
 */
140
public Cell createCell(int column, CellType type) {
141
    // TODO Auto-generated method stub
142
    return null;
143
}
144
145
/**
146
 * <pre>
147
 * Not supported right now, as StreamedWorkbook
148
 * supports only reading.
149
 * </pre>
150
 * 
151
 */
152
public void removeCell(Cell cell) {
153
    // TODO Auto-generated method stub
154
    
155
}
156
157
/**
158
 * <pre>
159
 *  Returns the cell on specified cell number
160
 * </pre>
161
 */
162
public Cell getCell(int cellnum) {
163
    StreamedCell cell = null;
164
    
165
    if(cells != null){
166
        cell = cells.get(cellnum);
167
    }
168
    
169
    return cell;
170
}
171
172
/**
173
 *  <pre>
174
 * Will be supported in future.
175
 *  </pre>
176
 */
177
public Cell getCell(int cellnum, MissingCellPolicy policy) {
178
    // TODO Auto-generated method stub
179
    return null;
180
}
181
182
public short getFirstCellNum() {
183
    short firstCellNumber = -1;
184
    
185
    if(cells != null && cells.size() > 0){
186
       firstCellNumber = (short)cells.get(0).getCellNumber();
187
    }
188
    
189
    return firstCellNumber;
190
}
191
192
public short getLastCellNum() {
193
    short lastCellNumber = -1;
194
    
195
    if(cells != null && cells.size() > 0){
196
        lastCellNumber = (short)cells.get((cells.size()-1)).getCellNumber();
197
    }
198
    
199
    return lastCellNumber;
200
}
201
202
/**
203
 *  <pre>
204
 * Will be supported in future.
205
 *  </pre>
206
 */
207
public int getPhysicalNumberOfCells() {
208
    // TODO Auto-generated method stub
209
    return 0;
210
}
211
212
/**
213
 * <pre>
214
 * Not supported right now, as StreamedWorkbook
215
 * supports only reading.
216
 * </pre>
217
 * 
218
 */
219
public void setHeight(short height) {
220
    // TODO Auto-generated method stub
221
    
222
}
223
224
/**
225
 * <pre>
226
 * Not supported right now, as StreamedWorkbook
227
 * supports only reading.
228
 * </pre>
229
 * 
230
 */
231
public void setZeroHeight(boolean zHeight) {
232
    // TODO Auto-generated method stub
233
    
234
}
235
236
/**
237
 *  <pre>
238
 * Will be supported in future.
239
 *  </pre>
240
 */
241
public boolean getZeroHeight() {
242
    // TODO Auto-generated method stub
243
    return false;
244
}
245
246
/**
247
 * <pre>
248
 * Not supported right now, as StreamedWorkbook
249
 * supports only reading.
250
 * </pre>
251
 * 
252
 */
253
public void setHeightInPoints(float height) {
254
    // TODO Auto-generated method stub
255
    
256
}
257
258
/**
259
 *  <pre>
260
 * Will be supported in future.
261
 *  </pre>
262
 */
263
public short getHeight() {
264
    // TODO Auto-generated method stub
265
    return 0;
266
}
267
268
/**
269
 *  <pre>
270
 * Will be supported in future.
271
 *  </pre>
272
 */
273
public float getHeightInPoints() {
274
    // TODO Auto-generated method stub
275
    return 0;
276
}
277
278
/**
279
 *  <pre>
280
 * Will be supported in future.
281
 *  </pre>
282
 */
283
public boolean isFormatted() {
284
    // TODO Auto-generated method stub
285
    return false;
286
}
287
288
/**
289
 *  <pre>
290
 * Will be supported in future.
291
 *  </pre>
292
 */
293
public CellStyle getRowStyle() {
294
    // TODO Auto-generated method stub
295
    return null;
296
}
297
298
/**
299
 * <pre>
300
 * Not supported right now, as StreamedWorkbook
301
 * supports only reading.
302
 * </pre>
303
 * 
304
 */
305
public void setRowStyle(CellStyle style) {
306
    // TODO Auto-generated method stub
307
    
308
}
309
310
/**
311
 * <pre>
312
 * Not supported right now. Use getCellIterator instead
313
 * </pre>
314
 */
315
public Iterator<Cell> cellIterator() {
316
    // TODO Auto-generated method stub
317
    return null;
318
}
319
320
/**
321
 *  <pre>
322
 * Will be supported in future.
323
 *  </pre>
324
 */
325
public Sheet getSheet() {
326
    // TODO Auto-generated method stub
327
    return null;
328
}
329
330
/**
331
 *  <pre>
332
 * Will be supported in future.
333
 *  </pre>
334
 */
335
public int getOutlineLevel() {
336
    // TODO Auto-generated method stub
337
    return 0;
338
}
339
340
341
}
(-)a/src/ooxml/java/org/apache/poi/xssf/streaming/reader/StreamedSheet.java (+1542 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
package org.apache.poi.xssf.streaming.reader;
18
19
import java.util.ArrayList;
20
import java.util.Collection;
21
import java.util.Iterator;
22
import java.util.List;
23
import java.util.Map;
24
25
import javax.xml.stream.XMLEventReader;
26
import javax.xml.stream.XMLStreamException;
27
import javax.xml.stream.events.XMLEvent;
28
29
import org.apache.poi.ss.usermodel.AutoFilter;
30
import org.apache.poi.ss.usermodel.Cell;
31
import org.apache.poi.ss.usermodel.CellRange;
32
import org.apache.poi.ss.usermodel.CellStyle;
33
import org.apache.poi.ss.usermodel.Comment;
34
import org.apache.poi.ss.usermodel.DataValidation;
35
import org.apache.poi.ss.usermodel.DataValidationHelper;
36
import org.apache.poi.ss.usermodel.Drawing;
37
import org.apache.poi.ss.usermodel.Footer;
38
import org.apache.poi.ss.usermodel.Header;
39
import org.apache.poi.ss.usermodel.Hyperlink;
40
import org.apache.poi.ss.usermodel.PrintSetup;
41
import org.apache.poi.ss.usermodel.Row;
42
import org.apache.poi.ss.usermodel.Sheet;
43
import org.apache.poi.ss.usermodel.SheetConditionalFormatting;
44
import org.apache.poi.ss.usermodel.Workbook;
45
import org.apache.poi.ss.util.CellAddress;
46
import org.apache.poi.ss.util.CellRangeAddress;
47
import org.apache.poi.ss.util.PaneInformation;
48
import org.apache.poi.xssf.model.SharedStringsTable;
49
import org.apache.poi.xssf.model.StylesTable;
50
51
/**
52
 * Represents an excel sheet.
53
 *
54
 */
55
public class StreamedSheet implements Sheet{
56
	private XMLEventReader xmlParser;
57
	private SharedStringsTable sharedStringsTable;
58
	private StylesTable stylesTable;
59
	private int numberOfColumns;
60
	private int sheetNumber;
61
	private StreamedSheetEventHandler eventHandler = null;
62
	
63
	/**
64
	 * <pre>
65
	 * Fetch all rows from the excel.
66
	 * </pre>
67
	 * 
68
	 * This method consumes only less memory, but it is 
69
	 * advisable to use it only for small excel files, since 
70
	 * it will fetch all rows in a single call.
71
	 * 
72
	 * @return Iterator<Row>
73
	 * @throws XMLStreamException
74
	 */
75
	public Iterator<StreamedRow> getAllRows() throws XMLStreamException{
76
		return getAllRows(this, sharedStringsTable, stylesTable);
77
	}
78
	
79
	/**
80
	 * <pre>
81
	 * Used to fetch N number of rows.
82
	 * </pre>
83
	 * 
84
	 * Recommended method to reduce memory utilization.
85
	 * It allows to read big excel files in batch.
86
	 * This gives control to the user to process the records already fetched, 
87
	 * before fetching next set of records.
88
	 * After reading N records, invoke the same method with number of rows to be
89
	 * fetched to get the next set of rows.
90
	 * <br>
91
	 * 
92
	 * *********************Usage****************************
93
	 * Iterator<StreamedRow> rows = sheet.getNRows(1);
94
	 * <br>
95
	 * while(rows.hasNext()){ //read the first 1 row
96
	 * <br>
97
	 *     StreamedRow row = rows.next();
98
	 * <br>
99
	 * }
100
	 *  <br>
101
	 * rows = sheet.getNRows(10);
102
	 *  <br>
103
     * while(rows.hasNext()){ //read the next 10 rows
104
     *  <br>
105
     *     StreamedRow row = rows.next();
106
     *  <br>
107
     * }
108
	 * 
109
	 * @param numberOfRows
110
	 * @return Iterator<Row>
111
	 * @throws XMLStreamException
112
	 */
113
	public Iterator<StreamedRow> getNRows(int numberOfRows) throws XMLStreamException{
114
		return getNRows(this, eventHandler, numberOfRows);
115
	}
116
117
	public boolean hasMoreRows(){
118
		return xmlParser.hasNext();
119
	}
120
	
121
	
122
	public XMLEventReader getXmlParser() {
123
		return xmlParser;
124
	}
125
126
	public void setXmlParser(XMLEventReader xmlParser) {
127
		this.xmlParser = xmlParser;
128
	}
129
130
	public void setSharedStringsTable(SharedStringsTable sharedStringsTable) {
131
		this.sharedStringsTable = sharedStringsTable;
132
	}
133
134
	public void setStylesTable(StylesTable stylesTable) {
135
		this.stylesTable = stylesTable;
136
	}
137
	
138
	public void createEventHandler(){
139
		eventHandler = new StreamedSheetEventHandler(sharedStringsTable, stylesTable);
140
	}
141
142
143
	public int getNumberOfColumns() {
144
		return numberOfColumns;
145
	}
146
147
148
	public void setNumberOfColumns(int numberOfColumns) {
149
		this.numberOfColumns = numberOfColumns;
150
	}
151
152
153
	public int getSheetNumber() {
154
		return sheetNumber;
155
	}
156
157
158
	public void setSheetNumber(int sheetNumber) {
159
		this.sheetNumber = sheetNumber;
160
	}
161
162
/*	@Override
163
	protected void finalize() throws Throwable {
164
		super.finalize();
165
		
166
		xmlParser = null;
167
		sharedStringsTable = null;
168
		stylesTable = null;
169
		eventHandler = null;
170
		
171
	}*/
172
	
173
	 /**
174
     * reads all data from sheet
175
     * @param xmlParser
176
     * @param sharedStringsTable
177
     * @param stylesTable
178
     * @return
179
     * @throws XMLStreamException 
180
     */
181
    private Iterator<StreamedRow> getAllRows(StreamedSheet sheet,
182
            SharedStringsTable sharedStringsTable, StylesTable stylesTable) throws XMLStreamException {
183
        List<StreamedRow> dataList = new ArrayList<StreamedRow>();
184
        StreamedSheetEventHandler eventHandler = new StreamedSheetEventHandler(sharedStringsTable, stylesTable);
185
        while(sheet.getXmlParser().hasNext()){
186
            XMLEvent event = sheet.getXmlParser().nextEvent();  
187
            eventHandler.handleEvent(event);
188
            if(eventHandler.isEndOfRow()){
189
                dataList.add(eventHandler.getRow());
190
                eventHandler.setEndOfRow(false);
191
            }
192
            sheet.setNumberOfColumns(eventHandler.getNumberOfColumns());
193
        }
194
        
195
        return dataList.iterator();
196
    }
197
    
198
    /**
199
     * Reads N Rows from excel
200
     * @param sheet
201
     * @param sharedStringsTable
202
     * @param stylesTable
203
     * @param numberOFRows
204
     * @return
205
     * @throws XMLStreamException
206
     */
207
    private Iterator<StreamedRow> getNRows(StreamedSheet sheet, StreamedSheetEventHandler eventHandler, int numberOfRows) throws XMLStreamException {
208
        List<StreamedRow> dataList = new ArrayList<StreamedRow>();      
209
        while(sheet.getXmlParser().hasNext()){
210
            XMLEvent event = sheet.getXmlParser().nextEvent();  
211
            eventHandler.handleEvent(event);
212
            if(eventHandler.isEndOfRow()){
213
                dataList.add(eventHandler.getRow());
214
                eventHandler.setEndOfRow(false);
215
            }
216
            sheet.setNumberOfColumns(eventHandler.getNumberOfColumns());
217
            if(dataList.size() == numberOfRows){
218
                break;
219
            }
220
        }
221
        
222
        return dataList.iterator();
223
    }
224
225
    /**
226
     * Not supported. Refer getAllRows or getNRows
227
     */
228
    public Iterator<Row> iterator() {
229
        // TODO Auto-generated method stub
230
        return null;
231
    }
232
233
    /**
234
     * <pre>
235
     * Not supported right now, as StreamedWorkbook
236
     * supports only reading.
237
     * </pre>
238
     * 
239
     */
240
    public Row createRow(int rownum) {
241
        // TODO Auto-generated method stub
242
        return null;
243
    }
244
245
    /**
246
     * <pre>
247
     * Not supported right now, as StreamedWorkbook
248
     * supports only reading.
249
     * </pre>
250
     * 
251
     */
252
    public void removeRow(Row row) {
253
        // TODO Auto-generated method stub
254
        
255
    }
256
257
    /**
258
     * <pre>
259
     *  Not supported due to memory footprint.
260
     * </pre>
261
     */
262
    public Row getRow(int rownum) {
263
        // TODO Auto-generated method stub
264
        return null;
265
    }
266
267
    /**
268
     * <pre>
269
     *  Not supported due to memory footprint.
270
     * </pre>
271
     */
272
    public int getPhysicalNumberOfRows() {
273
        // TODO Auto-generated method stub
274
        return 0;
275
    }
276
277
    /**
278
     * <pre>
279
     * Will be supported in the future.
280
     * </pre>
281
     * 
282
     */
283
    public int getFirstRowNum() {
284
        // TODO Auto-generated method stub
285
        return 0;
286
    }
287
288
    /**
289
     * <pre>
290
     *  Not supported due to memory footprint.
291
     * </pre>
292
     */
293
    public int getLastRowNum() {
294
        // TODO Auto-generated method stub
295
        return 0;
296
    }
297
298
    /**
299
     * <pre>
300
     * Not supported right now, as StreamedWorkbook
301
     * supports only reading.
302
     * </pre>
303
     * 
304
     */
305
    public void setColumnHidden(int columnIndex, boolean hidden) {
306
        // TODO Auto-generated method stub
307
        
308
    }
309
310
    /**
311
     * <pre>
312
     *  Not supported due to memory footprint.
313
     * </pre>
314
     */
315
    public boolean isColumnHidden(int columnIndex) {
316
        // TODO Auto-generated method stub
317
        return false;
318
    }
319
320
    /**
321
     * <pre>
322
     * Not supported right now, as StreamedWorkbook
323
     * supports only reading.
324
     * </pre>
325
     * 
326
     */
327
    public void setRightToLeft(boolean value) {
328
        // TODO Auto-generated method stub
329
        
330
    }
331
332
    /**
333
     * <pre>
334
     * Not supported right now, as StreamedWorkbook
335
     * supports only reading.
336
     * </pre>
337
     * 
338
     */
339
    public boolean isRightToLeft() {
340
        // TODO Auto-generated method stub
341
        return false;
342
    }
343
344
    /**
345
     * <pre>
346
     * Not supported right now, as StreamedWorkbook
347
     * supports only reading.
348
     * </pre>
349
     * 
350
     */
351
    public void setColumnWidth(int columnIndex, int width) {
352
        // TODO Auto-generated method stub
353
        
354
    }
355
356
    /**
357
     *  <pre>
358
     * Will be supported in future.
359
     *  </pre>
360
     */
361
    public int getColumnWidth(int columnIndex) {
362
        // TODO Auto-generated method stub
363
        return 0;
364
    }
365
366
367
    /**
368
     *  <pre>
369
     * Will be supported in future.
370
     *  </pre>
371
     */
372
    public float getColumnWidthInPixels(int columnIndex) {
373
        // TODO Auto-generated method stub
374
        return 0;
375
    }
376
377
    /**
378
     * <pre>
379
     * Not supported right now, as StreamedWorkbook
380
     * supports only reading.
381
     * </pre>
382
     * 
383
     */    
384
    public void setDefaultColumnWidth(int width) {
385
        // TODO Auto-generated method stub
386
        
387
    }
388
389
    /**
390
     *  <pre>
391
     * Will be supported in future.
392
     *  </pre>
393
     */
394
    public int getDefaultColumnWidth() {
395
        // TODO Auto-generated method stub
396
        return 0;
397
    }
398
399
    /**
400
     *  <pre>
401
     * Will be supported in future.
402
     *  </pre>
403
     */
404
    public short getDefaultRowHeight() {
405
        // TODO Auto-generated method stub
406
        return 0;
407
    }
408
409
    /**
410
     *  <pre>
411
     * Will be supported in future.
412
     *  </pre>
413
     */
414
    public float getDefaultRowHeightInPoints() {
415
        // TODO Auto-generated method stub
416
        return 0;
417
    }
418
419
    /**
420
     * <pre>
421
     * Not supported right now, as StreamedWorkbook
422
     * supports only reading.
423
     * </pre>
424
     * 
425
     */   
426
    public void setDefaultRowHeight(short height) {
427
        // TODO Auto-generated method stub
428
        
429
    }
430
431
    /**
432
     * <pre>
433
     * Not supported right now, as StreamedWorkbook
434
     * supports only reading.
435
     * </pre>
436
     * 
437
     */   
438
    public void setDefaultRowHeightInPoints(float height) {
439
        // TODO Auto-generated method stub
440
        
441
    }
442
443
    /**
444
     *  <pre>
445
     * Will be supported in future.
446
     *  </pre>
447
     */
448
    public CellStyle getColumnStyle(int column) {
449
        // TODO Auto-generated method stub
450
        return null;
451
    }
452
453
    /**
454
     * <pre>
455
     * Not supported right now, as StreamedWorkbook
456
     * supports only reading.
457
     * </pre>
458
     * 
459
     */   
460
    public int addMergedRegion(CellRangeAddress region) {
461
        // TODO Auto-generated method stub
462
        return 0;
463
    }
464
465
    /**
466
     * <pre>
467
     * Not supported right now, as StreamedWorkbook
468
     * supports only reading.
469
     * </pre>
470
     * 
471
     */   
472
    public int addMergedRegionUnsafe(CellRangeAddress region) {
473
        // TODO Auto-generated method stub
474
        return 0;
475
    }
476
477
    /**
478
     *  <pre>
479
     * Will be supported in future.
480
     *  </pre>
481
     */
482
    public void validateMergedRegions() {
483
        // TODO Auto-generated method stub
484
        
485
    }
486
487
    /**
488
     * <pre>
489
     * Not supported right now, as StreamedWorkbook
490
     * supports only reading.
491
     * </pre>
492
     * 
493
     */ 
494
    public void setVerticallyCenter(boolean value) {
495
        // TODO Auto-generated method stub
496
        
497
    }
498
499
    /**
500
     * <pre>
501
     * Not supported right now, as StreamedWorkbook
502
     * supports only reading.
503
     * </pre>
504
     * 
505
     */ 
506
    public void setHorizontallyCenter(boolean value) {
507
        // TODO Auto-generated method stub
508
        
509
    }
510
511
    /**
512
     *  <pre>
513
     * Will be supported in future.
514
     *  </pre>
515
     */
516
    public boolean getHorizontallyCenter() {
517
        // TODO Auto-generated method stub
518
        return false;
519
    }
520
521
    /**
522
     *  <pre>
523
     * Will be supported in future.
524
     *  </pre>
525
     */
526
    public boolean getVerticallyCenter() {
527
        // TODO Auto-generated method stub
528
        return false;
529
    }
530
531
    /**
532
     * <pre>
533
     * Not supported right now, as StreamedWorkbook
534
     * supports only reading.
535
     * </pre>
536
     * 
537
     */ 
538
    public void removeMergedRegion(int index) {
539
        // TODO Auto-generated method stub
540
        
541
    }
542
543
    /**
544
     * <pre>
545
     * Not supported right now, as StreamedWorkbook
546
     * supports only reading.
547
     * </pre>
548
     * 
549
     */ 
550
    public void removeMergedRegions(Collection<Integer> indices) {
551
        // TODO Auto-generated method stub
552
        
553
    }
554
555
    /**
556
     *  <pre>
557
     * Not supported due to memory footprint.
558
     *  </pre>
559
     */
560
    public int getNumMergedRegions() {
561
        // TODO Auto-generated method stub
562
        return 0;
563
    }
564
565
    /**
566
     *  <pre>
567
     * Not supported due to memory footprint.
568
     *  </pre>
569
     */
570
    public CellRangeAddress getMergedRegion(int index) {
571
        // TODO Auto-generated method stub
572
        return null;
573
    }
574
575
    /**
576
     *  <pre>
577
     * Not supported due to memory footprint.
578
     *  </pre>
579
     */
580
    public List<CellRangeAddress> getMergedRegions() {
581
        // TODO Auto-generated method stub
582
        return null;
583
    }
584
585
    /**
586
     * <pre>
587
     * Not supported. Use getAllRows or getNRows
588
     * </pre>
589
     */
590
    public Iterator<Row> rowIterator() {
591
        // TODO Auto-generated method stub
592
        return null;
593
    }
594
595
    /**
596
     * <pre>
597
     * Not supported right now, as StreamedWorkbook
598
     * supports only reading.
599
     * </pre>
600
     * 
601
     */ 
602
    public void setForceFormulaRecalculation(boolean value) {
603
        // TODO Auto-generated method stub
604
        
605
    }
606
607
    /**
608
     *  <pre>
609
     * Will be supported in future.
610
     *  </pre>
611
     */
612
    public boolean getForceFormulaRecalculation() {
613
        // TODO Auto-generated method stub
614
        return false;
615
    }
616
617
    /**
618
     * <pre>
619
     * Not supported right now, as StreamedWorkbook
620
     * supports only reading.
621
     * </pre>
622
     * 
623
     */ 
624
    public void setAutobreaks(boolean value) {
625
        // TODO Auto-generated method stub
626
        
627
    }
628
629
    /**
630
     * <pre>
631
     * Not supported right now, as StreamedWorkbook
632
     * supports only reading.
633
     * </pre>
634
     * 
635
     */ 
636
    public void setDisplayGuts(boolean value) {
637
        // TODO Auto-generated method stub
638
        
639
    }
640
641
    /**
642
     *  <pre>
643
     * Will be supported in future.
644
     *  </pre>
645
     */
646
    public void setDisplayZeros(boolean value) {
647
        // TODO Auto-generated method stub
648
        
649
    }
650
651
    /**
652
     *  <pre>
653
     * Will be supported in future.
654
     *  </pre>
655
     */
656
    public boolean isDisplayZeros() {
657
        // TODO Auto-generated method stub
658
        return false;
659
    }
660
661
    /**
662
     * <pre>
663
     * Not supported right now, as StreamedWorkbook
664
     * supports only reading.
665
     * </pre>
666
     * 
667
     */ 
668
    public void setFitToPage(boolean value) {
669
        // TODO Auto-generated method stub
670
        
671
    }
672
673
    /**
674
     * <pre>
675
     * Not supported right now, as StreamedWorkbook
676
     * supports only reading.
677
     * </pre>
678
     * 
679
     */ 
680
    public void setRowSumsBelow(boolean value) {
681
        // TODO Auto-generated method stub
682
        
683
    }
684
685
    /**
686
     * <pre>
687
     * Not supported right now, as StreamedWorkbook
688
     * supports only reading.
689
     * </pre>
690
     * 
691
     */ 
692
    public void setRowSumsRight(boolean value) {
693
        // TODO Auto-generated method stub
694
        
695
    }
696
697
    /**
698
     *  <pre>
699
     * Will be supported in future.
700
     *  </pre>
701
     */
702
    public boolean getAutobreaks() {
703
        // TODO Auto-generated method stub
704
        return false;
705
    }
706
707
    /**
708
     *  <pre>
709
     * Will be supported in future.
710
     *  </pre>
711
     */
712
    public boolean getDisplayGuts() {
713
        // TODO Auto-generated method stub
714
        return false;
715
    }
716
717
    /**
718
     *  <pre>
719
     * Will be supported in future.
720
     *  </pre>
721
     */
722
    public boolean getFitToPage() {
723
        // TODO Auto-generated method stub
724
        return false;
725
    }
726
727
    /**
728
     *  <pre>
729
     * Will be supported in future.
730
     *  </pre>
731
     */
732
    public boolean getRowSumsBelow() {
733
        // TODO Auto-generated method stub
734
        return false;
735
    }
736
737
    /**
738
     *  <pre>
739
     * Will be supported in future.
740
     *  </pre>
741
     */
742
    public boolean getRowSumsRight() {
743
        // TODO Auto-generated method stub
744
        return false;
745
    }
746
747
    /**
748
     *  <pre>
749
     * Will be supported in future.
750
     *  </pre>
751
     */
752
    public boolean isPrintGridlines() {
753
        // TODO Auto-generated method stub
754
        return false;
755
    }
756
757
    /**
758
     * <pre>
759
     * Not supported right now, as StreamedWorkbook
760
     * supports only reading.
761
     * </pre>
762
     * 
763
     */ 
764
    public void setPrintGridlines(boolean show) {
765
        // TODO Auto-generated method stub
766
        
767
    }
768
769
    /**
770
     *  <pre>
771
     * Will be supported in future.
772
     *  </pre>
773
     */
774
    public boolean isPrintRowAndColumnHeadings() {
775
        // TODO Auto-generated method stub
776
        return false;
777
    }
778
779
    /**
780
     * <pre>
781
     * Not supported right now, as StreamedWorkbook
782
     * supports only reading.
783
     * </pre>
784
     * 
785
     */ 
786
    public void setPrintRowAndColumnHeadings(boolean show) {
787
        // TODO Auto-generated method stub
788
        
789
    }
790
791
    /**
792
     *  <pre>
793
     * Will be supported in future.
794
     *  </pre>
795
     */
796
    public PrintSetup getPrintSetup() {
797
        // TODO Auto-generated method stub
798
        return null;
799
    }
800
801
    /**
802
     *  <pre>
803
     * Will be supported in future.
804
     *  </pre>
805
     */
806
    public Header getHeader() {
807
        // TODO Auto-generated method stub
808
        return null;
809
    }
810
811
    /**
812
     *  <pre>
813
     * Will be supported in future.
814
     *  </pre>
815
     */
816
    public Footer getFooter() {
817
        // TODO Auto-generated method stub
818
        return null;
819
    }
820
821
    /**
822
     * <pre>
823
     * Not supported right now, as StreamedWorkbook
824
     * supports only reading.
825
     * </pre>
826
     * 
827
     */ 
828
    public void setSelected(boolean value) {
829
        // TODO Auto-generated method stub
830
        
831
    }
832
833
    /**
834
     *  <pre>
835
     * Will be supported in future.
836
     *  </pre>
837
     */
838
    public double getMargin(short margin) {
839
        // TODO Auto-generated method stub
840
        return 0;
841
    }
842
843
    /**
844
     * <pre>
845
     * Not supported right now, as StreamedWorkbook
846
     * supports only reading.
847
     * </pre>
848
     * 
849
     */ 
850
    public void setMargin(short margin, double size) {
851
        // TODO Auto-generated method stub
852
        
853
    }
854
    
855
    /**
856
     *  <pre>
857
     * Will be supported in future.
858
     *  </pre>
859
     */
860
    public boolean getProtect() {
861
        // TODO Auto-generated method stub
862
        return false;
863
    }
864
865
    /**
866
     * <pre>
867
     * Not supported right now, as StreamedWorkbook
868
     * supports only reading.
869
     * </pre>
870
     * 
871
     */ 
872
    public void protectSheet(String password) {
873
        // TODO Auto-generated method stub
874
        
875
    }
876
877
    /**
878
     *  <pre>
879
     * Will be supported in future.
880
     *  </pre>
881
     */
882
    public boolean getScenarioProtect() {
883
        // TODO Auto-generated method stub
884
        return false;
885
    }
886
887
    /**
888
     * <pre>
889
     * Not supported right now, as StreamedWorkbook
890
     * supports only reading.
891
     * </pre>
892
     * 
893
     */ 
894
    public void setZoom(int numerator, int denominator) {
895
        // TODO Auto-generated method stub
896
        
897
    }
898
899
    /**
900
     * <pre>
901
     * Not supported right now, as StreamedWorkbook
902
     * supports only reading.
903
     * </pre>
904
     * 
905
     */ 
906
    public void setZoom(int scale) {
907
        // TODO Auto-generated method stub
908
        
909
    }
910
911
    /**
912
     *  <pre>
913
     * Will be supported in future.
914
     *  </pre>
915
     */
916
    public short getTopRow() {
917
        // TODO Auto-generated method stub
918
        return 0;
919
    }
920
921
    /**
922
     *  <pre>
923
     * Will be supported in future.
924
     *  </pre>
925
     */
926
    public short getLeftCol() {
927
        // TODO Auto-generated method stub
928
        return 0;
929
    }
930
931
    /**
932
     *  <pre>
933
     * Will be supported in future.
934
     *  </pre>
935
     */
936
    public void showInPane(int toprow, int leftcol) {
937
        // TODO Auto-generated method stub
938
        
939
    }
940
941
    /**
942
     * <pre>
943
     * Not supported right now, as StreamedWorkbook
944
     * supports only reading.
945
     * </pre>
946
     * 
947
     */ 
948
    public void shiftRows(int startRow, int endRow, int n) {
949
        // TODO Auto-generated method stub
950
        
951
    }
952
953
    /**
954
     * <pre>
955
     * Not supported right now, as StreamedWorkbook
956
     * supports only reading.
957
     * </pre>
958
     * 
959
     */ 
960
    public void shiftRows(int startRow, int endRow, int n,
961
            boolean copyRowHeight, boolean resetOriginalRowHeight) {
962
        // TODO Auto-generated method stub
963
        
964
    }
965
966
    /**
967
     * <pre>
968
     * Not supported right now, as StreamedWorkbook
969
     * supports only reading.
970
     * </pre>
971
     * 
972
     */ 
973
    public void createFreezePane(int colSplit, int rowSplit,
974
            int leftmostColumn, int topRow) {
975
        // TODO Auto-generated method stub
976
        
977
    }
978
979
    /**
980
     * <pre>
981
     * Not supported right now, as StreamedWorkbook
982
     * supports only reading.
983
     * </pre>
984
     * 
985
     */ 
986
    public void createFreezePane(int colSplit, int rowSplit) {
987
        // TODO Auto-generated method stub
988
        
989
    }
990
991
    /**
992
     * <pre>
993
     * Not supported right now, as StreamedWorkbook
994
     * supports only reading.
995
     * </pre>
996
     * 
997
     */ 
998
    public void createSplitPane(int xSplitPos, int ySplitPos,
999
            int leftmostColumn, int topRow, int activePane) {
1000
        // TODO Auto-generated method stub
1001
        
1002
    }
1003
1004
    /**
1005
     *  <pre>
1006
     * Will be supported in future.
1007
     *  </pre>
1008
     */
1009
    public PaneInformation getPaneInformation() {
1010
        // TODO Auto-generated method stub
1011
        return null;
1012
    }
1013
1014
    /**
1015
     * <pre>
1016
     * Not supported right now, as StreamedWorkbook
1017
     * supports only reading.
1018
     * </pre>
1019
     * 
1020
     */
1021
    public void setDisplayGridlines(boolean show) {
1022
        // TODO Auto-generated method stub
1023
        
1024
    }
1025
1026
    /**
1027
     *  <pre>
1028
     * Will be supported in future.
1029
     *  </pre>
1030
     */
1031
    public boolean isDisplayGridlines() {
1032
        // TODO Auto-generated method stub
1033
        return false;
1034
    }
1035
1036
    /**
1037
     * <pre>
1038
     * Not supported right now, as StreamedWorkbook
1039
     * supports only reading.
1040
     * </pre>
1041
     * 
1042
     */
1043
    public void setDisplayFormulas(boolean show) {
1044
        // TODO Auto-generated method stub
1045
        
1046
    }
1047
1048
    /**
1049
     *  <pre>
1050
     * Will be supported in future.
1051
     *  </pre>
1052
     */
1053
    public boolean isDisplayFormulas() {
1054
        // TODO Auto-generated method stub
1055
        return false;
1056
    }
1057
1058
    /**
1059
     * <pre>
1060
     * Not supported right now, as StreamedWorkbook
1061
     * supports only reading.
1062
     * </pre>
1063
     * 
1064
     */
1065
    public void setDisplayRowColHeadings(boolean show) {
1066
        // TODO Auto-generated method stub
1067
        
1068
    }
1069
1070
    /**
1071
     *  <pre>
1072
     * Will be supported in future.
1073
     *  </pre>
1074
     */
1075
    public boolean isDisplayRowColHeadings() {
1076
        // TODO Auto-generated method stub
1077
        return false;
1078
    }
1079
1080
    /**
1081
     * <pre>
1082
     * Not supported right now, as StreamedWorkbook
1083
     * supports only reading.
1084
     * </pre>
1085
     * 
1086
     */
1087
    public void setRowBreak(int row) {
1088
        // TODO Auto-generated method stub
1089
        
1090
    }
1091
1092
    /**
1093
     *  <pre>
1094
     * Will be supported in future.
1095
     *  </pre>
1096
     */
1097
    public boolean isRowBroken(int row) {
1098
        // TODO Auto-generated method stub
1099
        return false;
1100
    }
1101
1102
    /**
1103
     * <pre>
1104
     * Not supported right now, as StreamedWorkbook
1105
     * supports only reading.
1106
     * </pre>
1107
     * 
1108
     */
1109
    public void removeRowBreak(int row) {
1110
        // TODO Auto-generated method stub
1111
        
1112
    }
1113
1114
    /**
1115
     *  <pre>
1116
     * Will be supported in future.
1117
     *  </pre>
1118
     */
1119
    public int[] getRowBreaks() {
1120
        // TODO Auto-generated method stub
1121
        return null;
1122
    }
1123
1124
    /**
1125
     *  <pre>
1126
     * Will be supported in future.
1127
     *  </pre>
1128
     */
1129
    public int[] getColumnBreaks() {
1130
        // TODO Auto-generated method stub
1131
        return null;
1132
    }
1133
1134
    public void setColumnBreak(int column) {
1135
        // TODO Auto-generated method stub
1136
        
1137
    }
1138
1139
    /**
1140
     *  <pre>
1141
     * Will be supported in future.
1142
     *  </pre>
1143
     */
1144
    public boolean isColumnBroken(int column) {
1145
        // TODO Auto-generated method stub
1146
        return false;
1147
    }
1148
1149
    /**
1150
     * <pre>
1151
     * Not supported right now, as StreamedWorkbook
1152
     * supports only reading.
1153
     * </pre>
1154
     * 
1155
     */
1156
    public void removeColumnBreak(int column) {
1157
        // TODO Auto-generated method stub
1158
        
1159
    }
1160
1161
    /**
1162
     * <pre>
1163
     * Not supported right now, as StreamedWorkbook
1164
     * supports only reading.
1165
     * </pre>
1166
     * 
1167
     */
1168
    public void setColumnGroupCollapsed(int columnNumber, boolean collapsed) {
1169
        // TODO Auto-generated method stub
1170
        
1171
    }
1172
1173
    /**
1174
     * <pre>
1175
     * Not supported right now, as StreamedWorkbook
1176
     * supports only reading.
1177
     * </pre>
1178
     * 
1179
     */
1180
    public void groupColumn(int fromColumn, int toColumn) {
1181
        // TODO Auto-generated method stub
1182
        
1183
    }
1184
1185
    /**
1186
     * <pre>
1187
     * Not supported right now, as StreamedWorkbook
1188
     * supports only reading.
1189
     * </pre>
1190
     * 
1191
     */
1192
    public void ungroupColumn(int fromColumn, int toColumn) {
1193
        // TODO Auto-generated method stub
1194
        
1195
    }
1196
1197
    /**
1198
     * <pre>
1199
     * Not supported right now, as StreamedWorkbook
1200
     * supports only reading.
1201
     * </pre>
1202
     * 
1203
     */
1204
    public void groupRow(int fromRow, int toRow) {
1205
        // TODO Auto-generated method stub
1206
        
1207
    }
1208
1209
    /**
1210
     * <pre>
1211
     * Not supported right now, as StreamedWorkbook
1212
     * supports only reading.
1213
     * </pre>
1214
     * 
1215
     */
1216
    public void ungroupRow(int fromRow, int toRow) {
1217
        // TODO Auto-generated method stub
1218
        
1219
    }
1220
1221
    /**
1222
     * <pre>
1223
     * Not supported right now, as StreamedWorkbook
1224
     * supports only reading.
1225
     * </pre>
1226
     * 
1227
     */
1228
    public void setRowGroupCollapsed(int row, boolean collapse) {
1229
        // TODO Auto-generated method stub
1230
        
1231
    }
1232
1233
    /**
1234
     * <pre>
1235
     * Not supported right now, as StreamedWorkbook
1236
     * supports only reading.
1237
     * </pre>
1238
     * 
1239
     */
1240
    public void setDefaultColumnStyle(int column, CellStyle style) {
1241
        // TODO Auto-generated method stub
1242
        
1243
    }
1244
1245
    /**
1246
     * <pre>
1247
     * Not supported right now, as StreamedWorkbook
1248
     * supports only reading.
1249
     * </pre>
1250
     * 
1251
     */
1252
    public void autoSizeColumn(int column) {
1253
        // TODO Auto-generated method stub
1254
        
1255
    }
1256
1257
    /**
1258
     * <pre>
1259
     * Not supported right now, as StreamedWorkbook
1260
     * supports only reading.
1261
     * </pre>
1262
     * 
1263
     */
1264
    public void autoSizeColumn(int column, boolean useMergedCells) {
1265
        // TODO Auto-generated method stub
1266
        
1267
    }
1268
1269
    /**
1270
     *  <pre>
1271
     * Will be supported in future.
1272
     *  </pre>
1273
     */
1274
    public Comment getCellComment(int row, int column) {
1275
        // TODO Auto-generated method stub
1276
        return null;
1277
    }
1278
1279
    /**
1280
     *  <pre>
1281
     * Will be supported in future.
1282
     *  </pre>
1283
     */
1284
    public Comment getCellComment(CellAddress ref) {
1285
        // TODO Auto-generated method stub
1286
        return null;
1287
    }
1288
1289
    /**
1290
     *  <pre>
1291
     * Will be supported in future.
1292
     *  </pre>
1293
     */
1294
    public Map<CellAddress, ? extends Comment> getCellComments() {
1295
        // TODO Auto-generated method stub
1296
        return null;
1297
    }
1298
1299
    /**
1300
     *  <pre>
1301
     * Will be supported in future.
1302
     *  </pre>
1303
     */
1304
    public Drawing getDrawingPatriarch() {
1305
        // TODO Auto-generated method stub
1306
        return null;
1307
    }
1308
1309
    /**
1310
     * <pre>
1311
     * Not supported right now, as StreamedWorkbook
1312
     * supports only reading.
1313
     * </pre>
1314
     * 
1315
     */
1316
    public Drawing createDrawingPatriarch() {
1317
        // TODO Auto-generated method stub
1318
        return null;
1319
    }
1320
1321
    /**
1322
     * <pre>
1323
     * Will not be supported due to memory constraints
1324
     * </pre>
1325
     */
1326
    public Workbook getWorkbook() {
1327
        // TODO Auto-generated method stub
1328
        return null;
1329
    }
1330
1331
    /**
1332
     *  <pre>
1333
     * Will be supported in future.
1334
     *  </pre>
1335
     */
1336
    public String getSheetName() {
1337
        // TODO Auto-generated method stub
1338
        return null;
1339
    }
1340
1341
    /**
1342
     *  <pre>
1343
     * Will be supported in future.
1344
     *  </pre>
1345
     */
1346
    public boolean isSelected() {
1347
        // TODO Auto-generated method stub
1348
        return false;
1349
    }
1350
1351
    /**
1352
     * <pre>
1353
     * Not supported right now, as StreamedWorkbook
1354
     * supports only reading.
1355
     * </pre>
1356
     * 
1357
     */
1358
    public CellRange<? extends Cell> setArrayFormula(String formula,
1359
            CellRangeAddress range) {
1360
        // TODO Auto-generated method stub
1361
        return null;
1362
    }
1363
1364
    /**
1365
     * <pre>
1366
     * Not supported right now, as StreamedWorkbook
1367
     * supports only reading.
1368
     * </pre>
1369
     * 
1370
     */
1371
    public CellRange<? extends Cell> removeArrayFormula(Cell cell) {
1372
        // TODO Auto-generated method stub
1373
        return null;
1374
    }
1375
1376
    /**
1377
     *  <pre>
1378
     * Will be supported in future.
1379
     *  </pre>
1380
     */
1381
    public DataValidationHelper getDataValidationHelper() {
1382
        // TODO Auto-generated method stub
1383
        return null;
1384
    }
1385
1386
    /**
1387
     *  <pre>
1388
     * Will be supported in future.
1389
     *  </pre>
1390
     */
1391
    public List<? extends DataValidation> getDataValidations() {
1392
        // TODO Auto-generated method stub
1393
        return null;
1394
    }
1395
1396
    /**
1397
     * <pre>
1398
     * Not supported right now, as StreamedWorkbook
1399
     * supports only reading.
1400
     * </pre>
1401
     * 
1402
     */
1403
    public void addValidationData(DataValidation dataValidation) {
1404
        // TODO Auto-generated method stub
1405
        
1406
    }
1407
1408
    /**
1409
     * <pre>
1410
     * Not supported right now, as StreamedWorkbook
1411
     * supports only reading.
1412
     * </pre>
1413
     * 
1414
     */
1415
    public AutoFilter setAutoFilter(CellRangeAddress range) {
1416
        // TODO Auto-generated method stub
1417
        return null;
1418
    }
1419
1420
    /**
1421
     *  <pre>
1422
     * Will be supported in future.
1423
     *  </pre>
1424
     */
1425
    public SheetConditionalFormatting getSheetConditionalFormatting() {
1426
        // TODO Auto-generated method stub
1427
        return null;
1428
    }
1429
1430
    /**
1431
     *  <pre>
1432
     * Will be supported in future.
1433
     *  </pre>
1434
     */
1435
    public CellRangeAddress getRepeatingRows() {
1436
        // TODO Auto-generated method stub
1437
        return null;
1438
    }
1439
1440
    /**
1441
     *  <pre>
1442
     * Will be supported in future.
1443
     *  </pre>
1444
     */
1445
    public CellRangeAddress getRepeatingColumns() {
1446
        // TODO Auto-generated method stub
1447
        return null;
1448
    }
1449
1450
    /**
1451
     * <pre>
1452
     * Not supported right now, as StreamedWorkbook
1453
     * supports only reading.
1454
     * </pre>
1455
     * 
1456
     */
1457
    public void setRepeatingRows(CellRangeAddress rowRangeRef) {
1458
        // TODO Auto-generated method stub
1459
        
1460
    }
1461
1462
    /**
1463
     * <pre>
1464
     * Not supported right now, as StreamedWorkbook
1465
     * supports only reading.
1466
     * </pre>
1467
     * 
1468
     */
1469
    public void setRepeatingColumns(CellRangeAddress columnRangeRef) {
1470
        // TODO Auto-generated method stub
1471
        
1472
    }
1473
1474
    /**
1475
     *  <pre>
1476
     * Will be supported in future.
1477
     *  </pre>
1478
     */
1479
    public int getColumnOutlineLevel(int columnIndex) {
1480
        // TODO Auto-generated method stub
1481
        return 0;
1482
    }
1483
1484
    /**
1485
     *  <pre>
1486
     * Will be supported in future.
1487
     *  </pre>
1488
     */
1489
    public Hyperlink getHyperlink(int row, int column) {
1490
        // TODO Auto-generated method stub
1491
        return null;
1492
    }
1493
1494
    /**
1495
     *  <pre>
1496
     * Will be supported in future.
1497
     *  </pre>
1498
     */
1499
    public Hyperlink getHyperlink(CellAddress addr) {
1500
        // TODO Auto-generated method stub
1501
        return null;
1502
    }
1503
1504
1505
    /**
1506
     * <pre>
1507
     * Will not be supported due to memory constraints
1508
     * </pre>
1509
     */
1510
    public List<? extends Hyperlink> getHyperlinkList() {
1511
        // TODO Auto-generated method stub
1512
        return null;
1513
    }
1514
1515
    /**
1516
     *  <pre>
1517
     * Will be supported in future.
1518
     *  </pre>
1519
     */
1520
    public CellAddress getActiveCell() {
1521
        // TODO Auto-generated method stub
1522
        return null;
1523
    }
1524
1525
    /**
1526
     * <pre>
1527
     * Not supported right now, as StreamedWorkbook
1528
     * supports only reading.
1529
     * </pre>
1530
     * 
1531
     */
1532
    public void setActiveCell(CellAddress address) {
1533
        // TODO Auto-generated method stub
1534
        
1535
    }
1536
1537
1538
1539
	
1540
	
1541
	
1542
}
(-)a/src/ooxml/java/org/apache/poi/xssf/streaming/reader/StreamedSheetEventHandler.java (+419 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
package org.apache.poi.xssf.streaming.reader;
18
19
import java.util.ArrayList;
20
import java.util.Arrays;
21
import java.util.HashMap;
22
import java.util.Iterator;
23
import java.util.List;
24
import java.util.Map;
25
import java.util.Properties;
26
27
import javax.xml.namespace.QName;
28
import javax.xml.stream.events.Attribute;
29
import javax.xml.stream.events.Characters;
30
import javax.xml.stream.events.EndElement;
31
import javax.xml.stream.events.StartElement;
32
import javax.xml.stream.events.XMLEvent;
33
34
import org.apache.poi.ss.usermodel.DataFormatter;
35
import org.apache.poi.xssf.model.SharedStringsTable;
36
import org.apache.poi.xssf.model.StylesTable;
37
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
38
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
39
40
/**
41
 * Handler class that handles the event generated by StAX parser
42
 *
43
 */
44
public class StreamedSheetEventHandler {
45
	
46
	private boolean isEndOfRow = false;
47
	private boolean isRow = false;
48
	//private boolean mergeCell = false;
49
	
50
	private String formatString = null;
51
	private int formatIndex = 0;
52
	private String cellContentType;
53
	private int rowNumber = 0;
54
	private int numberOfColumns = 0;
55
	private List<String> cellPositions = null;
56
	private String currentCellPosition = null;
57
	private int previousCellIndex = -1;
58
	//private List<String> mergeList = null;  //merge cell not supported right now. Complexity involved if multiple rows are merged.
59
	
60
	private SharedStringsTable sharedStringsTable;
61
	private StylesTable stylesTable;
62
	
63
	private DataFormatter formatter = new DataFormatter();
64
	private Properties properties = new Properties();
65
	private Map<String, String> attributes = new HashMap<String, String>();
66
	private StringBuffer sb = new StringBuffer(250); 
67
	private List<String> dateFormatsAry =  new ArrayList<String>();
68
	
69
	private StreamedRow row;
70
	private StreamedCell cell;
71
	
72
	private final String dateFormat = "m/d/yy|DD/MM/YY|[$-F800]dddd\\,\\ mmmm\\ dd\\,\\ yyyy|m/d;@|m/d/yy;@|mm/dd/yy;@|"
73
	        + "[$-409]d\\-mmm;@|[$-409]d\\-mmm\\-yy;@|[$-409]dd\\-mmm\\-yy;@|[$-409]mmm\\-yy;@|[$-409]mmmm\\-yy;@|"
74
	        + "[$-409]mmmm\\ d\\,\\ yyyy;@|[$-409]m/d/yy\\ h:mm\\ AM/PM;@|m/d/yy\\ h:mm;@|[$-409]mmmmm;@|"
75
	        + "[$-409]mmmmm\\-yy;@|m/d/yyyy;@|[$-409]d\\-mmm\\-yyyy;@|[$-F400]h:mm:ss\\ AM/PM|"
76
	        + "h:mm;@|[$-409]h:mm\\ AM/PM;@|h:mm:ss;@|[$-409]h:mm:ss\\ AM/PM;@|mm:ss.0;@|[h]:mm:ss;@|"
77
	        + "[$-409]m/d/yy\\ h:mm\\ AM/PM;@|DD/MM/YYYY";
78
	
79
	public static final String DEFAULT_CELL_TYPE = "DEFAULT";
80
	public static final String STRING_CELL_TYPE = "STRING";
81
	public static final String BOOLEAN_CELL_TYPE = "BOOLEAN";
82
	public static final String DATE_CELL_TYPE = "DATE";
83
	public static final String FORMULA_CELL_TYPE = "FORMULA";
84
	public static final String NUMERIC_CELL_TYPE = "NUMERIC";
85
		
86
	
87
	/**
88
	 * Constructor
89
	 * @param sharedStringsTable
90
	 * @param stylesTable
91
	 */
92
	public StreamedSheetEventHandler(SharedStringsTable sharedStringsTable, StylesTable stylesTable){
93
		this.sharedStringsTable = sharedStringsTable;
94
		this.stylesTable = stylesTable;
95
		/*try{
96
			properties.load(getClass().getClassLoader().getResourceAsStream("excelreader.properties"));
97
		}catch(Exception e){
98
			e.printStackTrace();
99
		}*/
100
		
101
		/*if(properties.get("date.formatStrings") != null){
102
			dateFormat = properties.get("date.formatStrings").toString();			
103
		}*/
104
		
105
		dateFormatsAry.addAll(Arrays.asList(dateFormat.split("\\|")));
106
		
107
	}
108
	
109
	/**
110
	 * Handler method for handling the event generated for StAX parser.
111
	 * For each XML event(Start Tag, End Tag & Characters), StAX parser will 
112
	 * generate event. This event will be handled by this method.
113
	 * Attributes of each element are put in a map with key as attribute 
114
	 * and value as attribute value.
115
	 * @param event
116
	 */
117
	public void handleEvent(XMLEvent event){
118
		switch(event.getEventType()){
119
		case XMLEvent.START_ELEMENT:
120
			StartElement startElemet = event.asStartElement();
121
			QName startName = startElemet.getName();
122
			String name = startName.getLocalPart();					
123
			Iterator<Attribute> iterator = startElemet.getAttributes();
124
			while(iterator.hasNext()){
125
				Attribute attribute = iterator.next();					
126
				attributes.put(attribute.getName().getLocalPart(), attribute.getValue());
127
			}
128
			if((name.trim().equals("row")) && (attributes.size() > 0)){
129
				isRow = true;
130
			}
131
			
132
			/*else if(name.trim().equals("mergeCell")){
133
			    mergeCell = true;
134
			}
135
			
136
			if(isRow || mergeCell){
137
				startElement(name);
138
			}*/
139
			
140
			if(isRow){
141
                startElement(name);
142
            }
143
			break;
144
		case XMLEvent.CHARACTERS:
145
			Characters chars = event.asCharacters();
146
			if(isRow){
147
				characters(chars.getData());
148
			}
149
			break;
150
		case XMLEvent.END_ELEMENT:
151
			EndElement endElement = event.asEndElement();
152
			QName endName = endElement.getName();
153
			String endTageName = endName.getLocalPart();
154
			endElement(endTageName);
155
			attributes.clear();
156
			break;
157
		}
158
	}
159
	
160
	/**
161
	 * All the start element events are handled here.
162
	 * It identifies the things like rowNumber, cell content type etc
163
	 * @param name
164
	 */
165
	private void startElement(String name) {
166
		if(name.equals("row")) {
167
			previousCellIndex = -1;
168
			isEndOfRow = false;
169
			rowNumber = (Integer.parseInt(attributes.get("r")) - 1);
170
			row = new StreamedRow(rowNumber);
171
			if(rowNumber == 0){
172
				cellPositions = new ArrayList<String>();
173
			}
174
		}else if(name.equals("c")){
175
			String cellPosition =  attributes.get("r").toUpperCase();
176
			cellPosition = cellPosition.replaceAll(String.valueOf(rowNumber+1), "").trim();
177
			currentCellPosition = cellPosition;
178
			if(rowNumber == 0){
179
				numberOfColumns ++;								
180
				cellPositions.add(cellPosition);
181
			}
182
183
			cellContentType = getCellContentType();
184
		}else if(name.equals("f")){
185
			cellContentType = FORMULA_CELL_TYPE;
186
		}
187
		
188
		/*else if(name.equals("mergeCell")){
189
		    if(mergeList == null){
190
		        mergeList = 
191
		    }
192
		}*/
193
	}
194
	
195
	
196
	/**
197
	 * This method is invoked on end element event.
198
	 * Mostly used for setting the default values before handling 
199
	 * next start element.
200
	 * All data captured in characters event are stored here.
201
	 * Data will be stored only on reaching the end element.
202
	 * @param name
203
	 */
204
	private void endElement(String name) {
205
		if(name.equals("c")){
206
			if(isRow){
207
				storeData(sb.toString());
208
			}
209
			cellContentType = DEFAULT_CELL_TYPE;
210
			formatString = null;
211
			formatIndex = 0;			
212
			currentCellPosition = null;
213
			previousCellIndex++;
214
			sb.delete(0, sb.length());
215
		}else if(name.equals("row")){
216
			isEndOfRow = true;
217
			isRow = false;
218
		}else if(name.equals("f")){
219
			cellContentType = DEFAULT_CELL_TYPE;
220
		}
221
		
222
		/*else if(name.equals("mergeCell")){
223
		    mergeCell = false;
224
		}*/
225
	}
226
	
227
	/**
228
	 * Captures the data 
229
	 * @param data
230
	 */
231
	private void characters(String data) {
232
		if(!(cellContentType.equals(FORMULA_CELL_TYPE))){			
233
			sb.append(data);
234
		}
235
	}
236
	
237
	/**
238
	 * Creates cells and store data
239
	 * @param data
240
	 */
241
	private void storeData(String data){			
242
		if (!(cellContentType.equals(FORMULA_CELL_TYPE))) {
243
			if(cellContentType.equals(STRING_CELL_TYPE)){
244
				data = getStringData(data);
245
			}else if(cellContentType.equals(DATE_CELL_TYPE)){
246
				data = getDateAsString(data);
247
			}else if(cellContentType.equals(BOOLEAN_CELL_TYPE)){
248
				if(data.trim().equals("1")){
249
					data = "TRUE";
250
				}else if(data.trim().equals("0")){
251
					data = "FALSE";
252
				}else if(data.trim().equals("")){
253
					data = null;
254
				}else{
255
				    try{
256
				        int value = Integer.parseInt(data);
257
				        if(value > 0){
258
				            data = "TRUE";
259
				        }
260
				    }catch(Exception e){
261
				        data = null;
262
				    }
263
				}
264
			}
265
			
266
			if(cellPositions != null){
267
				fillEmptyCells();
268
			}
269
			
270
			cell = new StreamedCell();
271
			cell.setValue(data);
272
			cell.setCellNumber(previousCellIndex+1);
273
			row.getCells().add(cell);
274
					
275
		}
276
	}
277
	
278
	
279
	/**
280
	 * Identifies the cell content type
281
	 * @return
282
	 */
283
	private String getCellContentType(){
284
		String cellConentType = DEFAULT_CELL_TYPE;
285
		String cellType = attributes.get("t");
286
		if((null != cellType) && (cellType.equals("s"))){
287
			cellConentType = STRING_CELL_TYPE;
288
		}else if((null != cellType) && (equals("b"))){
289
			cellConentType = BOOLEAN_CELL_TYPE;
290
		}else{
291
			
292
			if(checkForDate()){
293
				cellConentType = DATE_CELL_TYPE;
294
			}else if(checkForBoolean()){
295
				cellConentType = BOOLEAN_CELL_TYPE;
296
			}
297
		}
298
		return cellConentType;
299
	}
300
	
301
	
302
	
303
	/**
304
	 * Identifies if the given cell is a boolean or not
305
	 * @return
306
	 */
307
	private boolean checkForBoolean(){
308
		String cellStyleType = attributes.get("s");
309
		if(cellStyleType != null){
310
			int styleIndex = Integer.parseInt(cellStyleType);
311
			XSSFCellStyle style = stylesTable.getStyleAt(styleIndex);
312
			formatIndex = style.getDataFormat();
313
			formatString = style.getDataFormatString();
314
			if(formatString.contains("TRUE")){
315
				return true;
316
			}else{
317
				return false;
318
			}
319
		}else{
320
			return false;
321
		}
322
	}
323
324
	/**
325
	 * identifies if the given cell is a Date cell or not
326
	 * @return
327
	 */
328
	private boolean checkForDate(){
329
		boolean isDate = false;
330
		String cellStyleType = attributes.get("s");
331
		if(cellStyleType != null){
332
			int styleIndex = Integer.parseInt(cellStyleType);
333
			XSSFCellStyle style = stylesTable.getStyleAt(styleIndex);
334
			formatIndex = style.getDataFormat();
335
			formatString = style.getDataFormatString();
336
			if(dateFormatsAry.contains(formatString.trim())){				
337
				isDate = true;
338
			}
339
		}
340
		
341
		return isDate;
342
	}
343
	
344
345
	/**
346
	 * fetches the String data from SharedStringsTable.xml
347
	 * @param data
348
	 * @return
349
	 */
350
	private String getStringData(String data){
351
		int index = Integer.parseInt(data);
352
		return new XSSFRichTextString(sharedStringsTable.getEntryAt(index)).toString().trim();
353
	}
354
	
355
	/**
356
	 * check the styles applied to the cell and fetches the exact content of date cell
357
	 * @param data
358
	 * @return
359
	 */
360
	private String getDateAsString(String data) {
361
		if ((data != null) && (!(data.trim().equals("")))) {
362
			/*return formatter.formatRawCellContents(Double.parseDouble(data),
363
					formatIndex, formatString);*/
364
		    
365
		    /*
366
		     * All dates are formatted with 'DD/MM/YYYY', this is to avoid confusion between
367
		     * 10/12/2017 & 10/12/1917. Otherwise both will be read as 10/12/17 if the format 
368
		     * is DD/MM/YY
369
		     */
370
		    return formatter.formatRawCellContents(Double.parseDouble(data),
371
                    formatIndex, "DD/MM/YYYY");
372
		} else {
373
			return null;
374
		}
375
	}
376
377
	
378
	/**
379
	 * Used to fill empty cells as null
380
	 */
381
	private void fillEmptyCells() {
382
		int currentCellIndex = cellPositions.indexOf(currentCellPosition);
383
		if ((currentCellIndex - previousCellIndex) > 1) {
384
			for (int i = (previousCellIndex + 1); i < currentCellIndex; i++) {
385
				cell = new StreamedCell();
386
				cell.setValue(null);
387
				row.getCells().add(cell);
388
			}
389
			
390
			previousCellIndex = currentCellIndex;
391
		}
392
	}
393
394
	public boolean isEndOfRow() {
395
		return isEndOfRow;
396
	}
397
398
	public void setEndOfRow(boolean isEndOfRow) {
399
		this.isEndOfRow = isEndOfRow;
400
	}
401
402
	public int getRowNumber() {
403
		return rowNumber;
404
	}
405
406
	public int getNumberOfColumns() {
407
		return numberOfColumns;
408
	}
409
410
	public List<String> getCellPositions() {
411
		return cellPositions;
412
	}
413
414
	public StreamedRow getRow() {
415
		return row;
416
	}
417
	
418
	
419
}
(-)a/src/ooxml/java/org/apache/poi/xssf/streaming/reader/StreamedWorkbook.java (+907 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
package org.apache.poi.xssf.streaming.reader;
18
19
import java.io.File;
20
import java.io.IOException;
21
import java.io.InputStream;
22
import java.io.OutputStream;
23
import java.util.ArrayList;
24
import java.util.Iterator;
25
import java.util.List;
26
27
import javax.xml.stream.XMLEventReader;
28
import javax.xml.stream.XMLInputFactory;
29
30
import org.apache.poi.openxml4j.opc.OPCPackage;
31
import org.apache.poi.ss.SpreadsheetVersion;
32
import org.apache.poi.ss.formula.udf.UDFFinder;
33
import org.apache.poi.ss.usermodel.CellStyle;
34
import org.apache.poi.ss.usermodel.CreationHelper;
35
import org.apache.poi.ss.usermodel.DataFormat;
36
import org.apache.poi.ss.usermodel.Font;
37
import org.apache.poi.ss.usermodel.Name;
38
import org.apache.poi.ss.usermodel.PictureData;
39
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
40
import org.apache.poi.ss.usermodel.Sheet;
41
import org.apache.poi.ss.usermodel.Workbook;
42
import org.apache.poi.util.POILogFactory;
43
import org.apache.poi.util.POILogger;
44
import org.apache.poi.xssf.eventusermodel.XSSFReader;
45
import org.apache.poi.xssf.model.SharedStringsTable;
46
import org.apache.poi.xssf.model.StylesTable;
47
48
/**
49
 * Represents the excel workbook
50
 *
51
 */
52
public class StreamedWorkbook implements Workbook{
53
    
54
    private static final POILogger logger = POILogFactory.getLogger(StreamedWorkbook.class);
55
	
56
	private File inputFile;
57
	
58
	private Iterator<InputStream> sheetIterator;
59
    private SharedStringsTable sharedStringsTable;
60
    private StylesTable stylesTable;
61
	
62
	/**
63
	 * <pre>
64
	 * Accepts the file path and return an instance of StreamedWorkBook
65
	 *</pre>
66
	 * @param filePath
67
	 * @throws Exception 
68
	 */
69
	public StreamedWorkbook(String filePath) throws Exception{
70
		if((filePath != null) && !(filePath.trim().isEmpty())){
71
			inputFile = new File(filePath);
72
			
73
			if(inputFile != null){
74
			    XSSFReader reader = getExcelReader(inputFile);
75
	            if(reader != null){
76
	                sheetIterator = reader.getSheetsData();
77
	                sharedStringsTable = reader.getSharedStringsTable();
78
	                stylesTable = reader.getStylesTable();
79
	            }
80
	        }
81
			
82
		}else{
83
            throw new Exception("No sheets found");
84
        }
85
	}
86
	
87
	/**
88
	 * <pre>
89
	 * Fetch all sheets from given excel file
90
	 * </pre>
91
	 * @return
92
	 * @throws Exception
93
	 */
94
	public Iterator<StreamedSheet> getSheetIterator() throws Exception{
95
	    
96
	    return getAllSheets();
97
	}
98
	
99
	
100
	 /**
101
     * Returns the list of sheets for the given excel file
102
     * @param file
103
     * @return
104
     * @throws Exception 
105
     */
106
    private Iterator<StreamedSheet>  getAllSheets() throws Exception{
107
        
108
        return getStreamedSheetIterator();
109
    }
110
    
111
    private Iterator<StreamedSheet> getStreamedSheetIterator() throws Exception{
112
        List<StreamedSheet> sheetList = null;
113
        XMLInputFactory factory = null; 
114
        
115
        if(sheetIterator != null){
116
            factory = XMLInputFactory.newInstance();
117
            sheetList =  new ArrayList<StreamedSheet>();
118
            int sheetNumber = 0;
119
            while(sheetIterator.hasNext()){
120
                InputStream sheetInputStream = sheetIterator.next();
121
                StreamedSheet sheet = createStreamedSheet(factory.createXMLEventReader(sheetInputStream), sheetNumber);
122
                sheetList.add(sheet);
123
                sheetNumber++;
124
            }
125
        }else{
126
            throw new Exception("Workbook already closed");
127
        }
128
        
129
        return sheetList.iterator();
130
        
131
    }
132
    
133
    /**
134
     * Creates and returns the instance of StreamedSheet
135
     *
136
     * @param parser
137
     * @param sheetNumber
138
     * @return
139
     */
140
    private StreamedSheet createStreamedSheet(XMLEventReader parser,int sheetNumber){
141
        StreamedSheet sheet = new StreamedSheet();
142
        sheet.setXmlParser(parser);
143
        sheet.setSharedStringsTable(sharedStringsTable);
144
        sheet.setStylesTable(stylesTable);
145
        sheet.setSheetNumber(sheetNumber);
146
        sheet.createEventHandler();
147
        
148
        return sheet;
149
    }
150
    
151
    
152
    
153
    /**
154
     * Receives the excel file and returns the file excel file reader
155
     * @param inputStream
156
     * @return
157
     * @throws Exception
158
     */
159
    private XSSFReader getExcelReader(File file) throws Exception{
160
        XSSFReader reader = null;
161
        OPCPackage pkg = null;
162
        pkg = OPCPackage.open(file);
163
        reader = new XSSFReader(pkg);
164
        return reader;
165
    }
166
    
167
    
168
    
169
    /*
170
     *  TO DO 
171
     */
172
173
    
174
    /**
175
     * <pre>
176
     * Will be supported in the future.
177
     * </pre>
178
     * 
179
     */
180
    public int getActiveSheetIndex() {
181
        // TODO Auto-generated method stub
182
        return 0;
183
    }
184
185
    /**
186
     * <pre>
187
     * Not supported right now, as StreamedWorkbook
188
     * supports only reading.
189
     * </pre>
190
     * 
191
     */
192
    public void setActiveSheet(int sheetIndex) {
193
        // TODO Auto-generated method stub
194
        
195
    }
196
197
    /**
198
     * <pre>
199
     * Will be supported in the future.
200
     * </pre>
201
     * 
202
     */
203
    public int getFirstVisibleTab() {
204
        // TODO Auto-generated method stub
205
        return 0;
206
    }
207
208
    /**
209
     * <pre>
210
     * Will be supported in the future.
211
     * </pre>
212
     * 
213
     */
214
    public void setFirstVisibleTab(int sheetIndex) {
215
        // TODO Auto-generated method stub
216
        
217
    }
218
219
    /**
220
     * <pre>
221
     * Will be supported in the future.
222
     * </pre>
223
     * 
224
     */
225
    public void setSheetOrder(String sheetname, int pos) {
226
        // TODO Auto-generated method stub
227
        
228
    }
229
230
    /**
231
     * <pre>
232
     * Will be supported in the future.
233
     * </pre>
234
     * 
235
     */
236
    public void setSelectedTab(int index) {
237
        // TODO Auto-generated method stub
238
        
239
    }
240
241
    /**
242
     * <pre>
243
     * Not supported right now, as StreamedWorkbook
244
     * supports only reading.
245
     * </pre>
246
     * 
247
     */
248
    public void setSheetName(int sheet, String name) {
249
        // TODO Auto-generated method stub
250
        
251
    }
252
253
    /**
254
     * <pre>
255
     * Will be supported in the future.
256
     * </pre>
257
     * 
258
     */    
259
    public String getSheetName(int sheet) {
260
        // TODO Auto-generated method stub
261
        return null;
262
    }
263
264
    /**
265
     * <pre>
266
     * Will be supported in the future.
267
     * </pre>
268
     * 
269
     */
270
    public int getSheetIndex(String name) {
271
        // TODO Auto-generated method stub
272
        return 0;
273
    }
274
275
    /**
276
     * <pre>
277
     * Will be supported in the future.
278
     * </pre>
279
     * 
280
     */
281
    public int getSheetIndex(Sheet sheet) {
282
        // TODO Auto-generated method stub
283
        return 0;
284
    }
285
286
    /**
287
     * <pre>
288
     * Not supported right now, as StreamedWorkbook
289
     * supports only reading.
290
     * </pre>
291
     * 
292
     */    
293
    public Sheet createSheet() {
294
        // TODO Auto-generated method stub
295
        return null;
296
    }
297
298
    /**
299
     * <pre>
300
     * Not supported right now, as StreamedWorkbook
301
     * supports only reading.
302
     * </pre>
303
     * 
304
     */
305
    public Sheet createSheet(String sheetname) {
306
        // TODO Auto-generated method stub
307
        return null;
308
    }
309
310
    /**
311
     * <pre>
312
     * Not supported right now, as StreamedWorkbook
313
     * supports only reading.
314
     * </pre>
315
     * 
316
     */
317
    public Sheet cloneSheet(int sheetNum) {
318
        // TODO Auto-generated method stub
319
        return null;
320
    }
321
322
    /**
323
     * <pre>
324
     * Use Iterator<StreamedSheet> getSheetIterator() instead.
325
     * </pre>
326
     * 
327
     */
328
    public Iterator<Sheet> sheetIterator() {
329
        // TODO Auto-generated method stub
330
        return null;
331
    }
332
333
    /**
334
     * <pre>
335
     *  Returns the number of sheets,
336
     * </pre>
337
     */
338
    public int getNumberOfSheets() {
339
        int sheetCount = 0;
340
        
341
        if(this.sheetIterator != null){
342
            while(sheetIterator.hasNext()){
343
                sheetIterator.next();
344
                sheetCount++;
345
            }
346
        }else{
347
            logger.log(POILogger.ERROR, "Workbook already closed");
348
        }
349
        
350
        
351
        return sheetCount;
352
    }
353
354
    /**
355
     * <pre>
356
     *  Currently not supported due to memory footprint.
357
     *  Will be supported in future.
358
     * </pre>
359
     */
360
    public Sheet getSheetAt(int index) {
361
        
362
/*        StreamedSheet sheet = null;
363
        int sheetCount = 0;
364
        XMLInputFactory factory = XMLInputFactory.newInstance();
365
        
366
        if(inputFile != null && inputFile.exists()){
367
            try {
368
                XSSFReader reader = getExcelReader(inputFile);
369
                
370
                
371
                
372
                while(reader.getSheetsData().hasNext()){
373
                    
374
                    if(index == sheetCount){
375
                        sheet = createStreamedSheet(factory.createXMLEventReader(reader.getSheetsData().next()), sheetCount);
376
                    }else{
377
                        reader.getSheetsData().next();
378
                    }
379
                    
380
                    sheetCount++;
381
                }
382
                
383
            } catch (Exception e) {
384
                logger.log(POILogger.ERROR, "No sheets found !!");
385
            }
386
        }
387
        return sheet;*/
388
        return null;
389
    }
390
391
    /**
392
     * <pre>
393
     * Will be supported in the future.
394
     * </pre>
395
     * 
396
     */
397
    public Sheet getSheet(String name) {
398
        // TODO Auto-generated method stub
399
        return null;
400
    }
401
402
    /**
403
     * <pre>
404
     * Not supported right now, as StreamedWorkbook
405
     * supports only reading.
406
     * </pre>
407
     * 
408
     */
409
    public void removeSheetAt(int index) {
410
        // TODO Auto-generated method stub
411
        
412
    }
413
414
    /**
415
     * <pre>
416
     * Not supported right now, as StreamedWorkbook
417
     * supports only reading.
418
     * </pre>
419
     * 
420
     */
421
    public Font createFont() {
422
        // TODO Auto-generated method stub
423
        return null;
424
    }
425
426
427
    /**
428
     * <pre>
429
     * Will be supported in the future.
430
     * </pre>
431
     * 
432
     */
433
    public Font findFont(short boldWeight, short color, short fontHeight,
434
            String name, boolean italic, boolean strikeout, short typeOffset,
435
            byte underline) {
436
        // TODO Auto-generated method stub
437
        return null;
438
    }
439
440
    /**
441
     * <pre>
442
     * Will be supported in the future.
443
     * </pre>
444
     * 
445
     */
446
    public Font findFont(boolean bold, short color, short fontHeight,
447
            String name, boolean italic, boolean strikeout, short typeOffset,
448
            byte underline) {
449
        // TODO Auto-generated method stub
450
        return null;
451
    }
452
453
    /**
454
     * <pre>
455
     * Will be supported in the future.
456
     * </pre>
457
     * 
458
     */
459
    public short getNumberOfFonts() {
460
        // TODO Auto-generated method stub
461
        return 0;
462
    }
463
464
    /**
465
     * <pre>
466
     * Will be supported in the future.
467
     * </pre>
468
     * 
469
     */
470
    public Font getFontAt(short idx) {
471
        // TODO Auto-generated method stub
472
        return null;
473
    }
474
475
    /**
476
     * <pre>
477
     * Not supported right now, as StreamedWorkbook
478
     * supports only reading.
479
     * </pre>
480
     * 
481
     */
482
    public CellStyle createCellStyle() {
483
        // TODO Auto-generated method stub
484
        return null;
485
    }
486
487
488
    /**
489
     * <pre>
490
     * Will be supported in the future.
491
     * </pre>
492
     * 
493
     */
494
    public int getNumCellStyles() {
495
        // TODO Auto-generated method stub
496
        return 0;
497
    }
498
499
    /**
500
     * <pre>
501
     * Will be supported in the future.
502
     * </pre>
503
     * 
504
     */
505
    public CellStyle getCellStyleAt(int idx) {
506
        // TODO Auto-generated method stub
507
        return null;
508
    }
509
510
    
511
    /**
512
     * <pre>
513
     * Not supported right now, as StreamedWorkbook
514
     * supports only reading.
515
     * </pre>
516
     * 
517
     */    
518
    public void write(OutputStream stream) throws IOException {
519
        // TODO Auto-generated method stub
520
        
521
    }
522
523
    /**
524
     * <pre>
525
     * Close the workbook
526
     * </pre>
527
     * 
528
     */    
529
    public void close() throws IOException {
530
        if(sheetIterator != null){
531
            while(sheetIterator.hasNext()){
532
                sheetIterator.next().close();
533
            }
534
            sheetIterator = null;
535
        }
536
    }
537
538
    /**
539
     * <pre>
540
     * Will be supported in the future.
541
     * </pre>
542
     * 
543
     */    
544
    public int getNumberOfNames() {
545
        // TODO Auto-generated method stub
546
        return 0;
547
    }
548
549
    /**
550
     * <pre>
551
     * Will be supported in the future.
552
     * </pre>
553
     * 
554
     */
555
    public Name getName(String name) {
556
        // TODO Auto-generated method stub
557
        return null;
558
    }
559
560
    /**
561
     * <pre>
562
     * Will be supported in the future.
563
     * </pre>
564
     * 
565
     */
566
    public List<? extends Name> getNames(String name) {
567
        // TODO Auto-generated method stub
568
        return null;
569
    }
570
571
    /**
572
     * <pre>
573
     * Will be supported in the future.
574
     * </pre>
575
     * 
576
     */
577
    public List<? extends Name> getAllNames() {
578
        // TODO Auto-generated method stub
579
        return null;
580
    }
581
582
    /**
583
     * <pre>
584
     * Will be supported in the future.
585
     * </pre>
586
     * 
587
     */
588
    public Name getNameAt(int nameIndex) {
589
        // TODO Auto-generated method stub
590
        return null;
591
    }
592
593
    
594
    /**
595
     * <pre>
596
     * Not supported right now, as StreamedWorkbook
597
     * supports only reading.
598
     * </pre>
599
     * 
600
     */       
601
    public Name createName() {
602
        // TODO Auto-generated method stub
603
        return null;
604
    }
605
606
    /**
607
     * <pre>
608
     * Will be supported in the future.
609
     * </pre>
610
     * 
611
     */    
612
    public int getNameIndex(String name) {
613
        // TODO Auto-generated method stub
614
        return 0;
615
    }
616
617
    /**
618
     * <pre>
619
     * Not supported right now, as StreamedWorkbook
620
     * supports only reading.
621
     * </pre>
622
     * 
623
     */   
624
    public void removeName(int index) {
625
        // TODO Auto-generated method stub
626
        
627
    }
628
629
    /**
630
     * <pre>
631
     * Not supported right now, as StreamedWorkbook
632
     * supports only reading.
633
     * </pre>
634
     * 
635
     */   
636
    public void removeName(String name) {
637
        // TODO Auto-generated method stub
638
        
639
    }
640
641
    /**
642
     * <pre>
643
     * Not supported right now, as StreamedWorkbook
644
     * supports only reading.
645
     * </pre>
646
     * 
647
     */   
648
    public void removeName(Name name) {
649
        // TODO Auto-generated method stub
650
        
651
    }
652
653
    /**
654
     * <pre>
655
     * Not supported right now, as StreamedWorkbook
656
     * supports only reading.
657
     * </pre>
658
     * 
659
     */      
660
    public int linkExternalWorkbook(String name, Workbook workbook) {
661
        // TODO Auto-generated method stub
662
        return 0;
663
    }
664
665
    /**
666
     * <pre>
667
     * Not supported right now, as StreamedWorkbook
668
     * supports only reading.
669
     * </pre>
670
     * 
671
     */   
672
    public void setPrintArea(int sheetIndex, String reference) {
673
        // TODO Auto-generated method stub
674
        
675
    }
676
677
    /**
678
     * <pre>
679
     * Not supported right now, as StreamedWorkbook
680
     * supports only reading.
681
     * </pre>
682
     * 
683
     */   
684
    public void setPrintArea(int sheetIndex, int startColumn, int endColumn,
685
            int startRow, int endRow) {
686
        // TODO Auto-generated method stub
687
        
688
    }
689
690
    /**
691
     * <pre>
692
     * Will be supported in the future.
693
     * </pre>
694
     * 
695
     */ 
696
    public String getPrintArea(int sheetIndex) {
697
        // TODO Auto-generated method stub
698
        return null;
699
    }
700
701
    /**
702
     * <pre>
703
     * Not supported right now, as StreamedWorkbook
704
     * supports only reading.
705
     * </pre>
706
     * 
707
     */      
708
    public void removePrintArea(int sheetIndex) {
709
        // TODO Auto-generated method stub
710
        
711
    }
712
713
    /**
714
     * <pre>
715
     * Will be supported in the future.
716
     * </pre>
717
     * 
718
     */ 
719
    public MissingCellPolicy getMissingCellPolicy() {
720
        // TODO Auto-generated method stub
721
        return null;
722
    }
723
724
    /**
725
     * <pre>
726
     * Not supported right now, as StreamedWorkbook
727
     * supports only reading.
728
     * </pre>
729
     * 
730
     */  
731
    public void setMissingCellPolicy(MissingCellPolicy missingCellPolicy) {
732
        // TODO Auto-generated method stub
733
        
734
    }
735
736
    /**
737
     * <pre>
738
     * Not supported right now, as StreamedWorkbook
739
     * supports only reading.
740
     * </pre>
741
     * 
742
     */  
743
    public DataFormat createDataFormat() {
744
        // TODO Auto-generated method stub
745
        return null;
746
    }
747
748
    /**
749
     * <pre>
750
     * Not supported right now, as StreamedWorkbook
751
     * supports only reading.
752
     * </pre>
753
     * 
754
     */  
755
    public int addPicture(byte[] pictureData, int format) {
756
        // TODO Auto-generated method stub
757
        return 0;
758
    }
759
760
    /**
761
     * <pre>
762
     * Not supported due to memory footprint.
763
     * </pre>
764
     * 
765
     */ 
766
    public List<? extends PictureData> getAllPictures() {
767
        // TODO Auto-generated method stub
768
        return null;
769
    }
770
771
    /**
772
     * <pre>
773
     * Will be supported in the future.
774
     * </pre>
775
     * 
776
     */ 
777
    public CreationHelper getCreationHelper() {
778
        // TODO Auto-generated method stub
779
        return null;
780
    }
781
782
    /**
783
     * <pre>
784
     * Will be supported in the future.
785
     * </pre>
786
     * 
787
     */ 
788
    public boolean isHidden() {
789
        // TODO Auto-generated method stub
790
        return false;
791
    }
792
793
    /**
794
     * <pre>
795
     * Not supported right now, as StreamedWorkbook
796
     * supports only reading.
797
     * </pre>
798
     * 
799
     */  
800
    public void setHidden(boolean hiddenFlag) {
801
        // TODO Auto-generated method stub
802
        
803
    }
804
805
    /**
806
     * <pre>
807
     * Will be supported in the future.
808
     * </pre>
809
     * 
810
     */ 
811
    public boolean isSheetHidden(int sheetIx) {
812
        // TODO Auto-generated method stub
813
        return false;
814
    }
815
816
    /**
817
     * <pre>
818
     * Will be supported in the future.
819
     * </pre>
820
     * 
821
     */ 
822
    public boolean isSheetVeryHidden(int sheetIx) {
823
        // TODO Auto-generated method stub
824
        return false;
825
    }
826
827
    /**
828
     * <pre>
829
     * Not supported right now, as StreamedWorkbook
830
     * supports only reading.
831
     * </pre>
832
     * 
833
     */ 
834
    public void setSheetHidden(int sheetIx, boolean hidden) {
835
        // TODO Auto-generated method stub
836
        
837
    }
838
839
    /**
840
     * <pre>
841
     * Not supported right now, as StreamedWorkbook
842
     * supports only reading.
843
     * </pre>
844
     * 
845
     */ 
846
    public void setSheetHidden(int sheetIx, int hidden) {
847
        // TODO Auto-generated method stub
848
        
849
    }
850
851
    /**
852
     * <pre>
853
     * Not supported right now, as StreamedWorkbook
854
     * supports only reading.
855
     * </pre>
856
     * 
857
     */ 
858
    public void addToolPack(UDFFinder toopack) {
859
        // TODO Auto-generated method stub
860
        
861
    }
862
863
    /**
864
     * <pre>
865
     * Not supported right now, as StreamedWorkbook
866
     * supports only reading.
867
     * </pre>
868
     * 
869
     */ 
870
    public void setForceFormulaRecalculation(boolean value) {
871
        // TODO Auto-generated method stub
872
        
873
    }
874
875
    /**
876
     * <pre>
877
     * Will be supported in the future.
878
     * </pre>
879
     * 
880
     */     
881
    public boolean getForceFormulaRecalculation() {
882
        // TODO Auto-generated method stub
883
        return false;
884
    }
885
886
    /**
887
     * <pre>
888
     * Will be supported in the future.
889
     * </pre>
890
     * 
891
     */ 
892
    public SpreadsheetVersion getSpreadsheetVersion() {
893
        // TODO Auto-generated method stub
894
        return null;
895
    }
896
897
898
    /**
899
     * <pre>
900
     * Will be supported in the future.
901
     * </pre>
902
     * 
903
     */ 
904
    public Iterator<Sheet> iterator() {
905
        return null;
906
    }
907
}
(-)a/src/ooxml/testcases/org/apache/poi/xssf/streaming/reader/TestStreamedWorkbook.java (+485 lines)
Line 0 Link Here
1
/*
2
 *  ====================================================================
3
 *    Licensed to the Apache Software Foundation (ASF) under one or more
4
 *    contributor license agreements.  See the NOTICE file distributed with
5
 *    this work for additional information regarding copyright ownership.
6
 *    The ASF licenses this file to You under the Apache License, Version 2.0
7
 *    (the "License"); you may not use this file except in compliance with
8
 *    the License.  You may obtain a copy of the License at
9
 *
10
 *        http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 *    Unless required by applicable law or agreed to in writing, software
13
 *    distributed under the License is distributed on an "AS IS" BASIS,
14
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 *    See the License for the specific language governing permissions and
16
 *    limitations under the License.
17
 * ====================================================================
18
 */
19
package org.apache.poi.xssf.streaming.reader;
20
21
import static org.junit.Assert.assertEquals;
22
import static org.junit.Assert.fail;
23
24
import java.io.File;
25
import java.util.Iterator;
26
27
import org.apache.poi.POIDataSamples;
28
import org.junit.Test;
29
30
31
public class TestStreamedWorkbook{
32
33
   @Test
34
    public void testInvalidFilePath() throws Exception{
35
       StreamedWorkbook workbook = null;
36
       try {
37
            workbook = new StreamedWorkbook(null);
38
            fail("expected exception");
39
        } catch (Exception e) {
40
            assertEquals("No sheets found", e.getMessage());
41
        }
42
        
43
        if(workbook != null)workbook.close();
44
        
45
    }
46
    
47
    @Test
48
    public void testInvalidFile() throws Exception{ 
49
        POIDataSamples files = POIDataSamples.getSpreadSheetInstance();
50
        File f= files.getFile("InvalidFile.txt");
51
        StreamedWorkbook workbook = null;
52
        try {
53
        
54
            workbook = new StreamedWorkbook(f.getAbsolutePath());
55
            workbook.getSheetIterator();
56
        } catch (Exception e) {
57
            assertEquals("No valid entries or contents found, this is not a valid OOXML (Office Open XML) file", e.getMessage());
58
        }
59
        
60
        if(workbook != null)workbook.close();
61
    }
62
    
63
   
64
    @Test
65
    public void testSheetCount() throws Exception{
66
        POIDataSamples files = POIDataSamples.getSpreadSheetInstance();
67
        File f= files.getFile("SpreadSheetSample04022017.xlsx");
68
        StreamedWorkbook workbook = new StreamedWorkbook(f.getAbsolutePath());
69
        int count = 0;
70
        
71
        try {
72
            Iterator<StreamedSheet> sheetIterator = workbook.getSheetIterator();
73
            
74
            while(sheetIterator.hasNext()){
75
                sheetIterator.next();
76
                count++;
77
            }
78
            
79
            assertEquals(2, count);
80
        } catch (Exception e) {
81
            
82
        }
83
        
84
        workbook.close();
85
       
86
    }
87
    
88
    @Test
89
    public void testTotalNumberOfSheets() throws Exception{
90
        POIDataSamples files = POIDataSamples.getSpreadSheetInstance();
91
        File f= files.getFile("SpreadSheetSample04022017.xlsx");
92
        StreamedWorkbook workbook = new StreamedWorkbook(f.getAbsolutePath());
93
        
94
        try {
95
            int sheetCount = workbook.getNumberOfSheets();
96
            
97
            assertEquals(2, sheetCount);
98
        } catch (Exception e) {
99
        }
100
       
101
        workbook.close();
102
    }
103
    
104
/*    @Test
105
    public void testSheetAt() throws Exception{         //Not supported <TO DO>
106
        POIDataSamples files = POIDataSamples.getSpreadSheetInstance();
107
        File f= files.getFile("SpreadSheetSample04022017.xlsx");
108
        StreamedWorkbook workbook = new StreamedWorkbook(f.getAbsolutePath());
109
        int sheetIndex = 0;
110
        
111
        StreamedSheet sheet = (StreamedSheet)workbook.getSheetAt(sheetIndex);
112
        
113
        assertEquals(sheetIndex, sheet.getSheetNumber());
114
        
115
        sheetIndex = 1;
116
        
117
        sheet = (StreamedSheet)workbook.getSheetAt(sheetIndex);
118
        System.out.println(sheet.getSheetNumber());
119
        
120
        assertEquals(sheetIndex, sheet.getSheetNumber());
121
        
122
        workbook.close();
123
        
124
    }*/
125
    
126
    @Test
127
    public void testTotalRowCount() throws Exception{
128
        POIDataSamples files = POIDataSamples.getSpreadSheetInstance();
129
        File f= files.getFile("SpreadSheetSample04022017.xlsx");
130
        StreamedWorkbook workbook = new StreamedWorkbook(f.getAbsolutePath());
131
        int sheetCount = 0;
132
        
133
        long count = 0;
134
        
135
        Iterator<StreamedSheet> sheetIterator = workbook.getSheetIterator();
136
        
137
        while((sheetIterator.hasNext()) && (sheetCount == 0)){
138
            StreamedSheet sheet = sheetIterator.next();
139
            
140
            Iterator<StreamedRow> rows = sheet.getAllRows();
141
            
142
            while(rows.hasNext()){
143
                rows.next();
144
                count++;
145
            }
146
            
147
            assertEquals(6, count);
148
            sheetCount++;
149
        }
150
        
151
        workbook.close();
152
    }
153
    
154
    @Test
155
    public void testNRowCount() throws Exception{
156
        POIDataSamples files = POIDataSamples.getSpreadSheetInstance();
157
        File f= files.getFile("SpreadSheetSample04022017.xlsx");
158
        StreamedWorkbook workbook = new StreamedWorkbook(f.getAbsolutePath());
159
        
160
        long count = 0;
161
        int sheetCount = 0;
162
        
163
        Iterator<StreamedSheet> sheetIterator = workbook.getSheetIterator();
164
        
165
        while((sheetIterator.hasNext()) && (sheetCount == 0)){
166
            StreamedSheet sheet = sheetIterator.next();
167
            
168
            Iterator<StreamedRow> rows = sheet.getNRows(4);
169
            
170
            while(rows.hasNext()){
171
                rows.next();
172
                count++;
173
            }
174
            
175
            assertEquals(4, count);
176
            sheetCount++;
177
        }
178
        
179
        workbook.close();
180
    }
181
    
182
    @Test
183
    public void testCellCount() throws Exception{
184
        POIDataSamples files = POIDataSamples.getSpreadSheetInstance();
185
        File f= files.getFile("SpreadSheetSample04022017.xlsx");
186
        StreamedWorkbook workbook = new StreamedWorkbook(f.getAbsolutePath());
187
        int sheetCount = 0;
188
        
189
        Iterator<StreamedSheet> sheetIterator = workbook.getSheetIterator();
190
        
191
        while((sheetIterator.hasNext()) && (sheetCount == 0)){
192
            StreamedSheet sheet = sheetIterator.next();
193
            
194
            Iterator<StreamedRow> rows = sheet.getAllRows();
195
            
196
            while(rows.hasNext()){
197
                long count = 0;
198
                StreamedRow row = rows.next();
199
                
200
                Iterator<StreamedCell> cellIterator = row.getCellIterator();
201
                
202
                while(cellIterator.hasNext()){
203
                    cellIterator.next();
204
                    count++;
205
                }
206
                
207
                assertEquals(7, count);
208
            }
209
            
210
            sheetCount++;
211
        }
212
        
213
        workbook.close();
214
    }
215
    
216
    
217
    @Test
218
    public void testStartingRowAndCellNumber() throws Exception{
219
        POIDataSamples files = POIDataSamples.getSpreadSheetInstance();
220
        File f= files.getFile("SpreadSheetSample04022017.xlsx");
221
        StreamedWorkbook workbook = new StreamedWorkbook(f.getAbsolutePath());
222
        int sheetCount = 0;
223
        
224
        
225
        Iterator<StreamedSheet> sheetIterator = workbook.getSheetIterator();
226
        
227
        while((sheetIterator.hasNext()) && (sheetCount == 0)){
228
            int rowCount = 0;
229
            int cellCount = 0;
230
            StreamedSheet sheet = sheetIterator.next();
231
            
232
            Iterator<StreamedRow> rows = sheet.getAllRows();
233
            
234
            while(rows.hasNext()){
235
                StreamedRow row = rows.next();
236
                if(rowCount == 0){
237
                    assertEquals(rowCount, row.getRowNum());
238
                }
239
                rowCount++;
240
                
241
                
242
                Iterator<StreamedCell> cellIterator = row.getCellIterator();
243
                
244
                while(cellIterator.hasNext()){
245
                    StreamedCell cell = cellIterator.next();
246
                    if(cellCount == 0){
247
                        assertEquals(cellCount, cell.getCellNumber());
248
                    }
249
                    cellCount++;
250
                    
251
                }
252
                
253
            }
254
            
255
            sheetCount++;
256
        }
257
        
258
        workbook.close();
259
    }
260
    
261
    @Test
262
    public void testSheetData() throws Exception{
263
        POIDataSamples files = POIDataSamples.getSpreadSheetInstance();
264
        File f= files.getFile("SpreadSheetSample04022017.xlsx");
265
        StreamedWorkbook workbook = new StreamedWorkbook(f.getAbsolutePath());
266
        int sheetCount = 0;
267
        
268
        
269
        Iterator<StreamedSheet> sheetIterator = workbook.getSheetIterator();
270
        
271
        while((sheetIterator.hasNext()) && (sheetCount == 0)){
272
            int rowCount = 0;
273
            
274
            StreamedSheet sheet = sheetIterator.next();
275
            
276
            Iterator<StreamedRow> rows = sheet.getAllRows();
277
            
278
            while(rows.hasNext()){
279
                
280
                StreamedRow row = rows.next();
281
                
282
                int cellCount = 0;
283
                
284
                Iterator<StreamedCell> cellIterator = row.getCellIterator();
285
                
286
                while(cellIterator.hasNext()){
287
                    StreamedCell cell = cellIterator.next();
288
                    if(rowCount == 1){
289
                        
290
                        if(cellCount == 0){
291
                            assertEquals("1", cell.getValue());
292
                        }else if(cellCount == 1){
293
                            assertEquals("Item1", cell.getValue());
294
                        }else if(cellCount == 2){
295
                            assertEquals("201", cell.getValue());
296
                        }else if(cellCount == 3){
297
                            assertEquals("100.11", cell.getValue());
298
                        }else if(cellCount == 4){
299
                            assertEquals("TRUE", cell.getValue());
300
                        }else if(cellCount == 5){
301
                            assertEquals("04/02/1917", cell.getValue());
302
                        }else if(cellCount == 6){
303
                            assertEquals("90.11", cell.getValue());
304
                        }
305
                    }else if(rowCount == 3){
306
                        if(cellCount == 4){
307
                            assertEquals(null, cell.getValue());
308
                        }
309
                    }
310
                    
311
                    cellCount++;
312
                    
313
                }
314
                
315
                
316
                rowCount++;
317
                
318
            }
319
            
320
            sheetCount++;
321
        }
322
        
323
        workbook.close();
324
    }
325
    
326
    @Test
327
    public void testBatchData() throws Exception{
328
        
329
        POIDataSamples files = POIDataSamples.getSpreadSheetInstance();
330
        File f= files.getFile("SpreadSheetSample04022017.xlsx");
331
        StreamedWorkbook workbook = new StreamedWorkbook(f.getAbsolutePath());
332
        int sheetCount = 0;
333
        
334
        
335
        Iterator<StreamedSheet> sheetIterator = workbook.getSheetIterator();
336
        
337
        while(sheetIterator.hasNext()){
338
            
339
            StreamedSheet sheet = sheetIterator.next();
340
            
341
            if(sheetCount == 1){
342
                Iterator<StreamedRow> rows = sheet.getNRows(1);
343
                
344
                while(rows.hasNext()){
345
                    StreamedRow row = rows.next();
346
                    assertEquals("Row Number:0 --> Item | item description | Strore | Price | Promotion applied | MFD | Discount rate |", row.toString().trim());
347
                }
348
                
349
                
350
                rows = sheet.getNRows(4);
351
                
352
                while(rows.hasNext()){
353
                    StreamedRow row = rows.next();
354
                    assertEquals("Row Number:1 --> 1 | Item1 | 201 | 100.11 | TRUE | 04/02/1917 | 90.11 |", row.toString().trim());
355
                    row = rows.next();
356
                    assertEquals("Row Number:2 --> 2 | Item2 | 202 | 101.11 | TRUE | 05/02/1917 | 91.11 |", row.toString().trim());
357
                    row = rows.next();
358
                    assertEquals("Row Number:3 --> 3 | Item3 | 203 | 102.11 | TRUE | 06/02/1917 | 92.11 |", row.toString().trim());
359
                    row = rows.next();
360
                    assertEquals("Row Number:4 --> 4 | Item4 | 204 | 103.11 | TRUE | 07/02/1917 | 93.11 |", row.toString().trim());
361
                }
362
                
363
                
364
                
365
                rows = sheet.getNRows(4);
366
                
367
                while(rows.hasNext()){
368
                    StreamedRow row = rows.next();
369
                    assertEquals("Row Number:5 --> 5 | Item5 | 205 | 104.11 | TRUE | 08/02/1917 | 94.11 |", row.toString().trim());
370
                    row = rows.next();
371
                    assertEquals("Row Number:6 --> 6 | Item6 | 206 | 105.11 | TRUE | 09/02/1917 | 95.11 |", row.toString().trim());
372
                    row = rows.next();
373
                    assertEquals("Row Number:7 --> 7 | Item7 | 207 | 106.11 | TRUE | 10/02/1917 | 96.11 |", row.toString().trim());
374
                    row = rows.next();
375
                    assertEquals("Row Number:8 --> 8 | Item8 | 208 | 107.11 | FALSE | 11/02/1917 | 97.11 |", row.toString().trim());
376
                }
377
                
378
            }
379
            
380
381
            sheetCount++;
382
            
383
        }
384
        
385
        workbook.close();
386
        
387
    }
388
    
389
    
390
    @Test
391
    public void testGetCell() throws Exception{
392
        POIDataSamples files = POIDataSamples.getSpreadSheetInstance();
393
        File f= files.getFile("SpreadSheetSample04022017.xlsx");
394
        StreamedWorkbook workbook = new StreamedWorkbook(f.getAbsolutePath());
395
        int sheetCount = 0;
396
        
397
        
398
        Iterator<StreamedSheet> sheetIterator = workbook.getSheetIterator();
399
        
400
        while((sheetIterator.hasNext()) && (sheetCount == 0)){
401
            
402
            StreamedSheet sheet = sheetIterator.next();
403
            
404
            Iterator<StreamedRow> rows = sheet.getAllRows();
405
            
406
            while(rows.hasNext()){
407
                
408
                StreamedRow row = rows.next();
409
                
410
                if(row.getRowNum() == 3){
411
                    StreamedCell cell = (StreamedCell)row.getCell(5);
412
                    assertEquals("06/02/1917", cell.getValue());
413
                    
414
                    try{
415
                        cell = (StreamedCell)row.getCell(10);
416
                    }catch(Exception exception){
417
                        assertEquals(true, exception instanceof IndexOutOfBoundsException);
418
                    }
419
                }
420
                
421
422
            }
423
            
424
            sheetCount++;
425
        }
426
    }
427
    
428
    
429
    @Test
430
    public void testGetFirstAndLastCellNum() throws Exception{
431
        POIDataSamples files = POIDataSamples.getSpreadSheetInstance();
432
        File f= files.getFile("SpreadSheetSample04022017.xlsx");
433
        StreamedWorkbook workbook = new StreamedWorkbook(f.getAbsolutePath());
434
        int sheetCount = 0;
435
        
436
        
437
        Iterator<StreamedSheet> sheetIterator = workbook.getSheetIterator();
438
        
439
        while((sheetIterator.hasNext()) && (sheetCount == 0)){
440
            
441
            StreamedSheet sheet = sheetIterator.next();
442
            
443
            Iterator<StreamedRow> rows = sheet.getAllRows();
444
            
445
            while(rows.hasNext()){
446
                
447
                StreamedRow row = rows.next();
448
                
449
                if(row.getRowNum() == 3){
450
                    StreamedCell cell = (StreamedCell)row.getCell(row.getFirstCellNum());
451
                    assertEquals("3", cell.getValue());
452
                    assertEquals("3", cell.getStringCellValue());
453
                    
454
                    cell = (StreamedCell)row.getCell(row.getLastCellNum());
455
                    assertEquals("92.11", cell.getValue());
456
                    assertEquals("92.11", cell.getStringCellValue());
457
                }
458
            }
459
            
460
            sheetCount++;
461
        }
462
    }
463
    
464
    @Test
465
    public void testReopenClosedWorkbook(){
466
        
467
        try{
468
            POIDataSamples files = POIDataSamples.getSpreadSheetInstance();
469
            File f= files.getFile("SpreadSheetSample04022017.xlsx");
470
            StreamedWorkbook workbook = new StreamedWorkbook(f.getAbsolutePath());
471
            
472
            workbook.close();
473
            
474
            Iterator<StreamedSheet> sheetIterator = workbook.getSheetIterator();
475
        }catch(Exception e){
476
            assertEquals("Workbook already closed", e.getMessage());
477
        }
478
            
479
480
    }
481
    
482
    
483
    
484
    
485
}

Return to bug 60707