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:54:09 UTC
svn commit: r1613472 - in /pdfbox/branches/1.8:
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/r...
Author: lehmi
Date: Fri Jul 25 16:54:08 2014
New Revision: 1613472
URL: http://svn.apache.org/r1613472
Log:
PDFBOX-2239: add missing values to PDComplexFileSpecification, fix for some setters, improved unit test (based on a proposal by Tim Allison)
Added:
pdfbox/branches/1.8/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/common/testPDF_multiFormatEmbFiles.pdf (with props)
Modified:
pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.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/pdmodel/common/TestEmbeddedFiles.java
Modified: pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java?rev=1613472&r1=1613471&r2=1613472&view=diff
==============================================================================
--- pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java (original)
+++ pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java Fri Jul 25 16:54:08 2014
@@ -114,7 +114,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);
@@ -123,6 +123,34 @@ 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/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=1613472&r1=1613471&r2=1613472&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 Fri Jul 25 16:54:08 2014
@@ -30,6 +30,7 @@ import org.apache.pdfbox.cos.COSStream;
public class PDComplexFileSpecification extends PDFileSpecification
{
private COSDictionary fs;
+ private COSDictionary efDictionary;
/**
* Default Constructor.
@@ -68,6 +69,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;
+ }
+
/**
* Convert this standard java object to a COS object.
*
@@ -89,32 +109,32 @@ 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;
}
/**
* This will get the unicode file name.
*
* @return The file name.
+ *
+ * @deprecated use {@link #getFileUnicode()} instead
*/
public String getUnicodeFile()
{
@@ -122,6 +142,26 @@ public class PDComplexFileSpecification
}
/**
+ * This will get the unicode file name.
+ *
+ * @return The file name.
+ */
+ 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 +270,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 +285,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 +300,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 +320,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 +335,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 +355,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 +375,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 +390,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();
@@ -361,7 +401,43 @@ public class PDComplexFileSpecification
ef.setItem( COSName.UNIX, file );
}
}
-
+
+ /**
+ * 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.
*
Modified: 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=1613472&r1=1613471&r2=1613472&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/TestEmbeddedFiles.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/TestEmbeddedFiles.java Fri Jul 25 16:54:08 2014
@@ -17,13 +17,14 @@
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;
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;
@@ -70,5 +71,52 @@ 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/branches/1.8/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/common/testPDF_multiFormatEmbFiles.pdf
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/common/testPDF_multiFormatEmbFiles.pdf?rev=1613472&view=auto
==============================================================================
Binary file - no diff available.
Propchange: pdfbox/branches/1.8/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/common/testPDF_multiFormatEmbFiles.pdf
------------------------------------------------------------------------------
svn:mime-type = application/pdf