Browse Source

bug 59705: consolidate bounds checking of column index into a helper method

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1760986 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_3_15_FINAL^2
Javen O'Neal 7 years ago
parent
commit
36d73d88fe

+ 2
- 2
src/java/org/apache/poi/ss/usermodel/DataConsolidateFunction.java View File

VAR(10, "Var"), VAR(10, "Var"),
VARP(11, "Varp"); VARP(11, "Varp");


private int value;
private String name;
private final int value;
private final String name;


DataConsolidateFunction(int value, String name) { DataConsolidateFunction(int value, String name) {
this.value = value; this.value = value;

+ 42
- 32
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java View File

import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataConsolidateFunction; import org.apache.poi.ss.usermodel.DataConsolidateFunction;
} }


protected AreaReference getPivotArea() { protected AreaReference getPivotArea() {
AreaReference pivotArea = new AreaReference(getPivotCacheDefinition().
getCTPivotCacheDefinition().getCacheSource().getWorksheetSource().getRef());
AreaReference pivotArea = new AreaReference(
getPivotCacheDefinition()
.getCTPivotCacheDefinition()
.getCacheSource()
.getWorksheetSource()
.getRef(),
SpreadsheetVersion.EXCEL2007);
return pivotArea; return pivotArea;
} }
/**
* Verify column index (relative to first column in pivot area) is within the
* pivot area
*
* @param columnIndex
* @throws IndexOutOfBoundsException
*/
private void checkColumnIndex(int columnIndex) throws IndexOutOfBoundsException {
AreaReference pivotArea = getPivotArea();
int size = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol() + 1;

if (columnIndex < 0 || columnIndex >= size) {
throw new IndexOutOfBoundsException("Column Index: " + columnIndex + ", Size: " + size);
}
}


/** /**
* Add a row label using data from the given column. * Add a row label using data from the given column.
* @param columnIndex the index of the column to be used as row label.
* @param columnIndex the index of the source column to be used as row label.
* {@code columnIndex} is 0-based indexed and relative to the first column in the source.
*/ */
@Beta @Beta
public void addRowLabel(int columnIndex) { public void addRowLabel(int columnIndex) {
checkColumnIndex(columnIndex);
AreaReference pivotArea = getPivotArea(); AreaReference pivotArea = getPivotArea();
int lastRowIndex = pivotArea.getLastCell().getRow() - pivotArea.getFirstCell().getRow();
int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();

if(columnIndex > lastColIndex) {
throw new IndexOutOfBoundsException();
}
final int lastRowIndex = pivotArea.getLastCell().getRow() - pivotArea.getFirstCell().getRow();
CTPivotFields pivotFields = pivotTableDefinition.getPivotFields(); CTPivotFields pivotFields = pivotTableDefinition.getPivotFields();


CTPivotField pivotField = CTPivotField.Factory.newInstance(); CTPivotField pivotField = CTPivotField.Factory.newInstance();


pivotField.setAxis(STAxis.AXIS_ROW); pivotField.setAxis(STAxis.AXIS_ROW);
pivotField.setShowAll(false); pivotField.setShowAll(false);
for(int i = 0; i <= lastRowIndex; i++) {
for (int i = 0; i <= lastRowIndex; i++) {
items.addNewItem().setT(STItemType.DEFAULT); items.addNewItem().setT(STItemType.DEFAULT);
} }
items.setCount(items.sizeOfItemArray()); items.setCount(items.sizeOfItemArray());
/** /**
* Add a column label using data from the given column and specified function * Add a column label using data from the given column and specified function
* @param columnIndex the index of the column to be used as column label.
* @param columnIndex the index of the source column to be used as column label.
* {@code columnIndex} is 0-based indexed and relative to the first column in the source.
* @param function the function to be used on the data * @param function the function to be used on the data
* The following functions exists: * The following functions exists:
* Sum, Count, Average, Max, Min, Product, Count numbers, StdDev, StdDevp, Var, Varp * Sum, Count, Average, Max, Min, Product, Count numbers, StdDev, StdDevp, Var, Varp
*/ */
@Beta @Beta
public void addColumnLabel(DataConsolidateFunction function, int columnIndex, String valueFieldName) { public void addColumnLabel(DataConsolidateFunction function, int columnIndex, String valueFieldName) {
AreaReference pivotArea = getPivotArea();
int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();

if(columnIndex > lastColIndex && columnIndex < 0) {
throw new IndexOutOfBoundsException();
}
checkColumnIndex(columnIndex);


addDataColumn(columnIndex, true); addDataColumn(columnIndex, true);
addDataField(function, columnIndex, valueFieldName); addDataField(function, columnIndex, valueFieldName);


/** /**
* Add a column label using data from the given column and specified function * Add a column label using data from the given column and specified function
* @param columnIndex the index of the column to be used as column label.
* @param columnIndex the index of the source column to be used as column label
* {@code columnIndex} is 0-based indexed and relative to the first column in the source..
* @param function the function to be used on the data * @param function the function to be used on the data
* The following functions exists: * The following functions exists:
* Sum, Count, Average, Max, Min, Product, Count numbers, StdDev, StdDevp, Var, Varp * Sum, Count, Average, Max, Min, Product, Count numbers, StdDev, StdDevp, Var, Varp
*/ */
@Beta @Beta
private void addDataField(DataConsolidateFunction function, int columnIndex, String valueFieldName) { private void addDataField(DataConsolidateFunction function, int columnIndex, String valueFieldName) {
checkColumnIndex(columnIndex);
AreaReference pivotArea = getPivotArea(); AreaReference pivotArea = getPivotArea();
int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();

if(columnIndex > lastColIndex && columnIndex < 0) {
throw new IndexOutOfBoundsException();
}
CTDataFields dataFields; CTDataFields dataFields;
if(pivotTableDefinition.getDataFields() != null) { if(pivotTableDefinition.getDataFields() != null) {
dataFields = pivotTableDefinition.getDataFields(); dataFields = pivotTableDefinition.getDataFields();
*/ */
@Beta @Beta
public void addDataColumn(int columnIndex, boolean isDataField) { public void addDataColumn(int columnIndex, boolean isDataField) {
AreaReference pivotArea = getPivotArea();
int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();
if(columnIndex > lastColIndex && columnIndex < 0) {
throw new IndexOutOfBoundsException();
}
checkColumnIndex(columnIndex);

CTPivotFields pivotFields = pivotTableDefinition.getPivotFields(); CTPivotFields pivotFields = pivotTableDefinition.getPivotFields();
CTPivotField pivotField = CTPivotField.Factory.newInstance(); CTPivotField pivotField = CTPivotField.Factory.newInstance();


*/ */
@Beta @Beta
public void addReportFilter(int columnIndex) { public void addReportFilter(int columnIndex) {
checkColumnIndex(columnIndex);
AreaReference pivotArea = getPivotArea(); AreaReference pivotArea = getPivotArea();
int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();
int lastRowIndex = pivotArea.getLastCell().getRow() - pivotArea.getFirstCell().getRow(); int lastRowIndex = pivotArea.getLastCell().getRow() - pivotArea.getFirstCell().getRow();


if(columnIndex > lastColIndex && columnIndex < 0) {
throw new IndexOutOfBoundsException();
}
CTPivotFields pivotFields = pivotTableDefinition.getPivotFields(); CTPivotFields pivotFields = pivotTableDefinition.getPivotFields();


CTPivotField pivotField = CTPivotField.Factory.newInstance(); CTPivotField pivotField = CTPivotField.Factory.newInstance();

Loading…
Cancel
Save