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
This commit is contained in:
Nick Burch 2012-09-29 21:36:27 +00:00
parent aa358ace11
commit e57875865e
3 changed files with 70 additions and 4 deletions

View 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>

View 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]
);

View 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());
}
}