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

(-)src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java (-17 / +136 lines)
Lines 16-32 Link Here
16
==================================================================== */
16
==================================================================== */
17
package org.apache.poi.xssf.eventusermodel;
17
package org.apache.poi.xssf.eventusermodel;
18
18
19
import java.util.Comparator;
20
import java.util.LinkedList;
21
import java.util.List;
22
import java.util.Queue;
23
24
import org.apache.poi.hssf.util.CellReference;
19
import org.apache.poi.ss.usermodel.BuiltinFormats;
25
import org.apache.poi.ss.usermodel.BuiltinFormats;
20
import org.apache.poi.ss.usermodel.DataFormatter;
26
import org.apache.poi.ss.usermodel.DataFormatter;
27
import org.apache.poi.xssf.model.CommentsTable;
21
import org.apache.poi.xssf.model.StylesTable;
28
import org.apache.poi.xssf.model.StylesTable;
22
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
29
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
30
import org.apache.poi.xssf.usermodel.XSSFComment;
23
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
31
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
32
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
24
import org.xml.sax.Attributes;
33
import org.xml.sax.Attributes;
25
import org.xml.sax.SAXException;
34
import org.xml.sax.SAXException;
26
import org.xml.sax.helpers.DefaultHandler;
35
import org.xml.sax.helpers.DefaultHandler;
27
36
28
/**
37
/**
29
 * This class handles the processing of a sheet#.xml 
38
 * This class handles the processing of a sheet#.xml
30
 *  sheet part of a XSSF .xlsx file, and generates
39
 *  sheet part of a XSSF .xlsx file, and generates
31
 *  row and cell events for it.
40
 *  row and cell events for it.
32
 */
41
 */
Lines 44-55 Link Here
44
       SST_STRING,
53
       SST_STRING,
45
       NUMBER,
54
       NUMBER,
46
   }
55
   }
47
   
56
48
   /**
57
   /**
49
    * Table with the styles used for formatting
58
    * Table with the styles used for formatting
50
    */
59
    */
51
   private StylesTable stylesTable;
60
   private StylesTable stylesTable;
52
61
62
    /**
63
     * Table with cell comments
64
     */
65
    private CommentsTable commentsTable;
66
53
   private ReadOnlySharedStringsTable sharedStringsTable;
67
   private ReadOnlySharedStringsTable sharedStringsTable;
54
68
55
   /**
69
   /**
Lines 74-79 Link Here
74
   private short formatIndex;
88
   private short formatIndex;
75
   private String formatString;
89
   private String formatString;
76
   private final DataFormatter formatter;
90
   private final DataFormatter formatter;
91
   private int rowNum;
77
   private String cellRef;
92
   private String cellRef;
78
   private boolean formulasNotResults;
93
   private boolean formulasNotResults;
79
94
Lines 82-87 Link Here
82
   private StringBuffer formula = new StringBuffer();
97
   private StringBuffer formula = new StringBuffer();
83
   private StringBuffer headerFooter = new StringBuffer();
98
   private StringBuffer headerFooter = new StringBuffer();
84
99
100
    private Queue<CellReference> commentCellRefs;
101
102
    private static final Comparator<CellReference> cellRefComparator = new Comparator<CellReference>() {
103
        @Override
104
        public int compare(CellReference o1, CellReference o2) {
105
            int result = compare(o1.getRow(), o2.getRow());
106
            if (result == 0) {
107
                result = compare(o1.getCol(), o2.getCol());
108
            }
109
            return result;
110
        }
111
112
        public int compare(int x, int y) {
113
            return (x < y) ? -1 : ((x == y) ? 0 : 1);
114
        }
115
    };
116
85
   /**
117
   /**
86
    * Accepts objects needed while parsing.
118
    * Accepts objects needed while parsing.
87
    *
119
    *
Lines 90-105 Link Here
90
    */
122
    */
