pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From msahy...@apache.org
Subject svn commit: r1643669 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/
Date Sun, 07 Dec 2014 12:27:46 GMT
Author: msahyoun
Date: Sun Dec  7 12:27:45 2014
New Revision: 1643669

URL: http://svn.apache.org/r1643669
Log:
PDFBOX-2516 start using inheritable field attributes

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/PDFieldTreeNode.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/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/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDVariableText.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=1643669&r1=1643668&r2=1643669&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
Sun Dec  7 12:27:45 2014
@@ -17,11 +17,13 @@
 package org.apache.pdfbox.pdmodel.interactive.form;
 
 import org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.cos.COSString;
 import org.apache.pdfbox.pdmodel.common.COSArrayList;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -83,25 +85,54 @@ public abstract class PDButton extends P
         return retval;
     }
 
+
     /**
-     * This will will set the list of options for this button.
-     *
+     * Set the field options values.
+     * 
+     * The fields options represent the export value of each annotation in the field. 
+     * It may be used to:
+     * <ul>
+     *  <li>represent the export values in non-Latin writing systems.</li>
+     *  <li>allow radio buttons to be checked independently, even 
+     *  if they have the same export value.</li>
+     * </ul>
+     * 
+     * Providing an empty list or null will remove the entry.
+     * 
      * @param options The list of options for the button.
      */
     public void setOptions( List<String> options )
     {
-        getDictionary().setItem(COSName.OPT, COSArrayList.converterToCOSArray( options )
);
+        if (options == null || options.size() == 0)
+        {
+            getDictionary().removeItem(COSName.OPT);
+        }
+        else
+        {
+            getDictionary().setItem(COSName.OPT, COSArrayList.converterToCOSArray( options
) );
+        }
     }
     
     @Override
-    public Object getDefaultValue()
+    public COSBase getDefaultValue() throws IOException
     {
         // Button fields don't support the "DV" entry.
         return null;
     }
 
-    @Override
-    public void setDefaultValue(Object value)
+    /**
+     * Set the fields default value.
+     * 
+     * The field value holds a name object which is corresponding to the 
+     * appearance state representing the corresponding appearance 
+     * from the appearance directory.
+     *
+     * The default value is used to represent the initial state of the
+     * checkbox or to revert when resetting the form.
+     * 
+     * @param defaultValue the COSName object to set the field value.
+     */
+    public void setDefaultValue(COSName defaultValue)
     {
         // Button fields don't support the "DV" entry.
         throw new RuntimeException( "Button fields don't support the \"DV\" entry." );

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=1643669&r1=1643668&r2=1643669&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  7 12:27:45 2014
@@ -16,6 +16,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;
@@ -134,17 +136,63 @@ public final class PDCheckbox extends PD
     }
 
     @Override
-    public COSName getDefaultValue()
+    public COSName getDefaultValue() throws IOException
     {
-        return getDictionary().getCOSName(COSName.DV);
+        COSBase attribute = getInheritableAttribute(getDictionary(), COSName.V);
+
+        if (attribute instanceof COSName)
+        {
+            return (COSName) attribute;
+        }
+        else
+        {
+            throw new IOException("Expected a COSName entry but got " + attribute.getClass().getName());
+        }
     }
     
+    /**
+     * Set the fields default value.
+     * 
+     * The field value holds a name object which is corresponding to the 
+     * appearance state representing the corresponding appearance 
+     * from the appearance directory.
+     *
+     * The default value is used to represent the initial state of the
+     * checkbox or to revert when resetting the form.
+     * 
+     * @param defaultValue the COSName object to set the field value.
+     */
+    public void setDefaultValue(COSName defaultValue)
+    {
+        if (defaultValue == null)
+        {
+            getDictionary().removeItem(COSName.DV);
+        }
+        else
+        {
+            getDictionary().setItem(COSName.DV, defaultValue);
+        }
+    }
+    
+    
+    
     @Override
     public COSName getValue()
     {
         return getDictionary().getCOSName( COSName.V );
     }
 
+    /**
+     * Set the field value.
+     * 
+     * The field value holds a name object which is corresponding to the 
+     * appearance state representing the corresponding appearance 
+     * from the appearance directory.
+     *
+     * The default value is Off.
+     * 
+     * @param value the COSName object to set the field value.
+     */
     public void setValue(COSName value)
     {
         if (value == null)

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDFieldTreeNode.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDFieldTreeNode.java?rev=1643669&r1=1643668&r2=1643669&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDFieldTreeNode.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDFieldTreeNode.java
Sun Dec  7 12:27:45 2014
@@ -92,7 +92,79 @@ public abstract class PDFieldTreeNode im
         dictionary = field;
         parent = parentNode;
     }
