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

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1534818 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2013-10-22 22:04:16 +00:00
parent a32a1cf538
commit 5ac455fd72
2 changed files with 154 additions and 136 deletions

View 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);

View File

@ -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());
}
}