91
   public XSSFSheetXMLHandler(
123
   public XSSFSheetXMLHandler(
92
           StylesTable styles,
124
           StylesTable styles,
125
           CommentsTable comments,
93
           ReadOnlySharedStringsTable strings,
126
           ReadOnlySharedStringsTable strings,
94
           SheetContentsHandler sheetContentsHandler,
127
           SheetContentsHandler sheetContentsHandler,
95
           DataFormatter dataFormatter,
128
           DataFormatter dataFormatter,
96
           boolean formulasNotResults) {
129
           boolean formulasNotResults) {
97
       this.stylesTable = styles;
130
       this.stylesTable = styles;
131
       this.commentsTable = comments;
98
       this.sharedStringsTable = strings;
132
       this.sharedStringsTable = strings;
99
       this.output = sheetContentsHandler;
133
       this.output = sheetContentsHandler;
100
       this.formulasNotResults = formulasNotResults;
134
       this.formulasNotResults = formulasNotResults;
101
       this.nextDataType = xssfDataType.NUMBER;
135
       this.nextDataType = xssfDataType.NUMBER;
102
       this.formatter = dataFormatter;
136
       this.formatter = dataFormatter;
137
138
       if (commentsTable != null) {
139
           List<CTComment> commentList = commentsTable.getCTComments().getCommentList().getCommentList();
140
           commentCellRefs = new LinkedList<CellReference>();
141
           for (CTComment comment : commentList) {
142
               commentCellRefs.add(new CellReference(comment.getRef()));
143
           }
144
       }
103
   }
145
   }
104
   /**
146
   /**
105
    * Accepts objects needed while parsing.
147
    * Accepts objects needed while parsing.
Lines 109-118 Link Here
109
    */
151
    */
110
   public XSSFSheetXMLHandler(
152
   public XSSFSheetXMLHandler(
111
           StylesTable styles,
153
           StylesTable styles,
154
           CommentsTable comments,
112
           ReadOnlySharedStringsTable strings,
155
           ReadOnlySharedStringsTable strings,
113
           SheetContentsHandler sheetContentsHandler,
156
           SheetContentsHandler sheetContentsHandler,
114
           boolean formulasNotResults) {
157
           boolean formulasNotResults) {
115
       this(styles, strings, sheetContentsHandler, new DataFormatter(), formulasNotResults);
158
       this(styles, comments, strings, sheetContentsHandler, new DataFormatter(), formulasNotResults);
116
   }
159
   }
117
160
118
   private boolean isTextTag(String name) {
161
   private boolean isTextTag(String name) {
Lines 131-137 Link Here
131
      // It isn't a text tag
174
      // It isn't a text tag
132
      return false;
175
      return false;
133
   }
176
   }
134
   
177
135
   @Override
178
   @Override