+    
+    /**
+     * Returns the given attribute, inheriting from parent nodes if necessary.
+     *
+     * @param fieldDictionary field object
+     * @param key the key to look up
+     * @return COS value for the given key
+     */
+    public COSBase getInheritableAttribute(COSDictionary fieldDictionary, COSName key)
+    {
+        COSBase value = fieldDictionary.getCOSName(key);
+        if (value != null)
+        {
+            return value;
+        }
+
+        COSDictionary parentDictionary = (COSDictionary) fieldDictionary.getDictionaryObject(COSName.PARENT);
+        if (parentDictionary != null)
+        {
+            return getInheritableAttribute(parentDictionary, key);
+        }
 
+        return null;
+    }
+    
+    
+    /**
+     * Sets the given attribute, inheriting from parent nodes if necessary.
+     *
+     * @param fieldDictionary field object
+     * @param key the key to look up
+     * @param value the new attributes value
+     */
+    public void setInheritableAttribute(COSDictionary fieldDictionary, COSName key, COSBase
value)
+    {
+        if (fieldDictionary.getItem(key) != null)
+        {
+            fieldDictionary.setItem(key, value);
+        }
+        else
+        {
+            COSDictionary parentDictionary = (COSDictionary) fieldDictionary.getDictionaryObject(COSName.PARENT);
+            if (parentDictionary != null)
+            {
+                setInheritableAttribute(parentDictionary, key, value);
+            }
+        }
+    }
+    
+    /**
+     * Removes the given attribute, inheriting from parent nodes if necessary.
+     *
+     * @param fieldDictionary field object
+     * @param key the key to look up
+     */
+    public void removeInheritableAttribute(COSDictionary fieldDictionary, COSName key)
+    {
+        if (fieldDictionary.getItem(key) != null)
+        {
+            fieldDictionary.removeItem(key);
+        }
+        else
+        {
+            COSDictionary parentDictionary = (COSDictionary) fieldDictionary.getDictionaryObject(COSName.PARENT);
+            if (parentDictionary != null)
+            {
+                removeInheritableAttribute(parentDictionary, key);
+            }
+        }
+    }
+    
+    
+    
     /**
      * Get the FT entry of the field. This is a read only field and is set depending on the
actual type. The field type
      * is an inheritable attribute.
@@ -101,30 +173,47 @@ public abstract class PDFieldTreeNode im
      * 
      */
     public abstract String getFieldType();
-
+    
     /**
-     * getValue gets the value of the "V" entry.
+     * Get the value of the "DV" entry. The "DV" entry is an inheritable attribute.
      * 
-     * @return The value of this entry.
+     * This will return null if the "DV" entry doesn't exist or if it has no value assigned.
      * 
-     */
-    public abstract Object getValue();
-
-    /**
-     * getDefaultValue gets the value of the "DV" entry.
+     * 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();
+    public abstract Object getDefaultValue() throws IOException;
 
     /**
-     * setDefaultValue sets the entry "DV" to the given value.
+     * Get the value of the "V" entry. The "V" entry is an inheritable attribute.
      * 
-     * @param value the value
+     * This will return null if the "V" 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 V 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.
+     * 
+     * As a result it might be necessary to check the type of the value before
+     * reusing it.
+     * 
+     * @return The value of this entry.
+     * @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 void setDefaultValue(Object value);
+    public abstract Object getValue() throws IOException;
 
     /**
      * sets the field to be read-only.
@@ -165,7 +254,7 @@ public abstract class PDFieldTreeNode im
     }
 
     /**
-     * sets the field to be not exported..
+     * sets the field to be not exported.
      * 
      * @param noExport The new flag for noExport.
      */

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=1643669&r1=1643668&r2=1643669&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
Sun Dec  7 12:27:45 2014
@@ -95,7 +95,6 @@ public class PDNonTerminalField extends
         return null;
     }
     
