Fix some Sonar issues

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1809636 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2017-09-25 15:10:40 +00:00
parent 61a281e790
commit 43717f6936
5 changed files with 29 additions and 38 deletions

View File

@ -94,15 +94,15 @@ public class BigGridDemo {
String sheetRef = sheet.getPackagePart().getPartName().getName();
//save the template
FileOutputStream os = new FileOutputStream("template.xlsx");
wb.write(os);
os.close();
try (FileOutputStream os = new FileOutputStream("template.xlsx")) {
wb.write(os);
}
//Step 2. Generate XML file.
File tmp = File.createTempFile("sheet", ".xml");
Writer fw = new OutputStreamWriter(new FileOutputStream(tmp), XML_ENCODING);
generate(fw, styles);
fw.close();
try (Writer fw = new OutputStreamWriter(new FileOutputStream(tmp), XML_ENCODING)) {
generate(fw, styles);
}
//Step 3. Substitute the template entry with the generated data
try (FileOutputStream out = new FileOutputStream("big-grid.xlsx")) {

View File

@ -140,8 +140,8 @@ public class SXSSFWorkbook implements Workbook {
* <ul>
* <li>
* Access initial cells and rows in the template. After constructing
* {@link #SXSSFWorkbook(XSSFWorkbook)} all internal windows are empty and
* {@link SXSSFSheet#getRow} and {@link SXSSFRow#getCell} return <code>null</code>.
* all internal windows are empty and {@link SXSSFSheet#getRow} and
* {@link SXSSFRow#getCell} return <code>null</code>.
* </li>
* <li>
* Override existing cells and rows. The API silently allows that but
@ -369,30 +369,24 @@ public class SXSSFWorkbook implements Workbook {
protected void injectData(ZipEntrySource zipEntrySource, OutputStream out) throws IOException {
try {
ZipOutputStream zos = new ZipOutputStream(out);
try {
try (ZipOutputStream zos = new ZipOutputStream(out)) {
Enumeration<? extends ZipEntry> en = zipEntrySource.getEntries();
while (en.hasMoreElements()) {
ZipEntry ze = en.nextElement();
zos.putNextEntry(new ZipEntry(ze.getName()));
InputStream is = zipEntrySource.getInputStream(ze);
XSSFSheet xSheet=getSheetFromZipEntryName(ze.getName());
XSSFSheet xSheet = getSheetFromZipEntryName(ze.getName());
// See bug 56557, we should not inject data into the special ChartSheets
if(xSheet!=null && !(xSheet instanceof XSSFChartSheet)) {
SXSSFSheet sxSheet=getSXSSFSheet(xSheet);
InputStream xis = sxSheet.getWorksheetXMLInputStream();
try {
copyStreamAndInjectWorksheet(is,zos,xis);
} finally {
xis.close();
if (xSheet != null && !(xSheet instanceof XSSFChartSheet)) {
SXSSFSheet sxSheet = getSXSSFSheet(xSheet);
try (InputStream xis = sxSheet.getWorksheetXMLInputStream()) {
copyStreamAndInjectWorksheet(is, zos, xis);
}
} else {
IOUtils.copy(is, zos);
}
is.close();
}
} finally {
zos.close();
}
} finally {
zipEntrySource.close();
@ -918,11 +912,8 @@ public class SXSSFWorkbook implements Workbook {
File tmplFile = TempFile.createTempFile("poi-sxssf-template", ".xlsx");
boolean deleted;
try {
FileOutputStream os = new FileOutputStream(tmplFile);
try {
try (FileOutputStream os = new FileOutputStream(tmplFile)) {
_wb.write(os);
} finally {
os.close();
}
//Substitute the template entries with the generated sheet data files

View File

@ -482,7 +482,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
*/
protected static OPCPackage newPackage(XSSFWorkbookType workbookType) {
try {
OPCPackage pkg = OPCPackage.create(new ByteArrayOutputStream());
OPCPackage pkg = OPCPackage.create(new ByteArrayOutputStream()); // NOSONAR - we do not want to close this here
// Main part
PackagePartName corePartName = PackagingURIHelper.createPartName(XSSFRelation.WORKBOOK.getDefaultFileName());
// Create main part relationship
@ -2361,7 +2361,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
@Override
public int addOlePackage(byte[] oleData, String label, String fileName, String command)
throws IOException {
throws IOException {
// find an unused part name
OPCPackage opc = getPackage();
PackagePartName pnOLE;
@ -2381,17 +2381,17 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
ByteArrayOutputStream bos = new ByteArrayOutputStream(oleData.length+500);
ole10.writeOut(bos);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryNode root = poifs.getRoot();
root.createDocument(Ole10Native.OLE10_NATIVE, new ByteArrayInputStream(bos.toByteArray()));
root.setStorageClsid(ClassID.OLE10_PACKAGE);
try (POIFSFileSystem poifs = new POIFSFileSystem()) {
DirectoryNode root = poifs.getRoot();
root.createDocument(Ole10Native.OLE10_NATIVE, new ByteArrayInputStream(bos.toByteArray()));
root.setStorageClsid(ClassID.OLE10_PACKAGE);
// TODO: generate CombObj stream
// TODO: generate CombObj stream
OutputStream os = pp.getOutputStream();
poifs.writeFilesystem(os);
os.close();
poifs.close();
try (OutputStream os = pp.getOutputStream()) {
poifs.writeFilesystem(os);
}
}
return oleId;
}

View File

@ -129,7 +129,7 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
*/
protected static OPCPackage newPackage() {
try {
OPCPackage pkg = OPCPackage.create(new ByteArrayOutputStream());
OPCPackage pkg = OPCPackage.create(new ByteArrayOutputStream()); // NOSONAR - we do not want to close this here
// Main part
PackagePartName corePartName = PackagingURIHelper.createPartName(XWPFRelation.DOCUMENT.getDefaultFileName());
// Create main part relationship

View File

@ -203,9 +203,9 @@ public class TestXSSFBReader {
@Override
public void headerFooter(String text, boolean isHeader, String tagName) {
if (isHeader) {
sb.append("<header tagName=\"" + tagName + "\">" + text + "</header>");
sb.append("<header tagName=\"").append(tagName).append("\">").append(text).append("</header>");
} else {
sb.append("<footer tagName=\"" + tagName + "\">" + text + "</footer>");
sb.append("<footer tagName=\"").append(tagName).append("\">").append(text).append("</footer>");
}
}