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

(-)src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java (-22 / +25 lines)
Lines 37-43 Link Here
37
import org.xml.sax.helpers.DefaultHandler;
37
import org.xml.sax.helpers.DefaultHandler;
38
38
39
/**
39
/**
40
 * This class handles the processing of a sheet#.xml 
40
 * This class handles the processing of a sheet#.xml
41
 *  sheet part of a XSSF .xlsx file, and generates
41
 *  sheet part of a XSSF .xlsx file, and generates
42
 *  row and cell events for it.
42
 *  row and cell events for it.
43
 */
43
 */
Lines 57-63 Link Here
57
       SST_STRING,
57
       SST_STRING,
58
       NUMBER,
58
       NUMBER,
59
   }
59
   }
60
   
60
61
   /**
61
   /**
62
    * Table with the styles used for formatting
62
    * Table with the styles used for formatting
63
    */
63
    */
Lines 130-136 Link Here
130
       this.formatter = dataFormatter;
130
       this.formatter = dataFormatter;
131
       init();
131
       init();
132
   }
132
   }
133
   
133
134
   /**
134
   /**
135
    * Accepts objects needed while parsing.
135
    * Accepts objects needed while parsing.
136
    *
136
    *
Lines 145-151 Link Here
145
           boolean formulasNotResults) {
145
           boolean formulasNotResults) {
146
       this(styles, null, strings, sheetContentsHandler, dataFormatter, formulasNotResults);
146
       this(styles, null, strings, sheetContentsHandler, dataFormatter, formulasNotResults);
147
   }
147
   }
148
   
148
149
   /**
149
   /**
150
    * Accepts objects needed while parsing.
150
    * Accepts objects needed while parsing.
151
    *
151
    *
Lines 159-165 Link Here
159
           boolean formulasNotResults) {
159
           boolean formulasNotResults) {
160
       this(styles, strings, sheetContentsHandler, new DataFormatter(), formulasNotResults);
160
       this(styles, strings, sheetContentsHandler, new DataFormatter(), formulasNotResults);
161
   }
161
   }
162
   
162
163
   private void init() {
163
   private void init() {
164
       if (commentsTable != null) {
164
       if (commentsTable != null) {
165
           commentCellRefs = new LinkedList<>();
165
           commentCellRefs = new LinkedList<>();
Lines 167-173 Link Here
167
           for (CTComment comment : commentsTable.getCTComments().getCommentList().getCommentArray()) {
167
           for (CTComment comment : commentsTable.getCTComments().getCommentList().getCommentArray()) {
168
               commentCellRefs.add(new CellAddress(comment.getRef()));
168
               commentCellRefs.add(new CellAddress(comment.getRef()));
169
           }
169
           }
170
       }   
170
       }
171
   }
171
   }
172
172
173
   private boolean isTextTag(String name) {
173
   private boolean isTextTag(String name) {
Lines 186-192 Link Here
186
      // It isn't a text tag
186
      // It isn't a text tag
187
      return false;
187
      return false;
188
   }
188
   }
189
   
189
190
   @Override
190
   @Override
191
   @SuppressWarnings("unused")
191
   @SuppressWarnings("unused")
192
   public void startElement(String uri, String localName, String qName,
192
   public void startElement(String uri, String localName, String qName,
Lines 206-231 Link Here
206
       } else if ("f".equals(localName)) {
206
       } else if ("f".equals(localName)) {
207
          // Clear contents cache
207
          // Clear contents cache
208
          formula.setLength(0);
208
          formula.setLength(0);
209
          
209
210
          // Mark us as being a formula if not already
210
          // Mark us as being a formula if not already
211
          if(nextDataType == xssfDataType.NUMBER) {
211
          if(nextDataType == xssfDataType.NUMBER) {
212
             nextDataType = xssfDataType.FORMULA;
212
             nextDataType = xssfDataType.FORMULA;
213
          }
213
          }
214
          
214
215
          // Decide where to get the formula string from
215
          // Decide where to get the formula string from
216
          String type = attributes.getValue("t");
216
          String type = attributes.getValue("t");
217
          if(type != null && type.equals("shared")) {
217
          if(type != null && type.equals("shared")) {
218
             // Is it the one that defines the shared, or uses it?
218
             // Is it the one that defines the shared, or uses it?
219
             String ref = attributes.getValue("ref");
219
             String ref = attributes.getValue("ref");
220
             String si = attributes.getValue("si");
220
             String si = attributes.getValue("si");
221
             
221
222
             if(ref != null) {
222
             if(ref != null) {
223
                // This one defines it
223
                // This one defines it
224
                // TODO Save it somewhere
224
                // TODO Save it somewhere
225
                fIsOpen = true;
225
                fIsOpen = true;
226
             } else {
226
             } else {
227
                // This one uses a shared formula
227
                // This one uses a shared formula
228
                // TODO Retrieve the shared formula and tweak it to 
228
                // TODO Retrieve the shared formula and tweak it to
229
                //  match the current cell
229
                //  match the current cell
230
                if(formulasNotResults) {
230
                if(formulasNotResults) {
231
                    logger.log(POILogger.WARN, "shared formulas not yet supported!");
231
                    logger.log(POILogger.WARN, "shared formulas not yet supported!");
Lines 303-313 Link Here
303
       }
303
       }
304
304
305
       String thisStr = null;
305
       String thisStr = null;
306
       Double doubleValue = null;
306
307
307
       // v => contents of a cell
308
       // v => contents of a cell
308
       if (isTextTag(localName)) {
309
       if (isTextTag(localName)) {
309
           vIsOpen = false;
310
           vIsOpen = false;
310
           
311
311
           // Process the value contents as required, now we have it all
312
           // Process the value contents as required, now we have it all
312
           switch (nextDataType) {
313
           switch (nextDataType) {
313
               case BOOLEAN:
314
               case BOOLEAN:
Lines 324-330 Link Here
324
                      thisStr = formula.toString();
325
                      thisStr = formula.toString();
325
                   } else {
326
                   } else {
326
                      String fv = value.toString();
327
                      String fv = value.toString();
327
                      
328
328
                      if (this.formatString != null) {
329
                      if (this.formatString != null) {
329
                         try {
330
                         try {
330
                            // Try to use the value as a formattable number
331
                            // Try to use the value as a formattable number
Lines 365-383 Link Here
365
                       thisStr = formatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex, this.formatString);
366
                       thisStr = formatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex, this.formatString);
366
                   else
367
                   else
367
                       thisStr = n;
368
                       thisStr = n;
369
                   doubleValue = Double.parseDouble(n);
368
                   break;
370
                   break;
369
371
370
               default:
372
               default:
371
                   thisStr = "(TODO: Unexpected type: " + nextDataType + ")";
373
                   thisStr = "(TODO: Unexpected type: " + nextDataType + ")";
372
                   break;
374
                   break;
373
           }
375
           }
374
           
376
375
           // Do we have a comment for this cell?
377
           // Do we have a comment for this cell?
376
           checkForEmptyCellComments(EmptyCellCommentsCheckType.CELL);
378
           checkForEmptyCellComments(EmptyCellCommentsCheckType.CELL);
377
           XSSFComment comment = commentsTable != null ? commentsTable.findCellComment(new CellAddress(cellRef)) : null;
379
           XSSFComment comment = commentsTable != null ? commentsTable.findCellComment(new CellAddress(cellRef)) : null;
378
           
380
379
           // Output
381
           // Output
380
           output.cell(cellRef, thisStr, comment);
382
           output.cell(cellRef, thisStr, doubleValue, comment);
381
       } else if ("f".equals(localName)) {
383
       } else if ("f".equals(localName)) {
382
          fIsOpen = false;
384
          fIsOpen = false;
383
       } else if ("is".equals(localName)) {
385
       } else if ("is".equals(localName)) {
Lines 385-394 Link Here
385
       } else if ("row".equals(localName)) {
387
       } else if ("row".equals(localName)) {
386
          // Handle any "missing" cells which had comments attached
388
          // Handle any "missing" cells which had comments attached
387
          checkForEmptyCellComments(EmptyCellCommentsCheckType.END_OF_ROW);
389
          checkForEmptyCellComments(EmptyCellCommentsCheckType.END_OF_ROW);
388
          
390
389
          // Finish up the row
391
          // Finish up the row
390
          output.endRow(rowNum);
392
          output.endRow(rowNum);
391
          
393
392
          // some sheets do not have rowNum set in the XML, Excel can read them so we should try to read them as well
394
          // some sheets do not have rowNum set in the XML, Excel can read them so we should try to read them as well
393
          nextRowNum = rowNum + 1;
395
          nextRowNum = rowNum + 1;
394
       } else if ("sheetData".equals(localName)) {
396
       } else if ("sheetData".equals(localName)) {
Lines 427-433 Link Here
427
          headerFooter.append(ch, start, length);
429
          headerFooter.append(ch, start, length);
428
       }
430
       }
429
   }
431
   }
430
   
432
431
   /**
433
   /**
432
    * Do a check for, and output, comments in otherwise empty cells.
434
    * Do a check for, and output, comments in otherwise empty cells.
433
    */