-    @Override
     public void setDefaultValue(Object value)
     {
         // Nonterminal fields don't support the "DV" entry.

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=1643669&r1=1643668&r2=1643669&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
Sun Dec  7 12:27:45 2014
@@ -16,8 +16,10 @@
  */
 package org.apache.pdfbox.pdmodel.interactive.form;
 
+import java.io.IOException;
 import java.util.List;
 
+import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.pdmodel.common.COSObjectable;
@@ -66,28 +68,78 @@ public final class PDRadioButton extends
 
     
     @Override
-    public COSName getDefaultValue()
+    public COSName getDefaultValue() throws IOException
     {
-        return getDictionary().getCOSName(COSName.DV);
+        COSBase attribute = getInheritableAttribute(getDictionary(), COSName.DV);
+
+        if (attribute instanceof COSName)
+        {
+            return (COSName) attribute;
+        }
+        else
+        {
+            throw new IOException("Expected a COSName entry but got " + attribute.getClass().getName());
+        }
     }
     
+    /**
+     * Set the fields default value.
+     * 
+     * The field value holds a name object which is corresponding to the 
+     * appearance state representing the corresponding appearance 
+     * from the appearance directory.
+     *
+     * The default value is used to represent the initial state of the
+     * checkbox or to revert when resetting the form.
+     * 
+     * @param defaultValue the COSName object to set the field value.
+     */
+    public void setDefaultValue(COSName defaultValue)
+    {
+        if (defaultValue == null)
+        {
+            removeInheritableAttribute(getDictionary(),COSName.DV);
+        }
+        else
+        {
+            setInheritableAttribute(getDictionary(), COSName.DV, defaultValue);
+        }
+    }
     
     @Override
-    public COSName getValue()
+    public COSName getValue() throws IOException
     {
-        return getDictionary().getCOSName(COSName.V);
+        COSBase attribute = getInheritableAttribute(getDictionary(), COSName.V);
+
+        if (attribute instanceof COSName)
+        {
+            return (COSName) attribute;
+        }
+        else
+        {
+            throw new IOException("Expected a COSName entry but got " + attribute.getClass().getName());
+        }
     }
 
-    
+    /**
+     * Set the field value.
+     * 
+     * The field value holds a name object which is corresponding to the 
+     * appearance state of the child field being in the on state.
+     * 
+     * The default value is Off.
+     * 
+     * @param value the COSName object to set the field value.
+     */
     public void setValue(COSName value)
     {
         if (value == null)
         {
-            getDictionary().removeItem(COSName.V);
+            removeInheritableAttribute(getDictionary(),COSName.V);
         }
         else
         {
-            getDictionary().setItem(COSName.V, (COSName) value);
+            setInheritableAttribute(getDictionary(),COSName.V, value);
             List<COSObjectable> kids = getKids();
             for (COSObjectable kid : kids)
             {
@@ -106,5 +158,4 @@ public final class PDRadioButton extends
             }
         }
     }
-
 }

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=1643669&r1=1643668&r2=1643669&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
Sun Dec  7 12:27:45 2014
@@ -199,7 +199,6 @@ public class PDSignatureField extends PD
         return null;
     }
 
-    @Override
     public void setDefaultValue(Object value)
     {
         // Signature fields don't support the "DV" entry.

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=1643669&r1=1643668&r2=1643669&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
Sun Dec  7 12:27:45 2014
@@ -71,6 +71,30 @@ public final class PDTextField extends P
     }
 
     /**
+     * setValue sets the default value for the field.
+     * 
+     * @param value the default value
+     * 
+     */
+    public void setDefaultValue(String value)
+    {
+        if (value != null)
+        {
+            if (value instanceof String)
+            {
+                String stringValue = (String)value;
+                COSString fieldValue = new COSString(stringValue);
+                setInheritableAttribute(getDictionary(), COSName.DV, fieldValue);
+            }
+            // TODO stream instead of string
+        }  
+        else
+        {
+            removeInheritableAttribute(getDictionary(),COSName.DV);
+        }
+    }
+    
+    /**
      * setValue sets the entry "V" to the given value.
      * 
      * @param value the value
@@ -84,13 +108,13 @@ public final class PDTextField extends P
             {
                 String stringValue = (String)value;
                 COSString fieldValue = new COSString(stringValue);
-                getDictionary().setItem( COSName.V, fieldValue );
+                setInheritableAttribute(getDictionary(), COSName.V, fieldValue);
             }
             // TODO stream instead of string
         }  
         else
         {
-            getDictionary().removeItem(COSName.V);
+            removeInheritableAttribute(getDictionary(),COSName.DV);
         }
         
         updateFieldAppearances();
@@ -105,8 +129,6 @@ public final class PDTextField extends P
     @Override
     public Object getValue()
     {
-        return getDictionary().getItem(COSName.V );
+        return getInheritableAttribute(getDictionary(), COSName.V);
     }
-
-
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDVariableText.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDVariableText.java?rev=1643669&r1=1643668&r2=1643669&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDVariableText.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDVariableText.java
Sun Dec  7 12:27:45 2014
@@ -305,7 +305,6 @@ public abstract class PDVariableText ext
         return getDictionary().getDictionaryObject(COSName.DV);
     }
 
-    @Override
     public void setDefaultValue(Object value)
     {
         // Text fields don't support the "DV" entry.



Mime
View raw message