]> source.dussan.org Git - poi.git/commitdiff
Fix some Sonar issues
authorDominik Stadler <centic@apache.org>
Mon, 25 Sep 2017 15:10:40 +0000 (15:10 +0000)
committerDominik Stadler <centic@apache.org>
Mon, 25 Sep 2017 15:10:40 +0000 (15:10 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1809636 13f79535-47bb-0310-9956-ffa450edef68

src/examples/src/org/apache/poi/xssf/usermodel/examples/BigGridDemo.java
src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFBReader.java

index a005f16781e3fb77dc826cb51129d8d7a7a75b56..439b16ce85512c9f19fc4decc1256cea75ad3ccd 100644 (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")) {
index 931ddcce5f412a2af6ca236288c793d5a30d0e02..29b0bddab43137c15fc91bddc612f552c04632ea 100644 (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
index 9e0f6013f21462a075ad35d4a96641ddebac42ee..dc6890ecafe5230d740286a010f103a746380e04 100644 (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;
     }
index 7cea879cbc3d17cc002e81f16fa1107e14d9db21..37f377d8f8c755ad0874f2a74ee65b26c53bbffe 100644 (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
index 7052459194aac839d5c14d80fa57b24b91c7a36b..a35bbdd133372c6c507204e3d3e778ad26e294c3 100644 (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>");
 
             }
         }