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(); String sheetRef = sheet.getPackagePart().getPartName().getName();
//save the template //save the template
FileOutputStream os = new FileOutputStream("template.xlsx"); try (FileOutputStream os = new FileOutputStream("template.xlsx")) {
wb.write(os); wb.write(os);
os.close(); }
//Step 2. Generate XML file. //Step 2. Generate XML file.
File tmp = File.createTempFile("sheet", ".xml"); File tmp = File.createTempFile("sheet", ".xml");
Writer fw = new OutputStreamWriter(new FileOutputStream(tmp), XML_ENCODING); try (Writer fw = new OutputStreamWriter(new FileOutputStream(tmp), XML_ENCODING)) {
generate(fw, styles); generate(fw, styles);
fw.close(); }
//Step 3. Substitute the template entry with the generated data //Step 3. Substitute the template entry with the generated data
try (FileOutputStream out = new FileOutputStream("big-grid.xlsx")) { try (FileOutputStream out = new FileOutputStream("big-grid.xlsx")) {

View File

@ -140,8 +140,8 @@ public class SXSSFWorkbook implements Workbook {
* <ul> * <ul>
* <li> * <li>
* Access initial cells and rows in the template. After constructing * Access initial cells and rows in the template. After constructing
* {@link #SXSSFWorkbook(XSSFWorkbook)} all internal windows are empty and * all internal windows are empty and {@link SXSSFSheet#getRow} and
* {@link SXSSFSheet#getRow} and {@link SXSSFRow#getCell} return <code>null</code>. * {@link SXSSFRow#getCell} return <code>null</code>.
* </li> * </li>
* <li> * <li>
* Override existing cells and rows. The API silently allows that but * 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 { protected void injectData(ZipEntrySource zipEntrySource, OutputStream out) throws IOException {
try { try {
ZipOutputStream zos = new ZipOutputStream(out); try (ZipOutputStream zos = new ZipOutputStream(out)) {
try {
Enumeration<? extends ZipEntry> en = zipEntrySource.getEntries(); Enumeration<? extends ZipEntry> en = zipEntrySource.getEntries();
while (en.hasMoreElements()) { while (en.hasMoreElements()) {
ZipEntry ze = en.nextElement(); ZipEntry ze = en.nextElement();
zos.putNextEntry(new ZipEntry(ze.getName())); zos.putNextEntry(new ZipEntry(ze.getName()));
InputStream is = zipEntrySource.getInputStream(ze); 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 // See bug 56557, we should not inject data into the special ChartSheets
if(xSheet!=null && !(xSheet instanceof XSSFChartSheet)) { if (xSheet != null && !(xSheet instanceof XSSFChartSheet)) {
SXSSFSheet sxSheet=getSXSSFSheet(xSheet); SXSSFSheet sxSheet = getSXSSFSheet(xSheet);
InputStream xis = sxSheet.getWorksheetXMLInputStream(); try (InputStream xis = sxSheet.getWorksheetXMLInputStream()) {
try { copyStreamAndInjectWorksheet(is, zos, xis);
copyStreamAndInjectWorksheet(is,zos,xis);
} finally {
xis.close();
} }
} else { } else {
IOUtils.copy(is, zos); IOUtils.copy(is, zos);
} }
is.close(); is.close();
} }
} finally {
zos.close();
} }
} finally { } finally {
zipEntrySource.close(); zipEntrySource.close();
@ -918,11 +912,8 @@ public class SXSSFWorkbook implements Workbook {
File tmplFile = TempFile.createTempFile("poi-sxssf-template", ".xlsx"); File tmplFile = TempFile.createTempFile("poi-sxssf-template", ".xlsx");
boolean deleted; boolean deleted;
try { try {
FileOutputStream os = new FileOutputStream(tmplFile); try (FileOutputStream os = new FileOutputStream(tmplFile)) {
try {
_wb.write(os); _wb.write(os);
} finally {
os.close();
} }
//Substitute the template entries with the generated sheet data files //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) { protected static OPCPackage newPackage(XSSFWorkbookType workbookType) {
try { try {
OPCPackage pkg = OPCPackage.create(new ByteArrayOutputStream()); OPCPackage pkg = OPCPackage.create(new ByteArrayOutputStream()); // NOSONAR - we do not want to close this here
// Main part // Main part
PackagePartName corePartName = PackagingURIHelper.createPartName(XSSFRelation.WORKBOOK.getDefaultFileName()); PackagePartName corePartName = PackagingURIHelper.createPartName(XSSFRelation.WORKBOOK.getDefaultFileName());
// Create main part relationship // Create main part relationship
@ -2361,7 +2361,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
@Override @Override
public int addOlePackage(byte[] oleData, String label, String fileName, String command) public int addOlePackage(byte[] oleData, String label, String fileName, String command)
throws IOException { throws IOException {
// find an unused part name // find an unused part name
OPCPackage opc = getPackage(); OPCPackage opc = getPackage();
PackagePartName pnOLE; PackagePartName pnOLE;
@ -2381,17 +2381,17 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
ByteArrayOutputStream bos = new ByteArrayOutputStream(oleData.length+500); ByteArrayOutputStream bos = new ByteArrayOutputStream(oleData.length+500);
ole10.writeOut(bos); ole10.writeOut(bos);
POIFSFileSystem poifs = new POIFSFileSystem(); try (POIFSFileSystem poifs = new POIFSFileSystem()) {
DirectoryNode root = poifs.getRoot(); DirectoryNode root = poifs.getRoot();
root.createDocument(Ole10Native.OLE10_NATIVE, new ByteArrayInputStream(bos.toByteArray())); root.createDocument(Ole10Native.OLE10_NATIVE, new ByteArrayInputStream(bos.toByteArray()));
root.setStorageClsid(ClassID.OLE10_PACKAGE); root.setStorageClsid(ClassID.OLE10_PACKAGE);
// TODO: generate CombObj stream // TODO: generate CombObj stream
OutputStream os = pp.getOutputStream(); try (OutputStream os = pp.getOutputStream()) {
poifs.writeFilesystem(os); poifs.writeFilesystem(os);
os.close(); }
poifs.close(); }
return oleId; return oleId;
} }

View File

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

View File

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