]> source.dussan.org Git - poi.git/commitdiff
bug 59705: consolidate bounds checking of column index into a helper method
authorJaven O'Neal <onealj@apache.org>
Fri, 16 Sep 2016 00:32:40 +0000 (00:32 +0000)
committerJaven O'Neal <onealj@apache.org>
Fri, 16 Sep 2016 00:32:40 +0000 (00:32 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1760986 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/usermodel/DataConsolidateFunction.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java

index f54fb4f7b4376e2b84e55de2cbfe2d01c4476a67..d40a8d11c5bff7fce7d5b10305e00e1067efb0aa 100644 (file)
@@ -38,8 +38,8 @@ public enum DataConsolidateFunction {
     VAR(10, "Var"),
     VARP(11, "Varp");
 
-    private int value;
-    private String name;
+    private final int value;
+    private final String name;
 
     DataConsolidateFunction(int value, String name) {
         this.value = value;
index 9f686491992c3f29fbf787e3e96893d6bcfa690d..bf85f65de1ac36e1f503c8e70de7aec4fc15de84 100644 (file)
@@ -30,6 +30,7 @@ import javax.xml.namespace.QName;
 import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.openxml4j.opc.PackagePart;
 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.CellType;
 import org.apache.poi.ss.usermodel.DataConsolidateFunction;
@@ -213,24 +214,43 @@ public class XSSFPivotTable extends POIXMLDocumentPart {
     }
 
     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;
     }
+    
+    /**
+     * 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.
-     * @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
     public void addRowLabel(int columnIndex) {
+        checkColumnIndex(columnIndex);
+        
         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();
 
         CTPivotField pivotField = CTPivotField.Factory.newInstance();
@@ -238,7 +258,7 @@ public class XSSFPivotTable extends POIXMLDocumentPart {
 
         pivotField.setAxis(STAxis.AXIS_ROW);
         pivotField.setShowAll(false);
-        for(int i = 0; i <= lastRowIndex; i++) {
+        for (int i = 0; i <= lastRowIndex; i++) {
             items.addNewItem().setT(STItemType.DEFAULT);
         }
         items.setCount(items.sizeOfItemArray());
@@ -270,7 +290,8 @@ public class XSSFPivotTable extends POIXMLDocumentPart {
     
     /**
      * 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
      * The following functions exists:
      * Sum, Count, Average, Max, Min, Product, Count numbers, StdDev, StdDevp, Var, Varp
@@ -278,12 +299,7 @@ public class XSSFPivotTable extends POIXMLDocumentPart {
      */
     @Beta
     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);
         addDataField(function, columnIndex, valueFieldName);
@@ -303,7 +319,8 @@ public class XSSFPivotTable extends POIXMLDocumentPart {
 
     /**
      * 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
      * The following functions exists:
      * Sum, Count, Average, Max, Min, Product, Count numbers, StdDev, StdDevp, Var, Varp
@@ -323,12 +340,10 @@ public class XSSFPivotTable extends POIXMLDocumentPart {
      */
     @Beta
     private void addDataField(DataConsolidateFunction function, int columnIndex, String valueFieldName) {
+        checkColumnIndex(columnIndex);
+        
         AreaReference pivotArea = getPivotArea();
-        int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();
-
-        if(columnIndex > lastColIndex && columnIndex < 0) {
-            throw new IndexOutOfBoundsException();
-        }
+        
         CTDataFields dataFields;
         if(pivotTableDefinition.getDataFields() != null) {
             dataFields = pivotTableDefinition.getDataFields();
@@ -352,11 +367,8 @@ public class XSSFPivotTable extends POIXMLDocumentPart {
      */
     @Beta
     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();
         CTPivotField pivotField = CTPivotField.Factory.newInstance();
 
@@ -371,13 +383,11 @@ public class XSSFPivotTable extends POIXMLDocumentPart {
      */
     @Beta
     public void addReportFilter(int columnIndex) {
+        checkColumnIndex(columnIndex);
+        
         AreaReference pivotArea = getPivotArea();
-        int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();
         int lastRowIndex = pivotArea.getLastCell().getRow() - pivotArea.getFirstCell().getRow();
 
-        if(columnIndex > lastColIndex && columnIndex < 0) {
-            throw new IndexOutOfBoundsException();
-        }
         CTPivotFields pivotFields = pivotTableDefinition.getPivotFields();
 
         CTPivotField pivotField = CTPivotField.Factory.newInstance();