]> source.dussan.org Git - poi.git/commitdiff
Patch from bug #55650 from Andrej - Avoid AIOOBE if a non-existant Xfs is requested...
authorNick Burch <nick@apache.org>
Tue, 22 Oct 2013 22:04:16 +0000 (22:04 +0000)
committerNick Burch <nick@apache.org>
Tue, 22 Oct 2013 22:04:16 +0000 (22:04 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1534818 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java

index 50dca1811c9106eafa13ee010b9c60e6a2b88407..0f60994dde0ba24b2880aee0021f0c435f7eb948 100644 (file)
@@ -20,17 +20,23 @@ package org.apache.poi.xssf.model;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
 import java.util.Map.Entry;
 
+import org.apache.poi.POIXMLDocumentPart;
+import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.poi.openxml4j.opc.PackageRelationship;
+import org.apache.poi.ss.usermodel.BuiltinFormats;
 import org.apache.poi.ss.usermodel.FontFamily;
 import org.apache.poi.ss.usermodel.FontScheme;
-import org.apache.poi.ss.usermodel.BuiltinFormats;
 import org.apache.poi.xssf.usermodel.XSSFCellStyle;
 import org.apache.poi.xssf.usermodel.XSSFFont;
 import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
 import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
-import org.apache.poi.POIXMLDocumentPart;
 import org.apache.xmlbeans.XmlException;
 import org.apache.xmlbeans.XmlOptions;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
@@ -49,8 +55,6 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;
-import org.apache.poi.openxml4j.opc.PackagePart;
-import org.apache.poi.openxml4j.opc.PackageRelationship;
 
 
 /**
@@ -302,7 +306,7 @@ public class StylesTable extends POIXMLDocumentPart {
    }
 
        public CTXf getCellStyleXfAt(int idx) {
-               return styleXfs.get(idx);
+               return idx < styleXfs.size() ? styleXfs.get(idx) : null;
        }
        public int putCellStyleXf(CTXf cellStyleXf) {
                styleXfs.add(cellStyleXf);
index 303cba95eee693cd1fc8aa107ff2e906fe5d0993..045d10bb31b5e0b59bbd5af3410d102fb09c49be 100644 (file)
@@ -229,136 +229,136 @@ public class TestXSSFCellStyle extends TestCase {
         assertFalse(ctBorder.isSetTop());
        }
 
-       public void testGetSetBorderThin() {\r
-        cellStyle.setBorderTop(CellStyle.BORDER_THIN);\r
-        assertEquals(CellStyle.BORDER_THIN, cellStyle.getBorderTop());\r
-        int borderId = (int)cellStyle.getCoreXf().getBorderId();\r
-        assertTrue(borderId > 0);\r
-        //check changes in the underlying xml bean\r
-        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();\r
-        assertEquals(STBorderStyle.THIN, ctBorder.getTop().getStyle());\r
-       }\r
-       \r
-       public void testGetSetBorderMedium() {\r
-        cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM);\r
-        assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderTop());\r
-        int borderId = (int)cellStyle.getCoreXf().getBorderId();\r
-        assertTrue(borderId > 0);\r
-        //check changes in the underlying xml bean\r
-        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();\r
-        assertEquals(STBorderStyle.MEDIUM, ctBorder.getTop().getStyle());\r
-       }\r
-       \r
-       public void testGetSetBorderThick() {\r
-        cellStyle.setBorderTop(CellStyle.BORDER_THICK);\r
-        assertEquals(CellStyle.BORDER_THICK, cellStyle.getBorderTop());\r
-        int borderId = (int)cellStyle.getCoreXf().getBorderId();\r
-        assertTrue(borderId > 0);\r
-        //check changes in the underlying xml bean\r
-        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();\r
-        assertEquals(STBorderStyle.THICK, ctBorder.getTop().getStyle());\r
-       }\r
-       \r
-       public void testGetSetBorderHair() {\r
-        cellStyle.setBorderTop(CellStyle.BORDER_HAIR);\r
-        assertEquals(CellStyle.BORDER_HAIR, cellStyle.getBorderTop());\r
-        int borderId = (int)cellStyle.getCoreXf().getBorderId();\r
-        assertTrue(borderId > 0);\r
-        //check changes in the underlying xml bean\r
-        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();\r
-        assertEquals(STBorderStyle.HAIR, ctBorder.getTop().getStyle());\r
-       }\r
-       \r
-       public void testGetSetBorderDotted() {\r
-        cellStyle.setBorderTop(CellStyle.BORDER_DOTTED);\r
-        assertEquals(CellStyle.BORDER_DOTTED, cellStyle.getBorderTop());\r
-        int borderId = (int)cellStyle.getCoreXf().getBorderId();\r
-        assertTrue(borderId > 0);\r
-        //check changes in the underlying xml bean\r
-        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();\r
-        assertEquals(STBorderStyle.DOTTED, ctBorder.getTop().getStyle());\r
-       }\r
-       \r
-       public void testGetSetBorderDashed() {\r
-        cellStyle.setBorderTop(CellStyle.BORDER_DASHED);\r
-        assertEquals(CellStyle.BORDER_DASHED, cellStyle.getBorderTop());\r
-        int borderId = (int)cellStyle.getCoreXf().getBorderId();\r
-        assertTrue(borderId > 0);\r
-        //check changes in the underlying xml bean\r
-        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();\r
-        assertEquals(STBorderStyle.DASHED, ctBorder.getTop().getStyle());\r
-       }\r
-       \r
-       public void testGetSetBorderDashDot() {\r
-        cellStyle.setBorderTop(CellStyle.BORDER_DASH_DOT);\r
-        assertEquals(CellStyle.BORDER_DASH_DOT, cellStyle.getBorderTop());\r
-        int borderId = (int)cellStyle.getCoreXf().getBorderId();\r
-        assertTrue(borderId > 0);\r
-        //check changes in the underlying xml bean\r
-        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();\r
-        assertEquals(STBorderStyle.DASH_DOT, ctBorder.getTop().getStyle());\r
-       }\r
-       \r
-       public void testGetSetBorderDashDotDot() {\r
-        cellStyle.setBorderTop(CellStyle.BORDER_DASH_DOT_DOT);\r
-        assertEquals(CellStyle.BORDER_DASH_DOT_DOT, cellStyle.getBorderTop());\r
-        int borderId = (int)cellStyle.getCoreXf().getBorderId();\r
-        assertTrue(borderId > 0);\r
-        //check changes in the underlying xml bean\r
-        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();\r
-        assertEquals(STBorderStyle.DASH_DOT_DOT, ctBorder.getTop().getStyle());\r
-       }\r
-       \r
-       public void testGetSetBorderMediumDashDot() {\r
-        cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASH_DOT);\r
-        assertEquals(CellStyle.BORDER_MEDIUM_DASH_DOT, cellStyle.getBorderTop());\r
-        int borderId = (int)cellStyle.getCoreXf().getBorderId();\r
-        assertTrue(borderId > 0);\r
-        //check changes in the underlying xml bean\r
-        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();\r
-        assertEquals(STBorderStyle.MEDIUM_DASH_DOT, ctBorder.getTop().getStyle());\r
-       }\r
-       \r
-       public void testGetSetBorderMediumDashDotDot() {\r
-        cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASH_DOT_DOT);\r
-        assertEquals(CellStyle.BORDER_MEDIUM_DASH_DOT_DOT, cellStyle.getBorderTop());\r
-        int borderId = (int)cellStyle.getCoreXf().getBorderId();\r
-        assertTrue(borderId > 0);\r
-        //check changes in the underlying xml bean\r
-        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();\r
-        assertEquals(STBorderStyle.MEDIUM_DASH_DOT_DOT, ctBorder.getTop().getStyle());\r
-       }\r
-       \r
-       public void testGetSetBorderMediumDashed() {\r
-        cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED);\r
-        assertEquals(CellStyle.BORDER_MEDIUM_DASHED, cellStyle.getBorderTop());\r
-        int borderId = (int)cellStyle.getCoreXf().getBorderId();\r
-        assertTrue(borderId > 0);\r
-        //check changes in the underlying xml bean\r
-        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();\r
-        assertEquals(STBorderStyle.MEDIUM_DASHED, ctBorder.getTop().getStyle());\r
-       }\r
-       \r
-       public void testGetSetBorderSlantDashDot() {\r
-        cellStyle.setBorderTop(CellStyle.BORDER_SLANTED_DASH_DOT);\r
-        assertEquals(CellStyle.BORDER_SLANTED_DASH_DOT, cellStyle.getBorderTop());\r
-        int borderId = (int)cellStyle.getCoreXf().getBorderId();\r
-        assertTrue(borderId > 0);\r
-        //check changes in the underlying xml bean\r
-        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();\r
-        assertEquals(STBorderStyle.SLANT_DASH_DOT, ctBorder.getTop().getStyle());\r
-       }\r
-       \r
-       public void testGetSetBorderDouble() {\r
-        cellStyle.setBorderTop(CellStyle.BORDER_DOUBLE);\r
-        assertEquals(CellStyle.BORDER_DOUBLE, cellStyle.getBorderTop());\r
-        int borderId = (int)cellStyle.getCoreXf().getBorderId();\r
-        assertTrue(borderId > 0);\r
-        //check changes in the underlying xml bean\r
-        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();\r
-        assertEquals(STBorderStyle.DOUBLE, ctBorder.getTop().getStyle());\r
-       }\r
-       \r
+       public void testGetSetBorderThin() {
+        cellStyle.setBorderTop(CellStyle.BORDER_THIN);
+        assertEquals(CellStyle.BORDER_THIN, cellStyle.getBorderTop());
+        int borderId = (int)cellStyle.getCoreXf().getBorderId();
+        assertTrue(borderId > 0);
+        //check changes in the underlying xml bean
+        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
+        assertEquals(STBorderStyle.THIN, ctBorder.getTop().getStyle());
+       }
+       
+       public void testGetSetBorderMedium() {
+        cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM);
+        assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderTop());
+        int borderId = (int)cellStyle.getCoreXf().getBorderId();
+        assertTrue(borderId > 0);
+        //check changes in the underlying xml bean
+        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
+        assertEquals(STBorderStyle.MEDIUM, ctBorder.getTop().getStyle());
+       }
+       
+       public void testGetSetBorderThick() {
+        cellStyle.setBorderTop(CellStyle.BORDER_THICK);
+        assertEquals(CellStyle.BORDER_THICK, cellStyle.getBorderTop());
+        int borderId = (int)cellStyle.getCoreXf().getBorderId();
+        assertTrue(borderId > 0);
+        //check changes in the underlying xml bean
+        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
+        assertEquals(STBorderStyle.THICK, ctBorder.getTop().getStyle());
+       }
+       
+       public void testGetSetBorderHair() {
+        cellStyle.setBorderTop(CellStyle.BORDER_HAIR);
+        assertEquals(CellStyle.BORDER_HAIR, cellStyle.getBorderTop());
+        int borderId = (int)cellStyle.getCoreXf().getBorderId();
+        assertTrue(borderId > 0);
+        //check changes in the underlying xml bean
+        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
+        assertEquals(STBorderStyle.HAIR, ctBorder.getTop().getStyle());
+       }
+       
+       public void testGetSetBorderDotted() {
+        cellStyle.setBorderTop(CellStyle.BORDER_DOTTED);
+        assertEquals(CellStyle.BORDER_DOTTED, cellStyle.getBorderTop());
+        int borderId = (int)cellStyle.getCoreXf().getBorderId();
+        assertTrue(borderId > 0);
+        //check changes in the underlying xml bean
+        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
+        assertEquals(STBorderStyle.DOTTED, ctBorder.getTop().getStyle());
+       }
+       
+       public void testGetSetBorderDashed() {
+        cellStyle.setBorderTop(CellStyle.BORDER_DASHED);
+        assertEquals(CellStyle.BORDER_DASHED, cellStyle.getBorderTop());
+        int borderId = (int)cellStyle.getCoreXf().getBorderId();
+        assertTrue(borderId > 0);
+        //check changes in the underlying xml bean
+        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
+        assertEquals(STBorderStyle.DASHED, ctBorder.getTop().getStyle());
+       }
+       
+       public void testGetSetBorderDashDot() {
+        cellStyle.setBorderTop(CellStyle.BORDER_DASH_DOT);
+        assertEquals(CellStyle.BORDER_DASH_DOT, cellStyle.getBorderTop());
+        int borderId = (int)cellStyle.getCoreXf().getBorderId();
+        assertTrue(borderId > 0);
+        //check changes in the underlying xml bean
+        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
+        assertEquals(STBorderStyle.DASH_DOT, ctBorder.getTop().getStyle());
+       }
+       
+       public void testGetSetBorderDashDotDot() {
+        cellStyle.setBorderTop(CellStyle.BORDER_DASH_DOT_DOT);
+        assertEquals(CellStyle.BORDER_DASH_DOT_DOT, cellStyle.getBorderTop());
+        int borderId = (int)cellStyle.getCoreXf().getBorderId();
+        assertTrue(borderId > 0);
+        //check changes in the underlying xml bean
+        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
+        assertEquals(STBorderStyle.DASH_DOT_DOT, ctBorder.getTop().getStyle());
+       }
+       
+       public void testGetSetBorderMediumDashDot() {
+        cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASH_DOT);
+        assertEquals(CellStyle.BORDER_MEDIUM_DASH_DOT, cellStyle.getBorderTop());
+        int borderId = (int)cellStyle.getCoreXf().getBorderId();
+        assertTrue(borderId > 0);
+        //check changes in the underlying xml bean
+        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
+        assertEquals(STBorderStyle.MEDIUM_DASH_DOT, ctBorder.getTop().getStyle());
+       }
+       
+       public void testGetSetBorderMediumDashDotDot() {
+        cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASH_DOT_DOT);
+        assertEquals(CellStyle.BORDER_MEDIUM_DASH_DOT_DOT, cellStyle.getBorderTop());
+        int borderId = (int)cellStyle.getCoreXf().getBorderId();
+        assertTrue(borderId > 0);
+        //check changes in the underlying xml bean
+        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
+        assertEquals(STBorderStyle.MEDIUM_DASH_DOT_DOT, ctBorder.getTop().getStyle());
+       }
+       
+       public void testGetSetBorderMediumDashed() {
+        cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED);
+        assertEquals(CellStyle.BORDER_MEDIUM_DASHED, cellStyle.getBorderTop());
+        int borderId = (int)cellStyle.getCoreXf().getBorderId();
+        assertTrue(borderId > 0);
+        //check changes in the underlying xml bean
+        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
+        assertEquals(STBorderStyle.MEDIUM_DASHED, ctBorder.getTop().getStyle());
+       }
+       
+       public void testGetSetBorderSlantDashDot() {
+        cellStyle.setBorderTop(CellStyle.BORDER_SLANTED_DASH_DOT);
+        assertEquals(CellStyle.BORDER_SLANTED_DASH_DOT, cellStyle.getBorderTop());
+        int borderId = (int)cellStyle.getCoreXf().getBorderId();
+        assertTrue(borderId > 0);
+        //check changes in the underlying xml bean
+        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
+        assertEquals(STBorderStyle.SLANT_DASH_DOT, ctBorder.getTop().getStyle());
+       }
+       
+       public void testGetSetBorderDouble() {
+        cellStyle.setBorderTop(CellStyle.BORDER_DOUBLE);
+        assertEquals(CellStyle.BORDER_DOUBLE, cellStyle.getBorderTop());
+        int borderId = (int)cellStyle.getCoreXf().getBorderId();
+        assertTrue(borderId > 0);
+        //check changes in the underlying xml bean
+        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
+        assertEquals(STBorderStyle.DOUBLE, ctBorder.getTop().getStyle());
+       }
+       
        public void testGetSetBottomBorderColor() {
         //defaults
         assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getBottomBorderColor());
@@ -836,4 +836,18 @@ public class TestXSSFCellStyle extends TestCase {
         assertNull(style.getStyleXf());
     }
 
+    /**
+     * Avoid ArrayIndexOutOfBoundsException  when getting cell style
+     * in a workbook that has an empty xf table.
+     */
+    public void testBug55650() {
+        XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("52348.xlsx");
+        StylesTable st = workbook.getStylesSource();
+        assertEquals(0, st._getStyleXfsSize());
+
+        // no exception at this point
+        XSSFCellStyle style = workbook.getSheetAt(0).getRow(0).getCell(0).getCellStyle();
+        assertNull(style.getStyleXf());
+    }
+
 }