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 23:41:56 UTC

svn commit: r1647219 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form: PDComboBox.java PDFieldTreeNode.java PDListBox.java PDTextField.java

Author: msahyoun
Date: Sun Dec 21 22:41:55 2014
New Revision: 1647219

URL: http://svn.apache.org/r1647219
Log:
PDFBOX-2516 remove duplicate code

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDComboBox.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/PDListBox.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDTextField.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDComboBox.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDComboBox.java?rev=1647219&r1=1647218&r2=1647219&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDComboBox.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDComboBox.java Sun Dec 21 22:41:55 2014
@@ -75,7 +75,7 @@ public final class PDComboBox extends PD
     public Object getDefaultValue()
     {
         // TODO add handling specific to combo box
-        return getInheritableAttribute(getDictionary(), COSName.DV);
+        return getInheritableAttribute(COSName.DV);
     }     
 
     @Override
@@ -91,9 +91,6 @@ public final class PDComboBox extends PD
         }
     }    
     
-    
-    
-    
     /**
      * setValue sets the entry "V" to the given value.
      * 

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=1647219&r1=1647218&r2=1647219&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 21 22:41:55 2014
@@ -94,41 +94,44 @@ public abstract class PDFieldTreeNode im
     }
 
     /**
-     * Returns the given attribute, inheriting from parent nodes if necessary.
+     * Returns the node in the field tree from which a specific attribute might be inherited.
      *
-     * @param key the key to look up
-     * @return COS value for the given key
-     */
-    protected COSBase getInheritableAttribute(COSName key)
+     * @param field the field from which to look for the attribute
+     * @param key the key to look for
+     * @return PDFieldTreeNode the node from which the attribute will be inherited or null
+     */    
+    public PDFieldTreeNode getInheritableAttributesNode(PDFieldTreeNode field, COSName key)
     {
-        return getInheritableAttribute(getDictionary(), key);
+        if (field.getDictionary().containsKey(key))
+        {
+            return field;
+        }
+        else
+        {
+            PDFieldTreeNode parentField = field.getParent();
+            if (parentField != null)
+            {
+                getInheritableAttributesNode(parentField, key);
+            }
+        }
+        return null;
     }    
     
-    
     /**
      * 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
      */
-    protected COSBase getInheritableAttribute(COSDictionary fieldDictionary, COSName key)
+    protected COSBase getInheritableAttribute(COSName key)
     {
-        COSBase value = fieldDictionary.getDictionaryObject(key);
-        
-        if (value != null)
+        PDFieldTreeNode attributesNode = getInheritableAttributesNode(this,key);
+        if (attributesNode != null)
         {
-            return value;
+            return attributesNode.getDictionary().getDictionaryObject(key);
         }
-
-        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.
@@ -138,63 +141,26 @@ public abstract class PDFieldTreeNode im
      */
     protected void setInheritableAttribute(COSName key, COSBase value)
     {
-        setInheritableAttribute(getDictionary(), key, value);
-    }  
-    
-    /**
-     * 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
-     */
-    protected void setInheritableAttribute(COSDictionary fieldDictionary, COSName key, COSBase value)
-    {
-        if (fieldDictionary.getItem(key) != null)
+        PDFieldTreeNode attributesNode = getInheritableAttributesNode(this,key);
+        if (attributesNode != null)
         {
-            fieldDictionary.setItem(key, value);
-        }
-        else
-        {
-            COSDictionary parentDictionary = (COSDictionary) fieldDictionary.getDictionaryObject(COSName.PARENT);
-            if (parentDictionary != null)
-            {
-                setInheritableAttribute(parentDictionary, key, value);
-            }
+            attributesNode.getDictionary().setItem(key, value);
         }
-    }
-
-    /**
-     * Removes the given attribute, inheriting from parent nodes if necessary.
-     *
-     * @param key the key to look up
-     */
-    protected void removeInheritableAttribute(COSName key)
-    {
-        removeInheritableAttribute(getDictionary(), key);
-    }      
+    }  
     
     /**
      * Removes the given attribute, inheriting from parent nodes if necessary.
      *
-     * @param fieldDictionary field object
      * @param key the key to look up
      */
-    protected void removeInheritableAttribute(COSDictionary fieldDictionary, COSName key)
+    protected void removeInheritableAttribute(COSName key)
     {
-        if (fieldDictionary.getItem(key) != null)
-        {
-            fieldDictionary.removeItem(key);
-        }
-        else
+        PDFieldTreeNode attributesNode = getInheritableAttributesNode(this,key);
+        if (attributesNode != null)
         {
-            COSDictionary parentDictionary = (COSDictionary) fieldDictionary.getDictionaryObject(COSName.PARENT);
-            if (parentDictionary != null)
-            {
-                removeInheritableAttribute(parentDictionary, key);
-            }
+            attributesNode.getDictionary().removeItem(key);
         }
-    }
+    }      
     
     /**
      * Get a text as text stream.
@@ -887,5 +853,4 @@ public abstract class PDFieldTreeNode im
         }
         return retval;
     }
-
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDListBox.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDListBox.java?rev=1647219&r1=1647218&r2=1647219&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDListBox.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDListBox.java Sun Dec 21 22:41:55 2014
@@ -76,7 +76,7 @@ public final class PDListBox extends PDC
     public Object getDefaultValue()
     {
         // TODO add handling specific to list box
-        return getInheritableAttribute(getDictionary(), COSName.DV);
+        return getInheritableAttribute(COSName.DV);
     }
     
     /**

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=1647219&r1=1647218&r2=1647219&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 21 22:41:55 2014
@@ -247,7 +247,7 @@ public final class PDTextField extends P
     @Override
     public String getDefaultValue()
     {
-        COSBase fieldValue = getInheritableAttribute(getDictionary(), COSName.DV);
+        COSBase fieldValue = getInheritableAttribute(COSName.DV);
         if (fieldValue instanceof COSString)
         {
             return ((COSString) fieldValue).getString();
@@ -293,7 +293,7 @@ public final class PDTextField extends P
     @Override
     public String getValue() throws IOException
     {
-        PDTextStream textStream = getAsTextStream(getInheritableAttribute(getDictionary(), COSName.V));
+        PDTextStream textStream = getAsTextStream(getInheritableAttribute(COSName.V));
 
         if (textStream != null) 
         {