Author: ssteiner
Date: Fri Mar  3 14:59:17 2017
New Revision: 1785316

URL: http://svn.apache.org/viewvc?rev=1785316&view=rev
Log:
FOP-2698: NPE when page resource missing

Modified:
    
xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxAdapter.java
    
xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/UniqueName.java
    
xmlgraphics/fop-pdf-images/trunk/test/java/org/apache/fop/render/pdf/PDFBoxAdapterTestCase.java

Modified: 
xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxAdapter.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxAdapter.java?rev=1785316&r1=1785315&r2=1785316&view=diff
==============================================================================
--- 
xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxAdapter.java
 (original)
+++ 
xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxAdapter.java
 Fri Mar  3 14:59:17 2017
@@ -348,10 +348,10 @@ public class PDFBoxAdapter {
         if (pageNumbers.containsKey(targetPage.getPageIndex())) {
             pageNumbers.get(targetPage.getPageIndex()).set(0, 
targetPage.makeReference());
         }
-        PDResources sourcePageResources = page.getResources();
+        COSDictionary sourcePageResources = getResources(page);
         PDStream pdStream = getContents(page);
 
-        COSDictionary fonts = 
(COSDictionary)sourcePageResources.getCOSObject().getDictionaryObject(COSName.FONT);
+        COSDictionary fonts = 
(COSDictionary)sourcePageResources.getDictionaryObject(COSName.FONT);
         COSDictionary fontsBackup = null;
         UniqueName uniqueName = new UniqueName(key, sourcePageResources);
         String newStream = null;
@@ -375,8 +375,8 @@ public class PDFBoxAdapter {
 
         }
         pdStream = new PDStream(sourceDoc, new 
ByteArrayInputStream(newStream.getBytes("ISO-8859-1")));
-        mergeXObj(sourcePageResources.getCOSObject(), fontinfo, uniqueName);
-        PDFDictionary pageResources = 
(PDFDictionary)cloneForNewDocument(sourcePageResources.getCOSObject());
+        mergeXObj(sourcePageResources, fontinfo, uniqueName);
+        PDFDictionary pageResources = 
(PDFDictionary)cloneForNewDocument(sourcePageResources);
 
         PDFDictionary fontDict = (PDFDictionary)pageResources.get("Font");
         if (fontDict != null && pdfDoc.isMergeFontsEnabled()) {
@@ -392,9 +392,9 @@ public class PDFBoxAdapter {
                 }
             }
         }
-        updateXObj(sourcePageResources.getCOSObject(), pageResources);
+        updateXObj(sourcePageResources, pageResources);
         if (fontsBackup != null) {
-            sourcePageResources.getCOSObject().setItem(COSName.FONT, 
fontsBackup);
+            sourcePageResources.setItem(COSName.FONT, fontsBackup);
         }
 
         COSStream originalPageContents = pdStream.getCOSObject();
@@ -462,6 +462,14 @@ public class PDFBoxAdapter {
         return pdStream;
     }
 
+    private COSDictionary getResources(PDPage page) {
+        PDResources res = page.getResources();
+        if (res == null) {
+            return new COSDictionary();
+        }
+        return res.getCOSObject();
+    }
+
     private void mergeXObj(COSDictionary sourcePageResources, FontInfo 
fontinfo, UniqueName uniqueName)
         throws IOException {
         COSDictionary xobj = (COSDictionary) 
sourcePageResources.getDictionaryObject(COSName.XOBJECT);
@@ -515,7 +523,7 @@ public class PDFBoxAdapter {
         }
     }
 
-    private void transferPageDict(COSDictionary fonts, UniqueName uniqueName, 
PDResources sourcePageResources)
+    private void transferPageDict(COSDictionary fonts, UniqueName uniqueName, 
COSDictionary sourcePageResources)
         throws IOException {
         if (fonts != null) {
             for (Map.Entry<COSName, COSBase> f : fonts.entrySet()) {
@@ -523,7 +531,7 @@ public class PDFBoxAdapter {
                 targetPage.getPDFResources().addFont(name, 
(PDFDictionary)cloneForNewDocument(f.getValue()));
             }
         }
-        for (Map.Entry<COSName, COSBase> e : 
sourcePageResources.getCOSObject().entrySet()) {
+        for (Map.Entry<COSName, COSBase> e : sourcePageResources.entrySet()) {
             transferDict(e, uniqueName);
         }
     }

Modified: 
xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/UniqueName.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/UniqueName.java?rev=1785316&r1=1785315&r2=1785316&view=diff
==============================================================================
--- 
xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/UniqueName.java
 (original)
+++ 
xmlgraphics/fop-pdf-images/trunk/src/java/org/apache/fop/render/pdf/pdfbox/UniqueName.java
 Fri Mar  3 14:59:17 2017
@@ -25,15 +25,14 @@ import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.cos.COSObject;
-import org.apache.pdfbox.pdmodel.PDResources;
 
 public class UniqueName {
     private String key;
     private List<COSName> resourceNames;
 
-    public UniqueName(String key, PDResources sourcePageResources) {
+    public UniqueName(String key, COSDictionary sourcePageResources) {
         this.key = Integer.toString(key.hashCode());
-        resourceNames = getResourceNames(sourcePageResources.getCOSObject());
+        resourceNames = getResourceNames(sourcePageResources);
     }
 
     protected String getName(COSName cn) {

Modified: 
xmlgraphics/fop-pdf-images/trunk/test/java/org/apache/fop/render/pdf/PDFBoxAdapterTestCase.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop-pdf-images/trunk/test/java/org/apache/fop/render/pdf/PDFBoxAdapterTestCase.java?rev=1785316&r1=1785315&r2=1785316&view=diff
==============================================================================
--- 
xmlgraphics/fop-pdf-images/trunk/test/java/org/apache/fop/render/pdf/PDFBoxAdapterTestCase.java
 (original)
+++ 
xmlgraphics/fop-pdf-images/trunk/test/java/org/apache/fop/render/pdf/PDFBoxAdapterTestCase.java
 Fri Mar  3 14:59:17 2017
@@ -460,4 +460,14 @@ public class PDFBoxAdapterTestCase {
         }
         Assert.assertTrue(msg.startsWith("Error on PDF page: " + ERROR));
     }
+
+    @Test
+    public void testNoPageResource() throws IOException {
+        PDDocument doc = PDDocument.load(new File(CFF1));
+        PDPage page = doc.getPage(0);
+        page.setResources(null);
+        AffineTransform at = new AffineTransform();
+        getPDFBoxAdapter(false).createStreamFromPDFBoxPage(doc, page, CFF1, 
at, new FontInfo(), new Rectangle());
+        doc.close();
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscr...@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-h...@xmlgraphics.apache.org

Reply via email to