]> source.dussan.org Git - poi.git/commitdiff
Bug 56511: Add a null-check to ensure that the run actually has a font to not cause...
authorDominik Stadler <centic@apache.org>
Mon, 22 Dec 2014 13:06:58 +0000 (13:06 +0000)
committerDominik Stadler <centic@apache.org>
Mon, 22 Dec 2014 13:06:58 +0000 (13:06 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1647308 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java

index 515eb1010a866f53e57b9e9dd32b146e98d16b37..89568f0b520e144bb38bc4bdf4f60775b167c22c 100644 (file)
@@ -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;
index 5a45219ac38452e5a351a11a688aaf5193160667..6d053bea133ba76615550b55950e5290db7cac4f 100644 (file)
@@ -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("<xml-fragment>New Line</xml-fragment>", t2.xmlText());
         assertEquals("<xml-fragment xml:space=\"preserve\">\n\n</xml-fragment>", 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 ? "<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));
+    }
 }