Author: jahewson Date: Thu Mar 6 22:12:52 2014 New Revision: 1575070 URL: http://svn.apache.org/r1575070 Log: PDFBOX-1884: Avoid NPE in PDComplexFileSpecification
Added: pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/TestEmbeddedFiles.java (with props) pdfbox/branches/1.8/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/common/ pdfbox/branches/1.8/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/common/null_PDComplexFileSpecification.pdf (with props) Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/ExtractText.java pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/filespecification/PDComplexFileSpecification.java pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/TestAll.java Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/ExtractText.java URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/ExtractText.java?rev=1575070&r1=1575069&r2=1575070&view=diff ============================================================================== --- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/ExtractText.java (original) +++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/ExtractText.java Thu Mar 6 22:12:52 2014 @@ -292,7 +292,7 @@ public class ExtractText } PDComplexFileSpecification spec = (PDComplexFileSpecification) ent.getValue(); PDEmbeddedFile file = spec.getEmbeddedFile(); - if (file.getSubtype().equals("application/pdf")) + if (file != null && file.getSubtype().equals("application/pdf")) { if (debug) { Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/filespecification/PDComplexFileSpecification.java URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/filespecification/PDComplexFileSpecification.java?rev=1575070&r1=1575069&r2=1575070&view=diff ============================================================================== --- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/filespecification/PDComplexFileSpecification.java (original) +++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/filespecification/PDComplexFileSpecification.java Thu Mar 6 22:12:52 2014 @@ -41,13 +41,21 @@ public class PDComplexFileSpecification } /** - * Constructor. + * Constructor. Creates empty COSDictionary if dict is null. * * @param dict The dictionary that fulfils this file specification. */ public PDComplexFileSpecification( COSDictionary dict ) { - fs = dict; + if (dict == null) + { + fs = new COSDictionary(); + fs.setItem( COSName.TYPE, COSName.FILESPEC ); + } + else + { + fs = dict; + } } /** Modified: pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/TestAll.java URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/TestAll.java?rev=1575070&r1=1575069&r2=1575070&view=diff ============================================================================== --- pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/TestAll.java (original) +++ pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/TestAll.java Thu Mar 6 22:12:52 2014 @@ -31,6 +31,7 @@ import org.apache.pdfbox.io.ccitt.TestPa import org.apache.pdfbox.pdmodel.TestFDF; import org.apache.pdfbox.pdmodel.TestPDDocument; import org.apache.pdfbox.pdmodel.TestPDDocumentCatalog; +import org.apache.pdfbox.pdmodel.common.TestEmbeddedFiles; import org.apache.pdfbox.pdmodel.TestPDDocumentInformation; import org.apache.pdfbox.pdmodel.common.TestPDNameTreeNode; import org.apache.pdfbox.pdmodel.common.TestPDNumberTreeNode; @@ -89,6 +90,7 @@ public class TestAll extends TestCase suite.addTestSuite( TestPDDocument.class ); suite.addTestSuite( TestPDDocumentCatalog.class ); suite.addTestSuite( TestPDDocumentInformation.class ); + suite.addTestSuite( TestEmbeddedFiles.class ); suite.addTestSuite( org.apache.pdfbox.pdmodel.graphics.optionalcontent.TestOptionalContentGroups.class ); suite.addTestSuite( org.apache.pdfbox.util.TestLayerUtility.class ); suite.addTestSuite( org.apache.pdfbox.TestTextToPdf.class ); Added: pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/TestEmbeddedFiles.java URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/TestEmbeddedFiles.java?rev=1575070&view=auto ============================================================================== --- pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/TestEmbeddedFiles.java (added) +++ pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/TestEmbeddedFiles.java Thu Mar 6 22:12:52 2014 @@ -0,0 +1,74 @@ +/* + * 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.common; + +import java.io.IOException; +import java.util.Map; + +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDDocumentCatalog; +import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary; +import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode; +import org.apache.pdfbox.pdmodel.TestPDDocumentCatalog; +import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification; +import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile; +import org.junit.Test; + +import junit.framework.TestCase; + +public class TestEmbeddedFiles extends TestCase +{ + @Test + public void testNullEmbeddedFile() throws IOException + { + PDEmbeddedFile embeddedFile = null; + boolean ok = false; + try + { + PDDocument doc = PDDocument.load(TestEmbeddedFiles.class.getResourceAsStream( + "null_PDComplexFileSpecification.pdf")); + + PDDocumentCatalog catalog = doc.getDocumentCatalog(); + PDDocumentNameDictionary names = catalog.getNames(); + assertEquals("expected two files", 2, names.getEmbeddedFiles().getNames().size()); + PDEmbeddedFilesNameTreeNode embeddedFiles = names.getEmbeddedFiles(); + + PDComplexFileSpecification spec = (PDComplexFileSpecification) + embeddedFiles.getNames().get("non-existent-file.docx"); + + if (spec != null) + { + embeddedFile = spec.getEmbeddedFile(); + ok = true; + } + //now test for actual attachment + spec = (PDComplexFileSpecification)embeddedFiles.getNames().get("My first attachment"); + assertNotNull("one attachment actually exists", spec); + assertEquals("existing file length", 17660, spec.getEmbeddedFile().getLength()); + spec = (PDComplexFileSpecification)embeddedFiles + .getNames().get("non-existent-file.docx"); + } + catch (NullPointerException e) + { + assertNotNull("null pointer exception", null); + } + assertTrue("Was able to get file without exception", ok); + assertNull("EmbeddedFile was correctly null", embeddedFile); + } + +} + Propchange: pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/TestEmbeddedFiles.java ------------------------------------------------------------------------------ svn:eol-style = native Added: pdfbox/branches/1.8/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/common/null_PDComplexFileSpecification.pdf URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/common/null_PDComplexFileSpecification.pdf?rev=1575070&view=auto ============================================================================== Binary file - no diff available. Propchange: pdfbox/branches/1.8/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/common/null_PDComplexFileSpecification.pdf ------------------------------------------------------------------------------ svn:mime-type = application/pdf