aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2012-09-29 21:36:27 +0000
committerNick Burch <nick@apache.org>2012-09-29 21:36:27 +0000
commite4512b8d5ad3873b5dbc6c2e2e39465f4c621768 (patch)
tree99907b61bee3115413ffdbfc658810899c375b8f
parentaba9cd25bd9eb9daf63b4e048d48a76c7f76e960 (diff)
downloadpoi-e4512b8d5ad3873b5dbc6c2e2e39465f4c621768.tar.gz
poi-e4512b8d5ad3873b5dbc6c2e2e39465f4c621768.zip
Have HSSFOptimiser also remove un-used cell styles, in addition to duplicates
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1391891 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--src/documentation/content/xdocs/status.xml1
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/HSSFOptimiser.java29
-rw-r--r--src/testcases/org/apache/poi/hssf/usermodel/TestHSSFOptimiser.java44
3 files changed, 70 insertions, 4 deletions
diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml
index f8cfdb92b5..99234ca012 100644
--- a/src/documentation/content/xdocs/status.xml
+++ b/src/documentation/content/xdocs/status.xml
@@ -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>
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFOptimiser.java b/src/java/org/apache/poi/hssf/usermodel/HSSFOptimiser.java
index 50489ef24b..e8768dc2ac 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFOptimiser.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFOptimiser.java
@@ -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]
);
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFOptimiser.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFOptimiser.java
index fd41ed5231..6dc5f98e5e 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFOptimiser.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFOptimiser.java
@@ -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());
}
}