pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From msahy...@apache.org
Subject svn commit: r1701905 - in /pdfbox/trunk/pdfbox/src: main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java
Date Wed, 09 Sep 2015 06:19:20 GMT
Author: msahyoun
Date: Wed Sep  9 06:19:19 2015
New Revision: 1701905

URL: http://svn.apache.org/r1701905
Log:
PDFBOX-2965: avoid NPE in getField() if the /Fields entry is missing

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

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java?rev=1701905&r1=1701904&r2=1701905&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
Wed Sep  9 06:19:19 2015
@@ -234,9 +234,8 @@ public final class PDAcroForm implements
      *
      * @param fullyQualifiedName The name of the field to get.
      * @return The field with that name of null if one was not found.
-     * @throws IOException If there is an error getting the field type.
      */
-    public PDField getField(String fullyQualifiedName) throws IOException
+    public PDField getField(String fullyQualifiedName)
     {
         PDField retval = null;
         if (fieldCache != null)
@@ -248,35 +247,38 @@ public final class PDAcroForm implements
             String[] nameSubSection = fullyQualifiedName.split("\\.");
             COSArray fields = (COSArray) dictionary.getDictionaryObject(COSName.FIELDS);
 
-            for (int i = 0; i < fields.size() && retval == null; i++)
+            if (fields != null)
             {
-                COSDictionary element = (COSDictionary) fields.getObject(i);
-                if (element != null)
+                for (int i = 0; i < fields.size() && retval == null; i++)
                 {
-                    COSString fieldName =
-                        (COSString)element.getDictionaryObject(COSName.T);
-                    if (fieldName.getString().equals(fullyQualifiedName) ||
-                        fieldName.getString().equals(nameSubSection[0]))
+                    COSDictionary element = (COSDictionary) fields.getObject(i);
+                    if (element != null)
                     {
-                        PDField root = PDField.fromDictionary(this, element, null);
-                        if (root != null)
+                        COSString fieldName =
+                            (COSString)element.getDictionaryObject(COSName.T);
+                        if (fieldName.getString().equals(fullyQualifiedName) ||
+                            fieldName.getString().equals(nameSubSection[0]))
                         {
-                            if (nameSubSection.length > 1)
+                            PDField root = PDField.fromDictionary(this, element, null);
+                            if (root != null)
                             {
-                                PDField kid = root.findKid(nameSubSection, 1);
-                                if (kid != null)
+                                if (nameSubSection.length > 1)
                                 {
-                                    retval = kid;
+                                    PDField kid = root.findKid(nameSubSection, 1);
+                                    if (kid != null)
+                                    {
+                                        retval = kid;
+                                    }
+                                    else
+                                    {
+                                        retval = root;
+                                    }
                                 }
                                 else
                                 {
                                     retval = root;
                                 }
                             }
-                            else
-                            {
-                                retval = root;
-                            }
                         }
                     }
                 }

Added: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java?rev=1701905&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java
(added)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java
Wed Sep  9 06:19:19 2015
@@ -0,0 +1,78 @@
+/*
+ * 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 static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import java.io.IOException;
+
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test for the PDButton class.
+ *
+ */
+public class PDAcroFormTest
+{
+    
+    private PDDocument document;
+    private PDAcroForm acroForm;
+    
+    @Before
+    public void setUp()
+    {
+        document = new PDDocument();
+        acroForm = new PDAcroForm(document);
+        document.getDocumentCatalog().setAcroForm(acroForm);
+    }
+
+    @Test
+    public void testFieldsEntry()
+    {
+        // the /Fields entry has been created with the AcroForm
+        // as this is a required entry
+        assertNotNull(acroForm.getFields());
+        assertEquals(acroForm.getFields().size(),0);
+        
+        // there shouldn't be an exception if there is no such field
+        assertNull(acroForm.getField("foo"));
+        
+        // remove the required entry which is the case for some
+        // PDFs (see PDFBOX-2965)
+        acroForm.getCOSObject().removeItem(COSName.FIELDS);
+        
+        // ensure there is always an empty collection returned
+        assertNotNull(acroForm.getFields());
+        assertEquals(acroForm.getFields().size(),0);
+
+        // there shouldn't be an exception if there is no such field
+        assertNull(acroForm.getField("foo"));
+    }
+
+    @After
+    public void tearDown() throws IOException
+    {
+        document.close();
+    }
+
+}
+

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

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



Mime
View raw message