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:28 UTC

svn commit: r1825684 - /pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/utils/COSUtils.java

Author: tilman
Date: Thu Mar  1 20:05:28 2018
New Revision: 1825684

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

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

Modified: pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/utils/COSUtils.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/utils/COSUtils.java?rev=1825684&r1=1825683&r2=1825684&view=diff
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/utils/COSUtils.java (original)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/pdfbox/preflight/utils/COSUtils.java Thu Mar  1 20:05:28 2018
@@ -56,21 +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)
-            {
-                LOGGER.debug("Couldn't get COSObject from object pool - returning false", e);
-                return false;
-            }
-        }
-        return (elt instanceof COSDictionary);
+        return isClass(elt, doc, COSDictionary.class);
     }
 
     /**
@@ -109,22 +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)
-            {
-                LOGGER.debug("Couldn't get COSObject from object pool - returning false", e);
-                return false;
-            }
-        }
-
-        return (elt instanceof COSStream);
+        return isClass(elt, doc, COSStream.class);
     }
 
     /**
@@ -136,13 +107,26 @@ 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)
             {
@@ -151,7 +135,7 @@ public final class COSUtils
             }
         }
 
-        return (elt instanceof COSInteger);
+        return claz.isInstance(elt);
     }
 
     /**
@@ -175,22 +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)
-            {
-                LOGGER.debug("Couldn't get COSObject from object pool - returning false", e);
-                return false;
-            }
-        }
-
-        return (elt instanceof COSFloat);
+        return isClass(elt, doc, COSFloat.class);
     }
 
     /**
@@ -202,22 +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)
-            {
-                LOGGER.debug("Couldn't get COSObject from object pool - returning false", e);
-                return false;
-            }
-        }
-
-        return (elt instanceof COSArray);
+        return isClass(elt, doc, COSArray.class);
     }
 
     /**
@@ -232,24 +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)
-            {
-                LOGGER.debug("Couldn't get COSObject from object pool - returning null", e);
-                return null;
-            }
+            return (COSArray) getCOSObjectAsClass((COSObject) cbase, cDoc, COSArray.class);
         }
         else if (cbase instanceof COSArray)
         {
@@ -323,24 +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)
-            {
-                LOGGER.debug("Couldn't get COSObject from object pool - returning null", e);
-                return null;
-            }
+            return (COSDictionary) getCOSObjectAsClass((COSObject) cbase, cDoc, COSDictionary.class);
         }
         else if (cbase instanceof COSDictionary)
         {
@@ -364,24 +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)
-            {
-                LOGGER.debug("Couldn't get COSObject from object pool - returning null", e);
-                return null;
-            }
+            return (COSStream) getCOSObjectAsClass((COSObject) cbase, cDoc, COSStream.class);
         }
         else if (cbase instanceof COSStream)
         {
@@ -517,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;
+        }
+    }
 }