From 03f846c695a5ed18941276a29970666202923753 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Mon, 22 Dec 2014 13:06:58 +0000 Subject: [PATCH] Bug 56511: Add a null-check to ensure that the run actually has a font to not cause a NullPointerException but rather return null as documented git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1647308 13f79535-47bb-0310-9956-ffa450edef68 --- .../xssf/usermodel/XSSFRichTextString.java | 2 +- .../usermodel/TestXSSFRichTextString.java | 57 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java index 515eb1010a..89568f0b52 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java @@ -346,7 +346,7 @@ public class XSSFRichTextString implements RichTextString { for(int i = 0; i < st.sizeOfRArray(); i++){ CTRElt r = st.getRArray(i); - if(i == index) { + if(i == index && r.getRPr() != null) { XSSFFont fnt = new XSSFFont(toCTFont(r.getRPr())); fnt.setThemesTable(getThemesTable()); return fnt; diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java index 5a45219ac3..6d053bea13 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java @@ -22,6 +22,10 @@ import java.util.TreeMap; import junit.framework.TestCase; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.xssf.XSSFTestDataSamples; +import org.junit.Test; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STXstring; @@ -357,4 +361,57 @@ public final class TestXSSFRichTextString extends TestCase { assertEquals("New Line", t2.xmlText()); assertEquals("\n\n", t3.xmlText()); } + + + @Test + public void testBug56511() { + XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56511.xlsx"); + for (XSSFSheet sheet : wb) { + int lastRow = sheet.getLastRowNum(); + for (int rowIdx = sheet.getFirstRowNum(); rowIdx <= lastRow; rowIdx++) { + XSSFRow row = sheet.getRow(rowIdx); + if(row != null) { + int lastCell = row.getLastCellNum(); + + for (int cellIdx = row.getFirstCellNum(); cellIdx <= lastCell; cellIdx++) { + + XSSFCell cell = row.getCell(cellIdx); + if (cell != null) { + //System.out.println("row " + rowIdx + " column " + cellIdx + ": " + cell.getCellType() + ": " + cell.toString()); + + XSSFRichTextString richText = cell.getRichStringCellValue(); + int anzFormattingRuns = richText.numFormattingRuns(); + for (int run = 0; run < anzFormattingRuns; run++) { + /*XSSFFont font =*/ richText.getFontOfFormattingRun(run); + //System.out.println(" run " + run + // + " font " + (font == null ? "" : font.getFontName())); + } + } + } + } + } + } + } + + @Test + public void testBug56511_values() { + XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56511.xlsx"); + Sheet sheet = wb.getSheetAt(0); + Row row = sheet.getRow(0); + + // verify the values to ensure future changes keep the returned information equal + assertEquals(0, row.getCell(0).getRichStringCellValue().numFormattingRuns()); + assertEquals(0, row.getCell(1).getRichStringCellValue().numFormattingRuns()); + + XSSFRichTextString rt = (XSSFRichTextString) row.getCell(2).getRichStringCellValue(); + assertEquals(2, rt.numFormattingRuns()); + assertNotNull(rt.getFontOfFormattingRun(0)); + assertNotNull(rt.getFontOfFormattingRun(1)); + + rt = (XSSFRichTextString) row.getCell(3).getRichStringCellValue(); + assertEquals(3, rt.numFormattingRuns()); + assertNull(rt.getFontOfFormattingRun(0)); + assertNotNull(rt.getFontOfFormattingRun(1)); + assertNotNull(rt.getFontOfFormattingRun(2)); + } } -- 2.39.5