You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ms...@apache.org on 2014/12/21 10:10:25 UTC

svn commit: r1647113 - in /pdfbox/trunk: examples/src/main/java/org/apache/pdfbox/examples/fdf/SetField.java pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java

Author: msahyoun
Date: Sun Dec 21 09:10:25 2014
New Revision: 1647113

URL: http://svn.apache.org/r1647113
Log:
PDFBOX-2516 don't cache field value locally

Modified:
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/SetField.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java

Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/SetField.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/SetField.java?rev=1647113&r1=1647112&r2=1647113&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/SetField.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/SetField.java Sun Dec 21 09:10:25 2014
@@ -57,7 +57,7 @@ public class SetField
         {
             if (field instanceof PDCheckbox)
             {
-                ((PDCheckbox) field).setValue(COSName.getPDFName(value));
+                ((PDCheckbox) field).setValue(value);
             }
             else if (field instanceof PDChoice)
             {

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java?rev=1647113&r1=1647112&r2=1647113&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java Sun Dec 21 09:10:25 2014
@@ -31,8 +31,6 @@ import org.apache.pdfbox.cos.COSName;
 public final class PDCheckbox extends PDButton
 {
 
-    private COSName value;
-
     /**
      * Constructor.
      * 
@@ -43,40 +41,34 @@ public final class PDCheckbox extends PD
     public PDCheckbox( PDAcroForm theAcroForm, COSDictionary field, PDFieldTreeNode parentNode)
     {
         super( theAcroForm, field, parentNode);
-        COSDictionary ap = (COSDictionary) field.getDictionaryObject(COSName.AP);
-        if( ap != null )
-        {
-            COSBase n = ap.getDictionaryObject(COSName.N);
-
-            if( n instanceof COSDictionary )
-            {
-                for( COSName name : ((COSDictionary)n).keySet() )
-                {
-                    if( !name.equals( COSName.OFF ))
-                    {
-                        value = name;
-                    }
-                }
-
-            }
-        }
-        else
-        {
-            value = (COSName)getDictionary().getDictionaryObject( COSName.V);
-        }
     }
 
     /**
      * This will tell if this radio button is currently checked or not.
      *
      * @return true If the radio button is checked.
+     * @throws IOException 
      */
-    public boolean isChecked()
+    public boolean isChecked() throws IOException
     {
         boolean retval = false;
         String onValue = getOnValue();
+        String fieldValue = null;
+        try
+        {
+            fieldValue = getValue();
+        }
+        catch (IOException e)
+        {
+            // getting there means that the field value
+            // doesn't have a supported type.
+            // Ignoring as that will also mean that the field is not checked.
+            // Setting the value explicitly as Code Analysis (Sonar) doesn't like
+            // empty catch blocks.
+            fieldValue = null;
+        }
         COSName radioValue = (COSName)getDictionary().getDictionaryObject( COSName.AS );
-        if( radioValue != null && value != null && radioValue.getName().equals( onValue ) )
+        if( radioValue != null && fieldValue != null && radioValue.getName().equals( onValue ) )
         {
             retval = true;
         }
@@ -89,7 +81,9 @@ public final class PDCheckbox extends PD
      */
     public void check()
     {
-        getDictionary().setItem(COSName.AS, value);
+        String onValue = getOnValue();
+        setValue(onValue);
+        getDictionary().setItem(COSName.AS, COSName.getPDFName(onValue));
     }
 
     /**
@@ -177,9 +171,18 @@ public final class PDCheckbox extends PD
     
     
     @Override
-    public COSName getValue()
+    public String getValue() throws IOException
     {
-        return getDictionary().getCOSName( COSName.V );
+        COSBase attribute = getInheritableAttribute(COSName.V);
+
+        if (attribute instanceof COSName)
+        {
+            return ((COSName) attribute).getName();
+        }
+        else
+        {
+            throw new IOException("Expected a COSName entry but got " + attribute.getClass().getName());
+        }
     }
 
     /**
@@ -191,9 +194,9 @@ public final class PDCheckbox extends PD
      *
      * The default value is Off.
      * 
-     * @param value the COSName object to set the field value.
+     * @param value the new field value value.
      */
-    public void setValue(COSName value)
+    public void setValue(String value)
     {
         if (value == null)
         {
@@ -202,8 +205,9 @@ public final class PDCheckbox extends PD
         }
         else
         {
-            getDictionary().setItem(COSName.V, value);
-            getDictionary().setItem( COSName.AS, value);
+            COSName nameValue = COSName.getPDFName(value);
+            getDictionary().setItem(COSName.V, nameValue);
+            getDictionary().setItem( COSName.AS, nameValue);
         }
     }
 }