]> source.dussan.org Git - poi.git/commitdiff
Sonar Fixes
authorAndreas Beeker <kiwiwings@apache.org>
Sun, 29 Mar 2020 14:39:11 +0000 (14:39 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Sun, 29 Mar 2020 14:39:11 +0000 (14:39 +0000)
- use String.replace instead of String.replaceAll for literal values
- use constants from base class

- deprecated various references to constants of org.apache.poi.ss.usermodel.FontFormatting - to be replaced by o.a.p.s.u.Font in POI 5.0.0

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1875859 13f79535-47bb-0310-9956-ffa450edef68

23 files changed:
src/examples/src/org/apache/poi/hssf/usermodel/examples/AddDimensionedImage.java
src/examples/src/org/apache/poi/hssf/usermodel/examples/OfficeDrawing.java
src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java
src/examples/src/org/apache/poi/xslf/usermodel/ChartFromScratch.java
src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithRichText.java
src/examples/src/org/apache/poi/xwpf/usermodel/examples/ChartFromScratch.java
src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleImages.java
src/java/org/apache/poi/ddf/EscherProperties.java
src/java/org/apache/poi/ddf/EscherPropertyTypes.java
src/java/org/apache/poi/hssf/record/EmbeddedObjectRefSubRecord.java
src/java/org/apache/poi/hssf/record/cf/FontFormatting.java
src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java
src/java/org/apache/poi/hssf/usermodel/HSSFFontFormatting.java
src/java/org/apache/poi/ss/formula/functions/Roman.java
src/java/org/apache/poi/ss/usermodel/DataFormatter.java
src/java/org/apache/poi/ss/usermodel/ExcelStyleDateFormatter.java
src/java/org/apache/poi/ss/usermodel/Font.java
src/java/org/apache/poi/ss/usermodel/FontFormatting.java
src/java/org/apache/poi/ss/util/DateFormatConverter.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFontFormatting.java
src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java
src/testcases/org/apache/poi/hssf/record/TestCFRuleRecord.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java

index ca027992a7653bf49e6479e5a2048c78bf66816f..abef3e884f9e0847b05e42f820d273aff7e53151 100644 (file)
@@ -30,6 +30,7 @@ import org.apache.poi.hssf.usermodel.HSSFRow;
 import org.apache.poi.hssf.usermodel.HSSFSheet;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType;
+import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.ss.util.CellReference;
 import org.apache.poi.util.IOUtils;
 
@@ -304,7 +305,7 @@ public class AddDimensionedImage {
         // however.
         //int index = sheet.getWorkbook().addPicture(this.imageToBytes(imageFile),
         //            HSSFWorkbook.PICTURE_TYPE_JPEG);
-        int index = sheet.getWorkbook().addPicture(this.imageToBytes(imageFile), HSSFWorkbook.PICTURE_TYPE_PNG);
+        int index = sheet.getWorkbook().addPicture(this.imageToBytes(imageFile), Workbook.PICTURE_TYPE_PNG);
 
         // Get the drawing patriarch and create the picture.
         patriarch = sheet.createDrawingPatriarch();
index 39b388ea32c0439a7c51933345636287a2e523c9..7e703c5867436dc87a7b955d3432e5aadbfafa57 100644 (file)
 
 package org.apache.poi.hssf.usermodel.examples;
 
-import org.apache.poi.hssf.usermodel.*;
+import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import org.apache.poi.hssf.usermodel.HSSFChildAnchor;
+import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
+import org.apache.poi.hssf.usermodel.HSSFFont;
+import org.apache.poi.hssf.usermodel.HSSFPatriarch;
+import org.apache.poi.hssf.usermodel.HSSFPicture;
+import org.apache.poi.hssf.usermodel.HSSFPolygon;
+import org.apache.poi.hssf.usermodel.HSSFRichTextString;
+import org.apache.poi.hssf.usermodel.HSSFRow;
+import org.apache.poi.hssf.usermodel.HSSFShape;
+import org.apache.poi.hssf.usermodel.HSSFShapeGroup;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFSimpleShape;
+import org.apache.poi.hssf.usermodel.HSSFTextbox;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType;
-
-import java.io.*;
+import org.apache.poi.ss.usermodel.Font;
+import org.apache.poi.ss.usermodel.Workbook;
 
 /**
  * Demonstrates how to use the office drawing capabilities of POI.
- *
- * @author Glen Stampoultzis (glens at apache.org)
  */
-public class OfficeDrawing {
+public final class OfficeDrawing {
+    private OfficeDrawing() {}
+
     public static void main(String[] args) throws IOException {
         // Create the workbook and sheets.
         try (HSSFWorkbook wb = new HSSFWorkbook()) {
@@ -129,19 +147,19 @@ public class OfficeDrawing {
                 new HSSFClientAnchor(0,0,900,100,(short)3,3,(short)3,4));
         textbox2.setString(new HSSFRichTextString("Woo") );
         textbox2.setFillColor(200,0,0);
-        textbox2.setLineStyle(HSSFSimpleShape.LINESTYLE_DOTGEL);
+        textbox2.setLineStyle(HSSFShape.LINESTYLE_DOTGEL);
 
         // Create third one with some fancy font styling.
         HSSFTextbox textbox3 = patriarch.createTextbox(
                 new HSSFClientAnchor(0,0,900,100,(short)4,4,(short)5,4+1));
         HSSFFont font = wb.createFont();
         font.setItalic(true);
-        font.setUnderline(HSSFFont.U_DOUBLE);
+        font.setUnderline(Font.U_DOUBLE);
         HSSFRichTextString string = new HSSFRichTextString("Woo!!!");
         string.applyFont(2,5,font);
         textbox3.setString(string );
         textbox3.setFillColor(0x08000030);
-        textbox3.setLineStyle(HSSFSimpleShape.LINESTYLE_NONE);  // no line around the textbox.
+        textbox3.setLineStyle(HSSFShape.LINESTYLE_NONE);  // no line around the textbox.
         textbox3.setNoFill(true);    // make it transparent
     }
 
@@ -178,7 +196,7 @@ public class OfficeDrawing {
             int c;
             while ((c = fis.read()) != -1)
                 bos.write(c);
-            pictureIndex = wb.addPicture(bos.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG);
+            pictureIndex = wb.addPicture(bos.toByteArray(), Workbook.PICTURE_TYPE_PNG);
         }
         return pictureIndex;
     }
@@ -247,9 +265,9 @@ public class OfficeDrawing {
         double xRatio = 3.22;
         double yRatio = 0.6711;
 
-        int x1 = 000;
-        int y1 = 000;
-        int x2 = 000;
+        int x1 = 0;
+        int y1 = 0;
+        int x2 = 0;
         int y2 = 200;
         for (int i = 0; i < 20; i++)
         {
@@ -263,10 +281,10 @@ public class OfficeDrawing {
             x2 += 10;
         }
 
-        x1 = 000;
-        y1 = 000;
+        x1 = 0;
+        y1 = 0;
         x2 = 200;
-        y2 = 000;
+        y2 = 0;
         for (int i = 0; i < 20; i++)
         {
             HSSFClientAnchor a2 = new HSSFClientAnchor();
index f9e8bac0c366508b7e1894231800c1201f9407ce..ce17c668cb913d060789aa8f7ffc6a441ae3720e 100644 (file)
@@ -41,7 +41,9 @@ import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
 /**
  * Build a bar chart from a template pptx
  */
-public class BarChartDemo {
+public final class BarChartDemo {
+    private BarChartDemo() {}
+
     private static void usage(){
         System.out.println("Usage: BarChartDemo <bar-chart-template.pptx> <bar-chart-data.txt>");
         System.out.println("    bar-chart-template.pptx     template with a bar chart");
@@ -100,19 +102,19 @@ public class BarChartDemo {
         final XDDFBarChartData bar = (XDDFBarChartData) data.get(0);
 
         final int numOfPoints = categories.length;
-        final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnLanguages, columnLanguages));
-        final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnCountries, columnCountries));
-        final String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnSpeakers, columnSpeakers));
-        final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, columnLanguages);
-        final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange, columnCountries);
+        final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, COLUMN_LANGUAGES, COLUMN_LANGUAGES));
+        final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, COLUMN_COUNTRIES, COLUMN_COUNTRIES));
+        final String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, COLUMN_SPEAKERS, COLUMN_SPEAKERS));
+        final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, COLUMN_LANGUAGES);
+        final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange, COLUMN_COUNTRIES);
         values1[6] = 16.0; // if you ever want to change the underlying data, it has to be done before building the data source
-        final XDDFNumericalDataSource<? extends Number> valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, columnSpeakers);
+        final XDDFNumericalDataSource<? extends Number> valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, COLUMN_SPEAKERS);
 
         XDDFChartData.Series series1 = bar.getSeries(0);
         series1.replaceData(categoriesData, valuesData);
-        series1.setTitle(series[0], chart.setSheetTitle(series[0], columnCountries));
+        series1.setTitle(series[0], chart.setSheetTitle(series[0], COLUMN_COUNTRIES));
         XDDFChartData.Series series2 = bar.addSeries(categoriesData, valuesData2);
-        series2.setTitle(series[1], chart.setSheetTitle(series[1], columnSpeakers));
+        series2.setTitle(series[1], chart.setSheetTitle(series[1], COLUMN_SPEAKERS));
 
         chart.plot(bar);
         chart.setTitleText(chartTitle); // https://stackoverflow.com/questions/30532612
@@ -155,7 +157,7 @@ public class BarChartDemo {
         return chart;
     }
 
-    private static final int columnLanguages = 0;
-    private static final int columnCountries = 1;
-    private static final int columnSpeakers = 2;
+    private static final int COLUMN_LANGUAGES = 0;
+    private static final int COLUMN_COUNTRIES = 1;
+    private static final int COLUMN_SPEAKERS = 2;
 }
index c4c0c23a958b366c59779940a17d97e699748401..5fee24e363d74cbeaf7da36f75e00dd78d1136ef 100644 (file)
@@ -49,7 +49,9 @@ import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
 /**
  * Build a chart without reading template file
  */
-public class ChartFromScratch {
+public final class ChartFromScratch {
+    private ChartFromScratch() {}
+
     private static void usage(){
         System.out.println("Usage: ChartFromScratch <bar-chart-data.txt>");
         System.out.println("    bar-chart-data.txt          the model to set. First line is chart title, " +
@@ -138,14 +140,14 @@ public class ChartFromScratch {
         leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);
 
         final int numOfPoints = categories.length;
-        final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnLanguages, columnLanguages));
-        final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnCountries, columnCountries));
-        final String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnSpeakers, columnSpeakers));
-        final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, columnLanguages);
-        final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange, columnCountries);
+        final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, COLUMN_LANGUAGES, COLUMN_LANGUAGES));
+        final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, COLUMN_COUNTRIES, COLUMN_COUNTRIES));
+        final String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, COLUMN_SPEAKERS, COLUMN_SPEAKERS));
+        final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, COLUMN_LANGUAGES);
+        final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange, COLUMN_COUNTRIES);
         valuesData.setFormatCode("General");
         values1[6] = 16.0; // if you ever want to change the underlying data, it has to be done before building the data source
-        final XDDFNumericalDataSource<? extends Number> valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, columnSpeakers);
+        final XDDFNumericalDataSource<? extends Number> valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, COLUMN_SPEAKERS);
         valuesData2.setFormatCode("General");
 
 
@@ -153,10 +155,10 @@ public class ChartFromScratch {
         bar.setBarGrouping(BarGrouping.CLUSTERED);
 
         XDDFBarChartData.Series series1 = (XDDFBarChartData.Series) bar.addSeries(categoriesData, valuesData);
-        series1.setTitle(series[0], chart.setSheetTitle(series[0], columnCountries));
+        series1.setTitle(series[0], chart.setSheetTitle(series[0], COLUMN_COUNTRIES));
 
         XDDFBarChartData.Series series2 = (XDDFBarChartData.Series) bar.addSeries(categoriesData, valuesData2);
-        series2.setTitle(series[1], chart.setSheetTitle(series[1], columnSpeakers));
+        series2.setTitle(series[1], chart.setSheetTitle(series[1], COLUMN_SPEAKERS));
 
         bar.setVaryColors(true);
         bar.setBarDirection(BarDirection.COL);
@@ -171,7 +173,7 @@ public class ChartFromScratch {
         chart.setAutoTitleDeleted(false);
     }
 
-    private static final int columnLanguages = 0;
-    private static final int columnCountries = 1;
-    private static final int columnSpeakers = 2;
+    private static final int COLUMN_LANGUAGES = 0;
+    private static final int COLUMN_COUNTRIES = 1;
+    private static final int COLUMN_SPEAKERS = 2;
 }
index 9b0221457635fc582bbbf8edc476dba9533d9173..9fdda45c2c25e641146794518fe150137aa33bb3 100644 (file)
 
 package org.apache.poi.xssf.usermodel.examples;
 
-import org.apache.poi.xssf.usermodel.*;
-
 import java.io.FileOutputStream;
 import java.io.OutputStream;
 
+import org.apache.poi.ss.usermodel.Font;
+import org.apache.poi.xssf.usermodel.XSSFCell;
+import org.apache.poi.xssf.usermodel.XSSFColor;
+import org.apache.poi.xssf.usermodel.XSSFFont;
+import org.apache.poi.xssf.usermodel.XSSFRichTextString;
+import org.apache.poi.xssf.usermodel.XSSFRow;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+
 /**
  * Demonstrates how to work with rich text
  */
-public class WorkingWithRichText {
+public final class WorkingWithRichText {
+
+    private WorkingWithRichText() {}
 
     public static void main(String[] args) throws Exception {
         try (XSSFWorkbook wb = new XSSFWorkbook()) {
@@ -42,7 +51,7 @@ public class WorkingWithRichText {
 
             XSSFFont font2 = wb.createFont();
             font2.setItalic(true);
-            font2.setUnderline(XSSFFont.U_DOUBLE);
+            font2.setUnderline(Font.U_DOUBLE);
             font2.setColor(new XSSFColor(new java.awt.Color(0, 255, 0), wb.getStylesSource().getIndexedColors()));
             rt.applyFont(10, 19, font2);
 
index 3859a737aa232157d366f78d7c3fed959f44b527..6e4418f816c3698a262f937bd4d7edececacba6c 100644 (file)
@@ -49,7 +49,9 @@ import org.apache.poi.xwpf.usermodel.XWPFDocument;
 /**
  * Build a chart without reading template file
  */
-public class ChartFromScratch {
+public final class ChartFromScratch {
+    private ChartFromScratch() {}
+
     private static void usage(){
         System.out.println("Usage: ChartFromScratch <bar-chart-data.txt>");
         System.out.println("    bar-chart-data.txt          the model to set. First line is chart title, " +
@@ -109,14 +111,14 @@ public class ChartFromScratch {
         leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);
 
         final int numOfPoints = categories.length;
-        final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnLanguages, columnLanguages));
-        final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnCountries, columnCountries));
-        final String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnSpeakers, columnSpeakers));
-        final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, columnLanguages);
-        final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange, columnCountries);
+        final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, COLUMN_LANGUAGES, COLUMN_LANGUAGES));
+        final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, COLUMN_COUNTRIES, COLUMN_COUNTRIES));
+        final String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, COLUMN_SPEAKERS, COLUMN_SPEAKERS));
+        final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, COLUMN_LANGUAGES);
+        final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange, COLUMN_COUNTRIES);
         valuesData.setFormatCode("General");
         values1[6] = 16.0; // if you ever want to change the underlying data, it has to be done before building the data source
-        final XDDFNumericalDataSource<? extends Number> valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, columnSpeakers);
+        final XDDFNumericalDataSource<? extends Number> valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, COLUMN_SPEAKERS);
         valuesData2.setFormatCode("General");
 
 
@@ -124,10 +126,10 @@ public class ChartFromScratch {
         bar.setBarGrouping(BarGrouping.CLUSTERED);
 
         XDDFBarChartData.Series series1 = (XDDFBarChartData.Series) bar.addSeries(categoriesData, valuesData);
-        series1.setTitle(series[0], chart.setSheetTitle(series[0], columnCountries));
+        series1.setTitle(series[0], chart.setSheetTitle(series[0], COLUMN_COUNTRIES));
 
         XDDFBarChartData.Series series2 = (XDDFBarChartData.Series) bar.addSeries(categoriesData, valuesData2);
-        series2.setTitle(series[1], chart.setSheetTitle(series[1], columnSpeakers));
+        series2.setTitle(series[1], chart.setSheetTitle(series[1], COLUMN_SPEAKERS));
 
         bar.setVaryColors(true);
         bar.setBarDirection(BarDirection.COL);
@@ -142,8 +144,8 @@ public class ChartFromScratch {
         chart.setAutoTitleDeleted(false);
     }
 
-    private static final int columnLanguages = 0;
-    private static final int columnCountries = 1;
-    private static final int columnSpeakers = 2;
+    private static final int COLUMN_LANGUAGES = 0;
+    private static final int COLUMN_COUNTRIES = 1;
+    private static final int COLUMN_SPEAKERS = 2;
 }
 
index 7b25f061b92af447e9dc7962c174097d885e2ce0..2ebcb354bf3a472ed7abacde0c88252cc6709da3 100644 (file)
@@ -25,6 +25,7 @@ import java.io.IOException;
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.util.Units;
 import org.apache.poi.xwpf.usermodel.BreakType;
+import org.apache.poi.xwpf.usermodel.Document;
 import org.apache.poi.xwpf.usermodel.XWPFDocument;
 import org.apache.poi.xwpf.usermodel.XWPFParagraph;
 import org.apache.poi.xwpf.usermodel.XWPFRun;
@@ -32,7 +33,9 @@ import org.apache.poi.xwpf.usermodel.XWPFRun;
 /**
  * Demonstrates how to add pictures in a .docx document
  */
