diff --git a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java index 50dca1811..0f60994dd 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java @@ -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); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java index 303cba95e..045d10bb3 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java @@ -229,136 +229,136 @@ public class TestXSSFCellStyle extends TestCase { 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 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()); + } + }