pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jahew...@apache.org
Subject svn commit: r1679669 - in /pdfbox/trunk/pdfbox/src: main/java/org/apache/pdfbox/pdmodel/interactive/form/ test/java/org/apache/pdfbox/pdmodel/interactive/form/
Date Sat, 16 May 2015 00:31:39 GMT
Author: jahewson
Date: Sat May 16 00:31:39 2015
New Revision: 1679669

URL: http://svn.apache.org/r1679669
Log:
PDFBOX-2459: Introduce type safety for getDefaultValue() and setDefaultValue()

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoice.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDField.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDNonTerminalField.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDPushButton.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDSignatureField.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDTextField.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestCheckBox.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestFields.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java?rev=1679669&r1=1679668&r2=1679669&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java
Sat May 16 00:31:39 2015
@@ -113,39 +113,6 @@ public abstract class PDButton extends P
     public void setRadioButton(boolean radiobutton)
     {
         dictionary.setFlag(COSName.FF, FLAG_RADIO, radiobutton);
-    }   
-    
-    
-    @Override
-    public String getDefaultValue() throws IOException
-    {
-        COSBase attribute = getInheritableAttribute(COSName.DV);
-        
-        if (attribute == null)
-        {
-            return "";
-        }
-        else if (attribute instanceof COSName)
-        {
-            return ((COSName) attribute).getName();
-        }
-        else
-        {
-            throw new IOException("Expected a COSName entry but got " + attribute.getClass().getName());
-        }
-    }
-    
-    @Override
-    public void setDefaultValue(String defaultValue)
-    {
-        if (defaultValue == null)
-        {
-            dictionary.removeItem(COSName.DV);
-        }
-        else
-        {
-            dictionary.setItem(COSName.DV, COSName.getPDFName(defaultValue));
-        }
     }
     
     /**

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=1679669&r1=1679668&r2=1679669&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
Sat May 16 00:31:39 2015
@@ -90,6 +90,21 @@ public final class PDCheckbox extends PD
         return value instanceof COSName && value.equals(COSName.YES);
     }
 
+    /**
+     * Returns the default value, if any.
+     *
+     * @return True if checked, false if not checked, null if missing.
+     */
+    public Boolean getDefaultValue()
+    {
+        COSBase value = getInheritableAttribute(COSName.DV);
+        if (value == null)
+        {
+            return null;
+        }
+        return value instanceof COSName && value.equals(COSName.YES);
+    }
+
     @Override
     public String getValueAsString()
     {
@@ -112,4 +127,16 @@ public final class PDCheckbox extends PD
         
         applyChange();
     }
+
+    /**
+     * Sets the default value.
+     *
+     * @param value True if checked
+     * @throws IOException if the value could not be set
+     */
+    public void setDefaultValue(boolean value) throws IOException
+    {
+        COSName name = value ? COSName.YES : COSName.OFF;
+        dictionary.setItem(COSName.DV, name);
+    }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoice.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoice.java?rev=1679669&r1=1679668&r2=1679669&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoice.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoice.java
Sat May 16 00:31:39 2015
@@ -67,42 +67,6 @@ public abstract class PDChoice extends P
     {
         super(acroForm, field, parent);
     }
