You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ms...@apache.org on 2018/02/10 20:58:39 UTC

svn commit: r1823795 - in /pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools: ExportFDF.java ExportXFDF.java ImportFDF.java ImportXFDF.java

Author: msahyoun
Date: Sat Feb 10 20:58:39 2018
New Revision: 1823795

URL: http://svn.apache.org/viewvc?rev=1823795&view=rev
Log:
PDFBOX-4071: use try-with

Modified:
    pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExportFDF.java
    pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExportXFDF.java
    pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImportFDF.java
    pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImportXFDF.java

Modified: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExportFDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExportFDF.java?rev=1823795&r1=1823794&r2=1823795&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExportFDF.java (original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExportFDF.java Sat Feb 10 20:58:39 2018
@@ -58,18 +58,14 @@ public class ExportFDF
 
     private void exportFDF( String[] args ) throws IOException
     {
-        PDDocument pdf = null;
-        FDFDocument fdf = null;
-
-        try
+        if( args.length != 1 && args.length != 2 )
         {
-            if( args.length != 1 && args.length != 2 )
-            {
-                usage();
-            }
-            else
+            usage();
+        }
+        else
+        {
+            try (PDDocument pdf = PDDocument.load( new File(args[0]) ))
             {
-                pdf = PDDocument.load( new File(args[0]) );
                 PDAcroForm form = pdf.getDocumentCatalog().getAcroForm();
                 if( form == null )
                 {
@@ -89,16 +85,13 @@ public class ExportFDF
                             fdfName = args[0].substring( 0, args[0].length() -4 ) + ".fdf";
                         }
                     }
-                    fdf = form.exportFDF();
-                    fdf.save( fdfName );
+                    try (FDFDocument fdf = form.exportFDF())
+                    {
+                        fdf.save( fdfName );
+                    }
                 }
             }
         }
-        finally
-        {
-            close( fdf );
-            close( pdf );
-        }
     }
 
     /**
@@ -113,34 +106,4 @@ public class ExportFDF
         System.err.println(message);
         System.exit(1);
     }
-
-    /**
-     * Close the document.
-     *
-     * @param doc The doc to close.
-     *
-     * @throws IOException If there is an error closing the document.
-     */
-    public void close( FDFDocument doc ) throws IOException
-    {
-        if( doc != null )
-        {
-            doc.close();
-        }
-    }
-
-    /**
-     * Close the document.
-     *
-     * @param doc The doc to close.
-     *
-     * @throws IOException If there is an error closing the document.
-     */
-    public void close( PDDocument doc ) throws IOException
-    {
-        if( doc != null )
-        {
-            doc.close();
-        }
-    }
 }

Modified: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExportXFDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExportXFDF.java?rev=1823795&r1=1823794&r2=1823795&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExportXFDF.java (original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExportXFDF.java Sat Feb 10 20:58:39 2018
@@ -59,18 +59,14 @@ public class ExportXFDF
 
     private void exportXFDF( String[] args ) throws IOException
     {
-        PDDocument pdf = null;
-        FDFDocument fdf = null;
-
-        try
+        if( args.length != 1 && args.length != 2 )
         {
-            if( args.length != 1 && args.length != 2 )
-            {
-                usage();
-            }
-            else
+            usage();
+        }
+        else
+        {
+            try (PDDocument pdf = PDDocument.load( new File(args[0]) ))
             {
-                pdf = PDDocument.load( new File(args[0]) );
                 PDAcroForm form = pdf.getDocumentCatalog().getAcroForm();
                 if( form == null )
                 {
@@ -90,16 +86,14 @@ public class ExportXFDF
                             fdfName = args[0].substring( 0, args[0].length() -4 ) + ".xfdf";
                         }
                     }
-                    fdf = form.exportFDF();
-                    fdf.saveXFDF( fdfName );
+                    
+                    try (FDFDocument fdf = form.exportFDF())
+                    {
+                        fdf.saveXFDF( fdfName );
+                    }
                 }
             }
         }
-        finally
-        {
-            close( fdf );
-            close( pdf );
-        }
     }
 
     /**
@@ -114,34 +108,4 @@ public class ExportXFDF
         System.err.println(message);
         System.exit(1);
     }
-
-    /**
-     * Close the document.
-     *
-     * @param doc The doc to close.
-     *
-     * @throws IOException If there is an error closing the document.
-     */
-    public void close( FDFDocument doc ) throws IOException
-    {
-        if( doc != null )
-        {
-            doc.close();
-        }
-    }
-
-    /**
-     * Close the document.
-     *
-     * @param doc The doc to close.
-     *
-     * @throws IOException If there is an error closing the document.
-     */
-    public void close( PDDocument doc ) throws IOException
-    {
-        if( doc != null )
-        {
-            doc.close();
-        }
-    }
 }

