Author: msahyoun
Date: Sat Apr  7 15:17:56 2018
New Revision: 1828596

URL: http://svn.apache.org/viewvc?rev=1828596&view=rev
Log:
PDFBOX-4185: support COSString, COSArray mixed options entries

Modified:
    
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/FieldUtils.java
    
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoiceTest.java

Modified: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/FieldUtils.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/FieldUtils.java?rev=1828596&r1=1828595&r2=1828596&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/FieldUtils.java
 (original)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/FieldUtils.java
 Sat Apr  7 15:17:56 2018
@@ -25,7 +25,6 @@ import java.util.List;
 import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSString;
-import org.apache.pdfbox.pdmodel.common.COSArrayList;
 
 /**
  * A set of utility methods to help with common AcroForm form and field 
related functions.
@@ -175,37 +174,24 @@ public final class FieldUtils
         }
         else if (items instanceof COSArray)
         {
-            // test if there is a single text or a two-element array 
-            COSBase entry = ((COSArray) items).get(0);
-            if (entry instanceof COSString)
+            List<String> entryList = new ArrayList<>(); 
+            for (COSBase entry : (COSArray) items)
             {
-                return 
COSArrayList.convertCOSStringCOSArrayToList((COSArray)items);
-            } 
-            else
-            {
-                return getItemsFromPair(items, pairIdx);
-            }            
+                if (entry instanceof COSString)
+                {
+                    entryList.add(((COSString) entry).getString());
+                }
+                else if (entry instanceof COSArray)
+                {
+                    COSArray cosArray = (COSArray) entry;
+                    if (cosArray.size() >= pairIdx +1 && cosArray.get(pairIdx) 
instanceof COSString)
+                    {
+                        entryList.add(((COSString) 
cosArray.get(pairIdx)).getString());
+                    }
+                }
+            }
+            return entryList;
         }
         return Collections.emptyList();
-    }    
-
-    /**
-     * Return either one of a list of two-element arrays entries.
-     *
-     * @param items the array of elements or two-element arrays
-     * @param pairIdx the index into the two-element array
-     * @return a List of single elements
-     */
-    private static List<String> getItemsFromPair(COSBase items, int pairIdx)
-    {
-        List<String> exportValues = new ArrayList<>();
-        int numItems = ((COSArray) items).size();
-        for (int i=0;i<numItems;i++)
-        {
-            COSArray pair = (COSArray) ((COSArray) items).get(i);
-            COSString displayValue = (COSString) pair.get(pairIdx);
-            exportValues.add(displayValue.getString());
-        }
-        return exportValues;        
     }
 }

Modified: 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoiceTest.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoiceTest.java?rev=1828596&r1=1828595&r2=1828596&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoiceTest.java
 (original)
+++ 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoiceTest.java
 Sat Apr  7 15:17:56 2018
@@ -20,7 +20,12 @@ import static org.junit.Assert.assertEqu
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSString;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.junit.Before;
 import org.junit.Test;
@@ -33,12 +38,17 @@ public class PDChoiceTest
 {
     private PDDocument document;
     private PDAcroForm acroForm;
+    private List<String> options;
 
     @Before
     public void setUp()
     {
         document = new PDDocument();
         acroForm = new PDAcroForm(document);
+        options = new ArrayList<>();
+        options.add(" ");
+        options.add("A");
+        options.add("B");
     }
 
     @Test
@@ -60,6 +70,78 @@ public class PDChoiceTest
         assertEquals(choiceField.getFieldType(), "Ch");
         assertTrue(choiceField.isCombo());
     }
+    
+    @Test
+    public void getOptionsFromStrings()
+    {
+        PDChoice choiceField = new PDComboBox(acroForm);
+        COSArray choiceFieldOptions = new COSArray();
+        choiceFieldOptions.add(new COSString(" "));
+        choiceFieldOptions.add(new COSString("A"));
+        choiceFieldOptions.add(new COSString("B"));
+        
+        // add the options using the low level COS model as the PD model will
+        // abstract the COSArray
+        choiceField.getCOSObject().setItem(COSName.OPT, choiceFieldOptions);
+
+        assertEquals(options, choiceField.getOptions());
+    }
+
+    @Test
+    public void getOptionsFromCOSArray()
+    {
+        PDChoice choiceField = new PDComboBox(acroForm);
+        COSArray choiceFieldOptions = new COSArray();
+
+        // add entry to options
+        COSArray entry = new COSArray();
+        entry.add(new COSString(" "));
+        choiceFieldOptions.add(entry);
+
+        // add entry to options
+        entry = new COSArray();
+        entry.add(new COSString("A"));
+        choiceFieldOptions.add(entry);
+
+        // add entry to options
+        entry = new COSArray();
+        entry.add(new COSString("B"));
+        choiceFieldOptions.add(entry);
+                
+        // add the options using the low level COS model as the PD model will
+        // abstract the COSArray
+        choiceField.getCOSObject().setItem(COSName.OPT, choiceFieldOptions);
+
+        assertEquals(options, choiceField.getOptions());
+    }
+    
+    /*
+     * Get the entries form a moxed values array. See PDFBOX-4185
+     */
+    @Test
+    public void getOptionsFromMixed()
+    {
+        PDChoice choiceField = new PDComboBox(acroForm);
+        COSArray choiceFieldOptions = new COSArray();
+
+        // add string entry to options
+        choiceFieldOptions.add(new COSString(" "));
 
+        // add array entry to options
+        COSArray entry = new COSArray();
+        entry.add(new COSString("A"));
+        choiceFieldOptions.add(entry);
+
+        // add array entry to options
+        entry = new COSArray();
+        entry.add(new COSString("B"));
+        choiceFieldOptions.add(entry);
+                
+        // add the options using the low level COS model as the PD model will
+        // abstract the COSArray
+        choiceField.getCOSObject().setItem(COSName.OPT, choiceFieldOptions);
+
+        assertEquals(options, choiceField.getOptions());
+    }
 }
 


Reply via email to