-
-    /**
-     * Get the fields default value.
-     * 
-     * The value is stored in the field dictionaries "DV" entry.
-     * 
-     * @return The value of this entry.
-     */
-    public String getDefaultValue()
-    {
-        return ((COSString) getInheritableAttribute(COSName.DV)).getString();
-    }    
-    
-    /**
-     * Set the default value for the field.
-     * 
-     * @param value the value
-     */
-    public void setDefaultValue(String value)
-    {
-        if (value != null)
-        {
-            if (getOptions().indexOf(value) == -1)
-            {
-                throw new IllegalArgumentException("The list box does not contain the given
value.");
-            }
-            else
-            {
-                dictionary.setString(COSName.DV, value);
-            }
-        }
-        else
-        {
-            dictionary.removeItem(COSName.DV);
-        }
-    }
     
     /**
      * This will get the option values "Opt".
@@ -421,6 +385,17 @@ public abstract class PDChoice extends P
         
         applyChange();
     }
+
+    /**
+     * Sets the default value of this field.
+     *
+     * @param value The name of the selected item.
+     * @throws IOException if the value could not be set
+     */
+    public void setDefaultValue(String value) throws IOException
+    {
+        dictionary.setString(COSName.DV, value);
+    }
     
     /**
      * Sets the entry "V" to the given values. Requires {@link #isMultiSelect()} to be true.
@@ -457,7 +432,26 @@ public abstract class PDChoice extends P
      */
     public List<String> getValue()
     {
-        COSBase value = dictionary.getDictionaryObject(COSName.V);
+        return getValueFor(COSName.V);
+    }
+
+    /**
+     * Returns the default values, or an empty string. This list always contains a single
item
+     * unless {@link #isMultiSelect()} is true.
+     *
+     * @return A non-null string.
+     */
+    public List<String> getDefaultValue()
+    {
+        return getValueFor(COSName.DV);
+    }
+
+    /**
+     * Returns the selected values, or an empty string, for the given key.
+     */
+    private List<String> getValueFor(COSName name)
+    {
+        COSBase value = dictionary.getDictionaryObject(name);
         if (value instanceof COSString)
         {
             List<String> array = new ArrayList<String>();

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDField.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDField.java?rev=1679669&r1=1679668&r2=1679669&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDField.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDField.java
Sat May 16 00:31:39 2015
@@ -104,43 +104,6 @@ public abstract class PDField implements
      * 
      */
     public abstract String getFieldType();
-    
-    /**
-     * Get the value of the "DV" entry. The "DV" entry is an inheritable attribute.
-     * 
-     * This will return null if the "DV" entry doesn't exist or if it has no value assigned.
-     * 
-     * The different field types do require specific object types for their value
-     * e.g. for RadioButtons the DV entry needs to be a name object.
-     * If the value doesn't match the expected type an IOException is thrown. Such a wrong
entry might
-     * have been set with a different library or by using PDFBox low level COS model.
-     * 
-     * To get the value in such cases the lower level COS model can be used.
-     * 
-     * @return The value of this field.
-     * @throws IOException If there is an error reading the data for this field
-     *      or the type is not in line with the fields required type.
-     * 
-     */
-    public abstract Object getDefaultValue() throws IOException;
-    
-    /**
-     * Set the value of the "DV" entry. The "DV" entry is an inheritable attribute.
-     * 
-     * The different field types do require specific object types for their value
-     * e.g. for RadioButtons the DV entry needs to be a name object. This needs to be handled
by the
-     * individual classes.
-     * 
-     * Trying to set the default value for a {@link PDPushButton} field will lead to an 
-     * {@link IllegalArgumentException} as PDPushButton fields do not support setting the

-     * entry although, common to all field types, the DV entry shall not be set.
-     * 
-     * As a result it might be necessary to check the type of the value before
-     * reusing it.
-     * 
-     * @param defaultValue The new default field value.
-     */    
-    public abstract void setDefaultValue(String defaultValue);
 
     /**
      * Returns a string representation of the "V" entry, or an empty string.

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDNonTerminalField.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDNonTerminalField.java?rev=1679669&r1=1679668&r2=1679669&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDNonTerminalField.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDNonTerminalField.java
Sat May 16 00:31:39 2015
@@ -196,26 +196,26 @@ public class PDNonTerminalField extends
     }
 
     /**
-     * @inheritDoc
+     * Returns the default value of this field. This may be of any kind which is valid for
this field's
+     * children.
      *
      * <p><b>Note:</b> while non-terminal fields <b>do</b>
inherit field values, this method returns
      * the local value, without inheritance.
      */
-    @Override
-    public Object getDefaultValue()
+    public COSBase getDefaultValue()
     {
-        return dictionary.getNameAsString(COSName.DV);
+        return dictionary.getDictionaryObject(COSName.DV);
     }
 
     /**
-     * @inheritDoc
+     * Sets the default of this field. This may be of any kind which is valid for this field's
+     * children.
      *
      * <p><b>Note:</b> while non-terminal fields <b>do</b>
inherit field values, this method returns
      * the local value, without inheritance.
      */
-    @Override
-    public void setDefaultValue(String defaultValue)
+    public void setDefaultValue(COSBase value)
     {
-        dictionary.setString(COSName.V, defaultValue);
+        dictionary.setItem(COSName.V, value);
     }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDPushButton.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDPushButton.java?rev=1679669&r1=1679668&r2=1679669&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDPushButton.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDPushButton.java
Sat May 16 00:31:39 2015
@@ -53,22 +53,6 @@ public class PDPushButton extends PDButt
     }
     
     @Override
-    public String getDefaultValue()
-    {
-        // PushButton fields don't support the "DV" entry.
-        return "";
-    }    
-    
-    @Override
-    public void setDefaultValue(String defaultValue)
-    {
-        if (defaultValue != null && !defaultValue.isEmpty())
-        {
-            throw new IllegalArgumentException("A PDPushButton shall not use the DV entry
in the field dictionary");
-        }
-    }
-    
-    @Override
     public List<String> getOptions()
     {
         return Collections.emptyList();

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java?rev=1679669&r1=1679668&r2=1679669&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java
Sat May 16 00:31:39 2015
@@ -149,6 +149,24 @@ public final class PDRadioButton extends
         }
     }
 
+    /**
+     * Returns the default value, if any.
+     *
+     * @return A non-null string.
+     */
+    public String getDefaultValue()
+    {
+        COSBase value = getInheritableAttribute(COSName.DV);
+        if (value instanceof COSName)
+        {
+            return ((COSName)value).getName();
+        }
+        else
+        {
+            return "";
+        }
+    }
+
     @Override
     public String getValueAsString()
     {
@@ -179,4 +197,15 @@ public final class PDRadioButton extends
         }
         applyChange();
     }
+
+    /**
+     * Sets the default value.
+     *
+     * @param value Name of radio button to select
+     * @throws IOException if the value could not be set
+     */
+    public void setDefaultValue(String value) throws IOException
+    {
+        dictionary.setName(COSName.DV, value);
+    }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDSignatureField.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDSignatureField.java?rev=1679669&r1=1679668&r2=1679669&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDSignatureField.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDSignatureField.java
Sat May 16 00:31:39 2015
@@ -122,6 +122,16 @@ public class PDSignatureField extends PD
     }
 
     /**
+     * Sets the default value of this field to be the given signature.
+     *
+     * @param value is the PDSignatureField
+     */
+    public void setDefaultValue(PDSignature value) throws IOException
+    {
+        dictionary.setItem(COSName.DV, value);
+    }
+
+    /**
      * Returns the signature contained in this field.
      * 
      * @return A signature dictionary.
@@ -135,6 +145,21 @@ public class PDSignatureField extends PD
         }
         return new PDSignature((COSDictionary)value);
     }
+
+    /**
+     * Returns the default value, if any.
+     *
+     * @return A signature dictionary.
+     */
+    public PDSignature getDefaultValue()
+    {
+        COSBase value = dictionary.getDictionaryObject(COSName.DV);
+        if (value == null)
+        {
+            return null;
+        }
+        return new PDSignature((COSDictionary)value);
+    }
     
     @Override
     public String getValueAsString()