136
   public void startElement(String uri, String localName, String name,
179
   public void startElement(String uri, String localName, String name,
137
                            Attributes attributes) throws SAXException {
180
                            Attributes attributes) throws SAXException {
Lines 146-171 Link Here
146
       } else if ("f".equals(name)) {
189
       } else if ("f".equals(name)) {
147
          // Clear contents cache
190
          // Clear contents cache
148
          formula.setLength(0);
191
          formula.setLength(0);
149
          
192
150
          // Mark us as being a formula if not already
193
          // Mark us as being a formula if not already
151
          if(nextDataType == xssfDataType.NUMBER) {
194
          if(nextDataType == xssfDataType.NUMBER) {
152
             nextDataType = xssfDataType.FORMULA;
195
             nextDataType = xssfDataType.FORMULA;
153
          }
196
          }
154
          
197
155
          // Decide where to get the formula string from
198
          // Decide where to get the formula string from
156
          String type = attributes.getValue("t");
199
          String type = attributes.getValue("t");
157
          if(type != null && type.equals("shared")) {
200
          if(type != null && type.equals("shared")) {
158
             // Is it the one that defines the shared, or uses it?
201
             // Is it the one that defines the shared, or uses it?
159
             String ref = attributes.getValue("ref");
202
             String ref = attributes.getValue("ref");
160
             String si = attributes.getValue("si");
203
             String si = attributes.getValue("si");
161
             
204
162
             if(ref != null) {
205
             if(ref != null) {
163
                // This one defines it
206
                // This one defines it
164
                // TODO Save it somewhere
207
                // TODO Save it somewhere
165
                fIsOpen = true;
208
                fIsOpen = true;
166
             } else {
209
             } else {
167
                // This one uses a shared formula
210
                // This one uses a shared formula
168
                // TODO Retrieve the shared formula and tweak it to 
211
                // TODO Retrieve the shared formula and tweak it to
169
                //  match the current cell
212
                //  match the current cell
170
                if(formulasNotResults) {
213
                if(formulasNotResults) {
171
                   System.err.println("Warning - shared formulas not yet supported!");
214
                   System.err.println("Warning - shared formulas not yet supported!");
Lines 186-192 Link Here
186
          headerFooter.setLength(0);
229
          headerFooter.setLength(0);
187
       }
230
       }
188
       else if("row".equals(name)) {
231
       else if("row".equals(name)) {
189
           int rowNum = Integer.parseInt(attributes.getValue("r")) - 1;
232
           rowNum = Integer.parseInt(attributes.getValue("r")) - 1;
190
           output.startRow(rowNum);
233
           output.startRow(rowNum);
191
       }
234
       }
192
       // c => cell
235
       // c => cell
Lines 235-241 Link Here
235
       // v => contents of a cell
278
       // v => contents of a cell
236
       if (isTextTag(name)) {
279
       if (isTextTag(name)) {
237
           vIsOpen = false;
280
           vIsOpen = false;
238
           
281
239
           // Process the value contents as required, now we have it all
282
           // Process the value contents as required, now we have it all
240
           switch (nextDataType) {
283
           switch (nextDataType) {
241
               case BOOLEAN:
284
               case BOOLEAN:
Lines 252-258 Link Here
252
                      thisStr = formula.toString();
295
                      thisStr = formula.toString();
253
                   } else {
296
                   } else {
254
                      String fv = value.toString();
297
                      String fv = value.toString();
255
                      
298
256
                      if (this.formatString != null) {
299
                      if (this.formatString != null) {
257
                         try {
300
                         try {
258
                            // Try to use the value as a formattable number
301
                            // Try to use the value as a formattable number
Lines 299-313 Link Here
299
                   thisStr = "(TODO: Unexpected type: " + nextDataType + ")";
342
                   thisStr = "(TODO: Unexpected type: " + nextDataType + ")";
300
                   break;
343
                   break;
301
           }
344
           }
302
           
345
303
           // Output
346
           // Output
304
           output.cell(cellRef, thisStr);
347
           checkForEmptyCellComments(EmptyCellCommentsCheckType.CELL);
348
           XSSFComment comment = commentsTable != null ? commentsTable.findCellComment(cellRef) : null;
349
           output.cell(cellRef, thisStr, comment);
305
       } else if ("f".equals(name)) {
350
       } else if ("f".equals(name)) {
306
          fIsOpen = false;
351
          fIsOpen = false;
307
       } else if ("is".equals(name)) {
352
       } else if ("is".equals(name)) {
308
          isIsOpen = false;
353
          isIsOpen = false;
309
       } else if ("row".equals(name)) {
354
       } else if ("row".equals(name)) {
310
          output.endRow();
355
           checkForEmptyCellComments(EmptyCellCommentsCheckType.END_OF_ROW);
356
           output.endRow(rowNum);
357
       } else if ("sheetData".equals(name)) {
358
           checkForEmptyCellComments(EmptyCellCommentsCheckType.END_OF_SHEET_DATA);
311
       }
359
       }
312
       else if("oddHeader".equals(name) || "evenHeader".equals(name) ||
360
       else if("oddHeader".equals(name) || "evenHeader".equals(name) ||
313
             "firstHeader".equals(name)) {
361
             "firstHeader".equals(name)) {
Lines 321-326 Link Here
321
       }
369
       }
322
   }
370
   }
323
371
372
    private enum EmptyCellCommentsCheckType {
373
        CELL,
374
        END_OF_ROW,
375
        END_OF_SHEET_DATA
376
    }
377
378
    /**
379
     * Do a check for, and output, comments in otherwise empty cells.
380
     */
381
    private void checkForEmptyCellComments(EmptyCellCommentsCheckType type) {
382
        if (commentCellRefs != null && !commentCellRefs.isEmpty()) {
383
            // we've reached the end of the sheet data; output any comments we haven't yet outputted
384
            if (type == EmptyCellCommentsCheckType.END_OF_SHEET_DATA) {
385
                while (!commentCellRefs.isEmpty()) {
386
                    outputEmptyCellComment(commentCellRefs.remove());
387
                }
388
                return;
389
            }
390
391
            // handle any empty cells with comments in rows prior to any rows with content in them
392
            if (this.cellRef == null) {
393
                if (type == EmptyCellCommentsCheckType.END_OF_ROW) {
394
                    while (!commentCellRefs.isEmpty()) {
395
                        if (commentCellRefs.peek().getRow() == rowNum) {
396
                            outputEmptyCellComment(commentCellRefs.remove());
397
                        } else {
398
                            return;
399
                        }
400
                    }
401
                    return;
402
                } else {
403
                    throw new IllegalStateException("Cell ref should be null only if there are only empty cells in the row; rowNum: " + rowNum);
404
                }
405
            }
406
407
            CellReference nextCommentCellRef;
408
            do {
409
                CellReference cellRef = new CellReference(this.cellRef);
410
                CellReference peekCellRef = commentCellRefs.peek();
411
                if (type == EmptyCellCommentsCheckType.CELL && cellRef.equals(peekCellRef)) {
412
                    // remove the comment cell ref from the list if we're about to handle it alongside the cell content
413
                    commentCellRefs.remove();
414
                    return;
415
                } else {
416
                    // fill in any gaps if there are empty cells with comment mixed in with non-empty cells
417
                    int comparison = cellRefComparator.compare(peekCellRef, cellRef);
418
                    if (comparison > 0 && type == EmptyCellCommentsCheckType.END_OF_ROW && peekCellRef.getRow() <= rowNum) {
419
                        nextCommentCellRef = commentCellRefs.remove();
420
                        outputEmptyCellComment(nextCommentCellRef);
421
                    } else if (comparison < 0 && type == EmptyCellCommentsCheckType.CELL && peekCellRef.getRow() <= rowNum) {
422
                        nextCommentCellRef = commentCellRefs.remove();
423
                        outputEmptyCellComment(nextCommentCellRef);
424
                    } else {
425
                        nextCommentCellRef = null;
426
                    }
427
                }
428
            } while (nextCommentCellRef != null && !commentCellRefs.isEmpty());
429
        }
430
    }
431
432
    /**
433
     * Output an empty-cell comment.
434
     */
435
    private void outputEmptyCellComment(CellReference cellRef) {
436
        String cellRefString = cellRef.formatAsString();
437
        XSSFComment comment = commentsTable.findCellComment(cellRefString);
438
        output.emptyCellComment(cellRefString, comment);
439
    }
440
324
   /**
441
   /**
325
    * Captures characters only if a suitable element is open.
442
    * Captures characters only if a suitable element is open.
326
    * Originally was just "v"; extended for inlineStr also.
443
    * Originally was just "v"; extended for inlineStr also.
Lines 347-355 Link Here
347
      /** A row with the (zero based) row number has started */
464
      /** A row with the (zero based) row number has started */
348
      public void startRow(int rowNum);
465
      public void startRow(int rowNum);
349
      /** A row with the (zero based) row number has ended */
466
      /** A row with the (zero based) row number has ended */
350
      public void endRow();
467
      public void endRow(int rowNum);
351
      /** A cell, with the given formatted value, was encountered */
468
      /** A cell, with the given formatted value and, possibly-null, comment, was encountered */
352
      public void cell(String cellReference, String formattedValue);
469
      public void cell(String cellReference, String formattedValue, XSSFComment comment);
470
      /** A comment for an otherwise-empty cell was encountered */
471
      public void emptyCellComment(String cellReference, XSSFComment comment);
353
      /** A header or footer has been encountered */
472
      /** A header or footer has been encountered */
354
      public void headerFooter(String text, boolean isHeader, String tagName);
473
      public void headerFooter(String text, boolean isHeader, String tagName);
355
   }
474
   }
(-)src/ooxml/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java (-9 / +27 lines)
Lines 39-45 Link Here
39
import org.apache.poi.xssf.eventusermodel.XSSFReader;
39
import org.apache.poi.xssf.eventusermodel.XSSFReader;
40
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
40
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
41
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;
41
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;
42
import org.apache.poi.xssf.model.CommentsTable;
42
import org.apache.poi.xssf.model.StylesTable;
43
import org.apache.poi.xssf.model.StylesTable;
44
import org.apache.poi.xssf.usermodel.XSSFComment;
43
import org.apache.poi.xssf.usermodel.XSSFShape;
45
import org.apache.poi.xssf.usermodel.XSSFShape;
44
import org.apache.poi.xssf.usermodel.XSSFSimpleShape;
46
import org.apache.poi.xssf.usermodel.XSSFSimpleShape;
45
import org.apache.xmlbeans.XmlException;
47
import org.apache.xmlbeans.XmlException;
Lines 60-65 Link Here
60
    private Locale locale;
62
    private Locale locale;
61
    private boolean includeTextBoxes = true;
63
    private boolean includeTextBoxes = true;
62
    private boolean includeSheetNames = true;
64
    private boolean includeSheetNames = true;
65
    private boolean includeCellComments = false;
63
    private boolean includeHeadersFooters = true;
66
    private boolean includeHeadersFooters = true;
64
    private boolean formulasNotResults = false;
67
    private boolean formulasNotResults = false;
65
68
Lines 112-122 Link Here
112
    }
115
    }
113
116
114
    /**
117
    /**
115
     * Would control the inclusion of cell comments from the document,
118
     * Should cell comments be included? Default is false
116
     *  if we supported it
117
     */
119
     */
118
    public void setIncludeCellComments(boolean includeCellComments) {
120
    public void setIncludeCellComments(boolean includeCellComments) {
119
        throw new IllegalStateException("Comment extraction not supported in streaming mode, please use XSSFExcelExtractor");
121
        this.includeCellComments = includeCellComments;
120
    }
122
    }
121
123
122
    public void setLocale(Locale locale) {
124
    public void setLocale(Locale locale) {
Lines 159-164 Link Here
159
    public void processSheet(
161
    public void processSheet(
160
            SheetContentsHandler sheetContentsExtractor,
162
            SheetContentsHandler sheetContentsExtractor,
161
            StylesTable styles,
163
            StylesTable styles,
164
            CommentsTable comments,
162
            ReadOnlySharedStringsTable strings,
165
            ReadOnlySharedStringsTable strings,
163
            InputStream sheetInputStream)
166
            InputStream sheetInputStream)
164
            throws IOException, SAXException {
167
            throws IOException, SAXException {
Lines 176-182 Link Here
176
          SAXParser saxParser = saxFactory.newSAXParser();
179
          SAXParser saxParser = saxFactory.newSAXParser();
177
          XMLReader sheetParser = saxParser.getXMLReader();
180
          XMLReader sheetParser = saxParser.getXMLReader();
178
          ContentHandler handler = new XSSFSheetXMLHandler(
181
          ContentHandler handler = new XSSFSheetXMLHandler(
179
                styles, strings, sheetContentsExtractor, formatter, formulasNotResults);
182
                styles, comments, strings, sheetContentsExtractor, formatter, formulasNotResults);
180
          sheetParser.setContentHandler(handler);
183
          sheetParser.setContentHandler(handler);
181
          sheetParser.parse(sheetSource);
184
          sheetParser.parse(sheetSource);
182
       } catch(ParserConfigurationException e) {
185
       } catch(ParserConfigurationException e) {
Lines 203-209 Link Here
203
                 text.append(iter.getSheetName());
206
                 text.append(iter.getSheetName());
204
                 text.append('\n');
207
                 text.append('\n');
205
              }
208
              }
206
              processSheet(sheetExtractor, styles, strings, stream);
209
              CommentsTable comments = includeCellComments ? iter.getSheetComments() : null;
210
              processSheet(sheetExtractor, styles, comments, strings, stream);
207
              if (includeHeadersFooters) {
211
              if (includeHeadersFooters) {
208
                  sheetExtractor.appendHeaderText(text);
212
                  sheetExtractor.appendHeaderText(text);
209
              }
213
              }
Lines 268-293 Link Here
268
            firstCellOfRow = true;
272
            firstCellOfRow = true;
269
        }
273
        }
270
274
271
        public void endRow() {
275
        public void endRow(int rowNum) {
272
            output.append('\n');
276
            output.append('\n');
273
        }
277
        }
274
278
275
        public void cell(String cellRef, String formattedValue) {
279
        public void cell(String cellRef, String formattedValue, XSSFComment comment) {
276
            if(firstCellOfRow) {
280
            if(firstCellOfRow) {
277
                firstCellOfRow = false;
281
                firstCellOfRow = false;
278
            } else {
282
            } else {
279
                output.append('\t');
283
                output.append('\t');
280
            }
284
            }
281
            output.append(formattedValue);
285
            if (formattedValue != null) {
286
                output.append(formattedValue);
287
            }
288
            if (includeCellComments && comment != null) {
289
                String commentText = comment.getString().getString().replace('\n', ' ');
290
                output.append(formattedValue != null ? " Comment by " : "Comment by ");
291
                if (commentText.startsWith(comment.getAuthor() + ": ")) {
292
                    output.append(commentText);
293
                } else {
294
                    output.append(comment.getAuthor()).append(": ").append(commentText);
295
                }
296
            }
282
        }
297
        }
283
298
299
        public void emptyCellComment(String cellRef, XSSFComment comment) {
300
            cell(cellRef, null, comment);
301
        }
302
284
        public void headerFooter(String text, boolean isHeader, String tagName) {
303
        public void headerFooter(String text, boolean isHeader, String tagName) {
285
            if (headerFooterMap != null) {
304
            if (headerFooterMap != null) {
286
                headerFooterMap.put(tagName, text);
305
                headerFooterMap.put(tagName, text);
287
            }
306
            }
288
        }
307
        }
289
308
290
291
        /**
309
        /**
292
         * Append the text for the named header or footer if found.
310
         * Append the text for the named header or footer if found.
293
         */
311
         */
(-)src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFEventBasedExcelExtractor.java (-2 / +66 lines)
Lines 20-32 Link Here
20
import java.util.regex.Matcher;
20
import java.util.regex.Matcher;
21
import java.util.regex.Pattern;
21
import java.util.regex.Pattern;
22
22
23
import junit.framework.TestCase;
24
25
import org.apache.poi.POITextExtractor;
23
import org.apache.poi.POITextExtractor;
26
import org.apache.poi.hssf.HSSFTestDataSamples;
24
import org.apache.poi.hssf.HSSFTestDataSamples;
27
import org.apache.poi.hssf.extractor.ExcelExtractor;
25
import org.apache.poi.hssf.extractor.ExcelExtractor;
28
import org.apache.poi.xssf.XSSFTestDataSamples;
26
import org.apache.poi.xssf.XSSFTestDataSamples;
29
27
28
import junit.framework.TestCase;
29
30
/**
30
/**
31
 * Tests for {@link XSSFEventBasedExcelExtractor}
31
 * Tests for {@link XSSFEventBasedExcelExtractor}
32
 */
32
 */
Lines 240-243 Link Here
240
        fixture.setIncludeHeadersFooters(false);
240
        fixture.setIncludeHeadersFooters(false);
241
        assertEquals(expectedOutputWithoutHeadersAndFooters, fixture.getText());
241
        assertEquals(expectedOutputWithoutHeadersAndFooters, fixture.getText());
242
    }
242
    }
243
244
    /**
245
      * Test that XSSFEventBasedExcelExtractor outputs comments when specified.
246
      * The output will contain two improvements over the output from
247
     *  XSSFExcelExtractor in that (1) comments from empty cells will be
248
      * outputted, and (2) the author will not be outputted twice.
249
      * <p>
250
      * This test will need to be modified if these improvements are ported to
251
      * XSSFExcelExtractor.
252
      */
253
    public void testCommentsComparedToNonEventBasedExtractor()
254
        throws Exception {
255
256
        String expectedOutputWithoutComments =
257
                "Sheet1\n" +
258
                "\n" +
259
                "abc\n" +
260
                "\n" +
261
                "123\n" +
262
                "\n" +
263
                "\n" +
264
                "\n";
265
266
        String nonEventBasedExtractorOutputWithComments =
267
                "Sheet1\n" +
268
                "\n" +
269
                "abc Comment by Shaun Kalley: Shaun Kalley: Comment A2\n" +
270
                "\n" +
271
                "123 Comment by Shaun Kalley: Shaun Kalley: Comment B4\n" +
272
                "\n" +
273
                "\n" +
274
                "\n";
275
276
        String eventBasedExtractorOutputWithComments =
277
                "Sheet1\n" +
278
                "Comment by Shaun Kalley: Comment A1\tComment by Shaun Kalley: Comment B1\n" +
279
                "abc Comment by Shaun Kalley: Comment A2\tComment by Shaun Kalley: Comment B2\n" +
280
                "Comment by Shaun Kalley: Comment A3\tComment by Shaun Kalley: Comment B3\n" +
281
                "Comment by Shaun Kalley: Comment A4\t123 Comment by Shaun Kalley: Comment B4\n" +
282
                "Comment by Shaun Kalley: Comment A5\tComment by Shaun Kalley: Comment B5\n" +
283
                "Comment by Shaun Kalley: Comment A7\tComment by Shaun Kalley: Comment B7\n" +
284
                "Comment by Shaun Kalley: Comment A8\tComment by Shaun Kalley: Comment B8\n";
285
286
        XSSFExcelExtractor extractor = new XSSFExcelExtractor(
287
                XSSFTestDataSamples.openSampleWorkbook("commentTest.xlsx"));
288
        try {
289
            assertEquals(expectedOutputWithoutComments, extractor.getText());
290
            extractor.setIncludeCellComments(true);
291
            assertEquals(nonEventBasedExtractorOutputWithComments, extractor.getText());
292
        } finally {
293
            extractor.close();
294
        }
295
296
        XSSFEventBasedExcelExtractor fixture =
297
                new XSSFEventBasedExcelExtractor(
298
                        XSSFTestDataSamples.openSamplePackage("commentTest.xlsx"));
299
        try {
300
            assertEquals(expectedOutputWithoutComments, fixture.getText());
301
            fixture.setIncludeCellComments(true);
302
            assertEquals(eventBasedExtractorOutputWithComments, fixture.getText());
303
        } finally {
304
            fixture.close();
305
        }
306
    }
243
}
307
}

Return to bug 56023