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 2015/07/09 15:41:15 UTC

svn commit: r1690103 - in /pdfbox/trunk/pdfbox/src: main/java/org/apache/pdfbox/pdmodel/interactive/form/ test/java/org/apache/pdfbox/pdmodel/interactive/form/

Author: msahyoun
Date: Thu Jul  9 13:41:15 2015
New Revision: 1690103

URL: http://svn.apache.org/r1690103
Log:
PDFBOX-2841: use Set to return options; rename and fix getExportValue()

Modified:
    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/PDRadioButton.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java

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=1690103&r1=1690102&r2=1690103&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 Thu Jul  9 13:41:15 2015
@@ -17,9 +17,8 @@
 package org.apache.pdfbox.pdmodel.interactive.form;
 
 import java.io.IOException;
-import java.util.ArrayList;
 import java.util.Collections;
-import java.util.List;
+import java.util.HashSet;
 import java.util.Set;
 
 import org.apache.pdfbox.cos.COSBase;
@@ -227,17 +226,17 @@ public final class PDCheckbox extends PD
      * @return the value setting the check box to the On state. 
      *          If an empty List is returned there is no appearance definition.
      */
-    public List<String> getOnValues()
+    public Set<String> getOnValues()
     {
         String onValue = getOnValue();
         
         if (onValue.isEmpty())
         {
-            return Collections.emptyList();
+            return Collections.emptySet();
         }
         else
         {
-            ArrayList<String> onValues = new ArrayList<String>();
+            Set<String> onValues = new HashSet<String>();
             onValues.add(onValue);
             return onValues;
         }

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=1690103&r1=1690102&r2=1690103&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 Thu Jul  9 13:41:15 2015
@@ -18,6 +18,7 @@ package org.apache.pdfbox.pdmodel.intera
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
@@ -86,7 +87,7 @@ public final class PDRadioButton extends
     }
 
     /**
-     * This will get the export value.
+     * This will get the selected export values.
      * <p>
      * A RadioButton might have an export value to allow field values
      * which can not be encoded as PDFDocEncoding or for the same export value 
@@ -94,31 +95,35 @@ public final class PDRadioButton extends
      * To define an export value the RadioButton must define options {@link #setExportValues(List)}
      * which correspond to the individual items within the RadioButton.</p>
      * <p>
-     * The method will either return the value from the options entry or in case there
+     * The method will either return the corresponding values from the options entry or in case there
      * is no such entry the fields value</p>
      * 
      * @return the export value of the field.
      * @throws IOException in case the fields value can not be retrieved
      */
-    public String getExportValue() throws IOException
+    public List<String> getSelectedExportValues() throws IOException
     {
+        List<String> onValues = getSelectableOnValues();
         List<String> exportValues = getExportValues();
+        ArrayList<String> selectedExportValues = new ArrayList<String>();
         if (exportValues.isEmpty())
         {
-            return getValue();
+            selectedExportValues.add(getValue());
+            return selectedExportValues;
         }
         else
         {
             String fieldValue = getValue();
-            List<String> onValues = getOnValues();
-            int idx = onValues.indexOf(fieldValue);
-            
-            if (idx != -1 && idx < exportValues.size())
+            int idx = 0;
+            for (String onValue : onValues)
             {
-                return exportValues.get(idx);
+                if (onValue.compareTo(fieldValue) == 0)
+                {
+                    selectedExportValues.add(exportValues.get(idx));
+                }
             }
+            return selectedExportValues;
         }
-        return "";
     }
 
     /**
@@ -205,6 +210,24 @@ public final class PDRadioButton extends
     }    
 
     /**
+     * Get the values to set individual radio buttons to the on state.
+     * 
+     * <p>The On value could be an arbitrary string as long as it is within the limitations of
+     * a PDF name object. The Off value shall always be 'Off'. If not set or not part of the normal
+     * appearance keys 'Off' is the default</p>
+     *
+     * @return the value setting the check box to the On state. 
+     *          If an empty string is returned there is no appearance definition.
+     */
+    public Set<String> getOnValues()
+    {
+        // we need a set as the radio buttons can appear multiple times
+        Set<String> onValues = new HashSet<String>();
+        onValues.addAll(getSelectableOnValues());
+        return onValues;
+    }
+    
+    /**
      * Checks value.
      *
      * @param value Name of radio button to select
@@ -212,7 +235,7 @@ public final class PDRadioButton extends
      */
     private void checkValue(String value) throws IllegalArgumentException
     {
-        List<String> onValues = getOnValues();
+        Set<String> onValues = getOnValues();
         if (COSName.Off.getName().compareTo(value) != 0 && !onValues.contains(value))
         {
             throw new IllegalArgumentException("value '" + value
@@ -222,19 +245,15 @@ public final class PDRadioButton extends
     }
     
     /**
-     * Get the List of values to set individual radio buttons to the on state.
+     * Get all potential ON values.
      * 
-     * <p>The On value could be an arbitrary string as long as it is within the limitations of
-     * a PDF name object. The Off value shall always be 'Off'. If not set or not part of the normal
-     * appearance keys 'Off' is the default</p>
-     *
-     * @return the value setting the check box to the On state. 
-     *          If an empty string is returned there is no appearance definition.
+     * @return the ON values.
      */
-    public List<String> getOnValues()
+    private List<String> getSelectableOnValues()
     {
         List<PDAnnotationWidget> widgets = this.getWidgets();
-        ArrayList<String> onValues = new ArrayList<String>();
+        // we need a set as the radio buttons can appear multiple times
+        List<String> onValues = new ArrayList<String>();
         
         for (PDAnnotationWidget widget : widgets)
         {
@@ -255,8 +274,7 @@ public final class PDRadioButton extends
                     }
                 }
             }
-        }
+        }        
         return onValues;
-    }  
-    
+    }    
 }

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java?rev=1690103&r1=1690102&r2=1690103&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java Thu Jul  9 13:41:15 2015
@@ -98,7 +98,7 @@ public class PDButtonTest
         assertNotNull(checkbox);
         assertEquals(checkbox.getOnValue(), "Yes");
         assertEquals(checkbox.getOnValues().size(), 1);
-        assertEquals(checkbox.getOnValues().get(0), "Yes");
+        assertTrue(checkbox.getOnValues().contains("Yes"));
     }
     
     @Test
@@ -143,8 +143,8 @@ public class PDButtonTest
         PDRadioButton radioButton = (PDRadioButton) acrobatAcroForm.getField("RadioButtonGroup");
         assertNotNull(radioButton);
         assertEquals(radioButton.getOnValues().size(), 2);
-        assertEquals(radioButton.getOnValues().get(0), "RadioButton01");
-        assertEquals(radioButton.getOnValues().get(1), "RadioButton02");
+        assertTrue(radioButton.getOnValues().contains("RadioButton01"));
+        assertTrue(radioButton.getOnValues().contains("RadioButton02"));
     }
     
     @Test

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java?rev=1690103&r1=1690102&r2=1690103&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java Thu Jul  9 13:41:15 2015
@@ -82,7 +82,7 @@ public class TestRadioButtons extends Te
             // test that there are no nulls returned for an empty field
             // only specific methods are tested here
             assertNotNull(radioButton.getDefaultValue());
-            assertNotNull(radioButton.getExportValue());
+            assertNotNull(radioButton.getSelectedExportValues());
             assertNotNull(radioButton.getExportValues());
             assertNotNull(radioButton.getValue());