You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2009/07/03 16:41:11 UTC

svn commit: r790931 - /incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSDictionary.java

Author: lehmi
Date: Fri Jul  3 14:41:11 2009
New Revision: 790931

URL: http://svn.apache.org/viewvc?rev=790931&view=rev
Log:
PDFBOX-455: make the parser more lenient in case of wrong basic types, reestablish checkstyle compliance

Thanks to Sean Bridges (sean dot bridges at gmail dot com) and Micheal Weller (gibbsnich at gmail dot com) for their hints/patches

Modified:
    incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSDictionary.java

Modified: incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSDictionary.java
URL: http://svn.apache.org/viewvc/incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSDictionary.java?rev=790931&r1=790930&r2=790931&view=diff
==============================================================================
--- incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSDictionary.java (original)
+++ incubator/pdfbox/trunk/src/main/java/org/apache/pdfbox/cos/COSDictionary.java Fri Jul  3 14:41:11 2009
@@ -583,10 +583,17 @@
     public String getNameAsString( COSName key )
     {
         String retval = null;
-        COSName name = (COSName)getDictionaryObject( key );
+        COSBase name = getDictionaryObject( key );
         if( name != null )
         {
-            retval = name.getName();
+            if ( name instanceof COSName) 
+            {
+                retval = ((COSName)name).getName();
+            }
+            else if ( name instanceof COSString) 
+            {
+                retval = ((COSString)name).getString();
+            }
         }
         return retval;
     }
@@ -648,10 +655,10 @@
     public String getString( COSName key )
     {
         String retval = null;
-        COSString name = (COSString)getDictionaryObject( key );
-        if( name != null )
+        COSBase value = getDictionaryObject( key );
+        if( value != null && value instanceof COSString)
         {
-            retval = name.getString();
+            retval = ((COSString)value).getString();
         }
         return retval;
     }
@@ -913,10 +920,10 @@
     public boolean getBoolean( COSName key, boolean defaultValue )
     {
         boolean retval = defaultValue;
-        COSBoolean bool = (COSBoolean)getDictionaryObject( key );
-        if( bool != null )
+        COSBase bool = getDictionaryObject( key );
+        if( bool != null && bool instanceof COSBoolean)
         {
-            retval = bool.getValue();
+            retval = ((COSBoolean)bool).getValue();
         }
         return retval;
     }
@@ -1018,10 +1025,10 @@
     public int getInt( String[] keyList, int defaultValue )
     {
         int retval = defaultValue;
-        COSNumber obj = (COSNumber)getDictionaryObject( keyList );
-        if( obj != null )
+        COSBase obj = getDictionaryObject( keyList );
+        if( obj != null && obj instanceof COSNumber)
         {
-            retval = obj.intValue();
+            retval = ((COSNumber)obj).intValue();
         }
         return retval;
     }
@@ -1091,10 +1098,10 @@
     public long getLong( String[] keyList, long defaultValue )
     {
         long retval = defaultValue;
-        COSNumber obj = (COSNumber)getDictionaryObject( keyList );
-        if( obj != null )
+        COSBase obj = getDictionaryObject( keyList );
+        if( obj != null && obj instanceof COSNumber)
         {
-            retval = obj.longValue();
+            retval = ((COSNumber)obj).longValue();
         }
         return retval;
     }
@@ -1177,10 +1184,10 @@
     public float getFloat( COSName key, float defaultValue )
     {
         float retval = defaultValue;
-        COSNumber obj = (COSNumber)getDictionaryObject( key );
-        if( obj != null )
+        COSBase obj = getDictionaryObject( key );
+        if( obj != null && obj instanceof COSNumber)
         {
-            retval = obj.floatValue();
+            retval = ((COSNumber)obj).floatValue();
         }
         return retval;
     }
@@ -1266,7 +1273,8 @@
              * pdf file, meaning that the first Size entry represents
              * all of the objects so we don't need to grab the second. 
              */
-            if(!key.getName().equals("Size") || !keys.contains(COSName.getPDFName("Size"))){
+            if(!key.getName().equals("Size") || !keys.contains(COSName.getPDFName("Size")))
+            {
                 setItem( key, value );
             }
         }
@@ -1322,19 +1330,19 @@
         return retval;
     }
     
-    /*
-	Don't just tell me it's a dictionary -- tell me its contents!
-    */
-	public String toString()
-    {
-        String RetVal = "COSDictionary{";
-	    for (int i = 0; i<size(); i++){
-		    COSName key = (COSName)keyList().get(i);
-		    RetVal = RetVal + "(" + key + ":" + getDictionaryObject(key).toString() + ") ";
-	    }
-	    RetVal = RetVal + "}";
-	    
-	    return RetVal;
+    /**
+     * {@inheritDoc}
+     */
+    public String toString()
+    {
+        String retVal = "COSDictionary{";
+        for (int i = 0; i<size(); i++)
+        {
+            COSName key = (COSName)keyList().get(i);
+            retVal = retVal + "(" + key + ":" + getDictionaryObject(key).toString() + ") ";
+        }
+        retVal = retVal + "}";
+        return retVal;
     }