SonarQube fixes - close resources

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1796962 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2017-05-31 00:31:53 +00:00
parent 7e6007e0bd
commit 95164e4a50
21 changed files with 695 additions and 668 deletions

View File

@ -34,7 +34,7 @@ public final class BulletsDemo {
public static void main(String[] args) throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow();
try {
HSLFSlide slide = ppt.createSlide();
HSLFTextBox shape = new HSLFTextBox();
@ -57,7 +57,8 @@ public final class BulletsDemo {
FileOutputStream out = new FileOutputStream("bullets.ppt");
ppt.write(out);
out.close();
} finally {
ppt.close();
}
}
}

View File

@ -34,6 +34,7 @@ public abstract class CreateHyperlink {
public static void main(String[] args) throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow();
try {
HSLFSlide slideA = ppt.createSlide();
ppt.createSlide();
HSLFSlide slideC = ppt.createSlide();
@ -58,7 +59,8 @@ public abstract class CreateHyperlink {
FileOutputStream out = new FileOutputStream("hyperlink.ppt");
ppt.write(out);
out.close();
} finally {
ppt.close();
}
}
}

View File

@ -41,6 +41,7 @@ public final class Graphics2DDemo {
public static void main(String[] args) throws Exception {
HSLFSlideShow ppt = new HSLFSlideShow();
try {
//bar chart data. The first value is the bar color, the second is the width
Object[] def = new Object[]{
Color.yellow, 40,
@ -79,7 +80,8 @@ public final class Graphics2DDemo {
FileOutputStream out = new FileOutputStream("hslf-graphics.ppt");
ppt.write(out);
out.close();
} finally {
ppt.close();
}
}
}

View File

@ -29,6 +29,7 @@ public abstract class HeadersFootersDemo {
public static void main(String[] args) throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow();
try {
HeadersFooters slideHeaders = ppt.getSlideHeadersFooters();
slideHeaders.setFootersText("Created by POI-HSLF");
slideHeaders.setSlideNumberVisible(true);
@ -43,8 +44,9 @@ public abstract class HeadersFootersDemo {
FileOutputStream out = new FileOutputStream("headers_footers.ppt");
ppt.write(out);
out.close();
} finally {
ppt.close();
}
}
}

View File

@ -31,15 +31,11 @@ import org.apache.poi.sl.usermodel.VerticalAlignment;
/**
* Demonstrates how to create tables
*
* @author Yegor Kozlov
*/
public final class TableDemo {
public static void main(String[] args) throws Exception {
//test data for the first taable
String[][] txt1 = {
//test data for the first table
static final String[][] txt1 = {
{"INPUT FILE", "NUMBER OF RECORDS"},
{"Item File", "11,559"},
{"Vendor File", "502"},
@ -48,10 +44,32 @@ public final class TableDemo {
{"Total PO History Spend", "$10,172,038"}
};
//test data for the second taable
static final String[][] txt2 = {
{"Data Source"},
{"CAS Internal Metrics - Item Master Summary\r" +
"CAS Internal Metrics - Vendor Summary\r" +
"CAS Internal Metrics - PO History Summary"}
};
public static void main(String[] args) throws Exception {
HSLFSlideShow ppt = new HSLFSlideShow();
try {
HSLFSlide slide = ppt.createSlide();
create1stTable(slide);
create2ndTable(slide);
FileOutputStream out = new FileOutputStream("hslf-table.ppt");
ppt.write(out);
out.close();
} finally {
ppt.close();
}
}
static void create1stTable(HSLFSlide slide) {
//six rows, two columns
HSLFTable table1 = slide.createTable(6, 2);
for (int i = 0; i < txt1.length; i++) {
@ -77,17 +95,11 @@ public final class TableDemo {
table1.setColumnWidth(0, 300);
table1.setColumnWidth(1, 150);
int pgWidth = ppt.getPageSize().width;
int pgWidth = slide.getSlideShow().getPageSize().width;
table1.moveTo((pgWidth - table1.getAnchor().getWidth())/2., 100.);
}
//test data for the second taable
String[][] txt2 = {
{"Data Source"},
{"CAS Internal Metrics - Item Master Summary\r" +
"CAS Internal Metrics - Vendor Summary\r" +
"CAS Internal Metrics - PO History Summary"}
};
static void create2ndTable(HSLFSlide slide) {
//two rows, one column
HSLFTable table2 = slide.createTable(2, 1);
for (int i = 0; i < txt2.length; i++) {
@ -120,11 +132,5 @@ public final class TableDemo {
dts2.setOutsideBorders(Color.black, 1.0);
table2.moveTo(200, 400);
FileOutputStream out = new FileOutputStream("hslf-table.ppt");
ppt.write(out);
out.close();
ppt.close();
}
}

View File

@ -731,14 +731,17 @@ public class AddDimensionedImage {
outputFile = args[1];
HSSFWorkbook workbook = new HSSFWorkbook();
try {
sheet = workbook.createSheet("Picture Test");
new AddDimensionedImage().addImageToSheet("A1", sheet,
imageFile, 125, 125,
AddDimensionedImage.EXPAND_ROW_AND_COLUMN);
fos = new FileOutputStream(outputFile);
workbook.write(fos);
} finally {
workbook.close();
}
}
catch(FileNotFoundException fnfEx) {
System.out.println("Caught an: " + fnfEx.getClass().getName());
System.out.println("Message: " + fnfEx.getMessage());

View File

@ -38,10 +38,9 @@ public class BigExample {
public static void main(String[] args) throws IOException {
int rownum;
// create a new file
FileOutputStream out = new FileOutputStream("workbook.xls");
// create a new workbook
HSSFWorkbook wb = new HSSFWorkbook();
try {
// create a new sheet
HSSFSheet s = wb.createSheet();
// declare a row object reference
@ -166,10 +165,15 @@ public class BigExample {
wb.removeSheetAt(1);
//end deleted sheet
// create a new file
FileOutputStream out = new FileOutputStream("workbook.xls");
// write the workbook to the output stream
// close our file (don't blow out our file handles
wb.write(out);
out.close();
} finally {
wb.close();
}
}
}

View File

@ -34,6 +34,7 @@ import org.apache.poi.ss.usermodel.BorderStyle;
public class Borders {
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
try {
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
@ -59,7 +60,8 @@ public class Borders {
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
} finally {
wb.close();
}
}
}

View File

@ -31,18 +31,17 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
/**
* Demonstrates how to work with excel cell comments.
* Demonstrates how to work with excel cell comments.<p>
*
* <p>
* Excel comment is a kind of a text shape,
* so inserting a comment is very similar to placing a text box in a worksheet
* </p>
*/
public class CellComments {
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
try {
HSSFSheet sheet = wb.createSheet("Cell comments in POI HSSF");
// Create the drawing patriarch. This is the top level container for all shapes including cell comments.
@ -100,7 +99,8 @@ public class CellComments {
FileOutputStream out = new FileOutputStream("poi_comment.xls");
wb.write(out);
out.close();
} finally {
wb.close();
}
}
}

View File

@ -29,6 +29,7 @@ import org.apache.poi.ss.usermodel.CellType;
public class CellTypes {
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
try {
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow(2);
row.createCell(0).setCellValue(1.1);
@ -41,7 +42,8 @@ public class CellTypes {
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
} finally {
wb.close();
}
}
}

View File

@ -41,6 +41,7 @@ public class UserDefinedFunctionExample {
public static void main( String[] args ) throws Exception {
if( args.length != 2 ) {
// e.g. src/examples/src/org/apache/poi/ss/examples/formula/mortgage-calculation.xls Sheet1!B4
System.out.println( "usage: UserDefinedFunctionExample fileName cellId" ) ;
return;
}
@ -50,10 +51,9 @@ public class UserDefinedFunctionExample {
File workbookFile = new File( args[0] ) ;
FileInputStream fis = new FileInputStream(workbookFile);
Workbook workbook = WorkbookFactory.create(fis);
fis.close();
Workbook workbook = WorkbookFactory.create(workbookFile, null, true);
try {
String[] functionNames = { "calculatePayment" } ;
FreeRefFunction[] functionImpls = { new CalculateMortgage() } ;
@ -75,7 +75,8 @@ public class UserDefinedFunctionExample {
CellValue value = evaluator.evaluate( cell ) ;
System.out.println("returns value: " + value ) ;
} finally {
workbook.close();
}
}
}

View File

@ -24,15 +24,14 @@ import java.io.FileOutputStream;
/**
* Merge multiple pptx presentations together
*
* @author Yegor Kozlov
*/
public final class MergePresentations {
public static void main(String args[]) throws Exception {
XMLSlideShow ppt = new XMLSlideShow();
for(String arg : args){
try {
for (String arg : args){
FileInputStream is = new FileInputStream(arg);
XMLSlideShow src = new XMLSlideShow(is);
is.close();
@ -47,8 +46,8 @@ public final class MergePresentations {
FileOutputStream out = new FileOutputStream("merged.pptx");
ppt.write(out);
out.close();
} finally {
ppt.close();
}
}
}

View File

@ -25,14 +25,13 @@ import java.io.IOException;
/**
* Demonstrates how to create slides with predefined layout
* and fill the placeholder shapes
*
* @author Yegor Kozlov
*/
public class Tutorial1 {
public static void main(String[] args) throws IOException{
XMLSlideShow ppt = new XMLSlideShow();
try {
// XSLFSlide#createSlide() with no arguments creates a blank slide
/*XSLFSlide blankSlide =*/ ppt.createSlide();
@ -68,7 +67,8 @@ public class Tutorial1 {
FileOutputStream out = new FileOutputStream("slides.pptx");
ppt.write(out);
out.close();
} finally {
ppt.close();
}
}
}

View File

@ -26,14 +26,13 @@ import java.io.IOException;
/**
* Basic paragraph and text formatting
*
* @author Yegor Kozlov
*/
public class Tutorial2 {
public static void main(String[] args) throws IOException{
XMLSlideShow ppt = new XMLSlideShow();
try {
XSLFSlide slide1 = ppt.createSlide();
XSLFTextBox shape1 = slide1.createTextBox();
// initial height of the text box is 100 pt but
@ -79,7 +78,8 @@ public class Tutorial2 {
FileOutputStream out = new FileOutputStream("text.pptx");
ppt.write(out);
out.close();
} finally {
ppt.close();
}
}
}

View File

@ -27,14 +27,13 @@ import org.apache.poi.sl.usermodel.Placeholder;
/**
* How to set slide title
*
* @author Yegor Kozlov
*/
public class Tutorial3 {
public static void main(String[] args) throws IOException{
XMLSlideShow ppt = new XMLSlideShow();
try {
XSLFSlide slide = ppt.createSlide();
XSLFTextShape titleShape = slide.createTextBox();
@ -45,7 +44,8 @@ public class Tutorial3 {
FileOutputStream out = new FileOutputStream("title.pptx");
ppt.write(out);
out.close();
} finally {
ppt.close();
}
}
}

View File

@ -29,14 +29,13 @@ import org.apache.poi.sl.usermodel.TextParagraph.TextAlign;
/**
* PPTX Tables
*
* @author Yegor Kozlov
*/
public class Tutorial4 {
public static void main(String[] args) throws IOException{
XMLSlideShow ppt = new XMLSlideShow();
try {
// XSLFSlide#createSlide() with no arguments creates a blank slide
XSLFSlide slide = ppt.createSlide();
@ -81,14 +80,13 @@ public class Tutorial4 {
cell.setFillColor(new Color(233, 247, 244));
}
}
FileOutputStream out = new FileOutputStream("table.pptx");
ppt.write(out);
out.close();
} finally {
ppt.close();
}
}
}

View File

@ -27,14 +27,13 @@ import org.apache.poi.sl.usermodel.PictureData.PictureType;
/**
* Images
*
* @author Yegor Kozlov
*/
public class Tutorial5 {
public static void main(String[] args) throws IOException{
XMLSlideShow ppt = new XMLSlideShow();
try {
XSLFSlide slide = ppt.createSlide();
File img = new File(System.getProperty("POI.testdata.path", "test-data"), "slideshow/clock.jpg");
@ -45,7 +44,8 @@ public class Tutorial5 {
FileOutputStream out = new FileOutputStream("images.pptx");
ppt.write(out);
out.close();
} finally {
ppt.close();
}
}
}

View File

@ -25,14 +25,13 @@ import java.io.IOException;
/**
* Hyperlinks
*
* @author Yegor Kozlov
*/
public class Tutorial6 {
public static void main(String[] args) throws IOException{
XMLSlideShow ppt = new XMLSlideShow();
try {
XSLFSlide slide1 = ppt.createSlide();
XSLFSlide slide2 = ppt.createSlide();
@ -55,7 +54,8 @@ public class Tutorial6 {
FileOutputStream out = new FileOutputStream("hyperlinks.pptx");
ppt.write(out);
out.close();
} finally {
ppt.close();
}
}
}

View File

@ -28,14 +28,13 @@ import org.apache.poi.sl.usermodel.AutoNumberingScheme;
/**
* Bullets and numbering
*
* @author Yegor Kozlov
*/
public class Tutorial7 {
public static void main(String[] args) throws IOException{
public static void main(String[] args) throws IOException {
XMLSlideShow ppt = new XMLSlideShow();
try {
XSLFSlide slide = ppt.createSlide();
XSLFTextBox shape = slide.createTextBox();
shape.setAnchor(new Rectangle(50, 50, 400, 200));
@ -84,7 +83,8 @@ public class Tutorial7 {
FileOutputStream out = new FileOutputStream("list.pptx");
ppt.write(out);
out.close();
} finally {
ppt.close();
}
}
}

View File

@ -164,12 +164,15 @@ public class EmbeddedExtractor implements Iterable<EmbeddedExtractor> {
protected EmbeddedData extract(DirectoryNode dn) throws IOException {
assert(canExtract(dn));
ByteArrayOutputStream bos = new ByteArrayOutputStream(20000);
POIFSFileSystem dest = new POIFSFileSystem();
try {
copyNodes(dn, dest.getRoot());
// start with a reasonable big size
ByteArrayOutputStream bos = new ByteArrayOutputStream(20000);
dest.writeFilesystem(bos);
} finally {
dest.close();
}
return new EmbeddedData(dn.getName(), bos.toByteArray(), CONTENT_TYPE_BYTES);
}

View File

@ -110,13 +110,15 @@ public class ExcelToFoConverter extends AbstractExcelConverter
public static Document process( File xlsFile ) throws Exception
{
final HSSFWorkbook workbook = ExcelToFoUtils.loadXls( xlsFile );
try {
ExcelToFoConverter excelToHtmlConverter = new ExcelToFoConverter(
XMLHelper.getDocumentBuilderFactory().newDocumentBuilder()
.newDocument() );
excelToHtmlConverter.processWorkbook( workbook );
Document doc = excelToHtmlConverter.getDocument();
return excelToHtmlConverter.getDocument();
} finally {
workbook.close();
return doc;
}
}
private final FoDocumentFacade foDocumentFacade;