pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1792308 - in /pdfbox/trunk/pdfbox/src: main/java/org/apache/pdfbox/multipdf/ main/java/org/apache/pdfbox/pdmodel/ main/java/org/apache/pdfbox/pdmodel/common/ main/java/org/apache/pdfbox/pdmodel/graphics/state/ test/java/org/apache/pdfbox/m...
Date Sat, 22 Apr 2017 17:36:45 GMT
Author: tilman
Date: Sat Apr 22 17:36:45 2017
New Revision: 1792308

URL: http://svn.apache.org/viewvc?rev=1792308&view=rev
Log:
PDFBOX-2852: use JDK7 syntax

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/LayerUtility.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/Overlay.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFCloneUtility.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PageLayout.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PageMode.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDPageLabels.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/RenderingIntent.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFCloneUtilityTest.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/LayerUtility.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/LayerUtility.java?rev=1792308&r1=1792307&r2=1792308&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/LayerUtility.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/LayerUtility.java Sat Apr
22 17:36:45 2017
@@ -83,14 +83,16 @@ public class LayerUtility
     public void wrapInSaveRestore(PDPage page) throws IOException
     {
         COSStream saveGraphicsStateStream = getDocument().getDocument().createCOSStream();
-        OutputStream saveStream = saveGraphicsStateStream.createOutputStream();
-        saveStream.write("q\n".getBytes("ISO-8859-1"));
-        saveStream.close();
+        try (OutputStream saveStream = saveGraphicsStateStream.createOutputStream())
+        {
+            saveStream.write("q\n".getBytes("ISO-8859-1"));
+        }
 
         COSStream restoreGraphicsStateStream = getDocument().getDocument().createCOSStream();
-        OutputStream restoreStream = restoreGraphicsStateStream.createOutputStream();
-        restoreStream.write("Q\n".getBytes("ISO-8859-1"));
-        restoreStream.close();
+        try (OutputStream restoreStream = restoreGraphicsStateStream.createOutputStream())
+        {
+            restoreStream.write("Q\n".getBytes("ISO-8859-1"));
+        }
 
         //Wrap the existing page's content in a save/restore pair (q/Q) to have a controlled
         //environment to add additional content.
@@ -239,15 +241,16 @@ public class LayerUtility
         PDOptionalContentGroup layer = new PDOptionalContentGroup(layerName);
         ocprops.addGroup(layer);
 
-        PDPageContentStream contentStream = new PDPageContentStream(
-                targetDoc, targetPage, AppendMode.APPEND, !DEBUG);
-        contentStream.beginMarkedContent(COSName.OC, layer);
-        contentStream.saveGraphicsState();
-        contentStream.transform(new Matrix(transform));
-        contentStream.drawForm(form);
-        contentStream.restoreGraphicsState();
-        contentStream.endMarkedContent();
-        contentStream.close();
+        try (PDPageContentStream contentStream = new PDPageContentStream(
+                targetDoc, targetPage, AppendMode.APPEND, !DEBUG))
+        {
+            contentStream.beginMarkedContent(COSName.OC, layer);
+            contentStream.saveGraphicsState();
+            contentStream.transform(new Matrix(transform));
+            contentStream.drawForm(form);
+            contentStream.restoreGraphicsState();
+            contentStream.endMarkedContent();
+        }
 
         return layer;
     }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/Overlay.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/Overlay.java?rev=1792308&r1=1792307&r2=1792308&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/Overlay.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/Overlay.java Sat Apr 22 17:36:45
2017
@@ -279,19 +279,20 @@ public class Overlay
         List<COSStream> contentStreams = createContentStreamList(contents);
         // concatenate streams
         COSStream concatStream = new COSStream();
-        OutputStream out = concatStream.createOutputStream(COSName.FLATE_DECODE);
-        for (COSStream contentStream : contentStreams)
+        try (OutputStream out = concatStream.createOutputStream(COSName.FLATE_DECODE))
         {
-            InputStream in = contentStream.createInputStream();
-            byte[] buf = new byte[2048];
-            int n;
-            while ((n = in.read(buf)) > 0)
+            for (COSStream contentStream : contentStreams)
             {
-                out.write(buf, 0, n);
+                InputStream in = contentStream.createInputStream();
+                byte[] buf = new byte[2048];
+                int n;
+                while ((n = in.read(buf)) > 0)
+                {
+                    out.write(buf, 0, n);
+                }
+                out.flush();
             }
-            out.flush();
         }
