poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From one...@apache.org
Subject svn commit: r1717923 - /poi/trunk/src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java
Date Fri, 04 Dec 2015 10:33:28 GMT
Author: onealj
Date: Fri Dec  4 10:33:28 2015
New Revision: 1717923

URL: http://svn.apache.org/viewvc?rev=1717923&view=rev
Log:
findbugs: check return value of File.mkdir(s), and don't accidentally catch RuntimeException

Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java?rev=1717923&r1=1717922&r2=1717923&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java Fri Dec  4 10:33:28 2015
@@ -21,6 +21,7 @@ import static org.apache.poi.POIXMLTypeL
 
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.OutputStream;
 import java.util.Enumeration;
 import java.util.zip.ZipEntry;
@@ -28,6 +29,7 @@ import java.util.zip.ZipFile;
 
 import org.apache.poi.openxml4j.opc.internal.ZipHelper;
 import org.apache.poi.util.IOUtils;
+import org.apache.xmlbeans.XmlException;
 import org.apache.xmlbeans.XmlObject;
 import org.apache.xmlbeans.XmlOptions;
 
@@ -49,13 +51,32 @@ public final class XSSFDump {
             }
         }
     }
+    
+    private static void createDirIfMissing(File directory) throws RuntimeException {
+        if (!directory.exists()) {
+            boolean dirWasCreated = directory.mkdir();
+            if (!dirWasCreated) {
+                throw new RuntimeException("Unable to create directory: " + directory);
+            }
+        }
+    }
+    
+    private static void recursivelyCreateDirIfMissing(File directory) throws RuntimeException
{
+        if (!directory.exists()) {
+            boolean dirsWereCreated = directory.mkdirs();
+            if (!dirsWereCreated) {
+                throw new RuntimeException("Unable to recursively create directory: " + directory);
+            }
+        }
+    }
+    
 
     public static void dump(ZipFile zip) throws Exception {
         String zipname = zip.getName();
         int sep = zipname.lastIndexOf('.');
         File root = new File(zipname.substring(0, sep));
-        root.mkdir();
-        System.out.println("Dupming to directory " + root);
+        createDirIfMissing(root);
+        System.out.println("Dumping to directory " + root);
 
         Enumeration<? extends ZipEntry> en = zip.entries();
         while(en.hasMoreElements()){
@@ -64,7 +85,7 @@ public final class XSSFDump {
             int idx = name.lastIndexOf('/');
             if(idx != -1){
                 File bs = new File(root, name.substring(0, idx));
-                bs.mkdirs();
+                recursivelyCreateDirIfMissing(bs);
             }
 
             File f = new File(root, entry.getName());
@@ -76,7 +97,7 @@ public final class XSSFDump {
                         XmlOptions options = new XmlOptions();
                         options.setSavePrettyPrint();
                         xml.save(out, options);
-                    } catch (Exception e){
+                    } catch (XmlException e) {
                         System.err.println("Failed to parse " + entry.getName() + ", dumping
raw content");
                         IOUtils.copy(zip.getInputStream(entry), out);
                     }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message