]> source.dussan.org Git - poi.git/commitdiff
findbugs: check return value of File.mkdir(s), and don't accidentally catch RuntimeEx...
authorJaven O'Neal <onealj@apache.org>
Fri, 4 Dec 2015 10:33:28 +0000 (10:33 +0000)
committerJaven O'Neal <onealj@apache.org>
Fri, 4 Dec 2015 10:33:28 +0000 (10:33 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1717923 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java

index 2e660ac91f936320b2000b997414188faeff6c55..3e307f02d77695f55fc7076f04bc616e348c0b76 100644 (file)
@@ -21,6 +21,7 @@ import static org.apache.poi.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
 
 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);
                     }