-        out.close();
         return concatStream;
     }
 
@@ -464,9 +465,10 @@ public class Overlay
     private COSStream createStream(String content) throws IOException
     {
         COSStream stream = new COSStream();
-        OutputStream out = stream.createOutputStream(COSName.FLATE_DECODE);
-        out.write(content.getBytes("ISO-8859-1"));
-        out.close();
+        try (OutputStream out = stream.createOutputStream(COSName.FLATE_DECODE))
+        {
+            out.write(content.getBytes("ISO-8859-1"));
+        }
         return stream;
     }
 

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFCloneUtility.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFCloneUtility.java?rev=1792308&r1=1792307&r2=1792308&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFCloneUtility.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFCloneUtility.java Sat
Apr 22 17:36:45 2017
@@ -114,11 +114,11 @@ public class PDFCloneUtility
           {
               COSStream originalStream = (COSStream)base;
               COSStream stream = destination.getDocument().createCOSStream();
-              OutputStream output = stream.createRawOutputStream();
-              InputStream input = originalStream.createRawInputStream();
-              IOUtils.copy(input, output );
-              input.close();
-              output.close();
+              try (OutputStream output = stream.createRawOutputStream();
+                   InputStream input = originalStream.createRawInputStream())
+              {
+                  IOUtils.copy(input, output);
+              }
               clonedVersion.put( base, stream );
               for( Map.Entry<COSName, COSBase> entry :  originalStream.entrySet() )
               {
@@ -198,15 +198,14 @@ public class PDFCloneUtility
             // does that make sense???
               COSStream originalStream = (COSStream)base;
               COSStream stream = destination.getDocument().createCOSStream();
-              OutputStream output = stream.createOutputStream(originalStream.getFilters());
-              IOUtils.copy( originalStream.createInputStream(), output );
-              output.close();
+              try (OutputStream output = stream.createOutputStream(originalStream.getFilters()))
+              {
+                  IOUtils.copy(originalStream.createInputStream(), output);
+              }
               clonedVersion.put( base, stream );
               for( Map.Entry<COSName, COSBase> entry : originalStream.entrySet() )
               {
-                  stream.setItem(
-                          entry.getKey(),
-                          cloneForNewDocument(entry.getValue()));
+                  stream.setItem(entry.getKey(), cloneForNewDocument(entry.getValue()));
               }
               retval = stream;
           }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PageLayout.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PageLayout.java?rev=1792308&r1=1792307&r2=1792308&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PageLayout.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PageLayout.java Sat Apr 22
17:36:45 2017
@@ -44,31 +44,23 @@ public enum PageLayout
 
     public static PageLayout fromString(String value)
     {
-        if (value.equals("SinglePage"))
+        switch (value)
         {
-            return SINGLE_PAGE;
+            case "SinglePage":
+                return SINGLE_PAGE;
+            case "OneColumn":
+                return ONE_COLUMN;
+            case "TwoColumnLeft":
+                return TWO_COLUMN_LEFT;
+            case "TwoColumnRight":
+                return TWO_COLUMN_RIGHT;
+            case "TwoPageLeft":
+                return TWO_PAGE_LEFT;
+            case "TwoPageRight":
+                return TWO_PAGE_RIGHT;
+            default:
+                throw new IllegalArgumentException(value);
         }
-        else if (value.equals("OneColumn"))
-        {
-            return ONE_COLUMN;
-        }
-        else if (value.equals("TwoColumnLeft"))
-        {
-            return TWO_COLUMN_LEFT;
-        }
-        else if (value.equals("TwoColumnRight"))
-        {
-            return TWO_COLUMN_RIGHT;
-        }
-        else if (value.equals("TwoPageLeft"))
-        {
-            return TWO_PAGE_LEFT;
-        }
-        else if (value.equals("TwoPageRight"))
-        {
-            return TWO_PAGE_RIGHT;
-        }
-        throw new IllegalArgumentException(value);
     }
 
     private final String value;

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PageMode.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PageMode.java?rev=1792308&r1=1792307&r2=1792308&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PageMode.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PageMode.java Sat Apr 22 17:36:45
2017
@@ -44,31 +44,23 @@ public enum PageMode
 
     public static PageMode fromString(String value)
     {
-        if (value.equals("UseNone"))
+        switch (value)
         {
-            return USE_NONE;
+            case "UseNone":
+                return USE_NONE;
+            case "UseOutlines":
+                return USE_OUTLINES;
+            case "UseThumbs":
+                return USE_THUMBS;
+            case "FullScreen":
+                return FULL_SCREEN;
+            case "UseOC":
+                return USE_OPTIONAL_CONTENT;
+            case "UseAttachments":
+                return USE_ATTACHMENTS;
+            default:
+                throw new IllegalArgumentException(value);
         }
-        else if (value.equals("UseOutlines"))
-        {
-            return USE_OUTLINES;
-        }
-        else if (value.equals("UseThumbs"))
-        {
-            return USE_THUMBS;
-        }
-        else if (value.equals("FullScreen"))
-        {
-            return FULL_SCREEN;
-        }
-        else if (value.equals("UseOC"))
-        {
-            return USE_OPTIONAL_CONTENT;
-        }
-        else if (value.equals("UseAttachments"))
-        {
-            return USE_ATTACHMENTS;
-        }
-        throw new IllegalArgumentException(value);
     }
 
     private final String value;

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDPageLabels.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDPageLabels.java?rev=1792308&r1=1792307&r2=1792308&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDPageLabels.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDPageLabels.java Sat
Apr 22 17:36:45 2017
@@ -336,31 +336,26 @@ public class PDPageLabels implements COS
 
         private String getNumber(int pageIndex, String style)
         {
-            if (PDPageLabelRange.STYLE_DECIMAL.equals(style))
+            if (style != null)
             {
-                return Integer.toString(pageIndex);
-            }
-            else if (PDPageLabelRange.STYLE_LETTERS_LOWER.equals(style))
-            {
-                return makeLetterLabel(pageIndex);
-            }
-            else if (PDPageLabelRange.STYLE_LETTERS_UPPER.equals(style))
-            {
-                return makeLetterLabel(pageIndex).toUpperCase();
-            }
-            else if (PDPageLabelRange.STYLE_ROMAN_LOWER.equals(style))
-            {
-                return makeRomanLabel(pageIndex);
-            }
-            else if (PDPageLabelRange.STYLE_ROMAN_UPPER.equals(style))
-            {
-                return makeRomanLabel(pageIndex).toUpperCase();
-            }
-            else
-            {
-                // Fall back to decimals.
-                return Integer.toString(pageIndex);
+                switch (style)
+                {
+                    case PDPageLabelRange.STYLE_DECIMAL:
+                        return Integer.toString(pageIndex);
+                    case PDPageLabelRange.STYLE_LETTERS_LOWER:
+                        return makeLetterLabel(pageIndex);
+                    case PDPageLabelRange.STYLE_LETTERS_UPPER:
+                        return makeLetterLabel(pageIndex).toUpperCase();
+                    case PDPageLabelRange.STYLE_ROMAN_LOWER:
+                        return makeRomanLabel(pageIndex);
+                    case PDPageLabelRange.STYLE_ROMAN_UPPER:
+                        return makeRomanLabel(pageIndex).toUpperCase();
+                    default:
+                        break;
+                }
             }
+            // Fall back to decimals.
+            return Integer.toString(pageIndex);
         }
 
         /**

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/RenderingIntent.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/RenderingIntent.java?rev=1792308&r1=1792307&r2=1792308&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/RenderingIntent.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/RenderingIntent.java
Sat Apr 22 17:36:45 2017
@@ -46,21 +46,16 @@ public enum RenderingIntent
 
     public static RenderingIntent fromString(String value)
     {
-        if (value.equals("AbsoluteColorimetric"))
+        switch (value)
         {
-            return ABSOLUTE_COLORIMETRIC;
-        }
-        else if (value.equals("RelativeColorimetric"))
-        {
-          return RELATIVE_COLORIMETRIC;
-        }
-        else if (value.equals("Saturation"))
-        {
-          return SATURATION;
-        }
-        else if (value.equals("Perceptual"))
-        {
-          return PERCEPTUAL;
+            case "AbsoluteColorimetric":
+                return ABSOLUTE_COLORIMETRIC;
+            case "RelativeColorimetric":
+                return RELATIVE_COLORIMETRIC;
+            case "Saturation":
+                return SATURATION;
+            case "Perceptual":
+                return PERCEPTUAL;
         }
         // "If a conforming reader does not recognize the specified name, 
         // it shall use the RelativeColorimetric intent by default."

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFCloneUtilityTest.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFCloneUtilityTest.java?rev=1792308&r1=1792307&r2=1792308&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFCloneUtilityTest.java
(original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFCloneUtilityTest.java
Sat Apr 22 17:36:45 2017
@@ -39,15 +39,16 @@ public class PDFCloneUtilityTest extends
      */
     public void testClonePDFWithCosArrayStream() throws IOException
     {
-        PDDocument srcDoc = new PDDocument();
-        PDDocument dstDoc = new PDDocument();
-        PDPage pdPage = new PDPage();
-        srcDoc.addPage(pdPage);
-        new PDPageContentStream(srcDoc, pdPage, AppendMode.APPEND, true).close();
-        new PDPageContentStream(srcDoc, pdPage, AppendMode.APPEND, true).close();
-        new PDFCloneUtility(dstDoc).cloneForNewDocument(pdPage.getCOSObject());
-        srcDoc.close();
-        dstDoc.close();
+        try (PDDocument srcDoc = new PDDocument();
+             PDDocument dstDoc = new PDDocument())
+        {
+
+            PDPage pdPage = new PDPage();
+            srcDoc.addPage(pdPage);
+            new PDPageContentStream(srcDoc, pdPage, AppendMode.APPEND, true).close();
+            new PDPageContentStream(srcDoc, pdPage, AppendMode.APPEND, true).close();
+            new PDFCloneUtility(dstDoc).cloneForNewDocument(pdPage.getCOSObject());
+        }
     }
 
     /**
@@ -66,21 +67,24 @@ public class PDFCloneUtilityTest extends
         PDDocument srcDoc = new PDDocument();
         PDPage pdPage = new PDPage();
         srcDoc.addPage(pdPage);
-        PDPageContentStream pdPageContentStream1 = new PDPageContentStream(srcDoc, pdPage,
AppendMode.APPEND, false);
-        pdPageContentStream1.setNonStrokingColor(Color.black);
-        pdPageContentStream1.addRect(100, 600, 300, 100);
-        pdPageContentStream1.fill();
-        pdPageContentStream1.close();
-        PDPageContentStream pdPageContentStream2 = new PDPageContentStream(srcDoc, pdPage,
AppendMode.APPEND, false);
-        pdPageContentStream2.setNonStrokingColor(Color.red);
-        pdPageContentStream2.addRect(100, 500, 300, 100);
-        pdPageContentStream2.fill();
-        pdPageContentStream2.close();
-        PDPageContentStream pdPageContentStream3 = new PDPageContentStream(srcDoc, pdPage,
AppendMode.APPEND, false);
-        pdPageContentStream3.setNonStrokingColor(Color.yellow);
-        pdPageContentStream3.addRect(100, 400, 300, 100);
-        pdPageContentStream3.fill();
-        pdPageContentStream3.close();
+        try (PDPageContentStream pdPageContentStream1 = new PDPageContentStream(srcDoc, pdPage,
AppendMode.APPEND, false))
+        {
+            pdPageContentStream1.setNonStrokingColor(Color.black);
+            pdPageContentStream1.addRect(100, 600, 300, 100);
+            pdPageContentStream1.fill();
+        }
+        try (PDPageContentStream pdPageContentStream2 = new PDPageContentStream(srcDoc, pdPage,
AppendMode.APPEND, false))
+        {
+            pdPageContentStream2.setNonStrokingColor(Color.red);
+            pdPageContentStream2.addRect(100, 500, 300, 100);
+            pdPageContentStream2.fill();
+        }
+        try (PDPageContentStream pdPageContentStream3 = new PDPageContentStream(srcDoc, pdPage,
AppendMode.APPEND, false))
+        {
+            pdPageContentStream3.setNonStrokingColor(Color.yellow);
+            pdPageContentStream3.addRect(100, 400, 300, 100);
+            pdPageContentStream3.fill();
+        }
 
         srcDoc.save(TESTDIR + CLONESRC);
         PDFMergerUtility merger = new PDFMergerUtility();



Mime
View raw message