You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2014/07/25 18:35:06 UTC
svn commit: r1613465 - in /pdfbox/trunk:
examples/src/main/java/org/apache/pdfbox/examples/pdmodel/
pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/filespecification/
pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/
pdfbox/src/test/resource...
Author: lehmi
Date: Fri Jul 25 16:35:06 2014
New Revision: 1613465
URL: http://svn.apache.org/r1613465
Log:
PDFBOX-2239: add missing values to PDComplexFileSpecification, fix for some setters, improved unit test (based on a proposal by Tim Allison)
Added:
pdfbox/trunk/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/common/testPDF_multiFormatEmbFiles.pdf (with props)
Modified:
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/filespecification/PDComplexFileSpecification.java
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/TestEmbeddedFiles.java
Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java?rev=1613465&r1=1613464&r2=1613465&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java Fri Jul 25 16:35:06 2014
@@ -117,7 +117,7 @@ public class ExtractEmbeddedFiles
for (String filename : names.keySet())
{
PDComplexFileSpecification fileSpec = (PDComplexFileSpecification)names.get(filename);
- PDEmbeddedFile embeddedFile = fileSpec.getEmbeddedFile();
+ PDEmbeddedFile embeddedFile = getEmbeddedFile(fileSpec);
String embeddedFilename = filePath+filename;
File file = new File(filePath+filename);
System.out.println("Writing "+ embeddedFilename);
@@ -126,6 +126,33 @@ public class ExtractEmbeddedFiles
fos.close();
}
}
+
+ private static PDEmbeddedFile getEmbeddedFile(PDComplexFileSpecification fileSpec )
+ {
+ // search for the first available alternative of the embedded file
+ PDEmbeddedFile embeddedFile = null;
+ if (fileSpec != null)
+ {
+ embeddedFile = fileSpec.getEmbeddedFileUnicode();
+ if (embeddedFile == null)
+ {
+ embeddedFile = fileSpec.getEmbeddedFileDos();
+ }
+ if (embeddedFile == null)
+ {
+ embeddedFile = fileSpec.getEmbeddedFileMac();
+ }
+ if (embeddedFile == null)
+ {
+ embeddedFile = fileSpec.getEmbeddedFileUnix();
+ }
+ if (embeddedFile == null)
+ {
+ embeddedFile = fileSpec.getEmbeddedFile();
+ }
+ }
+ return embeddedFile;
+ }
/**
* This will print the usage for this program.
*/
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/filespecification/PDComplexFileSpecification.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/filespecification/PDComplexFileSpecification.java?rev=1613465&r1=1613464&r2=1613465&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/filespecification/PDComplexFileSpecification.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/filespecification/PDComplexFileSpecification.java Fri Jul 25 16:35:06 2014
@@ -30,6 +30,7 @@ import org.apache.pdfbox.cos.COSStream;
public class PDComplexFileSpecification extends PDFileSpecification
{
private COSDictionary fs;
+ private COSDictionary efDictionary;
/**
* Default Constructor.
@@ -78,6 +79,25 @@ public class PDComplexFileSpecification
return fs;
}
+ private COSDictionary getEFDictionary()
+ {
+ if (efDictionary == null && fs != null)
+ {
+ efDictionary = (COSDictionary)fs.getDictionaryObject( COSName.EF );
+ }
+ return efDictionary;
+ }
+
+ private COSBase getObjectFromEFDictionary(COSName key)
+ {
+ COSDictionary ef = getEFDictionary();
+ if (ef != null)
+ {
+ return ef.getDictionaryObject(key);
+ }
+ return null;
+ }
+
/**
* <p>Preferred method for getting the filename.
* It will determinate the recommended file name.</p>
@@ -89,26 +109,24 @@ public class PDComplexFileSpecification
*/
public String getFilename()
{
- if (getUnicodeFile() != null)
- {
- return getUnicodeFile();
- }
- else if (getFileDos() != null)
+ String filename = getFileUnicode();
+ if (filename == null)
{
- return getFileDos();
+ filename = getFileDos();
}
- else if (getFileMac() != null)
+ if (filename == null)
{
- return getFileMac();
+ filename = getFileMac();
}
- else if (getFileUnix() != null)
+ if (filename == null)
{
- return getFileUnix();
+ filename = getFileUnix();
}
- else
+ if (filename == null)
{
- return getFile();
+ filename = getFile();
}
+ return filename;
}
/**
@@ -116,12 +134,22 @@ public class PDComplexFileSpecification
*
* @return The file name.
*/
- public String getUnicodeFile()
+ public String getFileUnicode()
{
return fs.getString(COSName.UF);
}
/**
+ * This will set unicode file name.
+ *
+ * @param file The name of the file.
+ */
+ public void setFileUnicode( String file )
+ {
+ fs.setString( COSName.UF, file );
+ }
+
+ /**
* This will get the file name.
*
* @return The file name.
@@ -230,7 +258,7 @@ public class PDComplexFileSpecification
public PDEmbeddedFile getEmbeddedFile()
{
PDEmbeddedFile file = null;
- COSStream stream = (COSStream)fs.getObjectFromPath( "EF/F" );
+ COSStream stream = (COSStream)getObjectFromEFDictionary(COSName.F);
if( stream != null )
{
file = new PDEmbeddedFile( stream );
@@ -245,7 +273,7 @@ public class PDComplexFileSpecification
*/
public void setEmbeddedFile( PDEmbeddedFile file )
{
- COSDictionary ef = (COSDictionary)fs.getDictionaryObject( COSName.EF );
+ COSDictionary ef = getEFDictionary();
if( ef == null && file != null )
{
ef = new COSDictionary();
@@ -260,12 +288,12 @@ public class PDComplexFileSpecification
/**
* Get the embedded dos file.
*
- * @return The embedded file for this file spec.
+ * @return The embedded dos file for this file spec.
*/
public PDEmbeddedFile getEmbeddedFileDos()
{
PDEmbeddedFile file = null;
- COSStream stream = (COSStream)fs.getObjectFromPath( "EF/DOS" );
+ COSStream stream = (COSStream)getObjectFromEFDictionary( COSName.DOS );
if( stream != null )
{
file = new PDEmbeddedFile( stream );
@@ -280,7 +308,7 @@ public class PDComplexFileSpecification
*/
public void setEmbeddedFileDos( PDEmbeddedFile file )
{
- COSDictionary ef = (COSDictionary)fs.getDictionaryObject( COSName.DOS );
+ COSDictionary ef = getEFDictionary();
if( ef == null && file != null )
{
ef = new COSDictionary();
@@ -295,12 +323,12 @@ public class PDComplexFileSpecification
/**
* Get the embedded Mac file.
*
- * @return The embedded file for this file spec.
+ * @return The embedded Mac file for this file spec.
*/
public PDEmbeddedFile getEmbeddedFileMac()
{
PDEmbeddedFile file = null;
- COSStream stream = (COSStream)fs.getObjectFromPath( "EF/Mac" );
+ COSStream stream = (COSStream)getObjectFromEFDictionary( COSName.MAC );
if( stream != null )
{
file = new PDEmbeddedFile( stream );
@@ -315,7 +343,7 @@ public class PDComplexFileSpecification
*/
public void setEmbeddedFileMac( PDEmbeddedFile file )
{
- COSDictionary ef = (COSDictionary)fs.getDictionaryObject( COSName.MAC );
+ COSDictionary ef = getEFDictionary();
if( ef == null && file != null )
{
ef = new COSDictionary();
@@ -335,7 +363,7 @@ public class PDComplexFileSpecification
public PDEmbeddedFile getEmbeddedFileUnix()
{
PDEmbeddedFile file = null;
- COSStream stream = (COSStream)fs.getObjectFromPath( "EF/Unix" );
+ COSStream stream = (COSStream)getObjectFromEFDictionary( COSName.UNIX );
if( stream != null )
{
file = new PDEmbeddedFile( stream );
@@ -350,7 +378,7 @@ public class PDComplexFileSpecification
*/
public void setEmbeddedFileUnix( PDEmbeddedFile file )
{
- COSDictionary ef = (COSDictionary)fs.getDictionaryObject( COSName.UNIX );
+ COSDictionary ef = getEFDictionary();
if( ef == null && file != null )
{
ef = new COSDictionary();
@@ -363,6 +391,41 @@ public class PDComplexFileSpecification
}
/**
+ * Get the embedded unicode file.
+ *
+ * @return The embedded unicode file for this file spec.
+ */
+ public PDEmbeddedFile getEmbeddedFileUnicode()
+ {
+ PDEmbeddedFile file = null;
+ COSStream stream = (COSStream)getObjectFromEFDictionary( COSName.UF );
+ if( stream != null )
+ {
+ file = new PDEmbeddedFile( stream );
+ }
+ return file;
+ }
+
+ /**
+ * Set the embedded Unicode file for this spec.
+ *
+ * @param file The Unicode file to be embedded.
+ */
+ public void setEmbeddedFileUnicode( PDEmbeddedFile file )
+ {
+ COSDictionary ef = getEFDictionary();
+ if( ef == null && file != null )
+ {
+ ef = new COSDictionary();
+ fs.setItem( COSName.EF, ef );
+ }
+ if( ef != null )
+ {
+ ef.setItem( COSName.UF, file );
+ }
+ }
+
+ /**
* Set the file description.
*
* @param description The file description
Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/TestEmbeddedFiles.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/TestEmbeddedFiles.java?rev=1613465&r1=1613464&r2=1613465&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/TestEmbeddedFiles.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/TestEmbeddedFiles.java Fri Jul 25 16:35:06 2014
@@ -17,6 +17,9 @@
package org.apache.pdfbox.pdmodel.common;
import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.util.List;
+import java.util.Map;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
@@ -68,5 +71,51 @@ public class TestEmbeddedFiles extends T
assertNull("EmbeddedFile was correctly null", embeddedFile);
}
-}
+ @Test
+ public void testOSSpecificAttachments() throws IOException
+ {
+ PDEmbeddedFile nonOSFile = null;
+ PDEmbeddedFile macFile = null;
+ PDEmbeddedFile dosFile = null;
+ PDEmbeddedFile unixFile = null;
+
+ PDDocument doc = PDDocument.load(TestEmbeddedFiles.class
+ .getResourceAsStream("testPDF_multiFormatEmbFiles.pdf"));
+
+ PDDocumentCatalog catalog = doc.getDocumentCatalog();
+ PDDocumentNameDictionary names = catalog.getNames();
+ PDEmbeddedFilesNameTreeNode treeNode = names.getEmbeddedFiles();
+ List<PDNameTreeNode> kids = treeNode.getKids();
+ for (PDNameTreeNode kid : kids)
+ {
+ Map<String, COSObjectable> tmpNames = kid.getNames();
+ COSObjectable obj = tmpNames.get("My first attachment");
+ if (obj instanceof PDComplexFileSpecification)
+ {
+ PDComplexFileSpecification spec = (PDComplexFileSpecification) obj;
+ nonOSFile = spec.getEmbeddedFile();
+ macFile = spec.getEmbeddedFileMac();
+ dosFile = spec.getEmbeddedFileDos();
+ unixFile = spec.getEmbeddedFileUnix();
+ }
+ }
+
+ assertTrue("non os specific",
+ byteArrayContainsLC("non os specific", nonOSFile.getByteArray(), "ISO-8859-1"));
+ assertTrue("mac", byteArrayContainsLC("mac embedded", macFile.getByteArray(), "ISO-8859-1"));
+
+ assertTrue("dos", byteArrayContainsLC("dos embedded", dosFile.getByteArray(), "ISO-8859-1"));
+
+ assertTrue("unix",
+ byteArrayContainsLC("unix embedded", unixFile.getByteArray(), "ISO-8859-1"));
+
+ }
+
+ private boolean byteArrayContainsLC(String target, byte[] bytes, String encoding)
+ throws UnsupportedEncodingException
+ {
+ String s = new String(bytes, encoding);
+ return s.toLowerCase().contains(target);
+ }
+}
Added: pdfbox/trunk/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/common/testPDF_multiFormatEmbFiles.pdf
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/common/testPDF_multiFormatEmbFiles.pdf?rev=1613465&view=auto
==============================================================================
Binary file - no diff available.
Propchange: pdfbox/trunk/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/common/testPDF_multiFormatEmbFiles.pdf
------------------------------------------------------------------------------
svn:mime-type = application/pdf