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

svn commit: r1825685 - /pdfbox/branches/2.0/preflight/src/main/java/org/apache/pdfbox/preflight/utils/COSUtils.java

Author: tilman
Date: Thu Mar  1 20:05:39 2018
New Revision: 1825685

URL: http://svn.apache.org/viewvc?rev=1825685&view=rev
Log:
PDFBOX-4071: refactor to eliminate double code

Modified:
    pdfbox/branches/2.0/preflight/src/main/java/org/apache/pdfbox/preflight/utils/COSUtils.java

Modified: pdfbox/branches/2.0/preflight/src/main/java/org/apache/pdfbox/preflight/utils/COSUtils.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/preflight/src/main/java/org/apache/pdfbox/preflight/utils/COSUtils.java?rev=1825685&r1=1825684&r2=1825685&view=diff
==============================================================================
--- pdfbox/branches/2.0/preflight/src/main/java/org/apache/pdfbox/preflight/utils/COSUtils.java (original)
+++ pdfbox/branches/2.0/preflight/src/main/java/org/apache/pdfbox/preflight/utils/COSUtils.java Thu Mar  1 20:05:39 2018
@@ -56,20 +56,7 @@ public final class COSUtils
      */
     public static boolean isDictionary(COSBase elt, COSDocument doc)
     {
-        if (elt instanceof COSObject)
-        {
-            try
-            {
-                COSObjectKey key = new COSObjectKey((COSObject) elt);
-                COSObject obj = doc.getObjectFromPool(key);
-                return (obj != null && obj.getObject() instanceof COSDictionary);
-            }
-            catch (IOException e)
-            {
-                return false;
-            }
-        }
-        return (elt instanceof COSDictionary);
+        return isClass(elt, doc, COSDictionary.class);
     }
 
     /**
@@ -91,6 +78,7 @@ public final class COSUtils
             }
             catch (IOException e)
             {
+                LOGGER.debug("Couldn't get COSObject from object pool - returning false", e);
                 return false;
             }
         }
@@ -107,21 +95,7 @@ public final class COSUtils
      */
     public static boolean isStream(COSBase elt, COSDocument doc)
     {
-        if (elt instanceof COSObject)
-        {
-            try
-            {
-                COSObjectKey key = new COSObjectKey((COSObject) elt);
-                COSObject obj = doc.getObjectFromPool(key);
-                return (obj != null && obj.getObject() instanceof COSStream);
-            }
-            catch (IOException e)
-            {
-                return false;
-            }
-        }
-
-        return (elt instanceof COSStream);
+        return isClass(elt, doc, COSStream.class);
     }
 
     /**
@@ -133,21 +107,35 @@ public final class COSUtils
      */
     public static boolean isInteger(COSBase elt, COSDocument doc)
     {
+        return isClass(elt, doc, COSInteger.class);
+    }
+
+    /**
+     * return true if the elt is of class or a reference to a that class.
+     * 
+     * @param elt the object to check.
+     * @param doc the document.
+     * @param claz the class.
+     * @return true if the object is a of that class or a reference to it.
+     */
+    private static boolean isClass(COSBase elt, COSDocument doc, Class claz)
+    {
         if (elt instanceof COSObject)
         {
             try
             {
                 COSObjectKey key = new COSObjectKey((COSObject) elt);
                 COSObject obj = doc.getObjectFromPool(key);
-                return (obj != null && obj.getObject() instanceof COSInteger);
+                return (obj != null && claz.isInstance(obj.getObject()));
             }
             catch (IOException e)
             {
+                LOGGER.debug("Couldn't get COSObject from object pool - returning false", e);
                 return false;
             }
         }
 
-        return (elt instanceof COSInteger);
+        return claz.isInstance(elt);
     }
 
     /**
@@ -171,21 +159,7 @@ public final class COSUtils
      */
     public static boolean isFloat(COSBase elt, COSDocument doc)
     {
-        if (elt instanceof COSObject)
-        {
-            try
-            {
-                COSObjectKey key = new COSObjectKey((COSObject) elt);
-                COSObject obj = doc.getObjectFromPool(key);
-                return (obj != null && obj.getObject() instanceof COSFloat);
-            }
-            catch (IOException e)
-            {
-                return false;
-            }
-        }
-
-        return (elt instanceof COSFloat);
+        return isClass(elt, doc, COSFloat.class);
     }
 
     /**
@@ -197,21 +171,7 @@ public final class COSUtils
      */
     public static boolean isArray(COSBase elt, COSDocument doc)
     {
-        if (elt instanceof COSObject)
-        {
-            try
-            {
-                COSObjectKey key = new COSObjectKey((COSObject) elt);
-                COSObject obj = doc.getObjectFromPool(key);
-                return (obj != null && obj.getObject() instanceof COSArray);
-            }
-            catch (IOException e)
-            {
-                return false;
-            }
-        }
-
-        return (elt instanceof COSArray);
+        return isClass(elt, doc, COSArray.class);
     }
 
     /**
@@ -226,23 +186,7 @@ public final class COSUtils
     {
         if (cbase instanceof COSObject)
         {
-            try
-            {
-                COSObjectKey key = new COSObjectKey((COSObject) cbase);
-                COSObject obj = cDoc.getObjectFromPool(key);
-                if (obj != null && obj.getObject() instanceof COSArray)
-                {
-                    return (COSArray) obj.getObject();
-                }
-                else
-                {
-                    return null;
-                }
-            }
-            catch (IOException e)
-            {
-                return null;
-            }
+            return (COSArray) getCOSObjectAsClass((COSObject) cbase, cDoc, COSArray.class);
         }
         else if (cbase instanceof COSArray)
         {
@@ -286,6 +230,7 @@ public final class COSUtils
             }
             catch (IOException e)
             {
+                LOGGER.debug("Couldn't get COSObject from object pool - returning null", e);
                 return null;
             }
         }
@@ -315,23 +260,7 @@ public final class COSUtils
     {
         if (cbase instanceof COSObject)
         {
-            try
-            {
-                COSObjectKey key = new COSObjectKey((COSObject) cbase);
-                COSObject obj = cDoc.getObjectFromPool(key);
-                if (obj != null && obj.getObject() instanceof COSDictionary)
-                {
-                    return (COSDictionary) obj.getObject();
-                }
-                else
-                {
-                    return null;
-                }
-            }
-            catch (IOException e)
-            {
-                return null;
-            }
+            return (COSDictionary) getCOSObjectAsClass((COSObject) cbase, cDoc, COSDictionary.class);
         }
         else if (cbase instanceof COSDictionary)
         {
@@ -355,23 +284,7 @@ public final class COSUtils
     {
         if (cbase instanceof COSObject)
         {
-            try
-            {
-                COSObjectKey key = new COSObjectKey((COSObject) cbase);
-                COSObject obj = cDoc.getObjectFromPool(key);
-                if (obj != null && obj.getObject() instanceof COSStream)
-                {
-                    return (COSStream) obj.getObject();
-                }
-                else
-                {
-                    return null;
-                }
-            }
-            catch (IOException e)
-            {
-                return null;
-            }
+            return (COSStream) getCOSObjectAsClass((COSObject) cbase, cDoc, COSStream.class);
         }
         else if (cbase instanceof COSStream)
         {
@@ -414,6 +327,7 @@ public final class COSUtils
             }
             catch (IOException e)
             {
+                LOGGER.debug("Couldn't get COSObject from object pool - returning null", e);
                 return null;
             }
         }
@@ -458,6 +372,7 @@ public final class COSUtils
             }
             catch (IOException e)
             {
+                LOGGER.debug("Couldn't get COSObject from object pool - returning null", e);
                 return null;
             }
         }
@@ -488,7 +403,7 @@ public final class COSUtils
         }
         catch (IOException e)
         {
-            LOGGER.warn("Error occured during the close of a COSDocument : " + e.getMessage());
+            LOGGER.warn("Error occured during the close of a COSDocument : " + e.getMessage(), e);
         }
     }
 
@@ -505,4 +420,36 @@ public final class COSUtils
             closeDocumentQuietly(document.getDocument());
         }
     }
+    
+    /**
+     * Return the COSObject object as class if the COSObject object is a reference to an object of
+     * that class. If not, then this method returns null;
+     *
+     * @param cosObject the object to get.
+     * @param cDoc the document.
+     * @param claz the class.
+     * @return the object as class if the object is a reference to that class. Returns null
+     * otherwise.
+     */
+    private static COSBase getCOSObjectAsClass(COSObject cosObject, COSDocument cDoc, Class claz)
+    {
+        try
+        {
+            COSObjectKey key = new COSObjectKey(cosObject);
+            COSObject obj = cDoc.getObjectFromPool(key);
+            if (obj != null && claz.isInstance(obj.getObject()))
+            {
+                return obj.getObject();
+            }
+            else
+            {
+                return null;
+            }
+        }
+        catch (IOException e)
+        {
+            LOGGER.debug("Couldn't get COSObject from object pool - returning null", e);
+            return null;
+        }
+    }
 }