@@ -174,20 +199,6 @@ public class PDSignatureField extends PD
             dictionary.setItem(COSName.SV, sv);
         }
     }
-    
-    @Override
-    public Object getDefaultValue()
-    {
-        // Signature fields don't support the "DV" entry.
-        return null;
-    }
-
-    @Override
-    public void setDefaultValue(String defaultValue)
-    {
-        // Signature fields don't support the "DV" entry.
-        throw new IllegalArgumentException("Signature fields don't support the \"DV\" entry.");
    
-    }
 
     @Override
     void constructAppearances() throws IOException

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDTextField.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDTextField.java?rev=1679669&r1=1679668&r2=1679669&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDTextField.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDTextField.java
Sat May 16 00:31:39 2015
@@ -17,10 +17,8 @@
 package org.apache.pdfbox.pdmodel.interactive.form;
 
 import java.io.IOException;
-import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSName;
-import org.apache.pdfbox.cos.COSString;
 
 /**
  * A text field is a box or space for text fill-in data typically entered from a keyboard.
@@ -206,23 +204,6 @@ public final class PDTextField extends P
     {
         dictionary.setInt(COSName.MAX_LEN, maxLen);
     }
-    
-    @Override
-    public void setDefaultValue(String value)
-    {
-        dictionary.setString(COSName.DV, value);
-    }
-    
-    @Override
-    public String getDefaultValue()
-    {
-        COSBase fieldValue = getInheritableAttribute(COSName.DV);
-        if (fieldValue instanceof COSString)
-        {
-            return ((COSString) fieldValue).getString();
-        }
-        return "";
-    }
 
     /**
      * Sets the plain text value of this field.
@@ -237,6 +218,17 @@ public final class PDTextField extends P
     }
 
     /**
+     * Sets the default value of this field.
+     *
+     * @param value Plain text
+     * @throws IOException if the value could not be set
+     */
+    public void setDefaultValue(String value) throws IOException
+    {
+        dictionary.setString(COSName.DV, value);
+    }
+
+    /**
      * Returns the value of this field, or an empty string.
      * 
      * @return A non-null string.
@@ -246,6 +238,16 @@ public final class PDTextField extends P
         return getStringOrStream(getInheritableAttribute(COSName.V));
     }
 
+    /**
+     * Returns the default value of this field, or an empty string.
+     *
+     * @return A non-null string.
+     */
+    public String getDefaultValue()
+    {
+        return getStringOrStream(getInheritableAttribute(COSName.DV));
+    }
+
     @Override
     public String getValueAsString()
     {

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestCheckBox.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestCheckBox.java?rev=1679669&r1=1679668&r2=1679669&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestCheckBox.java
(original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestCheckBox.java
Sat May 16 00:31:39 2015
@@ -81,7 +81,6 @@ public class TestCheckBox extends TestCa
             
             // test that there are no nulls returned for an empty field
             // only specific methods are tested here
-            assertNotNull(checkBox.getDefaultValue());
             assertNotNull(checkBox.getOptions());
             assertNotNull(checkBox.getValue());
             

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestFields.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestFields.java?rev=1679669&r1=1679668&r2=1679669&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestFields.java
(original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestFields.java
Sat May 16 00:31:39 2015
@@ -164,9 +164,8 @@ public class TestFields extends TestCase
             // get the Checkbox with a DV entry
             PDCheckbox checkBox = (PDCheckbox)form.getField("Checkbox-DefaultValue");
             assertNotNull(checkBox);
-            assertEquals(checkBox.getDefaultValue(),"Yes");
-            assertEquals(checkBox.getDefaultValue(),
-                    ((COSName) checkBox.getCOSObject().getDictionaryObject(COSName.DV)).getName());
+            assertEquals(Boolean.TRUE, checkBox.getDefaultValue());
+            assertEquals(COSName.YES, checkBox.getCOSObject().getDictionaryObject(COSName.DV));
             
             // get the TextField with a DV entry
             textField = (PDTextField)form.getField("TextField-DefaultValue");



Mime
View raw message