Author: nick
Date: Tue Oct 22 22:04:16 2013
New Revision: 1534818

URL: http://svn.apache.org/r1534818
Log:
Patch from bug #55650 from Andrej - Avoid AIOOBE if a non-existant Xfs is 
requested for a styleAvoid AIOOBE if a non-existant Xfs is requested for a style

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

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java?rev=1534818&r1=1534817&r2=1534818&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java 
(original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java Tue Oct 
22 22:04:16 2013
@@ -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.spread
 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 POIXMLD
    }
 
        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);

Modified: 
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java?rev=1534818&r1=1534817&r2=1534818&view=diff
==============================================================================
--- 
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java
 (original)
+++ 
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java
 Tue Oct 22 22:04:16 2013
@@ -229,136 +229,136 @@ public class TestXSSFCellStyle extends T
         assertFalse(ctBorder.isSetTop());
        }
 
-       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 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 T
         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());
+    }
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to