Author: nick
Date: Sat Sep 29 21:36:27 2012
New Revision: 1391891

URL: http://svn.apache.org/viewvc?rev=1391891&view=rev
Log:
Have HSSFOptimiser also remove un-used cell styles, in addition to duplicates

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

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=1391891&r1=1391890&r2=1391891&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Sat Sep 29 21:36:27 
2012
@@ -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>

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFOptimiser.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFOptimiser.java?rev=1391891&r1=1391890&r2=1391891&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFOptimiser.java 
(original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFOptimiser.java Sat Sep 
29 21:36:27 2012
@@ -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]
                                        );

Modified: 
poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFOptimiser.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFOptimiser.java?rev=1391891&r1=1391890&r2=1391891&view=diff
==============================================================================
--- 
poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFOptimiser.java 
(original)
+++ 
poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFOptimiser.java 
Sat Sep 29 21:36:27 2012
@@ -22,11 +22,21 @@ public final class TestHSSFOptimiser ext
        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 ext
                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());
        }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to