Modified: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImportFDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImportFDF.java?rev=1823795&r1=1823794&r2=1823795&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImportFDF.java (original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImportFDF.java Sat Feb 10 20:58:39 2018
@@ -83,31 +83,22 @@ public class ImportFDF
 
     private void importFDF( String[] args ) throws IOException
     {
-        PDDocument pdf = null;
-        FDFDocument fdf = null;
-
-        try
+        if( args.length != 3 )
         {
-            if( args.length != 3 )
-            {
-                usage();
-            }
-            else
-            {
-                ImportFDF importer = new ImportFDF();
+            usage();
+        }
+        else
+        {
+            ImportFDF importer = new ImportFDF();
 
-                pdf = PDDocument.load( new File(args[0]) );
-                fdf = FDFDocument.load( args[1] );
+            try (PDDocument pdf = PDDocument.load( new File(args[0]) );
+                    FDFDocument fdf = FDFDocument.load( args[1] ))
+            {
                 importer.importFDF( pdf, fdf );
 
                 pdf.save( args[2] );
             }
         }
-        finally
-        {
-            close( fdf );
-            close( pdf );
-        }
     }
 
     /**
@@ -118,34 +109,4 @@ public class ImportFDF
         System.err.println( "usage: org.apache.pdfbox.tools.ImportFDF <pdf-file> <fdf-file> <output-file>" );
         System.exit(1);
     }
-
-    /**
-     * Close the document.
-     *
-     * @param doc The doc to close.
-     *
-     * @throws IOException If there is an error closing the document.
-     */
-    public void close( FDFDocument doc ) throws IOException
-    {
-        if( doc != null )
-        {
-            doc.close();
-        }
-    }
-
-    /**
-     * Close the document.
-     *
-     * @param doc The doc to close.
-     *
-     * @throws IOException If there is an error closing the document.
-     */
-    public void close( PDDocument doc ) throws IOException
-    {
-        if( doc != null )
-        {
-            doc.close();
-        }
-    }
 }

Modified: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImportXFDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImportXFDF.java?rev=1823795&r1=1823794&r2=1823795&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImportXFDF.java (original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImportXFDF.java Sat Feb 10 20:58:39 2018
@@ -77,30 +77,20 @@ public class ImportXFDF
 
     private void importXFDF( String[] args ) throws IOException
     {
-        PDDocument pdf = null;
-        FDFDocument fdf = null;
-
-        try
+        if( args.length != 3 )
         {
-            if( args.length != 3 )
-            {
-                usage();
-            }
-            else
+            usage();
+        }
+        else
+        {
+            ImportFDF importer = new ImportFDF();
+            try (PDDocument pdf = PDDocument.load( new File(args[0]) );
+                    FDFDocument fdf = FDFDocument.loadXFDF( args[1] ))
             {
-                ImportFDF importer = new ImportFDF();
-                pdf = PDDocument.load( new File(args[0]) );
-                fdf = FDFDocument.loadXFDF( args[1] );
-
                 importer.importFDF( pdf, fdf );
                 pdf.save( args[2] );
             }
         }
-        finally
-        {
-            close( fdf );
-            close( pdf );
-        }
     }
 
     /**
@@ -111,34 +101,4 @@ public class ImportXFDF
         System.err.println( "usage: org.apache.pdfbox.tools.ImportXFDF <pdf-file> <fdf-file> <output-file>" );
         System.exit(1);
     }
-
-    /**
-     * Close the document.
-     *
-     * @param doc The doc to close.
-     *
-     * @throws IOException If there is an error closing the document.
-     */
-    public void close( FDFDocument doc ) throws IOException
-    {
-        if( doc != null )
-        {
-            doc.close();
-        }
-    }
-
-    /**
-     * Close the document.
-     *
-     * @param doc The doc to close.
-     *
-     * @throws IOException If there is an error closing the document.
-     */
-    public void close( PDDocument doc ) throws IOException
-    {
-        if( doc != null )
-        {
-            doc.close();
-        }
-    }
 }