Author: lehmi
Date: Fri Aug 11 17:03:44 2017
New Revision: 1804825

URL: http://svn.apache.org/viewvc?rev=1804825&view=rev
Log:
PDFBOX-3888: DRY, moved duplicate code to COSParser, made some methods private

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/FDFParser.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java

Modified: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java?rev=1804825&r1=1804824&r2=1804825&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java 
(original)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java 
Fri Aug 11 17:03:44 2017
@@ -189,13 +189,60 @@ public class COSParser extends BaseParse
     }
 
     /**
+     * Read the trailer information and provide a COSDictionary containing the 
trailer information.
+     * 
+     * @return a COSDictionary containing the trailer information
+     * @throws IOException if something went wrong
+     */
+    protected COSDictionary retrieveTrailer() throws IOException
+    {
+        COSDictionary trailer = null;
+        boolean rebuildTrailer = false;
+        try
+        {
+            // parse startxref
+            // TODO FDF files don't have a startxref value, so that 
rebuildTrailer is triggered
+            long startXRefOffset = getStartxrefOffset();
+            if (startXRefOffset > -1)
+            {
+                trailer = parseXref(startXRefOffset);
+            }
+            else
+            {
+                rebuildTrailer = isLenient();
+            }
+        }
+        catch (IOException exception)
+        {
+            if (isLenient())
+            {
+                rebuildTrailer = true;
+            }
+            else
+            {
+                throw exception;
+            }
+        }
+        // check if the trailer contains a Root object
+        if (trailer != null && trailer.getItem(COSName.ROOT) == null)
+        {
+            rebuildTrailer = isLenient();
+        }
+        if (rebuildTrailer)
+        {
+            trailer = rebuildTrailer();
+        }
+        return trailer;
+    }
+
+    /**
      * Parses cross reference tables.
      * 
      * @param startXRefOffset start offset of the first table
      * @return the trailer dictionary
      * @throws IOException if something went wrong
      */
-    protected COSDictionary parseXref(long startXRefOffset) throws IOException
+    private COSDictionary parseXref(long startXRefOffset) throws IOException
     {
         source.seek(startXRefOffset);
         long startXrefOffset = Math.max(0, parseStartXref());
@@ -348,7 +395,7 @@ public class COSParser extends BaseParse
      * @return the offset of StartXref
      * @throws IOException If something went wrong.
      */
-    protected final long getStartxrefOffset() throws IOException
+    private final long getStartxrefOffset() throws IOException
     {
         byte[] buf;
         long skipBytes;
@@ -1772,7 +1819,7 @@ public class COSParser extends BaseParse
      * 
      * @throws IOException if something went wrong
      */
-    protected final COSDictionary rebuildTrailer() throws IOException
+    private final COSDictionary rebuildTrailer() throws IOException
     {
         COSDictionary trailer = null;
         Map<COSObjectKey, Long> bfCOSObjectKeyOffsets = 
getBFCOSObjectOffsets();

Modified: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/FDFParser.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/FDFParser.java?rev=1804825&r1=1804824&r2=1804825&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/FDFParser.java 
(original)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/FDFParser.java 
Fri Aug 11 17:03:44 2017
@@ -113,36 +113,7 @@ public class FDFParser extends COSParser
      */
     private void initialParse() throws IOException
     {
-        COSDictionary trailer = null;
-        // parse startxref
-        long startXRefOffset = getStartxrefOffset();
-        boolean rebuildTrailer = false;
-        if (startXRefOffset > 0)
-        {
-            try
-            {
-                trailer = parseXref(startXRefOffset);
-            }
-            catch (IOException exception)
-            {
-                if (isLenient())
-                {
-                    rebuildTrailer = true;
-                }
-                else
-                {
-                    throw exception;
-                }
-            }
-        }
-        else if (isLenient())
-        {
-            rebuildTrailer = true;
-        }
-        if (rebuildTrailer)
-        {
-            trailer = rebuildTrailer();
-        }
+        COSDictionary trailer = retrieveTrailer();
     
         COSBase rootObject = parseTrailerValuesDynamically(trailer);
     

Modified: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java?rev=1804825&r1=1804824&r2=1804825&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java 
(original)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java 
Fri Aug 11 17:03:44 2017
@@ -186,41 +186,7 @@ public class PDFParser extends COSParser
      */
     protected void initialParse() throws InvalidPasswordException, IOException
     {
-        COSDictionary trailer = null;
-        // parse startxref
-        long startXRefOffset = getStartxrefOffset();
-        boolean rebuildTrailer = false;
-        if (startXRefOffset > -1)
-        {
-            try
-            {
-                trailer = parseXref(startXRefOffset);
-            }
-            catch (IOException exception)
-            {
-                if (isLenient())
-                {
-                    rebuildTrailer = true;
-                }
-                else
-                {
-                    throw exception;
-                }
-            }
-        }
-        else if (isLenient())
-        {
-            rebuildTrailer = true;
-        }
-        // check if the trailer contains a Root object
-        if (isLenient() && trailer != null && trailer.getItem(COSName.ROOT) == 
null)
-        {
-            rebuildTrailer = true;
-        }
-        if (rebuildTrailer)
-        {
-            trailer = rebuildTrailer();
-        }
+        COSDictionary trailer = retrieveTrailer();
         // prepare decryption if necessary
         prepareDecryption();
     


Reply via email to