-public class SimpleImages {
+public final class SimpleImages {
+
+    private SimpleImages() {}
 
     public static void main(String[] args) throws IOException, InvalidFormatException {
         try (XWPFDocument doc = new XWPFDocument()) {
@@ -44,27 +47,27 @@ public class SimpleImages {
                 int format;
 
                 if (imgFile.endsWith(".emf")) {
-                    format = XWPFDocument.PICTURE_TYPE_EMF;
+                    format = Document.PICTURE_TYPE_EMF;
                 } else if (imgFile.endsWith(".wmf")) {
-                    format = XWPFDocument.PICTURE_TYPE_WMF;
+                    format = Document.PICTURE_TYPE_WMF;
                 } else if (imgFile.endsWith(".pict")) {
-                    format = XWPFDocument.PICTURE_TYPE_PICT;
+                    format = Document.PICTURE_TYPE_PICT;
                 } else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg")) {
-                    format = XWPFDocument.PICTURE_TYPE_JPEG;
+                    format = Document.PICTURE_TYPE_JPEG;
                 } else if (imgFile.endsWith(".png")) {
-                    format = XWPFDocument.PICTURE_TYPE_PNG;
+                    format = Document.PICTURE_TYPE_PNG;
                 } else if (imgFile.endsWith(".dib")) {
-                    format = XWPFDocument.PICTURE_TYPE_DIB;
+                    format = Document.PICTURE_TYPE_DIB;
                 } else if (imgFile.endsWith(".gif")) {
-                    format = XWPFDocument.PICTURE_TYPE_GIF;
+                    format = Document.PICTURE_TYPE_GIF;
                 } else if (imgFile.endsWith(".tiff")) {
-                    format = XWPFDocument.PICTURE_TYPE_TIFF;
+                    format = Document.PICTURE_TYPE_TIFF;
                 } else if (imgFile.endsWith(".eps")) {
-                    format = XWPFDocument.PICTURE_TYPE_EPS;
+                    format = Document.PICTURE_TYPE_EPS;
                 } else if (imgFile.endsWith(".bmp")) {
-                    format = XWPFDocument.PICTURE_TYPE_BMP;
+                    format = Document.PICTURE_TYPE_BMP;
                 } else if (imgFile.endsWith(".wpg")) {
-                    format = XWPFDocument.PICTURE_TYPE_WPG;
+                    format = Document.PICTURE_TYPE_WPG;
                 } else {
                     System.err.println("Unsupported picture: " + imgFile +
                             ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
index 415519b4537532a3bf210000331157b01a27e80e..7cebea603bdf45b4a499daa738fdf4ddeedf0f07 100644 (file)
@@ -24,7 +24,7 @@ import org.apache.poi.util.Removal;
  *
  * @deprecated use {@link EscherPropertyTypes} enum instead
  */
-@SuppressWarnings({"unused"})
+@SuppressWarnings({"unused","java:S115"})
 @Deprecated
 @Removal(version = "5.0.0")
 public interface EscherProperties {
index cd5b25a7d4ae2de2961b4cf2c0b8207fa2290131..a48972faf3eb8e0210d27a47bd5e5f6a6e569864 100644 (file)
@@ -25,6 +25,7 @@ import java.util.stream.Stream;
 /**
  * Provides a list of all known escher properties including the description and type.
  */
+@SuppressWarnings({"java:S115"})
 public enum EscherPropertyTypes {
     TRANSFORM__ROTATION(0x0004, "transform.rotation"),
     PROTECTION__LOCKROTATION(0x0077, "protection.lockrotation"),
index 8c2226becc411f6fc99721b9af76da5dacc28fda..4843b1c11622c7ee94567793e5fcbaa3b4abf218 100644 (file)
@@ -26,7 +26,7 @@ import org.apache.poi.ss.formula.ptg.Ref3DPtg;
 import org.apache.poi.ss.formula.ptg.RefPtg;
 import org.apache.poi.util.HexDump;
 import org.apache.poi.util.IOUtils;
-import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianConsts;
 import org.apache.poi.util.LittleEndianInput;
 import org.apache.poi.util.LittleEndianInputStream;
 import org.apache.poi.util.LittleEndianOutput;
@@ -94,13 +94,13 @@ public final class EmbeddedObjectRefSubRecord extends SubRecord {
                // 1223 void XclImpOleObj::ReadPictFmla( XclImpStream& rStrm, sal_uInt16 nRecSize )
 
                int streamIdOffset = in.readShort(); // OOO calls this 'nFmlaLen'
-               int remaining = size - LittleEndian.SHORT_SIZE;
+               int remaining = size - LittleEndianConsts.SHORT_SIZE;
 
                int dataLenAfterFormula = remaining - streamIdOffset;
                int formulaSize = in.readUShort();
-               remaining -= LittleEndian.SHORT_SIZE;
+               remaining -= LittleEndianConsts.SHORT_SIZE;
                field_1_unknown_int = in.readInt();
-               remaining -= LittleEndian.INT_SIZE;
+               remaining -= LittleEndianConsts.INT_SIZE;
                byte[] formulaRawBytes = readRawData(in, formulaSize);
                remaining -= formulaSize;
                field_2_refPtg = readRefPtg(formulaRawBytes);
@@ -116,16 +116,16 @@ public final class EmbeddedObjectRefSubRecord extends SubRecord {
                int stringByteCount;
                if (remaining >= dataLenAfterFormula + 3) {
                        int tag = in.readByte();
-                       stringByteCount = LittleEndian.BYTE_SIZE;
+                       stringByteCount = LittleEndianConsts.BYTE_SIZE;
                        if (tag != 0x03) {
                                throw new RecordFormatException("Expected byte 0x03 here");
                        }
                        int nChars = in.readUShort();
-                       stringByteCount += LittleEndian.SHORT_SIZE;
+                       stringByteCount += LittleEndianConsts.SHORT_SIZE;
                        if (nChars > 0) {
                                 // OOO: the 4th way Xcl stores a unicode string: not even a Grbit byte present if length 0
                                field_3_unicode_flag = ( in.readByte() & 0x01 ) != 0;
-                               stringByteCount += LittleEndian.BYTE_SIZE;
+                               stringByteCount += LittleEndianConsts.BYTE_SIZE;
                                if (field_3_unicode_flag) {
                                        field_4_ole_classname = StringUtil.readUnicodeLE(in, nChars);
                                        stringByteCount += nChars * 2;
@@ -144,9 +144,9 @@ public final class EmbeddedObjectRefSubRecord extends SubRecord {
                // Pad to next 2-byte boundary
                if (((stringByteCount + formulaSize) % 2) != 0) {
                        int b = in.readByte();
-                       remaining -= LittleEndian.BYTE_SIZE;
+                       remaining -= LittleEndianConsts.BYTE_SIZE;
                        if (field_2_refPtg != null && field_4_ole_classname == null) {
-                               field_4_unknownByte = Byte.valueOf((byte)b);
+                               field_4_unknownByte = (byte)b;
                        }
                }
                int nUnexpectedPadding = remaining - dataLenAfterFormula;
@@ -159,8 +159,8 @@ public final class EmbeddedObjectRefSubRecord extends SubRecord {
 
                // Fetch the stream ID
                if (dataLenAfterFormula >= 4) {
-                       field_5_stream_id = Integer.valueOf(in.readInt());
-                       remaining -= LittleEndian.INT_SIZE;
+                       field_5_stream_id = in.readInt();
+                       remaining -= LittleEndianConsts.INT_SIZE;
                } else {
                        field_5_stream_id = null;
                }
@@ -199,13 +199,10 @@ public final class EmbeddedObjectRefSubRecord extends SubRecord {
                int result = 2 + 4; // formulaSize + f2unknown_int
                result += formulaSize;
 
-               int stringLen;
-               if (field_4_ole_classname == null) {
-                       // don't write 0x03, stringLen, flag, text
-                       stringLen = 0;
-               } else {
+               // don't write 0x03, stringLen, flag, text
+               if (field_4_ole_classname != null) {
                        result += 1 + 2;  // 0x03, stringLen
-                       stringLen = field_4_ole_classname.length();
+                       int stringLen = field_4_ole_classname.length();
                        if (stringLen > 0) {
                                result += 1; // flag
                                if (field_3_unicode_flag) {
@@ -259,14 +256,11 @@ public final class EmbeddedObjectRefSubRecord extends SubRecord {
                }
                pos += formulaSize;
 
-               int stringLen;
-               if (field_4_ole_classname == null) {
-                       // don't write 0x03, stringLen, flag, text
-                       stringLen = 0;
-               } else {
+               // don't write 0x03, stringLen, flag, text
+               if (field_4_ole_classname != null) {
                        out.writeByte(0x03);
                        pos+=1;
-                       stringLen = field_4_ole_classname.length();
+                       int stringLen = field_4_ole_classname.length();
                        out.writeShort(stringLen);
                        pos+=2;
                        if (stringLen > 0) {
@@ -287,7 +281,6 @@ public final class EmbeddedObjectRefSubRecord extends SubRecord {
                switch(idOffset - (pos - 6)) { // 6 for 3 shorts: sid, dataSize, idOffset
                        case 1:
                                out.writeByte(field_4_unknownByte == null ? 0x00 : field_4_unknownByte.intValue());
-                               pos++;
                                break;
                        case 0:
                                break;
@@ -296,8 +289,7 @@ public final class EmbeddedObjectRefSubRecord extends SubRecord {
                }
 
                if (field_5_stream_id != null) {
-                       out.writeInt(field_5_stream_id.intValue());
-                       pos += 4;
+                       out.writeInt(field_5_stream_id);
                }
                out.write(field_6_unknown);
        }
@@ -349,10 +341,10 @@ public final class EmbeddedObjectRefSubRecord extends SubRecord {
                        sb.append("    .oleClassname  = ").append(field_4_ole_classname).append("\n");
                }
                if (field_4_unknownByte != null) {
-                       sb.append("    .f4unknown   = ").append(HexDump.byteToHex(field_4_unknownByte.intValue())).append("\n");
+                       sb.append("    .f4unknown   = ").append(HexDump.byteToHex(field_4_unknownByte)).append("\n");
                }
                if (field_5_stream_id != null) {
-                       sb.append("    .streamId      = ").append(HexDump.intToHex(field_5_stream_id.intValue())).append("\n");
+                       sb.append("    .streamId      = ").append(HexDump.intToHex(field_5_stream_id)).append("\n");
                }
                if (field_6_unknown.length > 0) {
                        sb.append("    .f7unknown     = ").append(HexDump.toHex(field_6_unknown)).append("\n");
index 22a4aed29ea07bc708bfff269640a097f4e2ebc7..d8b2d1127553477f4ea3d3e83b83c88f64669168 100644 (file)
@@ -64,22 +64,71 @@ public final class FontFormatting implements Duplicatable {
     private static final BitField shadowModified       = BitFieldFactory.getInstance(0x00000010);
     private static final BitField cancellationModified = BitFieldFactory.getInstance(0x00000080);
 
-    /** Escapement type - None */
+    /**
+     * Escapement type - None
+     *
+     * @deprecated use {@link org.apache.poi.ss.usermodel.Font#SS_NONE}
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
     public static final short SS_NONE  = 0;
-    /** Escapement type - Superscript */
+    /**
+     * Escapement type - Superscript
+     *
+     * @deprecated use {@link org.apache.poi.ss.usermodel.Font#SS_SUPER}
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
     public static final short SS_SUPER = 1;
-    /** Escapement type - Subscript */
+    /**
+     * Escapement type - Subscript
+     *
+     * @deprecated use {@link org.apache.poi.ss.usermodel.Font#SS_SUB}
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
     public static final short SS_SUB   = 2;
-    /** Underline type - None */
+    /**
+     * Underline type - None
+     *
+     * @deprecated use {@link org.apache.poi.ss.usermodel.Font#U_NONE}
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
     public static final byte U_NONE               = 0;
-    /** Underline type - Single */
+    /**
+     * Underline type - Single
+     *
+     * @deprecated use {@link org.apache.poi.ss.usermodel.Font#U_SINGLE}
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
     public static final byte U_SINGLE             = 1;
-    /** Underline type - Double */
+    /**
+     * Underline type - Double
+     *
+     * @deprecated use {@link org.apache.poi.ss.usermodel.Font#U_DOUBLE}
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
     public static final byte U_DOUBLE             = 2;
-    /** Underline type - Single Accounting */
+    /**
+     * Underline type - Single Accounting
+     *
+     * @deprecated use {@link org.apache.poi.ss.usermodel.Font#U_SINGLE_ACCOUNTING}
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
     public static final byte U_SINGLE_ACCOUNTING  = 0x21;
-    /** Underline type - Double Accounting */
+    /**
+     * Underline type - Double Accounting
+     *
+     * @deprecated use {@link org.apache.poi.ss.usermodel.Font#U_DOUBLE_ACCOUNTING}
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
     public static final byte U_DOUBLE_ACCOUNTING  = 0x22;
+
     /** Normal boldness (not bold) */
     private static final short FONT_WEIGHT_NORMAL = 0x190;
 
@@ -253,12 +302,8 @@ public final class FontFormatting implements Duplicatable {
      * @param bw - a number between 100-1000 for the fonts "boldness"
      */
 
-    private void setFontWeight(short pbw)
-    {
-        short bw = pbw;
-        if( bw<100) { bw=100; }
-        if( bw>1000){ bw=1000; }
-        setShort(OFFSET_FONT_WEIGHT, bw);
+    private void setFontWeight(short bw) {
+        setShort(OFFSET_FONT_WEIGHT, Math.max(100, Math.min(1000, bw)));
     }
 
     /**
@@ -298,9 +343,9 @@ public final class FontFormatting implements Duplicatable {
      * get the type of super or subscript for the font
      *
      * @return super or subscript option
-     * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#SS_NONE
-     * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#SS_SUPER
-     * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#SS_SUB
+     * @see org.apache.poi.ss.usermodel.Font#SS_NONE
+     * @see org.apache.poi.ss.usermodel.Font#SS_SUPER
+     * @see org.apache.poi.ss.usermodel.Font#SS_SUB
      */
     public short getEscapementType()
     {
@@ -311,9 +356,9 @@ public final class FontFormatting implements Duplicatable {
      * set the escapement type for the font
      *
      * @param escapementType  super or subscript option
-     * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#SS_NONE
-     * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#SS_SUPER
-     * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#SS_SUB
+     * @see org.apache.poi.ss.usermodel.Font#SS_NONE
+     * @see org.apache.poi.ss.usermodel.Font#SS_SUPER
+     * @see org.apache.poi.ss.usermodel.Font#SS_SUB
      */
     public void setEscapementType( short escapementType)
     {
@@ -325,11 +370,11 @@ public final class FontFormatting implements Duplicatable {
      *
      * @return font underlining type
      *
-     * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#U_NONE
-     * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#U_SINGLE
-     * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#U_DOUBLE
-     * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#U_SINGLE_ACCOUNTING
-     * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#U_DOUBLE_ACCOUNTING
+     * @see org.apache.poi.ss.usermodel.Font#U_NONE
+     * @see org.apache.poi.ss.usermodel.Font#U_SINGLE
+     * @see org.apache.poi.ss.usermodel.Font#U_DOUBLE
+     * @see org.apache.poi.ss.usermodel.Font#U_SINGLE_ACCOUNTING
+     * @see org.apache.poi.ss.usermodel.Font#U_DOUBLE_ACCOUNTING
      */
     public short getUnderlineType()
     {
@@ -341,11 +386,11 @@ public final class FontFormatting implements Duplicatable {
      *
      * @param underlineType underline option
      *
-     * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#U_NONE
-     * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#U_SINGLE
-     * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#U_DOUBLE
-     * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#U_SINGLE_ACCOUNTING
-     * @see org.apache.poi.hssf.usermodel.HSSFFontFormatting#U_DOUBLE_ACCOUNTING
+     * @see org.apache.poi.ss.usermodel.Font#U_NONE
+     * @see org.apache.poi.ss.usermodel.Font#U_SINGLE
+     * @see org.apache.poi.ss.usermodel.Font#U_DOUBLE
+     * @see org.apache.poi.ss.usermodel.Font#U_SINGLE_ACCOUNTING
+     * @see org.apache.poi.ss.usermodel.Font#U_DOUBLE_ACCOUNTING
      */
     public void setUnderlineType( short underlineType)
     {
@@ -366,7 +411,7 @@ public final class FontFormatting implements Duplicatable {
     private boolean getOptionFlag(BitField field) {
         int optionFlags = getInt(OFFSET_OPTION_FLAGS);
         int value = field.getValue(optionFlags);
-        return value==0? true : false ;
+        return value == 0;
     }
 
     private void setOptionFlag(boolean modified, BitField field)
@@ -536,7 +581,7 @@ public final class FontFormatting implements Duplicatable {
     }
 
     @Override
-    @SuppressWarnings("squid:S2975")
+    @SuppressWarnings({"squid:S2975", "MethodDoesntCallSuperMethod"})
     @Deprecated
     @Removal(version = "5.0.0")
     public FontFormatting clone() {
index 27e152b3afd712079d656fd18696c30e757fe004..29506743f69526d86b7424b8a5635c41cd2544f6 100644 (file)
@@ -21,7 +21,6 @@ import org.apache.poi.hssf.model.HSSFFormulaParser;
 import org.apache.poi.hssf.record.CFRule12Record;
 import org.apache.poi.hssf.record.CFRuleBase;
 import org.apache.poi.hssf.record.CFRuleBase.ComparisonOperator;
-import org.apache.poi.hssf.record.CFRuleRecord;
 import org.apache.poi.hssf.record.cf.BorderFormatting;
 import org.apache.poi.hssf.record.cf.ColorGradientFormatting;
 import org.apache.poi.hssf.record.cf.DataBarFormatting;
@@ -41,8 +40,9 @@ import org.apache.poi.ss.usermodel.ExcelNumberFormat;
  * It allows to specify formula based conditions for the Conditional Formatting
  * and the formatting settings such as font, border and pattern.
  */
+@SuppressWarnings("unused")
 public final class HSSFConditionalFormattingRule implements ConditionalFormattingRule {
-    private static final byte CELL_COMPARISON = CFRuleRecord.CONDITION_TYPE_CELL_VALUE_IS;
+    private static final byte CELL_COMPARISON = CFRuleBase.CONDITION_TYPE_CELL_VALUE_IS;
 
     private final CFRuleBase cfRuleRecord;
     private final HSSFWorkbook workbook;
@@ -62,7 +62,7 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
 
     /**
      * Only newer style formatting rules have priorities. For older ones,
-     *  we don't know priority for these, other than definition/model order, 
+     *  we don't know priority for these, other than definition/model order,
      *  which appears to be what Excel uses.
      * @see org.apache.poi.ss.usermodel.ConditionalFormattingRule#getPriority()
      */
@@ -71,7 +71,7 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
         if (rule12 == null) return 0;
         return rule12.getPriority();
     }
-    
+
     /**
      * Always true for HSSF files, per Microsoft Excel documentation
      * @see org.apache.poi.ss.usermodel.ConditionalFormattingRule#getStopIfTrue()
@@ -79,20 +79,20 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
     public boolean getStopIfTrue() {
         return true;
     }
-    
+
     CFRuleBase getCfRuleRecord() {
         return cfRuleRecord;
     }
     private CFRule12Record getCFRule12Record(boolean create) {
         if (cfRuleRecord instanceof CFRule12Record) {
-            // Good
-        } else {
-            if (create) throw new IllegalArgumentException("Can't convert a CF into a CF12 record");
-            return null;
+            return (CFRule12Record) cfRuleRecord;
         }
-        return (CFRule12Record)cfRuleRecord;
+        if (create) {
+            throw new IllegalArgumentException("Can't convert a CF into a CF12 record");
+        }
+        return null;
     }
-    
+
     /**
      * Always null for HSSF records, until someone figures out where to find it
      * @see org.apache.poi.ss.usermodel.ConditionalFormattingRule#getNumberFormat()
@@ -177,20 +177,20 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
     {
         return getPatternFormatting(true);
     }
-    
+
     private HSSFDataBarFormatting getDataBarFormatting(boolean create) {
         CFRule12Record cfRule12Record = getCFRule12Record(create);
         if (cfRule12Record == null) return null;
-        
+
         DataBarFormatting databarFormatting = cfRule12Record.getDataBarFormatting();
         if (databarFormatting == null) {
             if (!create) return null;
             cfRule12Record.createDataBarFormatting();
         }
-        
+
         return new HSSFDataBarFormatting(cfRule12Record, sheet);
     }
-    
+
     /**
      * @return databar / data-bar formatting object if defined, <code>null</code> otherwise
      */
@@ -204,11 +204,11 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
     public HSSFDataBarFormatting createDataBarFormatting() {
         return getDataBarFormatting(true);
     }
-    
+
     private HSSFIconMultiStateFormatting getMultiStateFormatting(boolean create) {
         CFRule12Record cfRule12Record = getCFRule12Record(create);
         if (cfRule12Record == null) return null;
-        
+
         IconMultiStateFormatting iconFormatting = cfRule12Record.getMultiStateFormatting();
         if (iconFormatting == null) {
             if (!create) return null;
@@ -216,7 +216,7 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
         }
         return new HSSFIconMultiStateFormatting(cfRule12Record, sheet);
     }
-    
+
     /**
      * @return icon / multi-state formatting object if defined, <code>null</code> otherwise
      */
@@ -230,11 +230,11 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
     public HSSFIconMultiStateFormatting createMultiStateFormatting() {
         return getMultiStateFormatting(true);
     }
-    
+
     private HSSFColorScaleFormatting getColorScaleFormatting(boolean create) {
         CFRule12Record cfRule12Record = getCFRule12Record(create);
         if (cfRule12Record == null) return null;
-        
+
         ColorGradientFormatting colorFormatting = cfRule12Record.getColorGradientFormatting();
         if (colorFormatting == null) {
             if (!create) return null;
@@ -243,13 +243,14 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
 
         return new HSSFColorScaleFormatting(cfRule12Record, sheet);
     }
-    
+
     /**
      * @return color scale / gradient formatting object if defined, <code>null</code> otherwise
      */
     public HSSFColorScaleFormatting getColorScaleFormatting() {
         return getColorScaleFormatting(false);
     }
+
     /**
      * create a new color scale / gradient formatting object if it does not exist,
      * otherwise just return the existing object.
@@ -257,7 +258,7 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
     public HSSFColorScaleFormatting createColorScaleFormatting() {
         return getColorScaleFormatting(true);
     }
-    
+
     /**
      * @return -  the conditiontype for the cfrule
      */
@@ -274,11 +275,11 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
     public ConditionFilterType getConditionFilterType() {
         return getConditionType() == ConditionType.FILTER ? ConditionFilterType.FILTER : null;
     }
-    
+
     public ConditionFilterData getFilterConfiguration() {
         return null;
     }
-    
+
     /**
      * @return - the comparisionoperatation for the cfrule
      */
@@ -308,17 +309,18 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
     public String getText() {
         return null; // not available here, unless it exists and is unimplemented in cfRuleRecord
     }
-    
-    protected String toFormulaString(Ptg[] parsedExpression) {
+
+    String toFormulaString(Ptg[] parsedExpression) {
         return toFormulaString(parsedExpression, workbook);
     }
-    protected static String toFormulaString(Ptg[] parsedExpression, HSSFWorkbook workbook) {
+
+    static String toFormulaString(Ptg[] parsedExpression, HSSFWorkbook workbook) {
         if(parsedExpression == null || parsedExpression.length == 0) {
             return null;
         }
         return HSSFFormulaParser.toFormulaString(workbook, parsedExpression);
     }
-    
+
     /**
      * Conditional format rules don't define stripes, so always 0
      * @see org.apache.poi.ss.usermodel.DifferentialStyleProvider#getStripeSize()
index 07f5fd472704e92fd8f0bc4d2f14ea43a490feeb..40cf87330635195c11a3be8c826c87a46242c880 100644 (file)
@@ -21,31 +21,59 @@ import org.apache.poi.hssf.record.CFRuleBase;
 import org.apache.poi.hssf.record.cf.FontFormatting;
 import org.apache.poi.hssf.util.HSSFColor;
 import org.apache.poi.ss.usermodel.Color;
+import org.apache.poi.util.Removal;
+
 /**
  * High level representation for Font Formatting component
  * of Conditional Formatting settings
  */
+@SuppressWarnings("unused")
 public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.FontFormatting {
-    /** Underline type - None */
-    public final static byte U_NONE              = FontFormatting.U_NONE;
-    /** Underline type - Single */
-    public final static byte U_SINGLE            = FontFormatting.U_SINGLE;
-    /** Underline type - Double */
-    public final static byte U_DOUBLE            = FontFormatting.U_DOUBLE;
-    /**  Underline type - Single Accounting */
-    public final static byte U_SINGLE_ACCOUNTING = FontFormatting.U_SINGLE_ACCOUNTING;
-    /** Underline type - Double Accounting */
-    public final static byte U_DOUBLE_ACCOUNTING = FontFormatting.U_DOUBLE_ACCOUNTING;
+    /**
+     * Underline type - None
+     * @deprecated use {@link org.apache.poi.ss.usermodel.Font#U_NONE}
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
+    public final static byte U_NONE              = org.apache.poi.ss.usermodel.Font.U_NONE;
+    /**
+     * Underline type - Single
+     * @deprecated use {@link org.apache.poi.ss.usermodel.Font#U_SINGLE}
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
+    public final static byte U_SINGLE            = org.apache.poi.ss.usermodel.Font.U_SINGLE;
+    /**
+     * Underline type - Double
+     * @deprecated use {@link org.apache.poi.ss.usermodel.Font#U_DOUBLE}
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
+    public final static byte U_DOUBLE            = org.apache.poi.ss.usermodel.Font.U_DOUBLE;
+    /**
+     * Underline type - Single Accounting
+     * @deprecated use {@link org.apache.poi.ss.usermodel.Font#U_SINGLE_ACCOUNTING}
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
+    public final static byte U_SINGLE_ACCOUNTING = org.apache.poi.ss.usermodel.Font.U_SINGLE_ACCOUNTING;
+    /**
+     * Underline type - Double Accounting
+     * @deprecated use {@link org.apache.poi.ss.usermodel.Font#U_DOUBLE_ACCOUNTING}
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
+    public final static byte U_DOUBLE_ACCOUNTING = org.apache.poi.ss.usermodel.Font.U_DOUBLE_ACCOUNTING;
 
     private final FontFormatting fontFormatting;
     private final HSSFWorkbook workbook;
 
-    protected HSSFFontFormatting(CFRuleBase cfRuleRecord, HSSFWorkbook workbook) {
+    HSSFFontFormatting(CFRuleBase cfRuleRecord, HSSFWorkbook workbook) {
         this.fontFormatting = cfRuleRecord.getFontFormatting();
         this.workbook = workbook;
     }
 
-    protected FontFormatting getFontFormattingBlock() {
+    FontFormatting getFontFormattingBlock() {
         return fontFormatting;
     }
 
@@ -53,9 +81,9 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
      * get the type of super or subscript for the font
      *
      * @return super or subscript option
-     * @see #SS_NONE
-     * @see #SS_SUPER
-     * @see #SS_SUB
+     * @see org.apache.poi.ss.usermodel.Font#SS_NONE
+     * @see org.apache.poi.ss.usermodel.Font#SS_SUPER
+     * @see org.apache.poi.ss.usermodel.Font#SS_SUB
      */
     public short getEscapementType()
     {
@@ -107,7 +135,7 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
     /**
      * @see org.apache.poi.hssf.record.cf.FontFormatting#getRawRecord()
      */
-    protected byte[] getRawRecord() {
+    byte[] getRawRecord() {
         return fontFormatting.getRawRecord();
     }
 
@@ -116,11 +144,11 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
      *
      * @return font underlining type
      *
-     * @see #U_NONE
-     * @see #U_SINGLE
-     * @see #U_DOUBLE
-     * @see #U_SINGLE_ACCOUNTING
-     * @see #U_DOUBLE_ACCOUNTING
+     * @see org.apache.poi.ss.usermodel.Font#U_NONE
+     * @see org.apache.poi.ss.usermodel.Font#U_SINGLE
+     * @see org.apache.poi.ss.usermodel.Font#U_DOUBLE
+     * @see org.apache.poi.ss.usermodel.Font#U_SINGLE_ACCOUNTING
+     * @see org.apache.poi.ss.usermodel.Font#U_DOUBLE_ACCOUNTING
      */
     public short getUnderlineType()
     {
@@ -138,7 +166,7 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
     }
 
     /**
-     * @return true if escapement type was modified from default   
+     * @return true if escapement type was modified from default
      */
     public boolean isEscapementTypeModified()
     {
@@ -146,7 +174,7 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
     }
 
     /**
-     * @return true if font cancellation was modified from default   
+     * @return true if font cancellation was modified from default
      */
     public boolean isFontCancellationModified()
     {
@@ -154,7 +182,7 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
     }
 
     /**
-     * @return true if font outline type was modified from default   
+     * @return true if font outline type was modified from default
      */
     public boolean isFontOutlineModified()
     {
@@ -162,7 +190,7 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
     }
 
     /**
-     * @return true if font shadow type was modified from default   
+     * @return true if font shadow type was modified from default
      */
     public boolean isFontShadowModified()
     {
@@ -170,7 +198,7 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
     }
 
     /**
-     * @return true if font style was modified from default   
+     * @return true if font style was modified from default
      */
     public boolean isFontStyleModified()
     {
@@ -178,7 +206,7 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
     }
 
     /**
-     * @return true if font style was set to <i>italic</i> 
+     * @return true if font style was set to <i>italic</i>
      */
     public boolean isItalic()
     {
@@ -210,7 +238,7 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
     }
 
     /**
-     * @return true if font underline type was modified from default   
+     * @return true if font underline type was modified from default
      */
     public boolean isUnderlineTypeModified()
     {
@@ -218,7 +246,7 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
     }
 
     /**
-     * @return true if font weight was modified from default   
+     * @return true if font weight was modified from default
      */
     public boolean isFontWeightModified()
     {
@@ -227,8 +255,8 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
 
     /**
      * set font style options.
-     * 
-     * @param italic - if true, set posture style to italic, otherwise to normal 
+     *
+     * @param italic - if true, set posture style to italic, otherwise to normal
      * @param bold if true, set font weight to bold, otherwise to normal
      */
 
@@ -253,18 +281,18 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
      * set the escapement type for the font
      *
      * @param escapementType  super or subscript option
-     * @see #SS_NONE
-     * @see #SS_SUPER
-     * @see #SS_SUB
+     * @see org.apache.poi.ss.usermodel.Font#SS_NONE
+     * @see org.apache.poi.ss.usermodel.Font#SS_SUPER
+     * @see org.apache.poi.ss.usermodel.Font#SS_SUB
      */
     public void setEscapementType(short escapementType) {
         switch(escapementType) {
-            case HSSFFontFormatting.SS_SUB:
-            case HSSFFontFormatting.SS_SUPER:
+            case org.apache.poi.ss.usermodel.Font.SS_SUB:
+            case org.apache.poi.ss.usermodel.Font.SS_SUPER:
                 fontFormatting.setEscapementType(escapementType);
                 fontFormatting.setEscapementTypeModified(true);
                 break;
-            case HSSFFontFormatting.SS_NONE:
+            case org.apache.poi.ss.usermodel.Font.SS_NONE:
                 fontFormatting.setEscapementType(escapementType);
                 fontFormatting.setEscapementTypeModified(false);
                 break;
@@ -273,7 +301,7 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
     }
 
     /**
-     * @param modified
+     * @param modified flag, if escapement type is modified
      * @see org.apache.poi.hssf.record.cf.FontFormatting#setEscapementTypeModified(boolean)
      */
     public void setEscapementTypeModified(boolean modified) {
@@ -281,7 +309,7 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
     }
 
     /**
-     * @param modified
+     * @param modified flag, if font cancellation is modified
      * @see org.apache.poi.hssf.record.cf.FontFormatting#setFontCancellationModified(boolean)
      */
     public void setFontCancellationModified(boolean modified)
@@ -290,7 +318,7 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
     }
 
     /**
-     * @param fci
+     * @param fci the font color index
      * @see org.apache.poi.hssf.record.cf.FontFormatting#setFontColorIndex(short)
      */
     public void setFontColorIndex(short fci)
@@ -299,7 +327,7 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
     }
 
     /**
-     * @param height
+     * @param height the font height
      * @see org.apache.poi.hssf.record.cf.FontFormatting#setFontHeight(int)
      */
     public void setFontHeight(int height)
@@ -308,7 +336,7 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
     }
 
     /**
-     * @param modified
+     * @param modified flag, if font outline is modified
      * @see org.apache.poi.hssf.record.cf.FontFormatting#setFontOutlineModified(boolean)
      */
     public void setFontOutlineModified(boolean modified)
@@ -317,7 +345,7 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
     }
 
     /**
-     * @param modified
+     * @param modified flag, if font shadow is modified
      * @see org.apache.poi.hssf.record.cf.FontFormatting#setFontShadowModified(boolean)
      */
     public void setFontShadowModified(boolean modified)
@@ -326,7 +354,7 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
     }
 
     /**
-     * @param modified
+     * @param modified flag, if font style is modified
      * @see org.apache.poi.hssf.record.cf.FontFormatting#setFontStyleModified(boolean)
      */
     public void setFontStyleModified(boolean modified)
@@ -335,7 +363,7 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
     }
 
     /**
-     * @param on
+     * @param on flag, if outline is set
      * @see org.apache.poi.hssf.record.cf.FontFormatting#setOutline(boolean)
      */
     public void setOutline(boolean on)
@@ -345,7 +373,7 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
     }
 
     /**
-     * @param on
+     * @param on flag, if shadow is set
      * @see org.apache.poi.hssf.record.cf.FontFormatting#setShadow(boolean)
      */
     public void setShadow(boolean on)
@@ -355,7 +383,7 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
     }
 
     /**
-     * @param strike
+     * @param strike flag, if strikeout is set
      * @see org.apache.poi.hssf.record.cf.FontFormatting#setStrikeout(boolean)
      */
     public void setStrikeout(boolean strike)
@@ -369,23 +397,23 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
      *
      * @param underlineType  super or subscript option
      *
-     * @see #U_NONE
-     * @see #U_SINGLE
-     * @see #U_DOUBLE
-     * @see #U_SINGLE_ACCOUNTING
-     * @see #U_DOUBLE_ACCOUNTING
+     * @see org.apache.poi.ss.usermodel.Font#U_NONE
+     * @see org.apache.poi.ss.usermodel.Font#U_SINGLE
+     * @see org.apache.poi.ss.usermodel.Font#U_DOUBLE
+     * @see org.apache.poi.ss.usermodel.Font#U_SINGLE_ACCOUNTING
+     * @see org.apache.poi.ss.usermodel.Font#U_DOUBLE_ACCOUNTING
      */
     public void setUnderlineType(short underlineType) {
         switch(underlineType) {
-            case HSSFFontFormatting.U_SINGLE:
-            case HSSFFontFormatting.U_DOUBLE:
-            case HSSFFontFormatting.U_SINGLE_ACCOUNTING:
-            case HSSFFontFormatting.U_DOUBLE_ACCOUNTING:
+            case org.apache.poi.ss.usermodel.Font.U_SINGLE:
+            case org.apache.poi.ss.usermodel.Font.U_DOUBLE:
+            case org.apache.poi.ss.usermodel.Font.U_SINGLE_ACCOUNTING:
+            case org.apache.poi.ss.usermodel.Font.U_DOUBLE_ACCOUNTING:
                 fontFormatting.setUnderlineType(underlineType);
                 setUnderlineTypeModified(true);
                 break;
-    
-            case HSSFFontFormatting.U_NONE:
+
+            case org.apache.poi.ss.usermodel.Font.U_NONE:
                 fontFormatting.setUnderlineType(underlineType);
                 setUnderlineTypeModified(false);
                 break;
@@ -394,11 +422,10 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
     }
 
     /**
-     * @param modified
+     * @param modified flag, if underline type is modified
      * @see org.apache.poi.hssf.record.cf.FontFormatting#setUnderlineTypeModified(boolean)
      */
-    public void setUnderlineTypeModified(boolean modified)
-    {
+    public void setUnderlineTypeModified(boolean modified) {
         fontFormatting.setUnderlineTypeModified(modified);
     }
 }
index d73e322574679abf5f8c2f5f561f557d76ecee47..f822990dc3e5983ba0cc5d70d115602acb28862f 100644 (file)
@@ -24,32 +24,63 @@ import org.apache.poi.ss.formula.eval.StringEval;
 import org.apache.poi.ss.formula.eval.ValueEval;
 
 /**
- * Implementation for Excel WeekNum() function.<p>
+ * Implementation for Excel Roman() function.<p>
  * <p>
- * <b>Syntax</b>:<br> <b>WeekNum  </b>(<b>Serial_num</b>,<b>Return_type</b>)<br>
+ * <b>Syntax</b>:<br> <b>Roman  </b>(<b>number</b>,<b>form</b>)<br>
  * <p>
- * Returns a number that indicates where the week falls numerically within a year.
+ * Converts an arabic numeral to roman, as text.
  * <p>
  * <p>
- * Serial_num     is a date within the week. Dates should be entered by using the DATE function,
- * or as results of other formulas or functions. For example, use DATE(2008,5,23)
- * for the 23rd day of May, 2008. Problems can occur if dates are entered as text.
- * Return_type     is a number that determines on which day the week begins. The default is 1.
- * 1   Week begins on Sunday. Weekdays are numbered 1 through 7.
- * 2   Week begins on Monday. Weekdays are numbered 1 through 7.
- *
- * @author cedric dot walter @ gmail dot com
+ * Number  Required. The Arabic numeral you want converted.<p>
+ * Form    Optional. A number specifying the type of roman numeral you want.
+ *         The roman numeral style ranges from Classic to Simplified, becoming more concise as the value of form increases.
+ * <p>
+ * Return_type     a roman numeral, as text
  */
 public class Roman extends Fixed2ArgFunction {
 
     //M (1000), CM (900), D (500), CD (400), C (100), XC (90), L (50), XL (40), X (10), IX (9), V (5), IV (4) and I (1).
-    public static final int[] VALUES = new int[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
-    public static final String[] ROMAN = new String[]
-            {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
+    private static final int[] VALUES = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
+
+    private static final String[] ROMAN = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
+
+    private static final String[][] REPLACEMENTS = {
+        { // form > 0
+            "XLV",  "VL",   //  45
+            "XCV",  "VC",   //  95
+            "CDL",  "LD",   // 450
+            "CML",  "LM",   // 950
+            "CMVC", "LMVL"  // 995
+        },{ // Form == 1 only
+            "CDXC", "LDXL", // 490
+            "CDVC", "LDVL", // 495
+            "CMXC", "LMXL", // 990
+            "XCIX", "VCIV", //  99
+            "XLIX", "VLIV"  //  49
+        },{ // form > 1
+            "XLIX", "IL",   //  49
+            "XCIX", "IC",   //  99
+            "CDXC", "XD",   // 490
+            "CDVC", "XDV",  // 495
+            "CDIC", "XDIX", // 499
+            "LMVL", "XMV",  // 995
+            "CMIC", "XMIX", // 999
+            "CMXC", "XM"    // 990
+        },{ // form > 2
+            "XDV",  "VD",   // 495
+            "XDIX", "VDIV", // 499
+            "XMV",  "VM",   // 995
+            "XMIX", "VMIV"  // 999
+        },{ // form == 4
+            "VDIV", "ID",   // 499
+            "VMIV", "IM"    // 999
+        }
+    };
+
 
 
     public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE, ValueEval formVE) {
-        int number = 0;
+        final int number;
         try {
             ValueEval ve = OperandResolver.getSingleValue(numberVE, srcRowIndex, srcColumnIndex);
             number = OperandResolver.coerceValueToInt(ve);
@@ -66,7 +97,7 @@ public class Roman extends Fixed2ArgFunction {
             return new StringEval("");
         }
 
-        int form = 0;
+        final int form;
         try {
             ValueEval ve = OperandResolver.getSingleValue(formVE, srcRowIndex, srcColumnIndex);
             form = OperandResolver.coerceValueToInt(ve);
@@ -90,7 +121,7 @@ public class Roman extends Fixed2ArgFunction {
     /**
      * Classic conversion.
      *
-     * @param number
+     * @param number the number
      */
     private String integerToRoman(int number) {
         StringBuilder result = new StringBuilder();
@@ -106,45 +137,21 @@ public class Roman extends Fixed2ArgFunction {
     /**
      * Use conversion rule to factor some parts and make them more concise
      *
-     * @param result
-     * @param form
+     * @param input the input string
+     * @param form the level of conciseness [0..4] with 4 being most concise and simplified
      */
-    public String makeConcise(String result, int form) {
-        if (form > 0) {
-            result = result.replaceAll("XLV", "VL"); //45
-            result = result.replaceAll("XCV", "VC"); //95
-            result = result.replaceAll("CDL", "LD"); //450
-            result = result.replaceAll("CML", "LM"); //950
-            result = result.replaceAll("CMVC", "LMVL"); //995
-        }
-        if (form == 1) {
-            result = result.replaceAll("CDXC", "LDXL"); //490
-            result = result.replaceAll("CDVC", "LDVL"); //495
-            result = result.replaceAll("CMXC", "LMXL"); //990
-            result = result.replaceAll("XCIX", "VCIV"); //99
-            result = result.replaceAll("XLIX", "VLIV"); //49
-        }
-        if (form > 1) {
-            result = result.replaceAll("XLIX", "IL"); //49
-            result = result.replaceAll("XCIX", "IC"); //99
-            result = result.replaceAll("CDXC", "XD"); //490
-            result = result.replaceAll("CDVC", "XDV"); //495
-            result = result.replaceAll("CDIC", "XDIX"); //499
-            result = result.replaceAll("LMVL", "XMV"); //995
-            result = result.replaceAll("CMIC", "XMIX"); //999
-            result = result.replaceAll("CMXC", "XM"); // 990
-        }
-        if (form > 2) {
-            result = result.replaceAll("XDV", "VD");  //495
-            result = result.replaceAll("XDIX", "VDIV"); //499
-            result = result.replaceAll("XMV", "VM"); // 995
-            result = result.replaceAll("XMIX", "VMIV"); //999
-        }
-        if (form == 4) {
-            result = result.replaceAll("VDIV", "ID"); //499
-            result = result.replaceAll("VMIV", "IM"); //999
+    public String makeConcise(final String input, final int form) {
+        String result = input;
+        for (int i=0; i<=form && i<=4 && form > 0; i++) {
+            if (i==1 && form>1) {
+                // Replacement[1] is only meant for form == 1
+                continue;
+            }
+            String[] repl = REPLACEMENTS[i];
+            for (int j=0; j<repl.length; j+=2) {
+                result = result.replace(repl[j],repl[j+1]);
+            }
         }
-
         return result;
     }
 }
index 19c0afaa7c502080aa7e38a55bcbbd6da3a333da..12b3412bdd2c4931ec153d3731615632fba3d2de 100644 (file)
@@ -118,6 +118,7 @@ import org.apache.poi.util.POILogger;
  *  You can use {@link DateFormatConverter} to do some of this localisation if
  *   you need it.
  */
+@SuppressWarnings("unused")
 public class DataFormatter implements Observer {
     private static final String defaultFractionWholePartFormat = "#";
     private static final String defaultFractionFractionPartFormat = "#/##";
@@ -342,13 +343,11 @@ public class DataFormatter implements Observer {
                 // Ask CellFormat to get a formatter for it
                 CellFormat cfmt = CellFormat.getInstance(locale, formatStr);
                 // CellFormat requires callers to identify date vs not, so do so
-                Object cellValueO = Double.valueOf(cellValue);
-                if (DateUtil.isADateFormat(formatIndex, formatStr) &&
-                        // don't try to handle Date value 0, let a 3 or 4-part format take care of it
-                        ((Double)cellValueO).doubleValue() != 0.0) {
-                    cellValueO = DateUtil.getJavaDate(cellValue, use1904Windowing);
-                }
-                // Wrap and return (non-cachable - CellFormat does that)
+                // don't try to handle Date value 0, let a 3 or 4-part format take care of it
+                Object cellValueO = (cellValue != 0.0 && DateUtil.isADateFormat(formatIndex, formatStr))
+                    ? DateUtil.getJavaDate(cellValue, use1904Windowing)
+                    : cellValue;
+                // Wrap and return (non-cacheable - CellFormat does that)
                 return new CellFormatResultWrapper( cfmt.apply(cellValueO) );
             } catch (Exception e) {
                 logger.log(POILogger.WARN, "Formatting failed for format " + formatStr + ", falling back", e);
@@ -357,7 +356,7 @@ public class DataFormatter implements Observer {
 
        // Excel's # with value 0 will output empty where Java will output 0. This hack removes the # from the format.
        if (emulateCSV && cellValue == 0.0 && formatStr.contains("#") && !formatStr.contains("0")) {
-           formatStr = formatStr.replaceAll("#", "");
+           formatStr = formatStr.replace("#", "");
        }
 
         // See if we already have it cached
@@ -397,38 +396,42 @@ public class DataFormatter implements Observer {
         String formatStr = sFormat;
 
         // Remove colour formatting if present
-        Matcher colourM = colorPattern.matcher(formatStr);
-        while(colourM.find()) {
-           String colour = colourM.group();
-
-           // Paranoid replacement...
-           int at = formatStr.indexOf(colour);
-           if(at == -1) break;
-           String nFormatStr = formatStr.substring(0,at) +
-              formatStr.substring(at+colour.length());
-           if(nFormatStr.equals(formatStr)) break;
-
-           // Try again in case there's multiple
-           formatStr = nFormatStr;
-           colourM = colorPattern.matcher(formatStr);
+        if (formatStr != null) {
+            Matcher colourM = colorPattern.matcher(formatStr);
+            while (colourM.find()) {
+                String colour = colourM.group();
+
+                // Paranoid replacement...
+                int at = formatStr.indexOf(colour);
+                if (at == -1) break;
+                String nFormatStr = formatStr.substring(0, at) +
+                        formatStr.substring(at + colour.length());
+                if (nFormatStr.equals(formatStr)) break;
+
+                // Try again in case there's multiple
+                formatStr = nFormatStr;
+                colourM = colorPattern.matcher(formatStr);
+            }
         }
 
         // Strip off the locale information, we use an instance-wide locale for everything
-        Matcher m = localePatternGroup.matcher(formatStr);
-        while(m.find()) {
-            String match = m.group();
-            String symbol = match.substring(match.indexOf('$') + 1, match.indexOf('-'));
-            if (symbol.indexOf('$') > -1) {
-                symbol = symbol.substring(0, symbol.indexOf('$')) +
-                        '\\' +
-                        symbol.substring(symbol.indexOf('$'));
+        if (formatStr != null) {
+            Matcher m = localePatternGroup.matcher(formatStr);
+            while (m.find()) {
+                String match = m.group();
+                String symbol = match.substring(match.indexOf('$') + 1, match.indexOf('-'));
+                if (symbol.indexOf('$') > -1) {
+                    symbol = symbol.substring(0, symbol.indexOf('$')) +
+                            '\\' +
+                            symbol.substring(symbol.indexOf('$'));
+                }
+                formatStr = m.replaceAll(symbol);
+                m = localePatternGroup.matcher(formatStr);
             }
-            formatStr = m.replaceAll(symbol);
-            m = localePatternGroup.matcher(formatStr);
         }
 
         // Check for special cases
-        if(formatStr == null || formatStr.trim().length() == 0) {
+        if(formatStr == null || formatStr.trim().isEmpty()) {
             return getDefaultFormat(cellValue);
         }
 
@@ -476,15 +479,15 @@ public class DataFormatter implements Observer {
 
     private Format createDateFormat(String pFormatStr, double cellValue) {
         String formatStr = pFormatStr;
-        formatStr = formatStr.replaceAll("\\\\-","-");
-        formatStr = formatStr.replaceAll("\\\\,",",");
-        formatStr = formatStr.replaceAll("\\\\\\.","."); // . is a special regexp char
-        formatStr = formatStr.replaceAll("\\\\ "," ");
-        formatStr = formatStr.replaceAll("\\\\/","/"); // weird: m\\/d\\/yyyy
-        formatStr = formatStr.replaceAll(";@", "");
-        formatStr = formatStr.replaceAll("\"/\"", "/"); // "/" is escaped for no reason in: mm"/"dd"/"yyyy
+        formatStr = formatStr.replace("\\-","-");
+        formatStr = formatStr.replace("\\,",",");
+        formatStr = formatStr.replace("\\.","."); // . is a special regexp char
+        formatStr = formatStr.replace("\\ "," ");
+        formatStr = formatStr.replace("\\/","/"); // weird: m\\/d\\/yyyy
+        formatStr = formatStr.replace(";@", "");
+        formatStr = formatStr.replace("\"/\"", "/"); // "/" is escaped for no reason in: mm"/"dd"/"yyyy
         formatStr = formatStr.replace("\"\"", "'");    // replace Excel quoting with Java style quoting
-        formatStr = formatStr.replaceAll("\\\\T","'T'"); // Quote the T is iso8601 style dates
+        formatStr = formatStr.replace("\\T","'T'"); // Quote the T is iso8601 style dates
 
 
         boolean hasAmPm = false;
@@ -494,12 +497,12 @@ public class DataFormatter implements Observer {
             hasAmPm = true;
             amPmMatcher = amPmPattern.matcher(formatStr);
         }
-        formatStr = formatStr.replaceAll("@", "a");
+        formatStr = formatStr.replace('@', 'a');
 
 
         Matcher dateMatcher = daysAsText.matcher(formatStr);
         if (dateMatcher.find()) {
-            String match = dateMatcher.group(0).toUpperCase(Locale.ROOT).replaceAll("D", "E");
+            String match = dateMatcher.group(0).toUpperCase(Locale.ROOT).replace('D', 'E');
             formatStr = dateMatcher.replaceAll(match);
         }
 
@@ -567,9 +570,7 @@ public class DataFormatter implements Observer {
             else if (c == 'm' || c == 'M') {
                 if(mIsMonth) {
                     sb.append('M');
-                    ms.add(
-                            Integer.valueOf(sb.length() -1)
-                    );
+                    ms.add(sb.length() - 1);
                 } else {
                     sb.append('m');
                 }
@@ -801,6 +802,7 @@ public class DataFormatter implements Observer {
      * Performs Excel-style date formatting, using the
      *  supplied Date and format
      */
+    @SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
     private String performDateFormatting(Date d, Format dateFormat) {
         Format df = dateFormat != null ? dateFormat : defaultDateformat;
         synchronized (df) {
@@ -821,6 +823,7 @@ public class DataFormatter implements Observer {
      * @param cfEvaluator ConditionalFormattingEvaluator (if available)
      * @return Formatted value
      */
+    @SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
     private String getFormattedDateString(Cell cell, ConditionalFormattingEvaluator cfEvaluator) {
         if (cell == null) {
             return null;
@@ -870,7 +873,7 @@ public class DataFormatter implements Observer {
         if (numberFormat == null) {
             return String.valueOf(d);
         }
-        String formatted = numberFormat.format(Double.valueOf(d));
+        String formatted = numberFormat.format(d);
         return formatted.replaceFirst("E(\\d)", "E+$1"); // to match Excel's E-notation
     }
 
@@ -921,7 +924,7 @@ public class DataFormatter implements Observer {
         String result;
         final String textValue = NumberToTextConverter.toText(value);
         if (textValue.indexOf('E') > -1) {
-            result = numberFormat.format(Double.valueOf(value));
+            result = numberFormat.format(value);
         }
         else {
             result = numberFormat.format(new BigDecimal(textValue));
index c5684b1c4d1d4e7a2912a24fc46fceb031b87bac..fcaed994d5fc4eb43d846a1c22b081725fbb300e 100644 (file)
@@ -91,13 +91,13 @@ public class ExcelStyleDateFormatter extends SimpleDateFormat {
      * with our detection sequences
      */
     private static String processFormatPattern(String f) {
-        String t = f.replaceAll("MMMMM", MMMMM_START_SYMBOL + "MMM" + MMMMM_TRUNCATE_SYMBOL);
-        t = t.replaceAll("\\[H]", String.valueOf(H_BRACKET_SYMBOL));
-        t = t.replaceAll("\\[HH]", String.valueOf(HH_BRACKET_SYMBOL));
-        t = t.replaceAll("\\[m]", String.valueOf(M_BRACKET_SYMBOL));
-        t = t.replaceAll("\\[mm]", String.valueOf(MM_BRACKET_SYMBOL));
-        t = t.replaceAll("\\[s]", String.valueOf(S_BRACKET_SYMBOL));
-        t = t.replaceAll("\\[ss]", String.valueOf(SS_BRACKET_SYMBOL));
+        String t = f.replace("MMMMM", MMMMM_START_SYMBOL + "MMM" + MMMMM_TRUNCATE_SYMBOL);
+        t = t.replace("[H]", String.valueOf(H_BRACKET_SYMBOL));
+        t = t.replace("[HH]", String.valueOf(HH_BRACKET_SYMBOL));
+        t = t.replace("[m]", String.valueOf(M_BRACKET_SYMBOL));
+        t = t.replace("[mm]", String.valueOf(MM_BRACKET_SYMBOL));
+        t = t.replace("[s]", String.valueOf(S_BRACKET_SYMBOL));
+        t = t.replace("[ss]", String.valueOf(SS_BRACKET_SYMBOL));
         t = t.replaceAll("s.000", "s.SSS");
         t = t.replaceAll("s.00", "s." + LL_BRACKET_SYMBOL);
         t = t.replaceAll("s.0", "s." + L_BRACKET_SYMBOL);
index e7abe58288feb13ea5ed0542eab76c32f2916076..4e95fb27517387b8d6bb7e1e7a88ef6063fbec92 100644 (file)
@@ -21,65 +21,57 @@ package org.apache.poi.ss.usermodel;
 import org.apache.poi.util.Removal;
 
 public interface Font {
+    // TODO: refactor and unify Font & FontFormatting in POI 5.0.0
+    // TODO: refactor the constants to enums in POI 5.0.0
 
     /**
      * normal type of black color.
      */
-
     short COLOR_NORMAL = 0x7fff;
 
     /**
      * Dark Red color
      */
-
     short COLOR_RED = 0xa;
 
     /**
      * no type offsetting (not super or subscript)
      */
-
     short SS_NONE = 0;
 
     /**
      * superscript
      */
-
     short SS_SUPER = 1;
 
     /**
      * subscript
      */
-
     short SS_SUB = 2;
 
     /**
      * not underlined
      */
-
     byte U_NONE = 0;
 
     /**
      * single (normal) underline
      */
-
     byte U_SINGLE = 1;
 
     /**
      * double underlined
      */
-
     byte U_DOUBLE = 2;
 
     /**
      * accounting style single underline
      */
-
     byte U_SINGLE_ACCOUNTING = 0x21;
 
     /**
      * accounting style double underline
      */
-
     byte U_DOUBLE_ACCOUNTING = 0x22;
 
     /**
index 79c5e4f845f56170e48f1202976fbc16b65ac60e..ca7335aac57dad64a0e96bd4e08595bd50bc91fc 100644 (file)
 
 package org.apache.poi.ss.usermodel;
 
+import org.apache.poi.util.Removal;
+
 /**
  * High level representation for Font Formatting component
  * of Conditional Formatting settings
  */
 public interface FontFormatting {
-    /** Escapement type - None */
-    public final static short SS_NONE  = 0;
-    /** Escapement type - Superscript */
-    public final static short SS_SUPER = 1;
-    /** Escapement type - Subscript */
-    public final static short SS_SUB   = 2;
-
-    /** Underline type - None */
-    public final static byte U_NONE              = 0;
-    /** Underline type - Single */
-    public final static byte U_SINGLE            = 1;
-    /** Underline type - Double */
-    public final static byte U_DOUBLE            = 2;
-    /**  Underline type - Single Accounting */
-    public final static byte U_SINGLE_ACCOUNTING = 0x21;
-    /** Underline type - Double Accounting */
-    public final static byte U_DOUBLE_ACCOUNTING = 0x22;
+    // TODO: refactor and unify Font & FontFormatting in POI 5.0.0
+
+    /**
+     * Escapement type - None
+     *
+     * @deprecated use {@link Font#SS_NONE} instead
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
+    short SS_NONE  = 0;
+    /**
+     * Escapement type - Superscript
+     *
+     * @deprecated use {@link Font#SS_SUPER} instead
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
+    short SS_SUPER = 1;
+    /**
+     * Escapement type - Subscript
+     *
+     * @deprecated use {@link Font#SS_SUB} instead
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
+    short SS_SUB   = 2;
+
+    /**
+     * Underline type - None
+     *
+     * @deprecated use {@link Font#U_NONE} instead
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
+    byte U_NONE              = 0;
+    /**
+     * Underline type - Single
+     *
+     * @deprecated use {@link Font#U_SINGLE} instead
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
+    byte U_SINGLE            = 1;
+    /**
+     * Underline type - Double
+     *
+     * @deprecated use {@link Font#U_DOUBLE} instead
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
+    byte U_DOUBLE            = 2;
+    /**
+     * Underline type - Single Accounting
+     *
+     * @deprecated use {@link Font#U_SINGLE_ACCOUNTING} instead
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
+    byte U_SINGLE_ACCOUNTING = 0x21;
+    /**
+     * Underline type - Double Accounting
+     *
+     * @deprecated use {@link Font#U_DOUBLE_ACCOUNTING} instead
+     */
+    @Deprecated
+    @Removal(version = "5.0.0")
+    byte U_DOUBLE_ACCOUNTING = 0x22;
 
     /**
      * get the type of super or subscript for the font
      *
      * @return super or subscript option
-     * @see #SS_NONE
-     * @see #SS_SUPER
-     * @see #SS_SUB
+     * @see Font#SS_NONE
+     * @see Font#SS_SUPER
+     * @see Font#SS_SUB
      */
     short getEscapementType();
 
@@ -56,9 +108,9 @@ public interface FontFormatting {
      * set the escapement type for the font
      *
      * @param escapementType  super or subscript option
-     * @see #SS_NONE
-     * @see #SS_SUPER
-     * @see #SS_SUB
+     * @see Font#SS_NONE
+     * @see Font#SS_SUPER
+     * @see Font#SS_SUB
      */
     void setEscapementType(short escapementType);
 
@@ -72,12 +124,12 @@ public interface FontFormatting {
      * @param color font colour index
      */
     void setFontColorIndex(short color);
-    
+
     /**
      * @return The colour of the font, or null if no colour applied
      */
     Color getFontColor();
-    
+
     /**
      * Sets the colour to use
      * @param color font colour to use
@@ -103,11 +155,11 @@ public interface FontFormatting {
      *
      * @return font underlining type
      *
-     * @see #U_NONE
-     * @see #U_SINGLE
-     * @see #U_DOUBLE
-     * @see #U_SINGLE_ACCOUNTING
-     * @see #U_DOUBLE_ACCOUNTING
+     * @see Font#U_NONE
+     * @see Font#U_SINGLE
+     * @see Font#U_DOUBLE
+     * @see Font#U_SINGLE_ACCOUNTING
+     * @see Font#U_DOUBLE_ACCOUNTING
      */
     short getUnderlineType();
 
@@ -116,11 +168,11 @@ public interface FontFormatting {
      *
      * @param underlineType  super or subscript option
      *
-     * @see #U_NONE
-     * @see #U_SINGLE
-     * @see #U_DOUBLE
-     * @see #U_SINGLE_ACCOUNTING
-     * @see #U_DOUBLE_ACCOUNTING
+     * @see Font#U_NONE
+     * @see Font#U_SINGLE
+     * @see Font#U_DOUBLE
+     * @see Font#U_SINGLE_ACCOUNTING
+     * @see Font#U_DOUBLE_ACCOUNTING
      */
     void setUnderlineType(short underlineType);
 
index 9a49da3b67b292e0815a229b1cd77e1761a42d46..6c044de6c7954d114580bf7887c0b192143a7120 100644 (file)
@@ -173,7 +173,7 @@ public final class DateFormatConverter  {
                String token;
                while( ( token = tokenizer.getNextToken() ) != null ) {
                        if( token.startsWith("'") ) {
-                               result.append( token.replaceAll("'", "\"") );
+                               result.append( token.replace('\'', '"') );
                        } else if( ! Character.isLetter( token.charAt( 0 ) ) ) {
                                result.append( token );
                        } else {
index a86c637eea1a227bdaaf825bede11a5694de1b3b..75ccf429ad9a65d1ffe468da85b5528ddbd86d88 100644 (file)
  */
 package org.apache.poi.xssf.usermodel;
 
-import org.apache.poi.ss.usermodel.*;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTUnderlineProperty;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues;
+import org.apache.poi.ss.usermodel.Color;
+import org.apache.poi.ss.usermodel.Font;
+import org.apache.poi.ss.usermodel.FontFormatting;
+import org.apache.poi.ss.usermodel.FontUnderline;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontSize;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTUnderlineProperty;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTVerticalAlignFontProperty;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignRun;
 
 
@@ -45,13 +48,13 @@ public class XSSFFontFormatting implements FontFormatting {
      * get the type of super or subscript for the font
      *
      * @return super or subscript option
-     * @see #SS_NONE
-     * @see #SS_SUPER
-     * @see #SS_SUB
+     * @see org.apache.poi.ss.usermodel.Font#SS_NONE
+     * @see org.apache.poi.ss.usermodel.Font#SS_SUPER
+     * @see org.apache.poi.ss.usermodel.Font#SS_SUB
      */
     @Override
     public short getEscapementType(){
-        if(_font.sizeOfVertAlignArray() == 0) return SS_NONE;
+        if(_font.sizeOfVertAlignArray() == 0) return org.apache.poi.ss.usermodel.Font.SS_NONE;
 
         CTVerticalAlignFontProperty prop = _font.getVertAlignArray(0);
         return (short)(prop.getVal().intValue() - 1);
@@ -61,14 +64,14 @@ public class XSSFFontFormatting implements FontFormatting {
      * set the escapement type for the font
      *
      * @param escapementType  super or subscript option
-     * @see #SS_NONE
-     * @see #SS_SUPER
-     * @see #SS_SUB
+     * @see org.apache.poi.ss.usermodel.Font#SS_NONE
+     * @see org.apache.poi.ss.usermodel.Font#SS_SUPER
+     * @see org.apache.poi.ss.usermodel.Font#SS_SUB
      */
     @Override
     public void setEscapementType(short escapementType){
         _font.setVertAlignArray(null);
-        if(escapementType != SS_NONE){
+        if(escapementType != org.apache.poi.ss.usermodel.Font.SS_NONE){
             _font.addNewVertAlign().setVal(STVerticalAlignRun.Enum.forInt(escapementType + 1));
         }
     }
@@ -76,13 +79,13 @@ public class XSSFFontFormatting implements FontFormatting {
     /**
      * XMLBeans and the XSD make this look like it can have multiple values, but it is maxOccurrs=1.
      * Use get*Array(), it is much faster than get*List().
-     * 
+     *
      * @see org.apache.poi.ss.usermodel.FontFormatting#isStruckout()
      */
     @Override
     public boolean isStruckout() {
         for (CTBooleanProperty bProp : _font.getStrikeArray()) return bProp.getVal();
-        return false; 
+        return false;
     }
 
     /**
@@ -159,22 +162,22 @@ public class XSSFFontFormatting implements FontFormatting {
      *
      * @return font underlining type
      *
-     * @see #U_NONE
-     * @see #U_SINGLE
-     * @see #U_DOUBLE
-     * @see #U_SINGLE_ACCOUNTING
-     * @see #U_DOUBLE_ACCOUNTING
+     * @see Font#U_NONE
+     * @see Font#U_SINGLE
+     * @see Font#U_DOUBLE
+     * @see Font#U_SINGLE_ACCOUNTING
+     * @see Font#U_DOUBLE_ACCOUNTING
      */
     @Override
     public short getUnderlineType(){
-        if(_font.sizeOfUArray() == 0) return U_NONE;
+        if(_font.sizeOfUArray() == 0) return Font.U_NONE;
         CTUnderlineProperty u = _font.getUArray(0);
         switch(u.getVal().intValue()){
-            case STUnderlineValues.INT_SINGLE: return U_SINGLE;
-            case STUnderlineValues.INT_DOUBLE: return U_DOUBLE;
-            case STUnderlineValues.INT_SINGLE_ACCOUNTING: return U_SINGLE_ACCOUNTING;
-            case STUnderlineValues.INT_DOUBLE_ACCOUNTING: return U_DOUBLE_ACCOUNTING;
-            default: return U_NONE;
+            case STUnderlineValues.INT_SINGLE: return Font.U_SINGLE;
+            case STUnderlineValues.INT_DOUBLE: return Font.U_DOUBLE;
+            case STUnderlineValues.INT_SINGLE_ACCOUNTING: return Font.U_SINGLE_ACCOUNTING;
+            case STUnderlineValues.INT_DOUBLE_ACCOUNTING: return Font.U_DOUBLE_ACCOUNTING;
+            default: return Font.U_NONE;
         }
     }
 
@@ -183,16 +186,16 @@ public class XSSFFontFormatting implements FontFormatting {
      *
      * @param underlineType  super or subscript option
      *
-     * @see #U_NONE
-     * @see #U_SINGLE
-     * @see #U_DOUBLE
-     * @see #U_SINGLE_ACCOUNTING
-     * @see #U_DOUBLE_ACCOUNTING
+     * @see Font#U_NONE
+     * @see Font#U_SINGLE
+     * @see Font#U_DOUBLE
+     * @see Font#U_SINGLE_ACCOUNTING
+     * @see Font#U_DOUBLE_ACCOUNTING
      */
     @Override
     public void setUnderlineType(short underlineType){
         _font.setUArray(null);
-        if(underlineType != U_NONE){
+        if(underlineType != Font.U_NONE){
             FontUnderline fenum = FontUnderline.valueOf(underlineType);
             STUnderlineValues.Enum val = STUnderlineValues.Enum.forInt(fenum.getValue());
             _font.addNewU().setVal(val);
index b297ed4f1d8a61c0a4aa8fe1a6823ce0363880cb..ae08634fec7565ccf3eeac1b262d48267e6ead50 100644 (file)
@@ -269,7 +269,7 @@ public class WordToTextConverter extends AbstractWordConverter
 
         currentBlock.appendChild( textDocumentFacade.createText( " ("
                 + UNICODECHAR_ZERO_WIDTH_SPACE
-                + hyperlink.replaceAll( "/", UNICODECHAR_ZERO_WIDTH_SPACE
+                + hyperlink.replace( "/", UNICODECHAR_ZERO_WIDTH_SPACE
                         + "\\/" + UNICODECHAR_ZERO_WIDTH_SPACE )
                 + UNICODECHAR_ZERO_WIDTH_SPACE + ")" ) );
     }
index 13336d525d861f68219946a65310253990c5b2de..23745f7fce49a33abfd6f2f73c4f1afc92d8c8dd 100644 (file)
@@ -301,12 +301,12 @@ public final class TestCFRuleRecord {
         fontFormatting.setBold(false);
         assertFalse(fontFormatting.isBold());
 
-        fontFormatting.setEscapementType(FontFormatting.SS_SUB);
-        assertEquals(FontFormatting.SS_SUB, fontFormatting.getEscapementType());
-        fontFormatting.setEscapementType(FontFormatting.SS_SUPER);
-        assertEquals(FontFormatting.SS_SUPER, fontFormatting.getEscapementType());
-        fontFormatting.setEscapementType(FontFormatting.SS_NONE);
-        assertEquals(FontFormatting.SS_NONE, fontFormatting.getEscapementType());
+        fontFormatting.setEscapementType(org.apache.poi.ss.usermodel.Font.SS_SUB);
+        assertEquals(org.apache.poi.ss.usermodel.Font.SS_SUB, fontFormatting.getEscapementType());
+        fontFormatting.setEscapementType(org.apache.poi.ss.usermodel.Font.SS_SUPER);
+        assertEquals(org.apache.poi.ss.usermodel.Font.SS_SUPER, fontFormatting.getEscapementType());
+        fontFormatting.setEscapementType(org.apache.poi.ss.usermodel.Font.SS_NONE);
+        assertEquals(org.apache.poi.ss.usermodel.Font.SS_NONE, fontFormatting.getEscapementType());
 
         fontFormatting.setEscapementTypeModified(false);
         assertFalse(fontFormatting.isEscapementTypeModified());
@@ -364,8 +364,8 @@ public final class TestCFRuleRecord {
         fontFormatting.setStrikeout(true);
         assertTrue(fontFormatting.isStruckout());
 
-        fontFormatting.setUnderlineType(FontFormatting.U_DOUBLE_ACCOUNTING);
-        assertEquals(FontFormatting.U_DOUBLE_ACCOUNTING, fontFormatting.getUnderlineType());
+        fontFormatting.setUnderlineType(org.apache.poi.ss.usermodel.Font.U_DOUBLE_ACCOUNTING);
+        assertEquals(org.apache.poi.ss.usermodel.Font.U_DOUBLE_ACCOUNTING, fontFormatting.getUnderlineType());
 
         fontFormatting.setUnderlineTypeModified(false);
         assertFalse(fontFormatting.isUnderlineTypeModified());
index 0399c964ed086a10b44dddce9a63c0fb4cc33002..fd33cf0319e3462f09c59ebb600571109db97ba8 100644 (file)
 
 package org.apache.poi.ss.usermodel;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+
 import org.apache.poi.hssf.usermodel.HSSFConditionalFormatting;
 import org.apache.poi.hssf.usermodel.HSSFConditionalFormattingRule;
 import org.apache.poi.ss.ITestDataProvider;
@@ -27,80 +36,76 @@ import org.apache.poi.ss.usermodel.IconMultiStateFormatting.IconSet;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.junit.Test;
 
-import java.io.IOException;
-
-import static org.junit.Assert.*;
-
 /**
  * Base tests for Conditional Formatting, for both HSSF and XSSF
  */
 public abstract class BaseTestConditionalFormatting {
     private final ITestDataProvider _testDataProvider;
 
-    public BaseTestConditionalFormatting(ITestDataProvider testDataProvider){
+    protected BaseTestConditionalFormatting(ITestDataProvider testDataProvider){
         _testDataProvider = testDataProvider;
     }
-    
+
     protected abstract void assertColour(String hexExpected, Color actual);
 
     @Test
     public void testBasic() throws IOException {
-        Workbook wb = _testDataProvider.createWorkbook();
-        Sheet sh = wb.createSheet();
-        SheetConditionalFormatting sheetCF = sh.getSheetConditionalFormatting();
-
-        assertEquals(0, sheetCF.getNumConditionalFormattings());
-        try {
-            assertNull(sheetCF.getConditionalFormattingAt(0));
-            fail("expected exception");
-        } catch (IllegalArgumentException e) {
-            assertTrue(e.getMessage().startsWith("Specified CF index 0 is outside the allowable range"));
-        }
+        try (Workbook wb = _testDataProvider.createWorkbook()) {
+            Sheet sh = wb.createSheet();
+            SheetConditionalFormatting sheetCF = sh.getSheetConditionalFormatting();
+
+            assertEquals(0, sheetCF.getNumConditionalFormattings());
+            try {
+                assertNull(sheetCF.getConditionalFormattingAt(0));
+                fail("expected exception");
+            } catch (IllegalArgumentException e) {
+                assertTrue(e.getMessage().startsWith("Specified CF index 0 is outside the allowable range"));
+            }
 
-        try {
-            sheetCF.removeConditionalFormatting(0);
-            fail("expected exception");
-        } catch (IllegalArgumentException e) {
-            assertTrue(e.getMessage().startsWith("Specified CF index 0 is outside the allowable range"));
-        }
+            try {
+                sheetCF.removeConditionalFormatting(0);
+                fail("expected exception");
+            } catch (IllegalArgumentException e) {
+                assertTrue(e.getMessage().startsWith("Specified CF index 0 is outside the allowable range"));
+            }
 
-        ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule("1");
-        ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule("2");
-        ConditionalFormattingRule rule3 = sheetCF.createConditionalFormattingRule("3");
-        ConditionalFormattingRule rule4 = sheetCF.createConditionalFormattingRule("4");
-        try {
-            sheetCF.addConditionalFormatting(null, rule1);
-            fail("expected exception");
-        } catch (IllegalArgumentException e) {
-            assertTrue(e.getMessage().startsWith("regions must not be null"));
-        }
-        try {
-            sheetCF.addConditionalFormatting(
-                    new CellRangeAddress[]{ CellRangeAddress.valueOf("A1:A3") },
-                    (ConditionalFormattingRule)null);
-            fail("expected exception");
-        } catch (IllegalArgumentException e) {
-            assertTrue(e.getMessage().startsWith("cfRules must not be null"));
-        }
+            ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule("1");
+            ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule("2");
+            ConditionalFormattingRule rule3 = sheetCF.createConditionalFormattingRule("3");
+            ConditionalFormattingRule rule4 = sheetCF.createConditionalFormattingRule("4");
+            try {
+                sheetCF.addConditionalFormatting(null, rule1);
+                fail("expected exception");
+            } catch (IllegalArgumentException e) {
+                assertTrue(e.getMessage().startsWith("regions must not be null"));
+            }
+            try {
+                sheetCF.addConditionalFormatting(
+                        new CellRangeAddress[]{CellRangeAddress.valueOf("A1:A3")},
+                        (ConditionalFormattingRule) null);
+                fail("expected exception");
+            } catch (IllegalArgumentException e) {
+                assertTrue(e.getMessage().startsWith("cfRules must not be null"));
+            }
 
-        try {
-            sheetCF.addConditionalFormatting(
-                    new CellRangeAddress[]{ CellRangeAddress.valueOf("A1:A3") },
-                    new ConditionalFormattingRule[0]);
-            fail("expected exception");
-        } catch (IllegalArgumentException e) {
-            assertTrue(e.getMessage().startsWith("cfRules must not be empty"));
-        }
+            try {
+                sheetCF.addConditionalFormatting(
+                        new CellRangeAddress[]{CellRangeAddress.valueOf("A1:A3")},
+                        new ConditionalFormattingRule[0]);
+                fail("expected exception");
+            } catch (IllegalArgumentException e) {
+                assertTrue(e.getMessage().startsWith("cfRules must not be empty"));
+            }
 
-        try {
-            sheetCF.addConditionalFormatting(
-                    new CellRangeAddress[]{ CellRangeAddress.valueOf("A1:A3") },
-                    new ConditionalFormattingRule[]{rule1, rule2, rule3, rule4});
-            fail("expected exception");
-        } catch (IllegalArgumentException e) {
-            assertTrue(e.getMessage().startsWith("Number of rules must not exceed 3"));
+            try {
+                sheetCF.addConditionalFormatting(
+                        new CellRangeAddress[]{CellRangeAddress.valueOf("A1:A3")},
+                        new ConditionalFormattingRule[]{rule1, rule2, rule3, rule4});
+                fail("expected exception");
+            } catch (IllegalArgumentException e) {
+                assertTrue(e.getMessage().startsWith("Number of rules must not exceed 3"));
+            }
         }
-        wb.close();
     }
 
     /**
@@ -108,660 +113,640 @@ public abstract class BaseTestConditionalFormatting {
      */
     @Test
     public void testBooleanFormulaConditions() throws IOException {
-        Workbook wb = _testDataProvider.createWorkbook();
-        Sheet sh = wb.createSheet();
-        SheetConditionalFormatting sheetCF = sh.getSheetConditionalFormatting();
-
-        ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule("SUM(A1:A5)>10");
-        assertEquals(ConditionType.FORMULA, rule1.getConditionType());
-        assertEquals("SUM(A1:A5)>10", rule1.getFormula1());
-        int formatIndex1 = sheetCF.addConditionalFormatting(
-                new CellRangeAddress[]{
-                        CellRangeAddress.valueOf("B1"),
-                        CellRangeAddress.valueOf("C3"),
-                }, rule1);
-        assertEquals(0, formatIndex1);
-        assertEquals(1, sheetCF.getNumConditionalFormattings());
-        CellRangeAddress[] ranges1 = sheetCF.getConditionalFormattingAt(formatIndex1).getFormattingRanges();
-        assertEquals(2, ranges1.length);
-        assertEquals("B1", ranges1[0].formatAsString());
-        assertEquals("C3", ranges1[1].formatAsString());
-
-        // adjacent address are merged
-        int formatIndex2 = sheetCF.addConditionalFormatting(
-                new CellRangeAddress[]{
-                        CellRangeAddress.valueOf("B1"),
-                        CellRangeAddress.valueOf("B2"),
-                        CellRangeAddress.valueOf("B3"),
-                }, rule1);
-        assertEquals(1, formatIndex2);
-        assertEquals(2, sheetCF.getNumConditionalFormattings());
-        CellRangeAddress[] ranges2 = sheetCF.getConditionalFormattingAt(formatIndex2).getFormattingRanges();
-        assertEquals(1, ranges2.length);
-        assertEquals("B1:B3", ranges2[0].formatAsString());
-        wb.close();
+        try (Workbook wb = _testDataProvider.createWorkbook()) {
+            Sheet sh = wb.createSheet();
+            SheetConditionalFormatting sheetCF = sh.getSheetConditionalFormatting();
+
+            ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule("SUM(A1:A5)>10");
+            assertEquals(ConditionType.FORMULA, rule1.getConditionType());
+            assertEquals("SUM(A1:A5)>10", rule1.getFormula1());
+            int formatIndex1 = sheetCF.addConditionalFormatting(
+                    new CellRangeAddress[]{
+                            CellRangeAddress.valueOf("B1"),
+                            CellRangeAddress.valueOf("C3"),
+                    }, rule1);
+            assertEquals(0, formatIndex1);
+            assertEquals(1, sheetCF.getNumConditionalFormattings());
+            CellRangeAddress[] ranges1 = sheetCF.getConditionalFormattingAt(formatIndex1).getFormattingRanges();
+            assertEquals(2, ranges1.length);
+            assertEquals("B1", ranges1[0].formatAsString());
+            assertEquals("C3", ranges1[1].formatAsString());
+
+            // adjacent address are merged
+            int formatIndex2 = sheetCF.addConditionalFormatting(
+                    new CellRangeAddress[]{
+                            CellRangeAddress.valueOf("B1"),
+                            CellRangeAddress.valueOf("B2"),
+                            CellRangeAddress.valueOf("B3"),
+                    }, rule1);
+            assertEquals(1, formatIndex2);
+            assertEquals(2, sheetCF.getNumConditionalFormattings());
+            CellRangeAddress[] ranges2 = sheetCF.getConditionalFormattingAt(formatIndex2).getFormattingRanges();
+            assertEquals(1, ranges2.length);
+            assertEquals("B1:B3", ranges2[0].formatAsString());
+        }
     }
 
     @Test
     public void testSingleFormulaConditions() throws IOException {
-        Workbook wb = _testDataProvider.createWorkbook();
-        Sheet sh = wb.createSheet();
-        SheetConditionalFormatting sheetCF = sh.getSheetConditionalFormatting();
-
-        ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(
-                ComparisonOperator.EQUAL, "SUM(A1:A5)+10");
-        assertEquals(ConditionType.CELL_VALUE_IS, rule1.getConditionType());
-        assertEquals("SUM(A1:A5)+10", rule1.getFormula1());
-        assertEquals(ComparisonOperator.EQUAL, rule1.getComparisonOperation());
-
-        ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(
-                ComparisonOperator.NOT_EQUAL, "15");
-        assertEquals(ConditionType.CELL_VALUE_IS, rule2.getConditionType());
-        assertEquals("15", rule2.getFormula1());
-        assertEquals(ComparisonOperator.NOT_EQUAL, rule2.getComparisonOperation());
-
-        ConditionalFormattingRule rule3 = sheetCF.createConditionalFormattingRule(
-                ComparisonOperator.NOT_EQUAL, "15");
-        assertEquals(ConditionType.CELL_VALUE_IS, rule3.getConditionType());
-        assertEquals("15", rule3.getFormula1());
-        assertEquals(ComparisonOperator.NOT_EQUAL, rule3.getComparisonOperation());
-
-        ConditionalFormattingRule rule4 = sheetCF.createConditionalFormattingRule(
-                ComparisonOperator.GT, "0");
-        assertEquals(ConditionType.CELL_VALUE_IS, rule4.getConditionType());
-        assertEquals("0", rule4.getFormula1());
-        assertEquals(ComparisonOperator.GT, rule4.getComparisonOperation());
-
-        ConditionalFormattingRule rule5 = sheetCF.createConditionalFormattingRule(
-                ComparisonOperator.LT, "0");
-        assertEquals(ConditionType.CELL_VALUE_IS, rule5.getConditionType());
-        assertEquals("0", rule5.getFormula1());
-        assertEquals(ComparisonOperator.LT, rule5.getComparisonOperation());
-
-        ConditionalFormattingRule rule6 = sheetCF.createConditionalFormattingRule(
-                ComparisonOperator.GE, "0");
-        assertEquals(ConditionType.CELL_VALUE_IS, rule6.getConditionType());
-        assertEquals("0", rule6.getFormula1());
-        assertEquals(ComparisonOperator.GE, rule6.getComparisonOperation());
-
-        ConditionalFormattingRule rule7 = sheetCF.createConditionalFormattingRule(
-                ComparisonOperator.LE, "0");
-        assertEquals(ConditionType.CELL_VALUE_IS, rule7.getConditionType());
-        assertEquals("0", rule7.getFormula1());
-        assertEquals(ComparisonOperator.LE, rule7.getComparisonOperation());
-
-        ConditionalFormattingRule rule8 = sheetCF.createConditionalFormattingRule(
-                ComparisonOperator.BETWEEN, "0", "5");
-        assertEquals(ConditionType.CELL_VALUE_IS, rule8.getConditionType());
-        assertEquals("0", rule8.getFormula1());
-        assertEquals("5", rule8.getFormula2());
-        assertEquals(ComparisonOperator.BETWEEN, rule8.getComparisonOperation());
-
-        ConditionalFormattingRule rule9 = sheetCF.createConditionalFormattingRule(
-                ComparisonOperator.NOT_BETWEEN, "0", "5");
-        assertEquals(ConditionType.CELL_VALUE_IS, rule9.getConditionType());
-        assertEquals("0", rule9.getFormula1());
-        assertEquals("5", rule9.getFormula2());
-        assertEquals(ComparisonOperator.NOT_BETWEEN, rule9.getComparisonOperation());
-        
-        wb.close();
+        try (Workbook wb = _testDataProvider.createWorkbook()) {
+            Sheet sh = wb.createSheet();
+            SheetConditionalFormatting sheetCF = sh.getSheetConditionalFormatting();
+
+            ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(
+                    ComparisonOperator.EQUAL, "SUM(A1:A5)+10");
+            assertEquals(ConditionType.CELL_VALUE_IS, rule1.getConditionType());
+            assertEquals("SUM(A1:A5)+10", rule1.getFormula1());
+            assertEquals(ComparisonOperator.EQUAL, rule1.getComparisonOperation());
+
+            ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(
+                    ComparisonOperator.NOT_EQUAL, "15");
+            assertEquals(ConditionType.CELL_VALUE_IS, rule2.getConditionType());
+            assertEquals("15", rule2.getFormula1());
+            assertEquals(ComparisonOperator.NOT_EQUAL, rule2.getComparisonOperation());
+
+            ConditionalFormattingRule rule3 = sheetCF.createConditionalFormattingRule(
+                    ComparisonOperator.NOT_EQUAL, "15");
+            assertEquals(ConditionType.CELL_VALUE_IS, rule3.getConditionType());
+            assertEquals("15", rule3.getFormula1());
+            assertEquals(ComparisonOperator.NOT_EQUAL, rule3.getComparisonOperation());
+
+            ConditionalFormattingRule rule4 = sheetCF.createConditionalFormattingRule(
+                    ComparisonOperator.GT, "0");
+            assertEquals(ConditionType.CELL_VALUE_IS, rule4.getConditionType());
+            assertEquals("0", rule4.getFormula1());
+            assertEquals(ComparisonOperator.GT, rule4.getComparisonOperation());
+
+            ConditionalFormattingRule rule5 = sheetCF.createConditionalFormattingRule(
+                    ComparisonOperator.LT, "0");
+            assertEquals(ConditionType.CELL_VALUE_IS, rule5.getConditionType());
+            assertEquals("0", rule5.getFormula1());
+            assertEquals(ComparisonOperator.LT, rule5.getComparisonOperation());
+
+            ConditionalFormattingRule rule6 = sheetCF.createConditionalFormattingRule(
+                    ComparisonOperator.GE, "0");
+            assertEquals(ConditionType.CELL_VALUE_IS, rule6.getConditionType());
+            assertEquals("0", rule6.getFormula1());
+            assertEquals(ComparisonOperator.GE, rule6.getComparisonOperation());
+
+            ConditionalFormattingRule rule7 = sheetCF.createConditionalFormattingRule(
+                    ComparisonOperator.LE, "0");
+            assertEquals(ConditionType.CELL_VALUE_IS, rule7.getConditionType());
+            assertEquals("0", rule7.getFormula1());
+            assertEquals(ComparisonOperator.LE, rule7.getComparisonOperation());
+
+            ConditionalFormattingRule rule8 = sheetCF.createConditionalFormattingRule(
+                    ComparisonOperator.BETWEEN, "0", "5");
+            assertEquals(ConditionType.CELL_VALUE_IS, rule8.getConditionType());
+            assertEquals("0", rule8.getFormula1());
+            assertEquals("5", rule8.getFormula2());
+            assertEquals(ComparisonOperator.BETWEEN, rule8.getComparisonOperation());
+
+            ConditionalFormattingRule rule9 = sheetCF.createConditionalFormattingRule(
+                    ComparisonOperator.NOT_BETWEEN, "0", "5");
+            assertEquals(ConditionType.CELL_VALUE_IS, rule9.getConditionType());
+            assertEquals("0", rule9.getFormula1());
+            assertEquals("5", rule9.getFormula2());
+            assertEquals(ComparisonOperator.NOT_BETWEEN, rule9.getComparisonOperation());
+        }
     }
 
     @Test
     public void testCopy() throws IOException {
-        Workbook wb = _testDataProvider.createWorkbook();
-        Sheet sheet1 = wb.createSheet();
-        Sheet sheet2 = wb.createSheet();
-        SheetConditionalFormatting sheet1CF = sheet1.getSheetConditionalFormatting();
-        SheetConditionalFormatting sheet2CF = sheet2.getSheetConditionalFormatting();
-        assertEquals(0, sheet1CF.getNumConditionalFormattings());
-        assertEquals(0, sheet2CF.getNumConditionalFormattings());
-
-        ConditionalFormattingRule rule1 = sheet1CF.createConditionalFormattingRule(
-                ComparisonOperator.EQUAL, "SUM(A1:A5)+10");
-
-        ConditionalFormattingRule rule2 = sheet1CF.createConditionalFormattingRule(
-                ComparisonOperator.NOT_EQUAL, "15");
-
-        // adjacent address are merged
-        int formatIndex = sheet1CF.addConditionalFormatting(
-                new CellRangeAddress[]{
-                        CellRangeAddress.valueOf("A1:A5"),
-                        CellRangeAddress.valueOf("C1:C5")
-                }, rule1, rule2);
-        assertEquals(0, formatIndex);
-        assertEquals(1, sheet1CF.getNumConditionalFormattings());
-
-        assertEquals(0, sheet2CF.getNumConditionalFormattings());
-        sheet2CF.addConditionalFormatting(sheet1CF.getConditionalFormattingAt(formatIndex));
-        assertEquals(1, sheet2CF.getNumConditionalFormattings());
-
-        ConditionalFormatting sheet2cf = sheet2CF.getConditionalFormattingAt(0);
-        assertEquals(2, sheet2cf.getNumberOfRules());
-        assertEquals("SUM(A1:A5)+10", sheet2cf.getRule(0).getFormula1());
-        assertEquals(ComparisonOperator.EQUAL, sheet2cf.getRule(0).getComparisonOperation());
-        assertEquals(ConditionType.CELL_VALUE_IS, sheet2cf.getRule(0).getConditionType());
-        assertEquals("15", sheet2cf.getRule(1).getFormula1());
-        assertEquals(ComparisonOperator.NOT_EQUAL, sheet2cf.getRule(1).getComparisonOperation());
-        assertEquals(ConditionType.CELL_VALUE_IS, sheet2cf.getRule(1).getConditionType());
-        
-        wb.close();
+        try (Workbook wb = _testDataProvider.createWorkbook()) {
+            Sheet sheet1 = wb.createSheet();
+            Sheet sheet2 = wb.createSheet();
+            SheetConditionalFormatting sheet1CF = sheet1.getSheetConditionalFormatting();
+            SheetConditionalFormatting sheet2CF = sheet2.getSheetConditionalFormatting();
+            assertEquals(0, sheet1CF.getNumConditionalFormattings());
+            assertEquals(0, sheet2CF.getNumConditionalFormattings());
+
+            ConditionalFormattingRule rule1 = sheet1CF.createConditionalFormattingRule(
+                    ComparisonOperator.EQUAL, "SUM(A1:A5)+10");
+
+            ConditionalFormattingRule rule2 = sheet1CF.createConditionalFormattingRule(
+                    ComparisonOperator.NOT_EQUAL, "15");
+
+            // adjacent address are merged
+            int formatIndex = sheet1CF.addConditionalFormatting(
+                    new CellRangeAddress[]{
+                            CellRangeAddress.valueOf("A1:A5"),
+                            CellRangeAddress.valueOf("C1:C5")
+                    }, rule1, rule2);
+            assertEquals(0, formatIndex);
+            assertEquals(1, sheet1CF.getNumConditionalFormattings());
+
+            assertEquals(0, sheet2CF.getNumConditionalFormattings());
+            sheet2CF.addConditionalFormatting(sheet1CF.getConditionalFormattingAt(formatIndex));
+            assertEquals(1, sheet2CF.getNumConditionalFormattings());
+
+            ConditionalFormatting sheet2cf = sheet2CF.getConditionalFormattingAt(0);
+            assertEquals(2, sheet2cf.getNumberOfRules());
+            assertEquals("SUM(A1:A5)+10", sheet2cf.getRule(0).getFormula1());
+            assertEquals(ComparisonOperator.EQUAL, sheet2cf.getRule(0).getComparisonOperation());
+            assertEquals(ConditionType.CELL_VALUE_IS, sheet2cf.getRule(0).getConditionType());
+            assertEquals("15", sheet2cf.getRule(1).getFormula1());
+            assertEquals(ComparisonOperator.NOT_EQUAL, sheet2cf.getRule(1).getComparisonOperation());
+            assertEquals(ConditionType.CELL_VALUE_IS, sheet2cf.getRule(1).getConditionType());
+        }
     }
 
     @Test
     public void testRemove() throws IOException {
-        Workbook wb = _testDataProvider.createWorkbook();
-        Sheet sheet1 = wb.createSheet();
-        SheetConditionalFormatting sheetCF = sheet1.getSheetConditionalFormatting();
-        assertEquals(0, sheetCF.getNumConditionalFormattings());
-
-        ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(
-                ComparisonOperator.EQUAL, "SUM(A1:A5)");
-
-        // adjacent address are merged
-        int formatIndex = sheetCF.addConditionalFormatting(
-                new CellRangeAddress[]{
-                        CellRangeAddress.valueOf("A1:A5")
-                }, rule1);
-        assertEquals(0, formatIndex);
-        assertEquals(1, sheetCF.getNumConditionalFormattings());
-        sheetCF.removeConditionalFormatting(0);
-        assertEquals(0, sheetCF.getNumConditionalFormattings());
-        try {
-            assertNull(sheetCF.getConditionalFormattingAt(0));
-            fail("expected exception");
-        } catch (IllegalArgumentException e) {
-            assertTrue(e.getMessage().startsWith("Specified CF index 0 is outside the allowable range"));
-        }
+        try (Workbook wb = _testDataProvider.createWorkbook()) {
+            Sheet sheet1 = wb.createSheet();
+            SheetConditionalFormatting sheetCF = sheet1.getSheetConditionalFormatting();
+            assertEquals(0, sheetCF.getNumConditionalFormattings());
+
+            ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(
+                    ComparisonOperator.EQUAL, "SUM(A1:A5)");
+
+            // adjacent address are merged
+            int formatIndex = sheetCF.addConditionalFormatting(
+                    new CellRangeAddress[]{
+                            CellRangeAddress.valueOf("A1:A5")
+                    }, rule1);
+            assertEquals(0, formatIndex);
+            assertEquals(1, sheetCF.getNumConditionalFormattings());
+            sheetCF.removeConditionalFormatting(0);
+            assertEquals(0, sheetCF.getNumConditionalFormattings());
+            try {
+                assertNull(sheetCF.getConditionalFormattingAt(0));
+                fail("expected exception");
+            } catch (IllegalArgumentException e) {
+                assertTrue(e.getMessage().startsWith("Specified CF index 0 is outside the allowable range"));
+            }
 
-        formatIndex = sheetCF.addConditionalFormatting(
-                new CellRangeAddress[]{
-                        CellRangeAddress.valueOf("A1:A5")
-                }, rule1);
-        assertEquals(0, formatIndex);
-        assertEquals(1, sheetCF.getNumConditionalFormattings());
-        sheetCF.removeConditionalFormatting(0);
-        assertEquals(0, sheetCF.getNumConditionalFormattings());
-        try {
-            assertNull(sheetCF.getConditionalFormattingAt(0));
-            fail("expected exception");
-        } catch (IllegalArgumentException e) {
-            assertTrue(e.getMessage().startsWith("Specified CF index 0 is outside the allowable range"));
+            formatIndex = sheetCF.addConditionalFormatting(
+                    new CellRangeAddress[]{
+                            CellRangeAddress.valueOf("A1:A5")
+                    }, rule1);
+            assertEquals(0, formatIndex);
+            assertEquals(1, sheetCF.getNumConditionalFormattings());
+            sheetCF.removeConditionalFormatting(0);
+            assertEquals(0, sheetCF.getNumConditionalFormattings());
+            try {
+                assertNull(sheetCF.getConditionalFormattingAt(0));
+                fail("expected exception");
+            } catch (IllegalArgumentException e) {
+                assertTrue(e.getMessage().startsWith("Specified CF index 0 is outside the allowable range"));
+            }
         }
-        
-        wb.close();
     }
-    
+
     @Test
     public void testCreateCF() throws IOException {
-        Workbook workbook = _testDataProvider.createWorkbook();
-        Sheet sheet = workbook.createSheet();
-        String formula = "7";
-
-        SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
-
-        ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(formula);
-        FontFormatting fontFmt = rule1.createFontFormatting();
-        fontFmt.setFontStyle(true, false);
-
-        BorderFormatting bordFmt = rule1.createBorderFormatting();
-        bordFmt.setBorderBottom(BorderStyle.THIN);
-        bordFmt.setBorderTop(BorderStyle.THICK);
-        bordFmt.setBorderLeft(BorderStyle.DASHED);
-        bordFmt.setBorderRight(BorderStyle.DOTTED);
-
-        PatternFormatting patternFmt = rule1.createPatternFormatting();
-        patternFmt.setFillBackgroundColor(IndexedColors.YELLOW.index);
-
-
-        ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(ComparisonOperator.BETWEEN, "1", "2");
-        ConditionalFormattingRule [] cfRules =
-        {
-            rule1, rule2
-        };
-
-        short col = 1;
-        CellRangeAddress [] regions = {
-            new CellRangeAddress(0, 65535, col, col)
-        };
-
-        sheetCF.addConditionalFormatting(regions, cfRules);
-        sheetCF.addConditionalFormatting(regions, cfRules);
-
-        // Verification
-        assertEquals(2, sheetCF.getNumConditionalFormattings());
-        sheetCF.removeConditionalFormatting(1);
-        assertEquals(1, sheetCF.getNumConditionalFormattings());
-        ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
-        assertNotNull(cf);
-
-        regions = cf.getFormattingRanges();
-        assertNotNull(regions);
-        assertEquals(1, regions.length);
-        CellRangeAddress r = regions[0];
-        assertEquals(1, r.getFirstColumn());
-        assertEquals(1, r.getLastColumn());
-        assertEquals(0, r.getFirstRow());
-        assertEquals(65535, r.getLastRow());
-
-        assertEquals(2, cf.getNumberOfRules());
-
-        rule1 = cf.getRule(0);
-        assertEquals("7",rule1.getFormula1());
-        assertNull(rule1.getFormula2());
-
-        FontFormatting    r1fp = rule1.getFontFormatting();
-        assertNotNull(r1fp);
-
-        assertTrue(r1fp.isItalic());
-        assertFalse(r1fp.isBold());
-
-        BorderFormatting  r1bf = rule1.getBorderFormatting();
-        assertNotNull(r1bf);
-        assertEquals(BorderStyle.THIN, r1bf.getBorderBottom());
-        assertEquals(BorderStyle.THICK,r1bf.getBorderTop());
-        assertEquals(BorderStyle.DASHED,r1bf.getBorderLeft());
-        assertEquals(BorderStyle.DOTTED,r1bf.getBorderRight());
-
-        PatternFormatting r1pf = rule1.getPatternFormatting();
-        assertNotNull(r1pf);
+        try (Workbook workbook = _testDataProvider.createWorkbook()) {
+            Sheet sheet = workbook.createSheet();
+            String formula = "7";
+
+            SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
+
+            ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(formula);
+            FontFormatting fontFmt = rule1.createFontFormatting();
+            fontFmt.setFontStyle(true, false);
+
+            BorderFormatting bordFmt = rule1.createBorderFormatting();
+            bordFmt.setBorderBottom(BorderStyle.THIN);
+            bordFmt.setBorderTop(BorderStyle.THICK);
+            bordFmt.setBorderLeft(BorderStyle.DASHED);
+            bordFmt.setBorderRight(BorderStyle.DOTTED);
+
+            PatternFormatting patternFmt = rule1.createPatternFormatting();
+            patternFmt.setFillBackgroundColor(IndexedColors.YELLOW.index);
+
+
+            ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(ComparisonOperator.BETWEEN, "1", "2");
+            ConditionalFormattingRule[] cfRules = {rule1, rule2};
+
+            short col = 1;
+            CellRangeAddress[] regions = {
+                    new CellRangeAddress(0, 65535, col, col)
+            };
+
+            sheetCF.addConditionalFormatting(regions, cfRules);
+            sheetCF.addConditionalFormatting(regions, cfRules);
+
+            // Verification
+            assertEquals(2, sheetCF.getNumConditionalFormattings());
+            sheetCF.removeConditionalFormatting(1);
+            assertEquals(1, sheetCF.getNumConditionalFormattings());
+            ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
+            assertNotNull(cf);
+
+            regions = cf.getFormattingRanges();
+            assertNotNull(regions);
+            assertEquals(1, regions.length);
+            CellRangeAddress r = regions[0];
+            assertEquals(1, r.getFirstColumn());
+            assertEquals(1, r.getLastColumn());
+            assertEquals(0, r.getFirstRow());
+            assertEquals(65535, r.getLastRow());
+
+            assertEquals(2, cf.getNumberOfRules());
+
+            rule1 = cf.getRule(0);
+            assertEquals("7", rule1.getFormula1());
+            assertNull(rule1.getFormula2());
+
+            FontFormatting r1fp = rule1.getFontFormatting();
+            assertNotNull(r1fp);
+
+            assertTrue(r1fp.isItalic());
+            assertFalse(r1fp.isBold());
+
+            BorderFormatting r1bf = rule1.getBorderFormatting();
+            assertNotNull(r1bf);
+            assertEquals(BorderStyle.THIN, r1bf.getBorderBottom());
+            assertEquals(BorderStyle.THICK, r1bf.getBorderTop());
+            assertEquals(BorderStyle.DASHED, r1bf.getBorderLeft());
+            assertEquals(BorderStyle.DOTTED, r1bf.getBorderRight());
+
+            PatternFormatting r1pf = rule1.getPatternFormatting();
+            assertNotNull(r1pf);
 //        assertEquals(IndexedColors.YELLOW.index,r1pf.getFillBackgroundColor());
 
-        rule2 = cf.getRule(1);
-        assertEquals("2",rule2.getFormula2());
-        assertEquals("1",rule2.getFormula1());
-        
-        workbook.close();
+            rule2 = cf.getRule(1);
+            assertEquals("2", rule2.getFormula2());
+            assertEquals("1", rule2.getFormula1());
+        }
     }
 
     @Test
     public void testClone() throws IOException {
+        try (Workbook wb = _testDataProvider.createWorkbook()) {
+            Sheet sheet = wb.createSheet();
+            String formula = "7";
 
-        Workbook wb = _testDataProvider.createWorkbook();
-        Sheet sheet = wb.createSheet();
-        String formula = "7";
-
-        SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
+            SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
 
-        ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(formula);
-        FontFormatting fontFmt = rule1.createFontFormatting();
-        fontFmt.setFontStyle(true, false);
+            ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(formula);
+            FontFormatting fontFmt = rule1.createFontFormatting();
+            fontFmt.setFontStyle(true, false);
 
-        PatternFormatting patternFmt = rule1.createPatternFormatting();
-        patternFmt.setFillBackgroundColor(IndexedColors.YELLOW.index);
+            PatternFormatting patternFmt = rule1.createPatternFormatting();
+            patternFmt.setFillBackgroundColor(IndexedColors.YELLOW.index);
 
 
-        ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(ComparisonOperator.BETWEEN, "1", "2");
-        ConditionalFormattingRule [] cfRules =
-        {
-            rule1, rule2
-        };
+            ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(ComparisonOperator.BETWEEN, "1", "2");
+            ConditionalFormattingRule[] cfRules = {rule1, rule2};
 
-        short col = 1;
-        CellRangeAddress [] regions = {
-            new CellRangeAddress(0, 65535, col, col)
-        };
+            short col = 1;
+            CellRangeAddress[] regions = {
+                    new CellRangeAddress(0, 65535, col, col)
+            };
 
-        sheetCF.addConditionalFormatting(regions, cfRules);
+            sheetCF.addConditionalFormatting(regions, cfRules);
 
-        try {
             wb.cloneSheet(0);
             assertEquals(2, wb.getNumberOfSheets());
-        } catch (RuntimeException e) {
-            if (e.getMessage().indexOf("needs to define a clone method") > 0) {
-                fail("Identified bug 45682");
-            }
-            throw e;
-        } finally {
-            wb.close();
+            // bug 45682 leads to runtime exception "needs to define a clone method"
         }
     }
 
     @Test
     public void testShiftRows() throws IOException {
-        Workbook wb = _testDataProvider.createWorkbook();
-        Sheet sheet = wb.createSheet();
-
-        SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
-
-        ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(
-                ComparisonOperator.BETWEEN, "SUM(A10:A15)", "1+SUM(B16:B30)");
-        FontFormatting fontFmt = rule1.createFontFormatting();
-        fontFmt.setFontStyle(true, false);
-
-        PatternFormatting patternFmt = rule1.createPatternFormatting();
-        patternFmt.setFillBackgroundColor(IndexedColors.YELLOW.index);
-
-        ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(
-                ComparisonOperator.BETWEEN, "SUM(A10:A15)", "1+SUM(B16:B30)");
-        BorderFormatting borderFmt = rule2.createBorderFormatting();
-        borderFmt.setBorderDiagonal(BorderStyle.MEDIUM);
-
-        CellRangeAddress [] regions = {
-            new CellRangeAddress(2, 4, 0, 0), // A3:A5
-        };
-        sheetCF.addConditionalFormatting(regions, rule1);
-        sheetCF.addConditionalFormatting(regions, rule2);
-
-        // This row-shift should destroy the CF region
-        sheet.shiftRows(10, 20, -9);
-        assertEquals(0, sheetCF.getNumConditionalFormattings());
-
-        // re-add the CF
-        sheetCF.addConditionalFormatting(regions, rule1);
-        sheetCF.addConditionalFormatting(regions, rule2);
-
-        // This row shift should only affect the formulas
-        sheet.shiftRows(14, 17, 8);
-        ConditionalFormatting cf1 = sheetCF.getConditionalFormattingAt(0);
-        assertEquals("SUM(A10:A23)", cf1.getRule(0).getFormula1());
-        assertEquals("1+SUM(B24:B30)", cf1.getRule(0).getFormula2());
-        ConditionalFormatting cf2 = sheetCF.getConditionalFormattingAt(1);
-        assertEquals("SUM(A10:A23)", cf2.getRule(0).getFormula1());
-        assertEquals("1+SUM(B24:B30)", cf2.getRule(0).getFormula2());
-
-        sheet.shiftRows(0, 8, 21);
-        cf1 = sheetCF.getConditionalFormattingAt(0);
-        assertEquals("SUM(A10:A21)", cf1.getRule(0).getFormula1());
-        assertEquals("1+SUM(#REF!)", cf1.getRule(0).getFormula2());
-        cf2 = sheetCF.getConditionalFormattingAt(1);
-        assertEquals("SUM(A10:A21)", cf2.getRule(0).getFormula1());
-        assertEquals("1+SUM(#REF!)", cf2.getRule(0).getFormula2());
-
-        wb.close();
+        try (Workbook wb = _testDataProvider.createWorkbook()) {
+            Sheet sheet = wb.createSheet();
+
+            SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
+
+            ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(
+                    ComparisonOperator.BETWEEN, "SUM(A10:A15)", "1+SUM(B16:B30)");
+            FontFormatting fontFmt = rule1.createFontFormatting();
+            fontFmt.setFontStyle(true, false);
+
+            PatternFormatting patternFmt = rule1.createPatternFormatting();
+            patternFmt.setFillBackgroundColor(IndexedColors.YELLOW.index);
+
+            ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(
+                    ComparisonOperator.BETWEEN, "SUM(A10:A15)", "1+SUM(B16:B30)");
+            BorderFormatting borderFmt = rule2.createBorderFormatting();
+            borderFmt.setBorderDiagonal(BorderStyle.MEDIUM);
+
+            CellRangeAddress[] regions = {
+                    new CellRangeAddress(2, 4, 0, 0), // A3:A5
+            };
+            sheetCF.addConditionalFormatting(regions, rule1);
+            sheetCF.addConditionalFormatting(regions, rule2);
+
+            // This row-shift should destroy the CF region
+            sheet.shiftRows(10, 20, -9);
+            assertEquals(0, sheetCF.getNumConditionalFormattings());
+
+            // re-add the CF
+            sheetCF.addConditionalFormatting(regions, rule1);
+            sheetCF.addConditionalFormatting(regions, rule2);
+
+            // This row shift should only affect the formulas
+            sheet.shiftRows(14, 17, 8);
+            ConditionalFormatting cf1 = sheetCF.getConditionalFormattingAt(0);
+            assertEquals("SUM(A10:A23)", cf1.getRule(0).getFormula1());
+            assertEquals("1+SUM(B24:B30)", cf1.getRule(0).getFormula2());
+            ConditionalFormatting cf2 = sheetCF.getConditionalFormattingAt(1);
+            assertEquals("SUM(A10:A23)", cf2.getRule(0).getFormula1());
+            assertEquals("1+SUM(B24:B30)", cf2.getRule(0).getFormula2());
+
+            sheet.shiftRows(0, 8, 21);
+            cf1 = sheetCF.getConditionalFormattingAt(0);
+            assertEquals("SUM(A10:A21)", cf1.getRule(0).getFormula1());
+            assertEquals("1+SUM(#REF!)", cf1.getRule(0).getFormula2());
+            cf2 = sheetCF.getConditionalFormattingAt(1);
+            assertEquals("SUM(A10:A21)", cf2.getRule(0).getFormula1());
+            assertEquals("1+SUM(#REF!)", cf2.getRule(0).getFormula2());
+        }
     }
 
     protected void testRead(String filename) throws IOException {
-        Workbook wb = _testDataProvider.openSampleWorkbook(filename);
-        Sheet sh = wb.getSheet("CF");
-        SheetConditionalFormatting sheetCF = sh.getSheetConditionalFormatting();
-        assertEquals(3, sheetCF.getNumConditionalFormattings());
-
-        ConditionalFormatting cf1 = sheetCF.getConditionalFormattingAt(0);
-        assertEquals(2, cf1.getNumberOfRules());
-
-        CellRangeAddress[] regions1 = cf1.getFormattingRanges();
-        assertEquals(1, regions1.length);
-        assertEquals("A1:A8", regions1[0].formatAsString());
-
-        // CF1 has two rules: values less than -3 are bold-italic red, values greater than 3 are green
-        ConditionalFormattingRule rule1 = cf1.getRule(0);
-        assertEquals(ConditionType.CELL_VALUE_IS, rule1.getConditionType());
-        assertEquals(ComparisonOperator.GT, rule1.getComparisonOperation());
-        assertEquals("3", rule1.getFormula1());
-        assertNull(rule1.getFormula2());
-        // fills and borders are not set
-        assertNull(rule1.getPatternFormatting());
-        assertNull(rule1.getBorderFormatting());
-
-        FontFormatting fmt1 = rule1.getFontFormatting();
+        try (Workbook wb = _testDataProvider.openSampleWorkbook(filename)) {
+            Sheet sh = wb.getSheet("CF");
+            SheetConditionalFormatting sheetCF = sh.getSheetConditionalFormatting();
+            assertEquals(3, sheetCF.getNumConditionalFormattings());
+
+            ConditionalFormatting cf1 = sheetCF.getConditionalFormattingAt(0);
+            assertEquals(2, cf1.getNumberOfRules());
+
+            CellRangeAddress[] regions1 = cf1.getFormattingRanges();
+            assertEquals(1, regions1.length);
+            assertEquals("A1:A8", regions1[0].formatAsString());
+
+            // CF1 has two rules: values less than -3 are bold-italic red, values greater than 3 are green
+            ConditionalFormattingRule rule1 = cf1.getRule(0);
+            assertEquals(ConditionType.CELL_VALUE_IS, rule1.getConditionType());
+            assertEquals(ComparisonOperator.GT, rule1.getComparisonOperation());
+            assertEquals("3", rule1.getFormula1());
+            assertNull(rule1.getFormula2());
+            // fills and borders are not set
+            assertNull(rule1.getPatternFormatting());
+            assertNull(rule1.getBorderFormatting());
+
+            FontFormatting fmt1 = rule1.getFontFormatting();
 //        assertEquals(IndexedColors.GREEN.index, fmt1.getFontColorIndex());
-        assertTrue(fmt1.isBold());
-        assertFalse(fmt1.isItalic());
-
-        ConditionalFormattingRule rule2 = cf1.getRule(1);
-        assertEquals(ConditionType.CELL_VALUE_IS, rule2.getConditionType());
-        assertEquals(ComparisonOperator.LT, rule2.getComparisonOperation());
-        assertEquals("-3", rule2.getFormula1());
-        assertNull(rule2.getFormula2());
-        assertNull(rule2.getPatternFormatting());
-        assertNull(rule2.getBorderFormatting());
-
-        FontFormatting fmt2 = rule2.getFontFormatting();
+            assertTrue(fmt1.isBold());
+            assertFalse(fmt1.isItalic());
+
+            ConditionalFormattingRule rule2 = cf1.getRule(1);
+            assertEquals(ConditionType.CELL_VALUE_IS, rule2.getConditionType());
+            assertEquals(ComparisonOperator.LT, rule2.getComparisonOperation());
+            assertEquals("-3", rule2.getFormula1());
+            assertNull(rule2.getFormula2());
+            assertNull(rule2.getPatternFormatting());
+            assertNull(rule2.getBorderFormatting());
+
+            FontFormatting fmt2 = rule2.getFontFormatting();
 //        assertEquals(IndexedColors.RED.index, fmt2.getFontColorIndex());
-        assertTrue(fmt2.isBold());
-        assertTrue(fmt2.isItalic());
+            assertTrue(fmt2.isBold());
+            assertTrue(fmt2.isItalic());
 
 
-        ConditionalFormatting cf2 = sheetCF.getConditionalFormattingAt(1);
-        assertEquals(1, cf2.getNumberOfRules());
-        CellRangeAddress[] regions2 = cf2.getFormattingRanges();
-        assertEquals(1, regions2.length);
-        assertEquals("B9", regions2[0].formatAsString());
+            ConditionalFormatting cf2 = sheetCF.getConditionalFormattingAt(1);
+            assertEquals(1, cf2.getNumberOfRules());
+            CellRangeAddress[] regions2 = cf2.getFormattingRanges();
+            assertEquals(1, regions2.length);
+            assertEquals("B9", regions2[0].formatAsString());
 
-        ConditionalFormattingRule rule3 = cf2.getRule(0);
-        assertEquals(ConditionType.FORMULA, rule3.getConditionType());
-        assertEquals(ComparisonOperator.NO_COMPARISON, rule3.getComparisonOperation());
-        assertEquals("$A$8>5", rule3.getFormula1());
-        assertNull(rule3.getFormula2());
+            ConditionalFormattingRule rule3 = cf2.getRule(0);
+            assertEquals(ConditionType.FORMULA, rule3.getConditionType());
+            assertEquals(ComparisonOperator.NO_COMPARISON, rule3.getComparisonOperation());
+            assertEquals("$A$8>5", rule3.getFormula1());
+            assertNull(rule3.getFormula2());
 
-        FontFormatting fmt3 = rule3.getFontFormatting();
+            FontFormatting fmt3 = rule3.getFontFormatting();
 //        assertEquals(IndexedColors.RED.index, fmt3.getFontColorIndex());
-        assertTrue(fmt3.isBold());
-        assertTrue(fmt3.isItalic());
+            assertTrue(fmt3.isBold());
+            assertTrue(fmt3.isItalic());
 
-        PatternFormatting fmt4 = rule3.getPatternFormatting();
+            PatternFormatting fmt4 = rule3.getPatternFormatting();
 //        assertEquals(IndexedColors.LIGHT_CORNFLOWER_BLUE.index, fmt4.getFillBackgroundColor());
 //        assertEquals(IndexedColors.AUTOMATIC.index, fmt4.getFillForegroundColor());
-        assertEquals(PatternFormatting.NO_FILL, fmt4.getFillPattern());
-        // borders are not set
-        assertNull(rule3.getBorderFormatting());
-
-        ConditionalFormatting cf3 = sheetCF.getConditionalFormattingAt(2);
-        CellRangeAddress[] regions3 = cf3.getFormattingRanges();
-        assertEquals(1, regions3.length);
-        assertEquals("B1:B7", regions3[0].formatAsString());
-        assertEquals(2, cf3.getNumberOfRules());
-
-        ConditionalFormattingRule rule4 = cf3.getRule(0);
-        assertEquals(ConditionType.CELL_VALUE_IS, rule4.getConditionType());
-        assertEquals(ComparisonOperator.LE, rule4.getComparisonOperation());
-        assertEquals("\"AAA\"", rule4.getFormula1());
-        assertNull(rule4.getFormula2());
-
-        ConditionalFormattingRule rule5 = cf3.getRule(1);
-        assertEquals(ConditionType.CELL_VALUE_IS, rule5.getConditionType());
-        assertEquals(ComparisonOperator.BETWEEN, rule5.getComparisonOperation());
-        assertEquals("\"A\"", rule5.getFormula1());
-        assertEquals("\"AAA\"", rule5.getFormula2());
-        
-        wb.close();
+            assertEquals(PatternFormatting.NO_FILL, fmt4.getFillPattern());
+            // borders are not set
+            assertNull(rule3.getBorderFormatting());
+
+            ConditionalFormatting cf3 = sheetCF.getConditionalFormattingAt(2);
+            CellRangeAddress[] regions3 = cf3.getFormattingRanges();
+            assertEquals(1, regions3.length);
+            assertEquals("B1:B7", regions3[0].formatAsString());
+            assertEquals(2, cf3.getNumberOfRules());
+
+            ConditionalFormattingRule rule4 = cf3.getRule(0);
+            assertEquals(ConditionType.CELL_VALUE_IS, rule4.getConditionType());
+            assertEquals(ComparisonOperator.LE, rule4.getComparisonOperation());
+            assertEquals("\"AAA\"", rule4.getFormula1());
+            assertNull(rule4.getFormula2());
+
+            ConditionalFormattingRule rule5 = cf3.getRule(1);
+            assertEquals(ConditionType.CELL_VALUE_IS, rule5.getConditionType());
+            assertEquals(ComparisonOperator.BETWEEN, rule5.getComparisonOperation());
+            assertEquals("\"A\"", rule5.getFormula1());
+            assertEquals("\"AAA\"", rule5.getFormula2());
+        }
     }
 
     public void testReadOffice2007(String filename) throws IOException {
-        Workbook wb = _testDataProvider.openSampleWorkbook(filename);
-        Sheet s = wb.getSheet("CF");
-
-        // Sanity check data
-        assertEquals("Values", s.getRow(0).getCell(0).toString());
-        assertEquals("10.0", s.getRow(2).getCell(0).toString());
-
-        // Check we found all the conditional formatting rules we should have
-        SheetConditionalFormatting sheetCF = s.getSheetConditionalFormatting();
-        int numCF = 3;
-        int numCF12 = 15;
-        int numCFEX = 0; // TODO This should be 2, but we don't support CFEX formattings yet, see #58149
-        assertEquals(numCF+numCF12+numCFEX, sheetCF.getNumConditionalFormattings());
-        
-        int fCF = 0, fCF12 = 0, fCFEX = 0;
-        for (int i=0; i<sheetCF.getNumConditionalFormattings(); i++) {
-            ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(i);
-            if (cf instanceof HSSFConditionalFormatting) {
-                String str = cf.toString();
-                if (str.contains("[CF]")) fCF++;
-                if (str.contains("[CF12]")) fCF12++;
-                if (str.contains("[CFEX]")) fCFEX++;
-            } else {
-                ConditionType type = cf.getRule(cf.getNumberOfRules()-1).getConditionType();
-                if (type == ConditionType.CELL_VALUE_IS ||
-                    type == ConditionType.FORMULA) {
-                    fCF++;
+        try (Workbook wb = _testDataProvider.openSampleWorkbook(filename)) {
+            Sheet s = wb.getSheet("CF");
+
+            // Sanity check data
+            assertEquals("Values", s.getRow(0).getCell(0).toString());
+            assertEquals("10.0", s.getRow(2).getCell(0).toString());
+
+            // Check we found all the conditional formatting rules we should have
+            SheetConditionalFormatting sheetCF = s.getSheetConditionalFormatting();
+            int numCF = 3;
+            int numCF12 = 15;
+            int numCFEX = 0; // TODO This should be 2, but we don't support CFEX formattings yet, see #58149
+            assertEquals(numCF + numCF12 + numCFEX, sheetCF.getNumConditionalFormattings());
+
+            int fCF = 0, fCF12 = 0, fCFEX = 0;
+            for (int i = 0; i < sheetCF.getNumConditionalFormattings(); i++) {
+                ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(i);
+                if (cf instanceof HSSFConditionalFormatting) {
+                    String str = cf.toString();
+                    if (str.contains("[CF]")) fCF++;
+                    if (str.contains("[CF12]")) fCF12++;
+                    if (str.contains("[CFEX]")) fCFEX++;
                 } else {
-                    // TODO Properly detect Ext ones from the xml
-                    fCF12++;
+                    ConditionType type = cf.getRule(cf.getNumberOfRules() - 1).getConditionType();
+                    if (type == ConditionType.CELL_VALUE_IS ||
+                            type == ConditionType.FORMULA) {
+                        fCF++;
+                    } else {
+                        // TODO Properly detect Ext ones from the xml
+                        fCF12++;
+                    }
                 }
             }
-        }
-        assertEquals(numCF, fCF);
-        assertEquals(numCF12, fCF12);
-        assertEquals(numCFEX, fCFEX);
-        
-        
-        // Check the rules / values in detail
-        
-        
-        // Highlight Positive values - Column C
-        ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
-        assertEquals(1, cf.getFormattingRanges().length);
-        assertEquals("C2:C17", cf.getFormattingRanges()[0].formatAsString());
-        
-        assertEquals(1, cf.getNumberOfRules());
-        ConditionalFormattingRule cr = cf.getRule(0);
-        assertEquals(ConditionType.CELL_VALUE_IS, cr.getConditionType());
-        assertEquals(ComparisonOperator.GT, cr.getComparisonOperation());
-        assertEquals("0", cr.getFormula1());
-        assertNull(cr.getFormula2());
-        // When it matches:
-        //   Sets the font colour to dark green
-        //   Sets the background colour to lighter green
-        // TODO Should the colours be slightly different between formats? Would CFEX support help for HSSF?
-        if (cr instanceof HSSFConditionalFormattingRule) {
-            assertColour("0:8080:0", cr.getFontFormatting().getFontColor());
-            assertColour("CCCC:FFFF:CCCC", cr.getPatternFormatting().getFillBackgroundColorColor());
-        } else {
-            assertColour("006100", cr.getFontFormatting().getFontColor());
-            assertColour("C6EFCE", cr.getPatternFormatting().getFillBackgroundColorColor());
-        }
-        
-        
-        // Highlight 10-30 - Column D
-        cf = sheetCF.getConditionalFormattingAt(1);
-        assertEquals(1, cf.getFormattingRanges().length);
-        assertEquals("D2:D17", cf.getFormattingRanges()[0].formatAsString());
-        
-        assertEquals(1, cf.getNumberOfRules());
-        cr = cf.getRule(0);
-        assertEquals(ConditionType.CELL_VALUE_IS, cr.getConditionType());
-        assertEquals(ComparisonOperator.BETWEEN, cr.getComparisonOperation());
-        assertEquals("10", cr.getFormula1());
-        assertEquals("30", cr.getFormula2());
-        // When it matches:
-        //   Sets the font colour to dark red
-        //   Sets the background colour to lighter red
-        // TODO Should the colours be slightly different between formats? Would CFEX support help for HSSF?
-        if (cr instanceof HSSFConditionalFormattingRule) {
-            assertColour("8080:0:8080", cr.getFontFormatting().getFontColor());
-            assertColour("FFFF:9999:CCCC", cr.getPatternFormatting().getFillBackgroundColorColor());
-        } else {
-            assertColour("9C0006", cr.getFontFormatting().getFontColor());
-            assertColour("FFC7CE", cr.getPatternFormatting().getFillBackgroundColorColor());
-        }
+            assertEquals(numCF, fCF);
+            assertEquals(numCF12, fCF12);
+            assertEquals(numCFEX, fCFEX);
+
+
+            // Check the rules / values in detail
+
+
+            // Highlight Positive values - Column C
+            ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
+            assertEquals(1, cf.getFormattingRanges().length);
+            assertEquals("C2:C17", cf.getFormattingRanges()[0].formatAsString());
+
+            assertEquals(1, cf.getNumberOfRules());
+            ConditionalFormattingRule cr = cf.getRule(0);
+            assertEquals(ConditionType.CELL_VALUE_IS, cr.getConditionType());
+            assertEquals(ComparisonOperator.GT, cr.getComparisonOperation());
+            assertEquals("0", cr.getFormula1());
+            assertNull(cr.getFormula2());
+            // When it matches:
+            //   Sets the font colour to dark green
+            //   Sets the background colour to lighter green
+            // TODO Should the colours be slightly different between formats? Would CFEX support help for HSSF?
+            if (cr instanceof HSSFConditionalFormattingRule) {
+                assertColour("0:8080:0", cr.getFontFormatting().getFontColor());
+                assertColour("CCCC:FFFF:CCCC", cr.getPatternFormatting().getFillBackgroundColorColor());
+            } else {
+                assertColour("006100", cr.getFontFormatting().getFontColor());
+                assertColour("C6EFCE", cr.getPatternFormatting().getFillBackgroundColorColor());
+            }
+
+
+            // Highlight 10-30 - Column D
+            cf = sheetCF.getConditionalFormattingAt(1);
+            assertEquals(1, cf.getFormattingRanges().length);
+            assertEquals("D2:D17", cf.getFormattingRanges()[0].formatAsString());
+
+            assertEquals(1, cf.getNumberOfRules());
+            cr = cf.getRule(0);
+            assertEquals(ConditionType.CELL_VALUE_IS, cr.getConditionType());
+            assertEquals(ComparisonOperator.BETWEEN, cr.getComparisonOperation());
+            assertEquals("10", cr.getFormula1());
+            assertEquals("30", cr.getFormula2());
+            // When it matches:
+            //   Sets the font colour to dark red
+            //   Sets the background colour to lighter red
+            // TODO Should the colours be slightly different between formats? Would CFEX support help for HSSF?
+            if (cr instanceof HSSFConditionalFormattingRule) {
+                assertColour("8080:0:8080", cr.getFontFormatting().getFontColor());
+                assertColour("FFFF:9999:CCCC", cr.getPatternFormatting().getFillBackgroundColorColor());
+            } else {
+                assertColour("9C0006", cr.getFontFormatting().getFontColor());
+                assertColour("FFC7CE", cr.getPatternFormatting().getFillBackgroundColorColor());
+            }
+
+
+            // Data Bars - Column E
+            cf = sheetCF.getConditionalFormattingAt(2);
+            assertEquals(1, cf.getFormattingRanges().length);
+            assertEquals("E2:E17", cf.getFormattingRanges()[0].formatAsString());
+            assertDataBar(cf, "FF63C384");
+
+
+            // Colours Red->Yellow->Green - Column F
+            cf = sheetCF.getConditionalFormattingAt(3);
+            assertEquals(1, cf.getFormattingRanges().length);
+            assertEquals("F2:F17", cf.getFormattingRanges()[0].formatAsString());
+            assertColorScale(cf, "F8696B", "FFEB84", "63BE7B");
+
+
+            // Colours Blue->White->Red - Column G
+            cf = sheetCF.getConditionalFormattingAt(4);
+            assertEquals(1, cf.getFormattingRanges().length);
+            assertEquals("G2:G17", cf.getFormattingRanges()[0].formatAsString());
+            assertColorScale(cf, "5A8AC6", "FCFCFF", "F8696B");
+
+
+            // Icons : Default - Column H, percentage thresholds
+            cf = sheetCF.getConditionalFormattingAt(5);
+            assertEquals(1, cf.getFormattingRanges().length);
+            assertEquals("H2:H17", cf.getFormattingRanges()[0].formatAsString());
+            assertIconSetPercentages(cf, IconSet.GYR_3_TRAFFIC_LIGHTS, 0d, 33d, 67d);
+
+
+            // Icons : 3 signs - Column I
+            cf = sheetCF.getConditionalFormattingAt(6);
+            assertEquals(1, cf.getFormattingRanges().length);
+            assertEquals("I2:I17", cf.getFormattingRanges()[0].formatAsString());
+            assertIconSetPercentages(cf, IconSet.GYR_3_SHAPES, 0d, 33d, 67d);
+
+
+            // Icons : 3 traffic lights 2 - Column J
+            cf = sheetCF.getConditionalFormattingAt(7);
+            assertEquals(1, cf.getFormattingRanges().length);
+            assertEquals("J2:J17", cf.getFormattingRanges()[0].formatAsString());
+            assertIconSetPercentages(cf, IconSet.GYR_3_TRAFFIC_LIGHTS_BOX, 0d, 33d, 67d);
+
+
+            // Icons : 4 traffic lights - Column K
+            cf = sheetCF.getConditionalFormattingAt(8);
+            assertEquals(1, cf.getFormattingRanges().length);
+            assertEquals("K2:K17", cf.getFormattingRanges()[0].formatAsString());
+            assertIconSetPercentages(cf, IconSet.GYRB_4_TRAFFIC_LIGHTS, 0d, 25d, 50d, 75d);
+
+
+            // Icons : 3 symbols with backgrounds - Column L
+            cf = sheetCF.getConditionalFormattingAt(9);
+            assertEquals(1, cf.getFormattingRanges().length);
+            assertEquals("L2:L17", cf.getFormattingRanges()[0].formatAsString());
+            assertIconSetPercentages(cf, IconSet.GYR_3_SYMBOLS_CIRCLE, 0d, 33d, 67d);
+
+
+            // Icons : 3 flags - Column M2 Only
+            cf = sheetCF.getConditionalFormattingAt(10);
+            assertEquals(1, cf.getFormattingRanges().length);
+            assertEquals("M2", cf.getFormattingRanges()[0].formatAsString());
+            assertIconSetPercentages(cf, IconSet.GYR_3_FLAGS, 0d, 33d, 67d);
+
+            // Icons : 3 flags - Column M (all)
+            cf = sheetCF.getConditionalFormattingAt(11);
+            assertEquals(1, cf.getFormattingRanges().length);
+            assertEquals("M2:M17", cf.getFormattingRanges()[0].formatAsString());
+            assertIconSetPercentages(cf, IconSet.GYR_3_FLAGS, 0d, 33d, 67d);
+
+
+            // Icons : 3 symbols 2 (no background) - Column N
+            cf = sheetCF.getConditionalFormattingAt(12);
+            assertEquals(1, cf.getFormattingRanges().length);
+            assertEquals("N2:N17", cf.getFormattingRanges()[0].formatAsString());
+            assertIconSetPercentages(cf, IconSet.GYR_3_SYMBOLS, 0d, 33d, 67d);
 
-        
-        // Data Bars - Column E
-        cf = sheetCF.getConditionalFormattingAt(2);
-        assertEquals(1, cf.getFormattingRanges().length);
-        assertEquals("E2:E17", cf.getFormattingRanges()[0].formatAsString());
-        assertDataBar(cf, "FF63C384");
-        
-        
-        // Colours Red->Yellow->Green - Column F
-        cf = sheetCF.getConditionalFormattingAt(3);
-        assertEquals(1, cf.getFormattingRanges().length);
-        assertEquals("F2:F17", cf.getFormattingRanges()[0].formatAsString());
-        assertColorScale(cf, "F8696B", "FFEB84", "63BE7B");
-
-        
-        // Colours Blue->White->Red - Column G
-        cf = sheetCF.getConditionalFormattingAt(4);
-        assertEquals(1, cf.getFormattingRanges().length);
-        assertEquals("G2:G17", cf.getFormattingRanges()[0].formatAsString());
-        assertColorScale(cf, "5A8AC6", "FCFCFF", "F8696B");
-
-        
-        // Icons : Default - Column H, percentage thresholds
-        cf = sheetCF.getConditionalFormattingAt(5);
-        assertEquals(1, cf.getFormattingRanges().length);
-        assertEquals("H2:H17", cf.getFormattingRanges()[0].formatAsString());
-        assertIconSetPercentages(cf, IconSet.GYR_3_TRAFFIC_LIGHTS, 0d, 33d, 67d);
-        
-        
-        // Icons : 3 signs - Column I
-        cf = sheetCF.getConditionalFormattingAt(6);
-        assertEquals(1, cf.getFormattingRanges().length);
-        assertEquals("I2:I17", cf.getFormattingRanges()[0].formatAsString());
-        assertIconSetPercentages(cf, IconSet.GYR_3_SHAPES, 0d, 33d, 67d);
-        
-        
-        // Icons : 3 traffic lights 2 - Column J
-        cf = sheetCF.getConditionalFormattingAt(7);
-        assertEquals(1, cf.getFormattingRanges().length);
-        assertEquals("J2:J17", cf.getFormattingRanges()[0].formatAsString());
-        assertIconSetPercentages(cf, IconSet.GYR_3_TRAFFIC_LIGHTS_BOX, 0d, 33d, 67d);
-        
-        
-        // Icons : 4 traffic lights - Column K
-        cf = sheetCF.getConditionalFormattingAt(8);
-        assertEquals(1, cf.getFormattingRanges().length);
-        assertEquals("K2:K17", cf.getFormattingRanges()[0].formatAsString());
-        assertIconSetPercentages(cf, IconSet.GYRB_4_TRAFFIC_LIGHTS, 0d, 25d, 50d, 75d);
-
-        
-        // Icons : 3 symbols with backgrounds - Column L
-        cf = sheetCF.getConditionalFormattingAt(9);
-        assertEquals(1, cf.getFormattingRanges().length);
-        assertEquals("L2:L17", cf.getFormattingRanges()[0].formatAsString());
-        assertIconSetPercentages(cf, IconSet.GYR_3_SYMBOLS_CIRCLE, 0d, 33d, 67d);
-
-        
-        // Icons : 3 flags - Column M2 Only
-        cf = sheetCF.getConditionalFormattingAt(10);
-        assertEquals(1, cf.getFormattingRanges().length);
-        assertEquals("M2", cf.getFormattingRanges()[0].formatAsString());
-        assertIconSetPercentages(cf, IconSet.GYR_3_FLAGS, 0d, 33d, 67d);
-
-        // Icons : 3 flags - Column M (all)
-        cf = sheetCF.getConditionalFormattingAt(11);
-        assertEquals(1, cf.getFormattingRanges().length);
-        assertEquals("M2:M17", cf.getFormattingRanges()[0].formatAsString());
-        assertIconSetPercentages(cf, IconSet.GYR_3_FLAGS, 0d, 33d, 67d);
-
-        
-        // Icons : 3 symbols 2 (no background) - Column N
-        cf = sheetCF.getConditionalFormattingAt(12);
-        assertEquals(1, cf.getFormattingRanges().length);
-        assertEquals("N2:N17", cf.getFormattingRanges()[0].formatAsString());
-        assertIconSetPercentages(cf, IconSet.GYR_3_SYMBOLS, 0d, 33d, 67d);
-
-        
-        // Icons : 3 arrows - Column O
-        cf = sheetCF.getConditionalFormattingAt(13);
-        assertEquals(1, cf.getFormattingRanges().length);
-        assertEquals("O2:O17", cf.getFormattingRanges()[0].formatAsString());
-        assertIconSetPercentages(cf, IconSet.GYR_3_ARROW, 0d, 33d, 67d);
-
-        
-        // Icons : 5 arrows grey - Column P    
-        cf = sheetCF.getConditionalFormattingAt(14);
-        assertEquals(1, cf.getFormattingRanges().length);
-        assertEquals("P2:P17", cf.getFormattingRanges()[0].formatAsString());
-        assertIconSetPercentages(cf, IconSet.GREY_5_ARROWS, 0d, 20d, 40d, 60d, 80d);
-
-        
-        // Icons : 3 stars (ext) - Column Q
-        // TODO Support EXT formattings
-
-        
-        // Icons : 4 ratings - Column R
-        cf = sheetCF.getConditionalFormattingAt(15);
-        assertEquals(1, cf.getFormattingRanges().length);
-        assertEquals("R2:R17", cf.getFormattingRanges()[0].formatAsString());
-        assertIconSetPercentages(cf, IconSet.RATINGS_4, 0d, 25d, 50d, 75d);
-
-        
-        // Icons : 5 ratings - Column S
-        cf = sheetCF.getConditionalFormattingAt(16);
-        assertEquals(1, cf.getFormattingRanges().length);
-        assertEquals("S2:S17", cf.getFormattingRanges()[0].formatAsString());
-        assertIconSetPercentages(cf, IconSet.RATINGS_5, 0d, 20d, 40d, 60d, 80d);
-
-        
-        // Custom Icon+Format - Column T
-        cf = sheetCF.getConditionalFormattingAt(17);
-        assertEquals(1, cf.getFormattingRanges().length);
-        assertEquals("T2:T17", cf.getFormattingRanges()[0].formatAsString());
-        
-        // TODO Support IconSet + Other CFs with 2 rules
+
+            // Icons : 3 arrows - Column O
+            cf = sheetCF.getConditionalFormattingAt(13);
+            assertEquals(1, cf.getFormattingRanges().length);
+            assertEquals("O2:O17", cf.getFormattingRanges()[0].formatAsString());
+            assertIconSetPercentages(cf, IconSet.GYR_3_ARROW, 0d, 33d, 67d);
+
+
+            // Icons : 5 arrows grey - Column P
+            cf = sheetCF.getConditionalFormattingAt(14);
+            assertEquals(1, cf.getFormattingRanges().length);
+            assertEquals("P2:P17", cf.getFormattingRanges()[0].formatAsString());
+            assertIconSetPercentages(cf, IconSet.GREY_5_ARROWS, 0d, 20d, 40d, 60d, 80d);
+
+
+            // Icons : 3 stars (ext) - Column Q
+            // TODO Support EXT formattings
+
+
+            // Icons : 4 ratings - Column R
+            cf = sheetCF.getConditionalFormattingAt(15);
+            assertEquals(1, cf.getFormattingRanges().length);
+            assertEquals("R2:R17", cf.getFormattingRanges()[0].formatAsString());
+            assertIconSetPercentages(cf, IconSet.RATINGS_4, 0d, 25d, 50d, 75d);
+
+
+            // Icons : 5 ratings - Column S
+            cf = sheetCF.getConditionalFormattingAt(16);
+            assertEquals(1, cf.getFormattingRanges().length);
+            assertEquals("S2:S17", cf.getFormattingRanges()[0].formatAsString());
+            assertIconSetPercentages(cf, IconSet.RATINGS_5, 0d, 20d, 40d, 60d, 80d);
+
+
+            // Custom Icon+Format - Column T
+            cf = sheetCF.getConditionalFormattingAt(17);
+            assertEquals(1, cf.getFormattingRanges().length);
+            assertEquals("T2:T17", cf.getFormattingRanges()[0].formatAsString());
+
+            // TODO Support IconSet + Other CFs with 2 rules
 //        assertEquals(2, cf.getNumberOfRules());
 //        cr = cf.getRule(0);
 //        assertIconSetPercentages(cr, IconSet.GYR_3_TRAFFIC_LIGHTS_BOX, 0d, 33d, 67d);
@@ -775,34 +760,36 @@ public abstract class BaseTestConditionalFormatting {
 //            assertEquals("MOD(ROW($T2),2)=1", cr.getFormula1());
 //        }
 //        assertEquals(null, cr.getFormula2());
-        
-        
-        // Mixed icons - Column U
-        // TODO Support EXT formattings
-        
-        wb.close();
+
+
+            // Mixed icons - Column U
+            // TODO Support EXT formattings
+
+        }
     }
-    
+
+    @SuppressWarnings("SameParameterValue")
     private void assertDataBar(ConditionalFormatting cf, String color) {
         assertEquals(1, cf.getNumberOfRules());
         ConditionalFormattingRule cr = cf.getRule(0);
         assertDataBar(cr, color);
     }
+
     private void assertDataBar(ConditionalFormattingRule cr, String color) {
         assertEquals(ConditionType.DATA_BAR, cr.getConditionType());
         assertEquals(ComparisonOperator.NO_COMPARISON, cr.getComparisonOperation());
         assertNull(cr.getFormula1());
         assertNull(cr.getFormula2());
-        
+
         DataBarFormatting databar = cr.getDataBarFormatting();
         assertNotNull(databar);
         assertFalse(databar.isIconOnly());
         assertTrue(databar.isLeftToRight());
         assertEquals(0, databar.getWidthMin());
         assertEquals(100, databar.getWidthMax());
-        
+
         assertColour(color, databar.getColor());
-        
+
         ConditionalFormattingThreshold th;
         th = databar.getMinThreshold();
         assertEquals(RangeType.MIN, th.getRangeType());
@@ -813,24 +800,25 @@ public abstract class BaseTestConditionalFormatting {
         assertNull(th.getValue());
         assertNull(th.getFormula());
     }
-    
+
     private void assertIconSetPercentages(ConditionalFormatting cf, IconSet iconset, Double...vals) {
         assertEquals(1, cf.getNumberOfRules());
         ConditionalFormattingRule cr = cf.getRule(0);
         assertIconSetPercentages(cr, iconset, vals);
-    }        
+    }
+
     private void assertIconSetPercentages(ConditionalFormattingRule cr, IconSet iconset, Double...vals) {
         assertEquals(ConditionType.ICON_SET, cr.getConditionType());
         assertEquals(ComparisonOperator.NO_COMPARISON, cr.getComparisonOperation());
         assertNull(cr.getFormula1());
         assertNull(cr.getFormula2());
-        
+
         IconMultiStateFormatting icon = cr.getMultiStateFormatting();
         assertNotNull(icon);
         assertEquals(iconset, icon.getIconSet());
         assertFalse(icon.isIconOnly());
         assertFalse(icon.isReversed());
-        
+
         assertNotNull(icon.getThresholds());
         assertEquals(vals.length, icon.getThresholds().length);
         for (int i=0; i<vals.length; i++) {
@@ -841,18 +829,19 @@ public abstract class BaseTestConditionalFormatting {
             assertNull(th.getFormula());
         }
     }
-    
+
     private void assertColorScale(ConditionalFormatting cf, String... colors) {
         assertEquals(1, cf.getNumberOfRules());
         ConditionalFormattingRule cr = cf.getRule(0);
         assertColorScale(cr, colors);
-    }        
+    }
+
     private void assertColorScale(ConditionalFormattingRule cr, String... colors) {
         assertEquals(ConditionType.COLOR_SCALE, cr.getConditionType());
         assertEquals(ComparisonOperator.NO_COMPARISON, cr.getComparisonOperation());
         assertNull(cr.getFormula1());
         assertNull(cr.getFormula2());
-        
+
         ColorScaleFormatting color = cr.getColorScaleFormatting();
         assertNotNull(color);
         assertNotNull(color.getColors());
@@ -860,7 +849,7 @@ public abstract class BaseTestConditionalFormatting {
         assertEquals(colors.length, color.getNumControlPoints());
         assertEquals(colors.length, color.getColors().length);
         assertEquals(colors.length, color.getThresholds().length);
-        
+
         // Thresholds should be Min / (evenly spaced) / Max
         int steps = 100 / (colors.length-1);
         for (int i=0; i<colors.length; i++) {
@@ -875,7 +864,7 @@ public abstract class BaseTestConditionalFormatting {
             }
             assertNull(th.getFormula());
         }
-        
+
         // Colors should match
         for (int i=0; i<colors.length; i++) {
             assertColour(colors[i], color.getColors()[i]);
@@ -884,544 +873,533 @@ public abstract class BaseTestConditionalFormatting {
 
     @Test
     public void testCreateFontFormatting() throws IOException {
-        Workbook workbook = _testDataProvider.createWorkbook();
-        Sheet sheet = workbook.createSheet();
-
-        SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
-
-        ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "7");
-        FontFormatting fontFmt = rule1.createFontFormatting();
-        assertFalse(fontFmt.isItalic());
-        assertFalse(fontFmt.isBold());
-        fontFmt.setFontStyle(true, true);
-        assertTrue(fontFmt.isItalic());
-        assertTrue(fontFmt.isBold());
-
-        assertEquals(-1, fontFmt.getFontHeight()); // not modified
-        fontFmt.setFontHeight(200);
-        assertEquals(200, fontFmt.getFontHeight()); 
-        fontFmt.setFontHeight(100);
-        assertEquals(100, fontFmt.getFontHeight());
-
-        assertEquals(FontFormatting.SS_NONE, fontFmt.getEscapementType());
-        fontFmt.setEscapementType(FontFormatting.SS_SUB);
-        assertEquals(FontFormatting.SS_SUB, fontFmt.getEscapementType());
-        fontFmt.setEscapementType(FontFormatting.SS_NONE);
-        assertEquals(FontFormatting.SS_NONE, fontFmt.getEscapementType());
-        fontFmt.setEscapementType(FontFormatting.SS_SUPER);
-        assertEquals(FontFormatting.SS_SUPER, fontFmt.getEscapementType());
-
-        assertEquals(FontFormatting.U_NONE, fontFmt.getUnderlineType());
-        fontFmt.setUnderlineType(FontFormatting.U_SINGLE);
-        assertEquals(FontFormatting.U_SINGLE, fontFmt.getUnderlineType());
-        fontFmt.setUnderlineType(FontFormatting.U_NONE);
-        assertEquals(FontFormatting.U_NONE, fontFmt.getUnderlineType());
-        fontFmt.setUnderlineType(FontFormatting.U_DOUBLE);
-        assertEquals(FontFormatting.U_DOUBLE, fontFmt.getUnderlineType());
-
-        assertEquals(-1, fontFmt.getFontColorIndex());
-        fontFmt.setFontColorIndex(IndexedColors.RED.index);
-        assertEquals(IndexedColors.RED.index, fontFmt.getFontColorIndex());
-        fontFmt.setFontColorIndex(IndexedColors.AUTOMATIC.index);
-        assertEquals(IndexedColors.AUTOMATIC.index, fontFmt.getFontColorIndex());
-        fontFmt.setFontColorIndex(IndexedColors.BLUE.index);
-        assertEquals(IndexedColors.BLUE.index, fontFmt.getFontColorIndex());
-
-        ConditionalFormattingRule [] cfRules = { rule1 };
-
-        CellRangeAddress [] regions = { CellRangeAddress.valueOf("A1:A5") };
-
-        sheetCF.addConditionalFormatting(regions, cfRules);
-
-        // Verification
-        ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
-        assertNotNull(cf);
-
-        assertEquals(1, cf.getNumberOfRules());
-
-        FontFormatting  r1fp = cf.getRule(0).getFontFormatting();
-        assertNotNull(r1fp);
-
-        assertTrue(r1fp.isItalic());
-        assertTrue(r1fp.isBold());
-        assertEquals(FontFormatting.SS_SUPER, r1fp.getEscapementType());
-        assertEquals(FontFormatting.U_DOUBLE, r1fp.getUnderlineType());
-        assertEquals(IndexedColors.BLUE.index, r1fp.getFontColorIndex());
-
-        workbook.close();
+        try (Workbook workbook = _testDataProvider.createWorkbook()) {
+            Sheet sheet = workbook.createSheet();
+
+            SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
+
+            ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "7");
+            FontFormatting fontFmt = rule1.createFontFormatting();
+            assertFalse(fontFmt.isItalic());
+            assertFalse(fontFmt.isBold());
+            fontFmt.setFontStyle(true, true);
+            assertTrue(fontFmt.isItalic());
+            assertTrue(fontFmt.isBold());
+
+            assertEquals(-1, fontFmt.getFontHeight()); // not modified
+            fontFmt.setFontHeight(200);
+            assertEquals(200, fontFmt.getFontHeight());
+            fontFmt.setFontHeight(100);
+            assertEquals(100, fontFmt.getFontHeight());
+
+            assertEquals(Font.SS_NONE, fontFmt.getEscapementType());
+            fontFmt.setEscapementType(Font.SS_SUB);
+            assertEquals(Font.SS_SUB, fontFmt.getEscapementType());
+            fontFmt.setEscapementType(Font.SS_NONE);
+            assertEquals(Font.SS_NONE, fontFmt.getEscapementType());
+            fontFmt.setEscapementType(Font.SS_SUPER);
+            assertEquals(Font.SS_SUPER, fontFmt.getEscapementType());
+
+            assertEquals(Font.U_NONE, fontFmt.getUnderlineType());
+            fontFmt.setUnderlineType(Font.U_SINGLE);
+            assertEquals(Font.U_SINGLE, fontFmt.getUnderlineType());
+            fontFmt.setUnderlineType(Font.U_NONE);
+            assertEquals(Font.U_NONE, fontFmt.getUnderlineType());
+            fontFmt.setUnderlineType(Font.U_DOUBLE);
+            assertEquals(Font.U_DOUBLE, fontFmt.getUnderlineType());
+
+            assertEquals(-1, fontFmt.getFontColorIndex());
+            fontFmt.setFontColorIndex(IndexedColors.RED.index);
+            assertEquals(IndexedColors.RED.index, fontFmt.getFontColorIndex());
+            fontFmt.setFontColorIndex(IndexedColors.AUTOMATIC.index);
+            assertEquals(IndexedColors.AUTOMATIC.index, fontFmt.getFontColorIndex());
+            fontFmt.setFontColorIndex(IndexedColors.BLUE.index);
+            assertEquals(IndexedColors.BLUE.index, fontFmt.getFontColorIndex());
+
+            ConditionalFormattingRule[] cfRules = {rule1};
+
+            CellRangeAddress[] regions = {CellRangeAddress.valueOf("A1:A5")};
+
+            sheetCF.addConditionalFormatting(regions, cfRules);
+
+            // Verification
+            ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
+            assertNotNull(cf);
+
+            assertEquals(1, cf.getNumberOfRules());
+
+            FontFormatting r1fp = cf.getRule(0).getFontFormatting();
+            assertNotNull(r1fp);
+
+            assertTrue(r1fp.isItalic());
+            assertTrue(r1fp.isBold());
+            assertEquals(Font.SS_SUPER, r1fp.getEscapementType());
+            assertEquals(Font.U_DOUBLE, r1fp.getUnderlineType());
+            assertEquals(IndexedColors.BLUE.index, r1fp.getFontColorIndex());
+        }
     }
 
     @Test
     public void testCreatePatternFormatting() throws IOException {
-        Workbook workbook = _testDataProvider.createWorkbook();
-        Sheet sheet = workbook.createSheet();
+        try (Workbook workbook = _testDataProvider.createWorkbook()) {
+            Sheet sheet = workbook.createSheet();
 
-        SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
+            SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
 
-        ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "7");
-        PatternFormatting patternFmt = rule1.createPatternFormatting();
+            ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "7");
+            PatternFormatting patternFmt = rule1.createPatternFormatting();
 
-        assertEquals(0, patternFmt.getFillBackgroundColor());
-        patternFmt.setFillBackgroundColor(IndexedColors.RED.index);
-        assertEquals(IndexedColors.RED.index, patternFmt.getFillBackgroundColor());
+            assertEquals(0, patternFmt.getFillBackgroundColor());
+            patternFmt.setFillBackgroundColor(IndexedColors.RED.index);
+            assertEquals(IndexedColors.RED.index, patternFmt.getFillBackgroundColor());
 
-        assertEquals(0, patternFmt.getFillForegroundColor());
-        patternFmt.setFillForegroundColor(IndexedColors.BLUE.index);
-        assertEquals(IndexedColors.BLUE.index, patternFmt.getFillForegroundColor());
+            assertEquals(0, patternFmt.getFillForegroundColor());
+            patternFmt.setFillForegroundColor(IndexedColors.BLUE.index);
+            assertEquals(IndexedColors.BLUE.index, patternFmt.getFillForegroundColor());
 
-        assertEquals(PatternFormatting.NO_FILL, patternFmt.getFillPattern());
-        patternFmt.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
-        assertEquals(PatternFormatting.SOLID_FOREGROUND, patternFmt.getFillPattern());
-        patternFmt.setFillPattern(PatternFormatting.NO_FILL);
-        assertEquals(PatternFormatting.NO_FILL, patternFmt.getFillPattern());
-        patternFmt.setFillPattern(PatternFormatting.BRICKS);
-        assertEquals(PatternFormatting.BRICKS, patternFmt.getFillPattern());
+            assertEquals(PatternFormatting.NO_FILL, patternFmt.getFillPattern());
+            patternFmt.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
+            assertEquals(PatternFormatting.SOLID_FOREGROUND, patternFmt.getFillPattern());
+            patternFmt.setFillPattern(PatternFormatting.NO_FILL);
+            assertEquals(PatternFormatting.NO_FILL, patternFmt.getFillPattern());
+            patternFmt.setFillPattern(PatternFormatting.BRICKS);
+            assertEquals(PatternFormatting.BRICKS, patternFmt.getFillPattern());
 
-        ConditionalFormattingRule [] cfRules = { rule1 };
+            ConditionalFormattingRule[] cfRules = {rule1};
 
-        CellRangeAddress [] regions = { CellRangeAddress.valueOf("A1:A5") };
+            CellRangeAddress[] regions = {CellRangeAddress.valueOf("A1:A5")};
 
-        sheetCF.addConditionalFormatting(regions, cfRules);
+            sheetCF.addConditionalFormatting(regions, cfRules);
 
-        // Verification
-        ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
-        assertNotNull(cf);
+            // Verification
+            ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
+            assertNotNull(cf);
 
-        assertEquals(1, cf.getNumberOfRules());
+            assertEquals(1, cf.getNumberOfRules());
 
-        PatternFormatting  r1fp = cf.getRule(0).getPatternFormatting();
-        assertNotNull(r1fp);
+            PatternFormatting r1fp = cf.getRule(0).getPatternFormatting();
+            assertNotNull(r1fp);
 
-        assertEquals(IndexedColors.RED.index, r1fp.getFillBackgroundColor());
-        assertEquals(IndexedColors.BLUE.index, r1fp.getFillForegroundColor());
-        assertEquals(PatternFormatting.BRICKS, r1fp.getFillPattern());
-        
-        workbook.close();
+            assertEquals(IndexedColors.RED.index, r1fp.getFillBackgroundColor());
+            assertEquals(IndexedColors.BLUE.index, r1fp.getFillForegroundColor());
+            assertEquals(PatternFormatting.BRICKS, r1fp.getFillPattern());
+        }
     }
-    
+
     @Test
     public void testAllCreateBorderFormatting() throws IOException {
         // Make sure it is possible to create a conditional formatting rule
         // with every type of Border Style
-        Workbook workbook = _testDataProvider.createWorkbook();
-        Sheet sheet = workbook.createSheet();
+        try (Workbook workbook = _testDataProvider.createWorkbook()) {
+            Sheet sheet = workbook.createSheet();
 
-        SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
+            SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
 
-        ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "7");
-        BorderFormatting borderFmt = rule1.createBorderFormatting();
+            ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "7");
+            BorderFormatting borderFmt = rule1.createBorderFormatting();
 
-        for (BorderStyle border : BorderStyle.values()) {
-            borderFmt.setBorderTop(border);
-            assertEquals(border, borderFmt.getBorderTop());
+            for (BorderStyle border : BorderStyle.values()) {
+                borderFmt.setBorderTop(border);
+                assertEquals(border, borderFmt.getBorderTop());
 
-            borderFmt.setBorderBottom(border);
-            assertEquals(border, borderFmt.getBorderBottom());
+                borderFmt.setBorderBottom(border);
+                assertEquals(border, borderFmt.getBorderBottom());
 
-            borderFmt.setBorderLeft(border);
-            assertEquals(border, borderFmt.getBorderLeft());
+                borderFmt.setBorderLeft(border);
+                assertEquals(border, borderFmt.getBorderLeft());
 
-            borderFmt.setBorderRight(border);
-            assertEquals(border, borderFmt.getBorderRight());
+                borderFmt.setBorderRight(border);
+                assertEquals(border, borderFmt.getBorderRight());
 
-            borderFmt.setBorderDiagonal(border);
-            assertEquals(border, borderFmt.getBorderDiagonal());
+                borderFmt.setBorderDiagonal(border);
+                assertEquals(border, borderFmt.getBorderDiagonal());
+            }
         }
-
-        workbook.close();
     }
 
     @Test
     public void testCreateBorderFormatting() throws IOException {
-        Workbook workbook = _testDataProvider.createWorkbook();
-        Sheet sheet = workbook.createSheet();
-
-        SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
-
-        ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "7");
-        BorderFormatting borderFmt = rule1.createBorderFormatting();
-
-        assertEquals(BorderStyle.NONE, borderFmt.getBorderBottom());
-        borderFmt.setBorderBottom(BorderStyle.DOTTED);
-        assertEquals(BorderStyle.DOTTED, borderFmt.getBorderBottom());
-        borderFmt.setBorderBottom(BorderStyle.NONE);
-        assertEquals(BorderStyle.NONE, borderFmt.getBorderBottom());
-        borderFmt.setBorderBottom(BorderStyle.THICK);
-        assertEquals(BorderStyle.THICK, borderFmt.getBorderBottom());
-
-        assertEquals(BorderStyle.NONE, borderFmt.getBorderTop());
-        borderFmt.setBorderTop(BorderStyle.DOTTED);
-        assertEquals(BorderStyle.DOTTED, borderFmt.getBorderTop());
-        borderFmt.setBorderTop(BorderStyle.NONE);
-        assertEquals(BorderStyle.NONE, borderFmt.getBorderTop());
-        borderFmt.setBorderTop(BorderStyle.THICK);
-        assertEquals(BorderStyle.THICK, borderFmt.getBorderTop());
-
-        assertEquals(BorderStyle.NONE, borderFmt.getBorderLeft());
-        borderFmt.setBorderLeft(BorderStyle.DOTTED);
-        assertEquals(BorderStyle.DOTTED, borderFmt.getBorderLeft());
-        borderFmt.setBorderLeft(BorderStyle.NONE);
-        assertEquals(BorderStyle.NONE, borderFmt.getBorderLeft());
-        borderFmt.setBorderLeft(BorderStyle.THIN);
-        assertEquals(BorderStyle.THIN, borderFmt.getBorderLeft());
-
-        assertEquals(BorderStyle.NONE, borderFmt.getBorderRight());
-        borderFmt.setBorderRight(BorderStyle.DOTTED);
-        assertEquals(BorderStyle.DOTTED, borderFmt.getBorderRight());
-        borderFmt.setBorderRight(BorderStyle.NONE);
-        assertEquals(BorderStyle.NONE, borderFmt.getBorderRight());
-        borderFmt.setBorderRight(BorderStyle.HAIR);
-        assertEquals(BorderStyle.HAIR, borderFmt.getBorderRight());
-
-        ConditionalFormattingRule [] cfRules = { rule1 };
-
-        CellRangeAddress [] regions = { CellRangeAddress.valueOf("A1:A5") };
-
-        sheetCF.addConditionalFormatting(regions, cfRules);
-
-        // Verification
-        ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
-        assertNotNull(cf);
-
-        assertEquals(1, cf.getNumberOfRules());
-
-        BorderFormatting  r1fp = cf.getRule(0).getBorderFormatting();
-        assertNotNull(r1fp);
-        assertEquals(BorderStyle.THICK, r1fp.getBorderBottom());
-        assertEquals(BorderStyle.THICK, r1fp.getBorderTop());
-        assertEquals(BorderStyle.THIN, r1fp.getBorderLeft());
-        assertEquals(BorderStyle.HAIR, r1fp.getBorderRight());
-        
-        workbook.close();
+        try (Workbook workbook = _testDataProvider.createWorkbook()) {
+            Sheet sheet = workbook.createSheet();
+
+            SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
+
+            ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "7");
+            BorderFormatting borderFmt = rule1.createBorderFormatting();
+
+            assertEquals(BorderStyle.NONE, borderFmt.getBorderBottom());
+            borderFmt.setBorderBottom(BorderStyle.DOTTED);
+            assertEquals(BorderStyle.DOTTED, borderFmt.getBorderBottom());
+            borderFmt.setBorderBottom(BorderStyle.NONE);
+            assertEquals(BorderStyle.NONE, borderFmt.getBorderBottom());
+            borderFmt.setBorderBottom(BorderStyle.THICK);
+            assertEquals(BorderStyle.THICK, borderFmt.getBorderBottom());
+
+            assertEquals(BorderStyle.NONE, borderFmt.getBorderTop());
+            borderFmt.setBorderTop(BorderStyle.DOTTED);
+            assertEquals(BorderStyle.DOTTED, borderFmt.getBorderTop());
+            borderFmt.setBorderTop(BorderStyle.NONE);
+            assertEquals(BorderStyle.NONE, borderFmt.getBorderTop());
+            borderFmt.setBorderTop(BorderStyle.THICK);
+            assertEquals(BorderStyle.THICK, borderFmt.getBorderTop());
+
+            assertEquals(BorderStyle.NONE, borderFmt.getBorderLeft());
+            borderFmt.setBorderLeft(BorderStyle.DOTTED);
+            assertEquals(BorderStyle.DOTTED, borderFmt.getBorderLeft());
+            borderFmt.setBorderLeft(BorderStyle.NONE);
+            assertEquals(BorderStyle.NONE, borderFmt.getBorderLeft());
+            borderFmt.setBorderLeft(BorderStyle.THIN);
+            assertEquals(BorderStyle.THIN, borderFmt.getBorderLeft());
+
+            assertEquals(BorderStyle.NONE, borderFmt.getBorderRight());
+            borderFmt.setBorderRight(BorderStyle.DOTTED);
+            assertEquals(BorderStyle.DOTTED, borderFmt.getBorderRight());
+            borderFmt.setBorderRight(BorderStyle.NONE);
+            assertEquals(BorderStyle.NONE, borderFmt.getBorderRight());
+            borderFmt.setBorderRight(BorderStyle.HAIR);
+            assertEquals(BorderStyle.HAIR, borderFmt.getBorderRight());
+
+            ConditionalFormattingRule[] cfRules = {rule1};
+
+            CellRangeAddress[] regions = {CellRangeAddress.valueOf("A1:A5")};
+
+            sheetCF.addConditionalFormatting(regions, cfRules);
+
+            // Verification
+            ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
+            assertNotNull(cf);
+
+            assertEquals(1, cf.getNumberOfRules());
+
+            BorderFormatting r1fp = cf.getRule(0).getBorderFormatting();
+            assertNotNull(r1fp);
+            assertEquals(BorderStyle.THICK, r1fp.getBorderBottom());
+            assertEquals(BorderStyle.THICK, r1fp.getBorderTop());
+            assertEquals(BorderStyle.THIN, r1fp.getBorderLeft());
+            assertEquals(BorderStyle.HAIR, r1fp.getBorderRight());
+        }
     }
-    
+
     @Test
     public void testCreateIconFormatting() throws IOException {
-        Workbook wb1 = _testDataProvider.createWorkbook();
-        Sheet sheet = wb1.createSheet();
-
-        SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
-        ConditionalFormattingRule rule1 = 
-                sheetCF.createConditionalFormattingRule(IconSet.GYRB_4_TRAFFIC_LIGHTS);
-        IconMultiStateFormatting iconFmt = rule1.getMultiStateFormatting();
-        
-        assertEquals(IconSet.GYRB_4_TRAFFIC_LIGHTS, iconFmt.getIconSet());
-        assertEquals(4, iconFmt.getThresholds().length);
-        assertFalse(iconFmt.isIconOnly());
-        assertFalse(iconFmt.isReversed());
-        
-        iconFmt.setIconOnly(true);
-        iconFmt.getThresholds()[0].setRangeType(RangeType.MIN);
-        iconFmt.getThresholds()[1].setRangeType(RangeType.NUMBER);
-        iconFmt.getThresholds()[1].setValue(10d);
-        iconFmt.getThresholds()[2].setRangeType(RangeType.PERCENT);
-        iconFmt.getThresholds()[2].setValue(75d);
-        iconFmt.getThresholds()[3].setRangeType(RangeType.MAX);
-        
-        CellRangeAddress [] regions = { CellRangeAddress.valueOf("A1:A5") };
-        sheetCF.addConditionalFormatting(regions, rule1);
-
-        // Save, re-load and re-check
-        Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
-        wb1.close();
-        sheet = wb2.getSheetAt(0);
-        sheetCF = sheet.getSheetConditionalFormatting();
-        assertEquals(1, sheetCF.getNumConditionalFormattings());
-        
-        ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
-        assertEquals(1, cf.getNumberOfRules());
-        rule1 = cf.getRule(0);
-        assertEquals(ConditionType.ICON_SET, rule1.getConditionType());
-        iconFmt = rule1.getMultiStateFormatting();
-        
-        assertEquals(IconSet.GYRB_4_TRAFFIC_LIGHTS, iconFmt.getIconSet());
-        assertEquals(4, iconFmt.getThresholds().length);
-        assertTrue(iconFmt.isIconOnly());
-        assertFalse(iconFmt.isReversed());
-
-        assertEquals(RangeType.MIN,    iconFmt.getThresholds()[0].getRangeType());
-        assertEquals(RangeType.NUMBER, iconFmt.getThresholds()[1].getRangeType());
-        assertEquals(RangeType.PERCENT,iconFmt.getThresholds()[2].getRangeType());
-        assertEquals(RangeType.MAX,    iconFmt.getThresholds()[3].getRangeType());
-        assertNull(iconFmt.getThresholds()[0].getValue());
-        assertEquals(10d,  iconFmt.getThresholds()[1].getValue(), 0);
-        assertEquals(75d,  iconFmt.getThresholds()[2].getValue(), 0);
-        assertNull(iconFmt.getThresholds()[3].getValue());
-        
-        wb2.close();
+        try (Workbook wb1 = _testDataProvider.createWorkbook()) {
+            Sheet sheet = wb1.createSheet();
+
+            SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
+            ConditionalFormattingRule rule1 =
+                    sheetCF.createConditionalFormattingRule(IconSet.GYRB_4_TRAFFIC_LIGHTS);
+            IconMultiStateFormatting iconFmt = rule1.getMultiStateFormatting();
+
+            assertEquals(IconSet.GYRB_4_TRAFFIC_LIGHTS, iconFmt.getIconSet());
+            assertEquals(4, iconFmt.getThresholds().length);
+            assertFalse(iconFmt.isIconOnly());
+            assertFalse(iconFmt.isReversed());
+
+            iconFmt.setIconOnly(true);
+            iconFmt.getThresholds()[0].setRangeType(RangeType.MIN);
+            iconFmt.getThresholds()[1].setRangeType(RangeType.NUMBER);
+            iconFmt.getThresholds()[1].setValue(10d);
+            iconFmt.getThresholds()[2].setRangeType(RangeType.PERCENT);
+            iconFmt.getThresholds()[2].setValue(75d);
+            iconFmt.getThresholds()[3].setRangeType(RangeType.MAX);
+
+            CellRangeAddress[] regions = {CellRangeAddress.valueOf("A1:A5")};
+            sheetCF.addConditionalFormatting(regions, rule1);
+
+            // Save, re-load and re-check
+            try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
+                sheet = wb2.getSheetAt(0);
+                sheetCF = sheet.getSheetConditionalFormatting();
+                assertEquals(1, sheetCF.getNumConditionalFormattings());
+
+                ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
+                assertEquals(1, cf.getNumberOfRules());
+                rule1 = cf.getRule(0);
+                assertEquals(ConditionType.ICON_SET, rule1.getConditionType());
+                iconFmt = rule1.getMultiStateFormatting();
+
+                assertEquals(IconSet.GYRB_4_TRAFFIC_LIGHTS, iconFmt.getIconSet());
+                assertEquals(4, iconFmt.getThresholds().length);
+                assertTrue(iconFmt.isIconOnly());
+                assertFalse(iconFmt.isReversed());
+
+                assertEquals(RangeType.MIN, iconFmt.getThresholds()[0].getRangeType());
+                assertEquals(RangeType.NUMBER, iconFmt.getThresholds()[1].getRangeType());
+                assertEquals(RangeType.PERCENT, iconFmt.getThresholds()[2].getRangeType());
+                assertEquals(RangeType.MAX, iconFmt.getThresholds()[3].getRangeType());
+                assertNull(iconFmt.getThresholds()[0].getValue());
+                assertEquals(10d, iconFmt.getThresholds()[1].getValue(), 0);
+                assertEquals(75d, iconFmt.getThresholds()[2].getValue(), 0);
+                assertNull(iconFmt.getThresholds()[3].getValue());
+            }
+        }
     }
-    
+
     @Test
     public void testCreateColorScaleFormatting() throws IOException {
-        Workbook wb1 = _testDataProvider.createWorkbook();
-        Sheet sheet = wb1.createSheet();
-
-        SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
-        ConditionalFormattingRule rule1 = 
-                sheetCF.createConditionalFormattingColorScaleRule();
-        ColorScaleFormatting clrFmt = rule1.getColorScaleFormatting();
-        
-        assertEquals(3, clrFmt.getNumControlPoints());
-        assertEquals(3, clrFmt.getColors().length);
-        assertEquals(3, clrFmt.getThresholds().length);
-        
-        clrFmt.getThresholds()[0].setRangeType(RangeType.MIN);
-        clrFmt.getThresholds()[1].setRangeType(RangeType.NUMBER);
-        clrFmt.getThresholds()[1].setValue(10d);
-        clrFmt.getThresholds()[2].setRangeType(RangeType.MAX);
-        
-        CellRangeAddress [] regions = { CellRangeAddress.valueOf("A1:A5") };
-        sheetCF.addConditionalFormatting(regions, rule1);
-        
-        // Save, re-load and re-check
-        Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
-        wb1.close();
-        sheet = wb2.getSheetAt(0);
-        sheetCF = sheet.getSheetConditionalFormatting();
-        assertEquals(1, sheetCF.getNumConditionalFormattings());
-        
-        ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
-        assertEquals(1, cf.getNumberOfRules());
-        rule1 = cf.getRule(0);
-        clrFmt = rule1.getColorScaleFormatting();
-        assertEquals(ConditionType.COLOR_SCALE, rule1.getConditionType());
-        
-        assertEquals(3, clrFmt.getNumControlPoints());
-        assertEquals(3, clrFmt.getColors().length);
-        assertEquals(3, clrFmt.getThresholds().length);
-
-        assertEquals(RangeType.MIN,    clrFmt.getThresholds()[0].getRangeType());
-        assertEquals(RangeType.NUMBER, clrFmt.getThresholds()[1].getRangeType());
-        assertEquals(RangeType.MAX,    clrFmt.getThresholds()[2].getRangeType());
-        assertNull(clrFmt.getThresholds()[0].getValue());
-        assertEquals(10d,  clrFmt.getThresholds()[1].getValue(), 0);
-        assertNull(clrFmt.getThresholds()[2].getValue());
-        
-        wb2.close();
+        try (Workbook wb1 = _testDataProvider.createWorkbook()) {
+            Sheet sheet = wb1.createSheet();
+
+            SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
+            ConditionalFormattingRule rule1 =
+                    sheetCF.createConditionalFormattingColorScaleRule();
+            ColorScaleFormatting clrFmt = rule1.getColorScaleFormatting();
+
+            assertEquals(3, clrFmt.getNumControlPoints());
+            assertEquals(3, clrFmt.getColors().length);
+            assertEquals(3, clrFmt.getThresholds().length);
+
+            clrFmt.getThresholds()[0].setRangeType(RangeType.MIN);
+            clrFmt.getThresholds()[1].setRangeType(RangeType.NUMBER);
+            clrFmt.getThresholds()[1].setValue(10d);
+            clrFmt.getThresholds()[2].setRangeType(RangeType.MAX);
+
+            CellRangeAddress[] regions = {CellRangeAddress.valueOf("A1:A5")};
+            sheetCF.addConditionalFormatting(regions, rule1);
+
+            // Save, re-load and re-check
+            try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
+                sheet = wb2.getSheetAt(0);
+                sheetCF = sheet.getSheetConditionalFormatting();
+                assertEquals(1, sheetCF.getNumConditionalFormattings());
+
+                ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
+                assertEquals(1, cf.getNumberOfRules());
+                rule1 = cf.getRule(0);
+                clrFmt = rule1.getColorScaleFormatting();
+                assertEquals(ConditionType.COLOR_SCALE, rule1.getConditionType());
+
+                assertEquals(3, clrFmt.getNumControlPoints());
+                assertEquals(3, clrFmt.getColors().length);
+                assertEquals(3, clrFmt.getThresholds().length);
+
+                assertEquals(RangeType.MIN, clrFmt.getThresholds()[0].getRangeType());
+                assertEquals(RangeType.NUMBER, clrFmt.getThresholds()[1].getRangeType());
+                assertEquals(RangeType.MAX, clrFmt.getThresholds()[2].getRangeType());
+                assertNull(clrFmt.getThresholds()[0].getValue());
+                assertEquals(10d, clrFmt.getThresholds()[1].getValue(), 0);
+                assertNull(clrFmt.getThresholds()[2].getValue());
+            }
+        }
     }
-    
+
     @Test
     public void testCreateDataBarFormatting() throws IOException {
-        Workbook wb1 = _testDataProvider.createWorkbook();
-        Sheet sheet = wb1.createSheet();
-
-        String colorHex = "FFFFEB84";
-        ExtendedColor color = wb1.getCreationHelper().createExtendedColor();
-        color.setARGBHex(colorHex);
-        SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
-        ConditionalFormattingRule rule1 = 
-                sheetCF.createConditionalFormattingRule(color);
-        DataBarFormatting dbFmt = rule1.getDataBarFormatting();
-
-        assertFalse(dbFmt.isIconOnly());
-        assertTrue(dbFmt.isLeftToRight());
-        assertEquals(0,   dbFmt.getWidthMin());
-        assertEquals(100, dbFmt.getWidthMax());
-        assertColour(colorHex, dbFmt.getColor());
-        
-        dbFmt.getMinThreshold().setRangeType(RangeType.MIN);
-        dbFmt.getMaxThreshold().setRangeType(RangeType.MAX);
-        
-        CellRangeAddress [] regions = { CellRangeAddress.valueOf("A1:A5") };
-        sheetCF.addConditionalFormatting(regions, rule1);
-        
-        // Save, re-load and re-check
-        Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
-        wb1.close();
-        sheet = wb2.getSheetAt(0);
-        sheetCF = sheet.getSheetConditionalFormatting();
-        assertEquals(1, sheetCF.getNumConditionalFormattings());
-        
-        ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
-        assertEquals(1, cf.getNumberOfRules());
-        rule1 = cf.getRule(0);
-        dbFmt = rule1.getDataBarFormatting();
-        assertEquals(ConditionType.DATA_BAR, rule1.getConditionType());
-
-        assertFalse(dbFmt.isIconOnly());
-        assertTrue(dbFmt.isLeftToRight());
-        assertEquals(0,   dbFmt.getWidthMin());
-        assertEquals(100, dbFmt.getWidthMax());
-        assertColour(colorHex, dbFmt.getColor());
-
-        assertEquals(RangeType.MIN, dbFmt.getMinThreshold().getRangeType());
-        assertEquals(RangeType.MAX, dbFmt.getMaxThreshold().getRangeType());
-        assertNull(dbFmt.getMinThreshold().getValue());
-        assertNull(dbFmt.getMaxThreshold().getValue());
-        
-        wb2.close();
+        try (Workbook wb1 = _testDataProvider.createWorkbook()) {
+            Sheet sheet = wb1.createSheet();
+
+            String colorHex = "FFFFEB84";
+            ExtendedColor color = wb1.getCreationHelper().createExtendedColor();
+            color.setARGBHex(colorHex);
+            SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
+            ConditionalFormattingRule rule1 =
+                    sheetCF.createConditionalFormattingRule(color);
+            DataBarFormatting dbFmt = rule1.getDataBarFormatting();
+
+            assertFalse(dbFmt.isIconOnly());
+            assertTrue(dbFmt.isLeftToRight());
+            assertEquals(0, dbFmt.getWidthMin());
+            assertEquals(100, dbFmt.getWidthMax());
+            assertColour(colorHex, dbFmt.getColor());
+
+            dbFmt.getMinThreshold().setRangeType(RangeType.MIN);
+            dbFmt.getMaxThreshold().setRangeType(RangeType.MAX);
+
+            CellRangeAddress[] regions = {CellRangeAddress.valueOf("A1:A5")};
+            sheetCF.addConditionalFormatting(regions, rule1);
+
+            // Save, re-load and re-check
+            try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
+                sheet = wb2.getSheetAt(0);
+                sheetCF = sheet.getSheetConditionalFormatting();
+                assertEquals(1, sheetCF.getNumConditionalFormattings());
+
+                ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);
+                assertEquals(1, cf.getNumberOfRules());
+                rule1 = cf.getRule(0);
+                dbFmt = rule1.getDataBarFormatting();
+                assertEquals(ConditionType.DATA_BAR, rule1.getConditionType());
+
+                assertFalse(dbFmt.isIconOnly());
+                assertTrue(dbFmt.isLeftToRight());
+                assertEquals(0, dbFmt.getWidthMin());
+                assertEquals(100, dbFmt.getWidthMax());
+                assertColour(colorHex, dbFmt.getColor());
+
+                assertEquals(RangeType.MIN, dbFmt.getMinThreshold().getRangeType());
+                assertEquals(RangeType.MAX, dbFmt.getMaxThreshold().getRangeType());
+                assertNull(dbFmt.getMinThreshold().getValue());
+                assertNull(dbFmt.getMaxThreshold().getValue());
+            }
+        }
     }
-    
+
     @Test
     public void testBug55380() throws IOException {
-        Workbook wb = _testDataProvider.createWorkbook();
-        Sheet sheet = wb.createSheet();
-        CellRangeAddress[] ranges = new CellRangeAddress[] {
-            CellRangeAddress.valueOf("C9:D30"), CellRangeAddress.valueOf("C7:C31")
-        };
-        ConditionalFormattingRule rule = sheet.getSheetConditionalFormatting().createConditionalFormattingRule("$A$1>0");
-        sheet.getSheetConditionalFormatting().addConditionalFormatting(ranges, rule);
-        wb.close();
+        try (Workbook wb = _testDataProvider.createWorkbook()) {
+            Sheet sheet = wb.createSheet();
+            CellRangeAddress[] ranges = new CellRangeAddress[]{
+                    CellRangeAddress.valueOf("C9:D30"), CellRangeAddress.valueOf("C7:C31")
+            };
+            ConditionalFormattingRule rule = sheet.getSheetConditionalFormatting().createConditionalFormattingRule("$A$1>0");
+            sheet.getSheetConditionalFormatting().addConditionalFormatting(ranges, rule);
+        }
     }
 
     @Test
-    public void testSetCellRangeAddresswithSingleRange() throws Exception {
-        Workbook wb = _testDataProvider.createWorkbook();
-        final Sheet sheet = wb.createSheet("S1");
-        final SheetConditionalFormatting cf = sheet.getSheetConditionalFormatting();
-        assertEquals(0, cf.getNumConditionalFormattings());
-        ConditionalFormattingRule rule1 = cf.createConditionalFormattingRule("$A$1>0");
-        cf.addConditionalFormatting(new CellRangeAddress[] {
-                CellRangeAddress.valueOf("A1:A5")
-        }, rule1);
-
-        assertEquals(1, cf.getNumConditionalFormattings());
-        ConditionalFormatting readCf = cf.getConditionalFormattingAt(0);
-        CellRangeAddress[] formattingRanges = readCf.getFormattingRanges();
-        assertEquals(1, formattingRanges.length);
-        CellRangeAddress formattingRange = formattingRanges[0];
-        assertEquals("A1:A5", formattingRange.formatAsString());
-
-        readCf.setFormattingRanges(new CellRangeAddress[] {
-                CellRangeAddress.valueOf("A1:A6")
-        });
-
-        readCf = cf.getConditionalFormattingAt(0);
-        formattingRanges = readCf.getFormattingRanges();
-        assertEquals(1, formattingRanges.length);
-        formattingRange = formattingRanges[0];
-        assertEquals("A1:A6", formattingRange.formatAsString());
+    public void testSetCellRangeAddresswithSingleRange() throws IOException {
+        try (Workbook wb = _testDataProvider.createWorkbook()) {
+            final Sheet sheet = wb.createSheet("S1");
+            final SheetConditionalFormatting cf = sheet.getSheetConditionalFormatting();
+            assertEquals(0, cf.getNumConditionalFormattings());
+            ConditionalFormattingRule rule1 = cf.createConditionalFormattingRule("$A$1>0");
+            cf.addConditionalFormatting(new CellRangeAddress[]{
+                    CellRangeAddress.valueOf("A1:A5")
+            }, rule1);
+
+            assertEquals(1, cf.getNumConditionalFormattings());
+            ConditionalFormatting readCf = cf.getConditionalFormattingAt(0);
+            CellRangeAddress[] formattingRanges = readCf.getFormattingRanges();
+            assertEquals(1, formattingRanges.length);
+            CellRangeAddress formattingRange = formattingRanges[0];
+            assertEquals("A1:A5", formattingRange.formatAsString());
+
+            readCf.setFormattingRanges(new CellRangeAddress[]{
+                    CellRangeAddress.valueOf("A1:A6")
+            });
+
+            readCf = cf.getConditionalFormattingAt(0);
+            formattingRanges = readCf.getFormattingRanges();
+            assertEquals(1, formattingRanges.length);
+            formattingRange = formattingRanges[0];
+            assertEquals("A1:A6", formattingRange.formatAsString());
+        }
     }
 
     @Test
-    public void testSetCellRangeAddressWithMultipleRanges() throws Exception {
-        Workbook wb = _testDataProvider.createWorkbook();
-        final Sheet sheet = wb.createSheet("S1");
-        final SheetConditionalFormatting cf = sheet.getSheetConditionalFormatting();
-        assertEquals(0, cf.getNumConditionalFormattings());
-        ConditionalFormattingRule rule1 = cf.createConditionalFormattingRule("$A$1>0");
-        cf.addConditionalFormatting(new CellRangeAddress[] {
-                CellRangeAddress.valueOf("A1:A5")
-        }, rule1);
-
-        assertEquals(1, cf.getNumConditionalFormattings());
-        ConditionalFormatting readCf = cf.getConditionalFormattingAt(0);
-        CellRangeAddress[] formattingRanges = readCf.getFormattingRanges();
-        assertEquals(1, formattingRanges.length);
-        CellRangeAddress formattingRange = formattingRanges[0];
-        assertEquals("A1:A5", formattingRange.formatAsString());
-
-        readCf.setFormattingRanges(new CellRangeAddress[] {
-                CellRangeAddress.valueOf("A1:A6"),
-                CellRangeAddress.valueOf("B1:B6")
-        });
-
-        readCf = cf.getConditionalFormattingAt(0);
-        formattingRanges = readCf.getFormattingRanges();
-        assertEquals(2, formattingRanges.length);
-        formattingRange = formattingRanges[0];
-        assertEquals("A1:A6", formattingRange.formatAsString());
-        formattingRange = formattingRanges[1];
-        assertEquals("B1:B6", formattingRange.formatAsString());
+    public void testSetCellRangeAddressWithMultipleRanges() throws IOException {
+        try (Workbook wb = _testDataProvider.createWorkbook()) {
+            final Sheet sheet = wb.createSheet("S1");
+            final SheetConditionalFormatting cf = sheet.getSheetConditionalFormatting();
+            assertEquals(0, cf.getNumConditionalFormattings());
+            ConditionalFormattingRule rule1 = cf.createConditionalFormattingRule("$A$1>0");
+            cf.addConditionalFormatting(new CellRangeAddress[]{
+                    CellRangeAddress.valueOf("A1:A5")
+            }, rule1);
+
+            assertEquals(1, cf.getNumConditionalFormattings());
+            ConditionalFormatting readCf = cf.getConditionalFormattingAt(0);
+            CellRangeAddress[] formattingRanges = readCf.getFormattingRanges();
+            assertEquals(1, formattingRanges.length);
+            CellRangeAddress formattingRange = formattingRanges[0];
+            assertEquals("A1:A5", formattingRange.formatAsString());
+
+            readCf.setFormattingRanges(new CellRangeAddress[]{
+                    CellRangeAddress.valueOf("A1:A6"),
+                    CellRangeAddress.valueOf("B1:B6")
+            });
+
+            readCf = cf.getConditionalFormattingAt(0);
+            formattingRanges = readCf.getFormattingRanges();
+            assertEquals(2, formattingRanges.length);
+            formattingRange = formattingRanges[0];
+            assertEquals("A1:A6", formattingRange.formatAsString());
+            formattingRange = formattingRanges[1];
+            assertEquals("B1:B6", formattingRange.formatAsString());
+        }
     }
 
     @Test(expected = IllegalArgumentException.class)
-    public void testSetCellRangeAddressWithNullRanges() throws Exception {
-        Workbook wb = _testDataProvider.createWorkbook();
-        final Sheet sheet = wb.createSheet("S1");
-        final SheetConditionalFormatting cf = sheet.getSheetConditionalFormatting();
-        assertEquals(0, cf.getNumConditionalFormattings());
-        ConditionalFormattingRule rule1 = cf.createConditionalFormattingRule("$A$1>0");
-        cf.addConditionalFormatting(new CellRangeAddress[] {
-                CellRangeAddress.valueOf("A1:A5")
-        }, rule1);
-
-        assertEquals(1, cf.getNumConditionalFormattings());
-        ConditionalFormatting readCf = cf.getConditionalFormattingAt(0);
-        readCf.setFormattingRanges(null);
+    public void testSetCellRangeAddressWithNullRanges() throws IOException {
+        try (Workbook wb = _testDataProvider.createWorkbook()) {
+            final Sheet sheet = wb.createSheet("S1");
+            final SheetConditionalFormatting cf = sheet.getSheetConditionalFormatting();
+            assertEquals(0, cf.getNumConditionalFormattings());
+            ConditionalFormattingRule rule1 = cf.createConditionalFormattingRule("$A$1>0");
+            cf.addConditionalFormatting(new CellRangeAddress[]{
+                    CellRangeAddress.valueOf("A1:A5")
+            }, rule1);
+
+            assertEquals(1, cf.getNumConditionalFormattings());
+            ConditionalFormatting readCf = cf.getConditionalFormattingAt(0);
+            readCf.setFormattingRanges(null);
+        }
     }
 
 
-    @SuppressWarnings("deprecation")
     @Test
-    public void test52122() throws Exception {
-        Workbook workbook = _testDataProvider.createWorkbook();
-        Sheet sheet = workbook.createSheet("Conditional Formatting Test");
-        sheet.setColumnWidth(0, 256 * 10);
-        sheet.setColumnWidth(1, 256 * 10);
-        sheet.setColumnWidth(2, 256 * 10);
-
-        // Create some content.
-        // row 0
-        Row row = sheet.createRow(0);
+    public void test52122() throws IOException {
+        try (Workbook workbook = _testDataProvider.createWorkbook()) {
+            Sheet sheet = workbook.createSheet("Conditional Formatting Test");
+            sheet.setColumnWidth(0, 256 * 10);
+            sheet.setColumnWidth(1, 256 * 10);
+            sheet.setColumnWidth(2, 256 * 10);
 
-        Cell cell0 = row.createCell(0);
-        cell0.setCellValue(100);
+            // Create some content.
+            // row 0
+            Row row = sheet.createRow(0);
 
-        Cell cell1 = row.createCell(1);
-        cell1.setCellValue(120);
+            Cell cell0 = row.createCell(0);
+            cell0.setCellValue(100);
 
-        Cell cell2 = row.createCell(2);
-        cell2.setCellValue(130);
+            Cell cell1 = row.createCell(1);
+            cell1.setCellValue(120);
 
-        // row 1
-        row = sheet.createRow(1);
+            Cell cell2 = row.createCell(2);
+            cell2.setCellValue(130);
 
-        cell0 = row.createCell(0);
-        cell0.setCellValue(200);
+            // row 1
+            row = sheet.createRow(1);
 
-        cell1 = row.createCell(1);
-        cell1.setCellValue(220);
+            cell0 = row.createCell(0);
+            cell0.setCellValue(200);
 
-        cell2 = row.createCell(2);
-        cell2.setCellValue(230);
+            cell1 = row.createCell(1);
+            cell1.setCellValue(220);
 
-        // row 2
-        row = sheet.createRow(2);
+            cell2 = row.createCell(2);
+            cell2.setCellValue(230);
 
-        cell0 = row.createCell(0);
-        cell0.setCellValue(300);
+            // row 2
+            row = sheet.createRow(2);
 
-        cell1 = row.createCell(1);
-        cell1.setCellValue(320);
+            cell0 = row.createCell(0);
+            cell0.setCellValue(300);
 
-        cell2 = row.createCell(2);
-        cell2.setCellValue(330);
+            cell1 = row.createCell(1);
+            cell1.setCellValue(320);
 
-        // Create conditional formatting, CELL1 should be yellow if CELL0 is not blank.
-        SheetConditionalFormatting formatting = sheet.getSheetConditionalFormatting();
+            cell2 = row.createCell(2);
+            cell2.setCellValue(330);
 
-        ConditionalFormattingRule rule = formatting.createConditionalFormattingRule("$A$1>75");
+            // Create conditional formatting, CELL1 should be yellow if CELL0 is not blank.
+            SheetConditionalFormatting formatting = sheet.getSheetConditionalFormatting();
 
-        PatternFormatting pattern = rule.createPatternFormatting();
-        pattern.setFillBackgroundColor(IndexedColors.BLUE.index);
-        pattern.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
+            ConditionalFormattingRule rule = formatting.createConditionalFormattingRule("$A$1>75");
 
-        CellRangeAddress[] range = {CellRangeAddress.valueOf("B2:C2")};
-        CellRangeAddress[] range2 = {CellRangeAddress.valueOf("B1:C1")};
+            PatternFormatting pattern = rule.createPatternFormatting();
+            pattern.setFillBackgroundColor(IndexedColors.BLUE.index);
+            pattern.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
 
-        formatting.addConditionalFormatting(range, rule);
-        formatting.addConditionalFormatting(range2, rule);
+            CellRangeAddress[] range = {CellRangeAddress.valueOf("B2:C2")};
+            CellRangeAddress[] range2 = {CellRangeAddress.valueOf("B1:C1")};
 
-        // Write file.
-        /*FileOutputStream fos = new FileOutputStream("c:\\temp\\52122_conditional-sheet.xls");
-        try {
-            workbook.write(fos);
-        } finally {
-            fos.close();
-        }*/
+            formatting.addConditionalFormatting(range, rule);
+            formatting.addConditionalFormatting(range2, rule);
 
-        Workbook wbBack = _testDataProvider.writeOutAndReadBack(workbook);
-        Sheet sheetBack = wbBack.getSheetAt(0);
-        final SheetConditionalFormatting sheetConditionalFormattingBack = sheetBack.getSheetConditionalFormatting();
-        assertNotNull(sheetConditionalFormattingBack);
-        final ConditionalFormatting formattingBack = sheetConditionalFormattingBack.getConditionalFormattingAt(0);
-        assertNotNull(formattingBack);
-        final ConditionalFormattingRule ruleBack = formattingBack.getRule(0);
-        assertNotNull(ruleBack);
-        final PatternFormatting patternFormattingBack1 = ruleBack.getPatternFormatting();
-        assertNotNull(patternFormattingBack1);
-        assertEquals(IndexedColors.BLUE.index, patternFormattingBack1.getFillBackgroundColor());
-        assertEquals(PatternFormatting.SOLID_FOREGROUND, patternFormattingBack1.getFillPattern());
+            try (Workbook wbBack = _testDataProvider.writeOutAndReadBack(workbook)) {
+                Sheet sheetBack = wbBack.getSheetAt(0);
+                final SheetConditionalFormatting sheetConditionalFormattingBack = sheetBack.getSheetConditionalFormatting();
+                assertNotNull(sheetConditionalFormattingBack);
+                final ConditionalFormatting formattingBack = sheetConditionalFormattingBack.getConditionalFormattingAt(0);
+                assertNotNull(formattingBack);
+                final ConditionalFormattingRule ruleBack = formattingBack.getRule(0);
+                assertNotNull(ruleBack);
+                final PatternFormatting patternFormattingBack1 = ruleBack.getPatternFormatting();
+                assertNotNull(patternFormattingBack1);
+                assertEquals(IndexedColors.BLUE.index, patternFormattingBack1.getFillBackgroundColor());
+                assertEquals(PatternFormatting.SOLID_FOREGROUND, patternFormattingBack1.getFillPattern());
+            }
+        }
     }
 }
\ No newline at end of file