]> source.dussan.org Git - poi.git/commitdiff
Have HSSFOptimiser also remove un-used cell styles, in addition to duplicates
authorNick Burch <nick@apache.org>
Sat, 29 Sep 2012 21:36:27 +0000 (21:36 +0000)
committerNick Burch <nick@apache.org>
Sat, 29 Sep 2012 21:36:27 +0000 (21:36 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1391891 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/usermodel/HSSFOptimiser.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFOptimiser.java

index f8cfdb92b5f0411662a6feec08ae12195752ca2d..99234ca012fd7af0f33463e72671f80e066fc38b 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.9-beta1" date="2012-??-??">
+          <action dev="poi-developers" type="add">HSSFOptimiser will now also tidy away un-used cell styles, in addition to duplicate styles</action>
           <action dev="poi-developers" type="fix">53493 - Fixed memory and temporary file leak in SXSSF </action>
           <action dev="poi-developers" type="fix">53780 - Fixed memory and temporary file leak in SXSSF </action>
           <action dev="poi-developers" type="fix">53380 - ArrayIndexOutOfBounds Excetion parsing word 97 document. </action>
index 50489ef24b56f38912512d5f065345d863e9bdf6..e8768dc2ac7a19602d0fc8183edf90ebaaf11f53 100644 (file)
@@ -165,7 +165,7 @@ public class HSSFOptimiser {
        
        /**
         * Goes through the Wokrbook, optimising the cell styles
-        *  by removing duplicate ones.
+        *  by removing duplicate ones, and ones that aren't used.
         * For best results, optimise the fonts via a call to
         *  {@link #optimiseFonts(HSSFWorkbook)} first.
         * @param workbook The workbook in which to optimise the cell styles
@@ -175,8 +175,10 @@ public class HSSFOptimiser {
                //  delete the record for it. Start off with no change
                short[] newPos = 
                        new short[workbook.getWorkbook().getNumExFormats()];
+               boolean[] isUsed = new boolean[newPos.length];
                boolean[] zapRecords = new boolean[newPos.length];
                for(int i=0; i<newPos.length; i++) {
+         isUsed[i] = false;
                        newPos[i] = (short)i;
                        zapRecords[i] = false;
                }
@@ -211,6 +213,27 @@ public class HSSFOptimiser {
                        }
                }
                
+               // Loop over all the cells in the file, and identify any user defined
+               //  styles aren't actually being used (don't touch built-in ones)
+      for(int sheetNum=0; sheetNum<workbook.getNumberOfSheets(); sheetNum++) {
+         HSSFSheet s = workbook.getSheetAt(sheetNum);
+         for (Row row : s) {
+            for (Cell cellI : row) {
+               HSSFCell cell = (HSSFCell)cellI;
+               short oldXf = cell.getCellValueRecord().getXFIndex();
+               isUsed[oldXf] = true;
+            }
+         }
+      }
+      // Mark any that aren't used as needing zapping
+      for (int i=21; i<isUsed.length; i++) {
+         if (! isUsed[i]) {
+            // Un-used style, can be removed
+            zapRecords[i] = true;
+            newPos[i] = 0;
+         }
+      }
+               
                // Update the new positions based on
                //  deletes that have occurred between
                //  the start and them
@@ -237,14 +260,14 @@ public class HSSFOptimiser {
                        }
                }
                
-               // Finally, update the cells to point at
-               //  their new extended format records
+               // Finally, update the cells to point at their new extended format records
                for(int sheetNum=0; sheetNum<workbook.getNumberOfSheets(); sheetNum++) {
                        HSSFSheet s = workbook.getSheetAt(sheetNum);
                        for (Row row : s) {
                           for (Cell cellI : row) {
                                        HSSFCell cell = (HSSFCell)cellI;
                                        short oldXf = cell.getCellValueRecord().getXFIndex();
+                                       
                                        HSSFCellStyle newStyle = workbook.getCellStyleAt(
                                                        newPos[oldXf]
                                        );
index fd41ed523125452ada6aad7f32acfe0d87630541..6dc5f98e5e7a34f25984eac6e522bb8358e7a320 100644 (file)
@@ -22,11 +22,21 @@ public final class TestHSSFOptimiser extends TestCase {
        public void testDoesNoHarmIfNothingToDo() {
                HSSFWorkbook wb = new HSSFWorkbook();
 
-               HSSFFont f = wb.createFont();
+               // New files start with 4 built in fonts, and 21 built in styles
+      assertEquals(4, wb.getNumberOfFonts());
+      assertEquals(21, wb.getNumCellStyles());
+
+      // Create a test font and style, and use them
+      HSSFFont f = wb.createFont();
                f.setFontName("Testing");
                HSSFCellStyle s = wb.createCellStyle();
                s.setFont(f);
+               
+               HSSFSheet sheet = wb.createSheet();
+               HSSFRow row = sheet.createRow(0);
+               row.createCell(0).setCellStyle(s);
 
+               // Should have one more than the default of each
                assertEquals(5, wb.getNumberOfFonts());
                assertEquals(22, wb.getNumCellStyles());
 
@@ -226,5 +236,37 @@ public final class TestHSSFOptimiser extends TestCase {
                assertEquals(21, r.getCell(6).getCellValueRecord().getXFIndex());
                // cs2 -> 22
                assertEquals(22, r.getCell(7).getCellValueRecord().getXFIndex());
+               
+               
+               // Add a new duplicate, and two that aren't used
+      HSSFCellStyle csD = wb.createCellStyle();
+      csD.setFont(f1);
+      r.createCell(8).setCellStyle(csD);
+      
+      HSSFFont f3 = wb.createFont();
+      f3.setFontHeight((short) 23);
+      f3.setFontName("Testing 3");
+      HSSFFont f4 = wb.createFont();
+      f4.setFontHeight((short) 24);
+      f4.setFontName("Testing 4");
+
+      HSSFCellStyle csU1 = wb.createCellStyle();
+      csU1.setFont(f3);
+      HSSFCellStyle csU2 = wb.createCellStyle();
+      csU2.setFont(f4);
+      
+      // Check before the optimise
+      assertEquals(8, wb.getNumberOfFonts());
+      assertEquals(28, wb.getNumCellStyles());
+      
+      // Optimise, should remove the two un-used ones and the one duplicate
+      HSSFOptimiser.optimiseCellStyles(wb);
+      
+      // Check
+      assertEquals(8, wb.getNumberOfFonts());
+      assertEquals(25, wb.getNumCellStyles());
+      
+      // csD -> cs1 -> 21
+      assertEquals(21, r.getCell(8).getCellValueRecord().getXFIndex());
        }
 }