Improve error messages when charting data is not valid

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1893212 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2021-09-10 06:15:30 +00:00
parent 3e7672d856
commit 0739e608a8
2 changed files with 16 additions and 11 deletions

View File

@ -166,7 +166,8 @@ public abstract class XDDFChartData {
if (categoryData != null && values != null) { if (categoryData != null && values != null) {
int numOfPoints = category.getPointCount(); int numOfPoints = category.getPointCount();
if (numOfPoints != values.getPointCount()) { if (numOfPoints != values.getPointCount()) {
throw new IllegalStateException("Category and values must have the same point count."); throw new IllegalStateException("Category and values must have the same point count, but had " +
numOfPoints + " categories and " + values.getPointCount() + " values.");
} }
} }
this.categoryData = category; this.categoryData = category;

View File

@ -47,8 +47,8 @@ public class XDDFDataSourcesFactory {
} }
if (categoryDS.getNumRef() != null && categoryDS.getNumRef().getNumCache() != null) { if (categoryDS.getNumRef() != null && categoryDS.getNumRef().getNumCache() != null) {
return new XDDFCategoryDataSource() { return new XDDFCategoryDataSource() {
private CTNumData category = (CTNumData) categoryDS.getNumRef().getNumCache().copy(); private final CTNumData category = (CTNumData) categoryDS.getNumRef().getNumCache().copy();
private String formatCode = category.isSetFormatCode() ? category.getFormatCode() : null; private final String formatCode = category.isSetFormatCode() ? category.getFormatCode() : null;
@Override @Override
public boolean isCellRange() { public boolean isCellRange() {
@ -72,6 +72,10 @@ public class XDDFDataSourcesFactory {
@Override @Override
public String getPointAt(int index) { public String getPointAt(int index) {
if (category.getPtArray().length <= index) {
throw new IllegalArgumentException("Cannot access 0-based index " + index +
" in point-array with " + category.getPtArray().length + " items");
}
return category.getPtArray(index).getV(); return category.getPtArray(index).getV();
} }
@ -80,7 +84,7 @@ public class XDDFDataSourcesFactory {
}; };
} else if (categoryDS.getStrRef() != null && categoryDS.getStrRef().getStrCache() != null) { } else if (categoryDS.getStrRef() != null && categoryDS.getStrRef().getStrCache() != null) {
return new XDDFCategoryDataSource() { return new XDDFCategoryDataSource() {
private CTStrData category = (CTStrData) categoryDS.getStrRef().getStrCache().copy(); private final CTStrData category = (CTStrData) categoryDS.getStrRef().getStrCache().copy();
@Override @Override
public boolean isCellRange() { public boolean isCellRange() {
@ -107,8 +111,8 @@ public class XDDFDataSourcesFactory {
}; };
} else if (categoryDS.getNumLit() != null) { } else if (categoryDS.getNumLit() != null) {
return new XDDFCategoryDataSource() { return new XDDFCategoryDataSource() {
private CTNumData category = (CTNumData) categoryDS.getNumLit().copy(); private final CTNumData category = (CTNumData) categoryDS.getNumLit().copy();
private String formatCode = category.isSetFormatCode() ? category.getFormatCode() : null; private final String formatCode = category.isSetFormatCode() ? category.getFormatCode() : null;
@Override @Override
public boolean isCellRange() { public boolean isCellRange() {
@ -150,7 +154,7 @@ public class XDDFDataSourcesFactory {
}; };
} else if (categoryDS.getStrLit() != null) { } else if (categoryDS.getStrLit() != null) {
return new XDDFCategoryDataSource() { return new XDDFCategoryDataSource() {
private CTStrData category = (CTStrData) categoryDS.getStrLit().copy(); private final CTStrData category = (CTStrData) categoryDS.getStrLit().copy();
@Override @Override
public boolean isCellRange() { public boolean isCellRange() {
@ -196,7 +200,7 @@ public class XDDFDataSourcesFactory {
} }
if (valuesDS.getNumRef() != null && valuesDS.getNumRef().getNumCache() != null) { if (valuesDS.getNumRef() != null && valuesDS.getNumRef().getNumCache() != null) {
return new XDDFNumericalDataSource<Double>() { return new XDDFNumericalDataSource<Double>() {
private CTNumData values = (CTNumData) valuesDS.getNumRef().getNumCache().copy(); private final CTNumData values = (CTNumData) valuesDS.getNumRef().getNumCache().copy();
private String formatCode = values.isSetFormatCode() ? values.getFormatCode() : null; private String formatCode = values.isSetFormatCode() ? values.getFormatCode() : null;
@Override @Override
@ -246,7 +250,7 @@ public class XDDFDataSourcesFactory {
}; };
} else if (valuesDS.getNumLit() != null) { } else if (valuesDS.getNumLit() != null) {
return new XDDFNumericalDataSource<Double>() { return new XDDFNumericalDataSource<Double>() {
private CTNumData values = (CTNumData) valuesDS.getNumLit().copy(); private final CTNumData values = (CTNumData) valuesDS.getNumLit().copy();
private String formatCode = values.isSetFormatCode() ? values.getFormatCode() : null; private String formatCode = values.isSetFormatCode() ? values.getFormatCode() : null;
@Override @Override
@ -457,7 +461,7 @@ public class XDDFDataSourcesFactory {
private final XSSFSheet sheet; private final XSSFSheet sheet;
private final CellRangeAddress cellRangeAddress; private final CellRangeAddress cellRangeAddress;
private final int numOfCells; private final int numOfCells;
private XSSFFormulaEvaluator evaluator; private final XSSFFormulaEvaluator evaluator;
protected AbstractCellRangeDataSource(XSSFSheet sheet, CellRangeAddress cellRangeAddress) { protected AbstractCellRangeDataSource(XSSFSheet sheet, CellRangeAddress cellRangeAddress) {
this.sheet = sheet; this.sheet = sheet;
@ -530,7 +534,7 @@ public class XDDFDataSourcesFactory {
public Double getPointAt(int index) { public Double getPointAt(int index) {
CellValue cellValue = getCellValueAt(index); CellValue cellValue = getCellValueAt(index);
if (cellValue != null && cellValue.getCellType() == CellType.NUMERIC) { if (cellValue != null && cellValue.getCellType() == CellType.NUMERIC) {
return Double.valueOf(cellValue.getNumberValue()); return cellValue.getNumberValue();
} else { } else {
return null; return null;
} }