435
    */
Lines 491-497 Link Here
491
       XSSFComment comment = commentsTable.findCellComment(cellRef);
493
       XSSFComment comment = commentsTable.findCellComment(cellRef);
492
       output.cell(cellRef.formatAsString(), null, comment);
494
       output.cell(cellRef.formatAsString(), null, comment);
493
   }
495
   }
494
   
496
495
   private enum EmptyCellCommentsCheckType {
497
   private enum EmptyCellCommentsCheckType {
496
       CELL,
498
       CELL,
497
       END_OF_ROW,
499
       END_OF_ROW,
Lines 510-518 Link Here
510
      public void endRow(int rowNum);
512
      public void endRow(int rowNum);
511
513
512
      /**
514
      /**
513
       * A cell, with the given formatted value (may be null), 
515
       * A cell, with the given formatted value (may be null),
516
       *  with the double value (may be null),
514
       *  and possibly a comment (may be null), was encountered */
517
       *  and possibly a comment (may be null), was encountered */
515
      public void cell(String cellReference, String formattedValue, XSSFComment comment);
518
      public void cell(String cellReference, String formattedValue, Double doubleValue, XSSFComment comment);
516
519
517
      /** A header or footer has been encountered */
520
      /** A header or footer has been encountered */
518
      public default void headerFooter(String text, boolean isHeader, String tagName) {}
521
      public default void headerFooter(String text, boolean isHeader, String tagName) {}

Return to bug 61858