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/01/12 11:48:26 UTC

svn commit: r1651060 - in /pdfbox/trunk/pdfbox/src: main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java

Author: msahyoun
Date: Mon Jan 12 10:48:26 2015
New Revision: 1651060

URL: http://svn.apache.org/r1651060
Log:
PDFBOX-2516 support the Opt entry

Added:
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java   (with props)
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java

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=1651060&r1=1651059&r2=1651060&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 Mon Jan 12 10:48:26 2015
@@ -17,11 +17,15 @@
 package org.apache.pdfbox.pdmodel.interactive.form;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.List;
 
+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 org.apache.pdfbox.pdmodel.common.COSObjectable;
 
 /**
@@ -115,6 +119,56 @@ public final class PDRadioButton extends
             setInheritableAttribute(COSName.DV, COSName.getPDFName(defaultValue));
         }
     }
+
+    /**
+     * This will get the option values - the "Opt" entry.
+     * 
+     * <p>The option values are used to define the export values
+     * for the field to 
+     * <ul>
+     *  <li>hold values in non-Latin writing systems as name objects, which represent the field value, are limited
+     *      to PDFDocEncoding
+     *  </li>
+     *  <li>allow radio buttons having the same export value to be handled independently
+     *  </li>
+     * </ul>
+     * </p>
+     * 
+     * @return List containing all possible options. If there is no Opt entry an empty list will be returned.
+     */
+    public List<String> getOptions()
+    {
+        COSBase value = getInheritableAttribute(COSName.OPT);
+        if (value instanceof COSString)
+        {
+            List<String> array = new ArrayList<String>();
+            array.add(((COSString) value).getString());
+            return array;
+        }
+        else if (value instanceof COSArray)
+        {
+            return COSArrayList.convertCOSStringCOSArrayToList((COSArray)value);
+        }
+        return new ArrayList<String>();
+    } 
+    
+    /**
+     * This will set the options.
+     * 
+     * @see #getOptions()
+     * @param values List containing all possible options. Supplying null will remove the Opt entry.
+     */
+    public void setOptions(List<String> values)
+    {
+        if (values != null)
+        {
+            setInheritableAttribute(COSName.OPT, COSArrayList.convertStringListToCOSStringCOSArray(values));
+        }
+        else
+        {
+            removeInheritableAttribute(COSName.OPT);
+        }
+    }
     
     @Override
     public String getValue() throws IOException

Added: 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=1651060&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java (added)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java Mon Jan 12 10:48:26 2015
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.pdmodel.interactive.form;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.pdmodel.PDDocument;
+
+/**
+ * This will test the functionality of Radio Buttons in PDFBox.
+ *
+ * @author Maruan Sahyoun
+ */
+public class TestRadioButtons extends TestCase
+{
+    
+    /**
+     * Constructor.
+     *
+     * @param name The name of the test to run.
+     */
+    public TestRadioButtons( String name )
+    {
+        super( name );
+    }
+
+    /**
+     * This will get the suite of test that this class holds.
+     *
+     * @return All of the tests that this class holds.
+     */
+    public static Test suite()
+    {
+        return new TestSuite( TestRadioButtons.class );
+    }
+
+    /**
+     * infamous main method.
+     *
+     * @param args The command line arguments.
+     */
+    public static void main( String[] args )
+    {
+        String[] arg = {TestRadioButtons.class.getName() };
+        junit.textui.TestRunner.main( arg );
+    }
+
+    /**
+     * This will test the radio button PDModel.
+     *
+     * @throws IOException If there is an error creating the field.
+     */
+    public void testRadioButtonPDModel() throws IOException
+    {
+        PDDocument doc = null;
+        try
+        {
+            doc = new PDDocument();
+            PDAcroForm form = new PDAcroForm( doc );
+            PDRadioButton radioButton = new PDRadioButton(form);
+            
+            // Test setting/getting option values - the dictionaries Opt entry
+            List<String> options = new ArrayList<String>();
+            options.add("Value01");
+            options.add("Value02");
+            radioButton.setOptions(options);
+
+            COSArray optItem = (COSArray) radioButton.getDictionary().getItem(COSName.OPT);
+
+            // assert that the values have been correctly set
+            assertNotNull(radioButton.getDictionary().getItem(COSName.OPT));
+            assertEquals(optItem.size(),2);
+            assertEquals(options.get(0), optItem.getString(0));
+            
+            // assert that the values can be retrieved correctly
+            List<String> retrievedOptions = radioButton.getOptions();
+            assertEquals(retrievedOptions.size(),2);
+            assertEquals(retrievedOptions, options);
+
+            // assert that the Opt entry is removed
+            radioButton.setOptions(null);
+            assertNull(radioButton.getDictionary().getItem(COSName.OPT));
+            // if there is no Opt entry an empty List shall be returned
+            assertEquals(radioButton.getOptions(), new ArrayList<String>());
+        }
+        finally
+        {
+            if( doc != null )
+            {
+                doc.close();
+            }
+        }
+    }
+}
\ No newline at end of file

Propchange: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain