You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ms...@apache.org on 2014/10/28 00:00:37 UTC
svn commit: r1634727 [3/3] - in /pdfbox/trunk/pdfbox/src:
main/java/org/apache/pdfbox/cos/ main/java/org/apache/pdfbox/io/
main/java/org/apache/pdfbox/pdfparser/ main/java/org/apache/pdfbox/pdmodel/
main/java/org/apache/pdfbox/pdmodel/common/ main/java...
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/PushBackInputStream.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/PushBackInputStream.java?rev=1634727&r1=1634726&r2=1634727&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/PushBackInputStream.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/PushBackInputStream.java Mon Oct 27 23:00:36 2014
@@ -53,8 +53,7 @@ public class PushBackInputStream extends
throw new IOException( "Error: input was null" );
}
- raInput = ( input instanceof RandomAccessRead ) ?
- (RandomAccessRead) input : null;
+ raInput = ( input instanceof RandomAccessRead ) ? (RandomAccessRead) input : null;
}
/**
@@ -219,19 +218,21 @@ public class PushBackInputStream extends
*/
public void seek( long newOffset ) throws IOException
{
- if ( raInput == null )
- throw new IOException( "Provided stream of type " + in.getClass().getSimpleName() +
- " is not seekable." );
-
- // clear unread buffer by skipping over all bytes of buffer
- int unreadLength = buf.length - pos;
- if ( unreadLength > 0 )
- {
- skip( unreadLength );
- }
-
- raInput.seek( newOffset );
- offset = newOffset;
+ if ( raInput == null )
+ {
+ throw new IOException( "Provided stream of type " + in.getClass().getSimpleName() +
+ " is not seekable." );
+ }
+
+ // clear unread buffer by skipping over all bytes of buffer
+ int unreadLength = buf.length - pos;
+ if ( unreadLength > 0 )
+ {
+ skip( unreadLength );
+ }
+
+ raInput.seek( newOffset );
+ offset = newOffset;
}
}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/NonSequentialPDFParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/NonSequentialPDFParser.java?rev=1634727&r1=1634726&r2=1634727&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/NonSequentialPDFParser.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/NonSequentialPDFParser.java Mon Oct 27 23:00:36 2014
@@ -83,11 +83,24 @@ public class NonSequentialPDFParser exte
private static final int X = 'x';
- public static final String SYSPROP_PARSEMINIMAL = "org.apache.pdfbox.pdfparser.nonSequentialPDFParser.parseMinimal";
- public static final String SYSPROP_EOFLOOKUPRANGE = "org.apache.pdfbox.pdfparser.nonSequentialPDFParser.eofLookupRange";
+ /**
+ * Only parse the PDF file minimally allowing access to basic information.
+ */
+ public static final String SYSPROP_PARSEMINIMAL =
+ "org.apache.pdfbox.pdfparser.nonSequentialPDFParser.parseMinimal";
+
+ /**
+ * The range within the %%EOF marker will be searched.
+ * Useful if there are additional characters after %%EOF within the PDF.
+ */
+ public static final String SYSPROP_EOFLOOKUPRANGE =
+ "org.apache.pdfbox.pdfparser.nonSequentialPDFParser.eofLookupRange";
private static final InputStream EMPTY_INPUT_STREAM = new ByteArrayInputStream(new byte[0]);
+ /**
+ * How many trailing bytes to read for EOF marker.
+ */
protected static final int DEFAULT_TRAIL_BYTECOUNT = 2048;
/**
* EOF-marker.
@@ -113,7 +126,7 @@ public class NonSequentialPDFParser exte
private boolean isLenient = true;
/**
- * Contains all found objects of a brute force search
+ * Contains all found objects of a brute force search.
*/
private HashMap<String, Long> bfSearchObjectOffsets = null;
private Vector<Long> bfSearchXRefOffsets = null;
@@ -148,6 +161,9 @@ public class NonSequentialPDFParser exte
*/
private boolean isTmpPDFFile = false;
+ /**
+ * The prefix for the temp file being used.
+ */
public static final String TMP_FILE_PREFIX = "tmpPDF";
// ------------------------------------------------------------------------
@@ -166,7 +182,8 @@ public class NonSequentialPDFParser exte
/**
* Constructs parser for given file using memory buffer.
*
- * @param filename the filename of the pdf to be parsed
+ * @param filename the filename of the pdf to be parsed.
+ * @param useScratchFiles use a buffer for temporary storage.
*
* @throws IOException If something went wrong.
*/
@@ -193,6 +210,7 @@ public class NonSequentialPDFParser exte
* storage.
*
* @param file the pdf to be parsed
+ * @param useScratchFiles use a buffer for temporary storage.
*
* @throws IOException If something went wrong.
*/
@@ -218,8 +236,9 @@ public class NonSequentialPDFParser exte
/**
* Constructs parser for given file using given buffer for temporary storage.
*
- * @param file the pdf to be parsed
- * @param decryptionPassword password to be used for decryption
+ * @param file the pdf to be parsed.
+ * @param decryptionPassword password to be used for decryption.
+ * @param useScratchFiles use a buffer for temporary storage.
*
* @throws IOException If something went wrong.
*/
@@ -267,6 +286,8 @@ public class NonSequentialPDFParser exte
* Constructor.
*
* @param input input stream representing the pdf.
+ * @param useScratchFiles use a buffer for temporary storage.
+ *
* @throws IOException If something went wrong.
*/
public NonSequentialPDFParser(InputStream input, boolean useScratchFiles) throws IOException
@@ -292,6 +313,8 @@ public class NonSequentialPDFParser exte
*
* @param input input stream representing the pdf.
* @param decryptionPassword password to be used for decryption.
+ * @param useScratchFiles use a buffer for temporary storage.
+ *
* @throws IOException If something went wrong.
*/
public NonSequentialPDFParser(InputStream input, String decryptionPassword, boolean useScratchFiles)
@@ -372,8 +395,8 @@ public class NonSequentialPDFParser exte
long fixedOffset = checkXRefOffset(startXrefOffset);
if (fixedOffset > -1)
{
- startXrefOffset = fixedOffset;
- document.setStartXref(startXrefOffset);
+ startXrefOffset = fixedOffset;
+ document.setStartXref(startXrefOffset);
}
long prev = startXrefOffset;
// ---- parse whole chain of xref tables/object streams using PREV
@@ -418,7 +441,7 @@ public class NonSequentialPDFParser exte
fixedOffset = checkXRefOffset(streamOffset);
if (fixedOffset > -1 && fixedOffset != streamOffset)
{
- streamOffset = (int)fixedOffset;
+ streamOffset = (int)fixedOffset;
trailer.setInt(COSName.XREF_STM, streamOffset);
}
setPdfSource(streamOffset);
@@ -872,7 +895,7 @@ public class NonSequentialPDFParser exte
*
* This method can only be called before the parsing of the file.
*
- * @param lenient
+ * @param lenient try to handle malformed PDFs.
*
* @throws IllegalArgumentException if the method is called after parsing.
*/
@@ -1551,24 +1574,24 @@ public class NonSequentialPDFParser exte
// data, so just read those first
while (whitespace == 0x20)
{
- whitespace = pdfSource.read();
+ whitespace = pdfSource.read();
}
if (whitespace == 0x0D)
{
- whitespace = pdfSource.read();
- if (whitespace != 0x0A)
- {
- // the spec says this is invalid but it happens in the
- // real world so we must support it
- pdfSource.unread(whitespace);
- }
+ whitespace = pdfSource.read();
+ if (whitespace != 0x0A)
+ {
+ // the spec says this is invalid but it happens in the
+ // real world so we must support it
+ pdfSource.unread(whitespace);
+ }
}
else if (whitespace != 0x0A)
{
- // no whitespace after 'stream'; PDF ref. says 'should' so
- // that is ok
- pdfSource.unread(whitespace);
+ // no whitespace after 'stream'; PDF ref. says 'should' so
+ // that is ok
+ pdfSource.unread(whitespace);
}
/*
@@ -1683,11 +1706,11 @@ public class NonSequentialPDFParser exte
*/
private long checkXRefOffset(long startXRefOffset) throws IOException
{
- // repair mode isn't available in non-lenient mode
- if (!isLenient)
- {
- return startXRefOffset;
- }
+ // repair mode isn't available in non-lenient mode
+ if (!isLenient)
+ {
+ return startXRefOffset;
+ }
setPdfSource(startXRefOffset);
if (pdfSource.peek() == X && checkBytesAtOffset(XREF_TABLE))
{
@@ -1709,8 +1732,8 @@ public class NonSequentialPDFParser exte
}
catch (IOException exception)
{
- // there wasn't an object of a xref stream
- // try to repair the offset
+ // there wasn't an object of a xref stream
+ // try to repair the offset
pdfSource.seek(startXRefOffset);
}
}
@@ -1784,11 +1807,11 @@ public class NonSequentialPDFParser exte
*/
private void checkXrefOffsets() throws IOException
{
- // repair mode isn't available in non-lenient mode
- if (!isLenient)
- {
- return;
- }
+ // repair mode isn't available in non-lenient mode
+ if (!isLenient)
+ {
+ return;
+ }
Map<COSObjectKey, Long> xrefOffset = xrefTrailerResolver.getXrefTable();
if (xrefOffset != null)
{
@@ -1950,30 +1973,31 @@ public class NonSequentialPDFParser exte
*/
private long bfSearchForXRef(long xrefOffset) throws IOException
{
- long newOffset = -1;
- bfSearchForXRefs();
- if (bfSearchXRefOffsets != null)
- {
- long currentDifference = -1;
- int currentOffsetIndex = -1;
- int numberOfOffsets = bfSearchXRefOffsets.size();
- // find the most likely value
- // TODO to be optimized, this won't work in every case
- for (int i=0; i<numberOfOffsets; i++)
- {
- long newDifference = xrefOffset - bfSearchXRefOffsets.get(i);
- // find the nearest offset
- if (currentDifference == -1 || (Math.abs(currentDifference) > Math.abs(newDifference)))
- {
- currentDifference = newDifference;
- currentOffsetIndex = i;
- }
- }
- if (currentOffsetIndex > -1)
- {
- newOffset = bfSearchXRefOffsets.remove(currentOffsetIndex);
- }
- }
+ long newOffset = -1;
+ bfSearchForXRefs();
+ if (bfSearchXRefOffsets != null)
+ {
+ long currentDifference = -1;
+ int currentOffsetIndex = -1;
+ int numberOfOffsets = bfSearchXRefOffsets.size();
+ // find the most likely value
+ // TODO to be optimized, this won't work in every case
+ for (int i = 0; i < numberOfOffsets; i++)
+ {
+ long newDifference = xrefOffset - bfSearchXRefOffsets.get(i);
+ // find the nearest offset
+ if (currentDifference == -1
+ || (Math.abs(currentDifference) > Math.abs(newDifference)))
+ {
+ currentDifference = newDifference;
+ currentOffsetIndex = i;
+ }
+ }
+ if (currentOffsetIndex > -1)
+ {
+ newOffset = bfSearchXRefOffsets.remove(currentOffsetIndex);
+ }
+ }
return newOffset;
}
@@ -1984,98 +2008,100 @@ public class NonSequentialPDFParser exte
*/
private void bfSearchForXRefs() throws IOException
{
- if (bfSearchXRefOffsets == null)
- {
+ if (bfSearchXRefOffsets == null)
+ {
// a pdf may contain more than one xref entry
- bfSearchXRefOffsets = new Vector<Long>();
- long originOffset = pdfSource.getOffset();
- pdfSource.seek(MINIMUM_SEARCH_OFFSET);
- // search for xref tables
- while(!pdfSource.isEOF())
- {
- if (checkBytesAtOffset(XREF_TABLE))
- {
- long newOffset = pdfSource.getOffset();
- pdfSource.seek(newOffset-1);
- // ensure that we don't read "startxref" instead of "xref"
- if (isWhitespace())
- {
- bfSearchXRefOffsets.add(newOffset);
- }
- pdfSource.seek(newOffset+4);
- }
- pdfSource.read();
- }
- pdfSource.seek(MINIMUM_SEARCH_OFFSET);
- // search for XRef streams
+ bfSearchXRefOffsets = new Vector<Long>();
+ long originOffset = pdfSource.getOffset();
+ pdfSource.seek(MINIMUM_SEARCH_OFFSET);
+ // search for xref tables
+ while (!pdfSource.isEOF())
+ {
+ if (checkBytesAtOffset(XREF_TABLE))
+ {
+ long newOffset = pdfSource.getOffset();
+ pdfSource.seek(newOffset - 1);
+ // ensure that we don't read "startxref" instead of "xref"
+ if (isWhitespace())
+ {
+ bfSearchXRefOffsets.add(newOffset);
+ }
+ pdfSource.seek(newOffset + 4);
+ }
+ pdfSource.read();
+ }
+ pdfSource.seek(MINIMUM_SEARCH_OFFSET);
+ // search for XRef streams
String objString = " obj";
byte[] string = objString.getBytes("ISO-8859-1");
- while(!pdfSource.isEOF())
- {
- if (checkBytesAtOffset(XREF_STREAM))
- {
- // search backwards for the beginning of the stream
- long newOffset = -1;
- long xrefOffset = pdfSource.getOffset();
- long currentOffset = xrefOffset;
- boolean objFound = false;
- for (int i=1; i<30 && !objFound;i++)
- {
- currentOffset = xrefOffset - (i*10);
- if (currentOffset > 0)
- {
- pdfSource.seek(currentOffset);
- for (int j=0; j<10;j++)
- {
- if (checkBytesAtOffset(string))
- {
- long tempOffset = currentOffset - 1;
- pdfSource.seek(tempOffset);
- int genID = pdfSource.peek();
- // is the next char a digit?
- if (genID > 47 && genID < 58)
- {
- genID -= 48;
- tempOffset--;
- pdfSource.seek(tempOffset);
- if (pdfSource.peek() == 32)
- {
- int length = 0;
- pdfSource.seek(--tempOffset);
- while (tempOffset > MINIMUM_SEARCH_OFFSET && pdfSource.peek() > 47
- && pdfSource.peek() < 58)
- {
- pdfSource.seek(--tempOffset);
- length++;
- }
- if (length > 0)
- {
- pdfSource.read();
- newOffset = pdfSource.getOffset();
- }
- }
- }
- LOG.debug("Fixed reference for xref stream "+xrefOffset + " -> "+newOffset);
- objFound = true;
- break;
- }
- else
- {
- currentOffset++;
- pdfSource.read();
- }
- }
- }
- }
- if (newOffset > -1)
- {
- bfSearchXRefOffsets.add(newOffset);
- }
- pdfSource.seek(xrefOffset+5);
- }
- pdfSource.read();
- }
- pdfSource.seek(originOffset);
- }
+ while (!pdfSource.isEOF())
+ {
+ if (checkBytesAtOffset(XREF_STREAM))
+ {
+ // search backwards for the beginning of the stream
+ long newOffset = -1;
+ long xrefOffset = pdfSource.getOffset();
+ long currentOffset = xrefOffset;
+ boolean objFound = false;
+ for (int i = 1; i < 30 && !objFound; i++)
+ {
+ currentOffset = xrefOffset - (i * 10);
+ if (currentOffset > 0)
+ {
+ pdfSource.seek(currentOffset);
+ for (int j = 0; j < 10; j++)
+ {
+ if (checkBytesAtOffset(string))
+ {
+ long tempOffset = currentOffset - 1;
+ pdfSource.seek(tempOffset);
+ int genID = pdfSource.peek();
+ // is the next char a digit?
+ if (genID > 47 && genID < 58)
+ {
+ genID -= 48;
+ tempOffset--;
+ pdfSource.seek(tempOffset);
+ if (pdfSource.peek() == 32)
+ {
+ int length = 0;
+ pdfSource.seek(--tempOffset);
+ while (tempOffset > MINIMUM_SEARCH_OFFSET
+ && pdfSource.peek() > 47
+ && pdfSource.peek() < 58)
+ {
+ pdfSource.seek(--tempOffset);
+ length++;
+ }
+ if (length > 0)
+ {
+ pdfSource.read();
+ newOffset = pdfSource.getOffset();
+ }
+ }
+ }
+ LOG.debug("Fixed reference for xref stream " + xrefOffset
+ + " -> " + newOffset);
+ objFound = true;
+ break;
+ }
+ else
+ {
+ currentOffset++;
+ pdfSource.read();
+ }
+ }
+ }
+ }
+ if (newOffset > -1)
+ {
+ bfSearchXRefOffsets.add(newOffset);
+ }
+ pdfSource.seek(xrefOffset + 5);
+ }
+ pdfSource.read();
+ }
+ pdfSource.seek(originOffset);
+ }
}
}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java?rev=1634727&r1=1634726&r2=1634727&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java Mon Oct 27 23:00:36 2014
@@ -69,7 +69,7 @@ public class PDFParser extends BaseParse
private List<ConflictObj> conflictList = new ArrayList<ConflictObj>();
/**
- * COSStream objects to check for length correctness
+ * COSStream objects to check for length correctness.
*/
private final HashSet<COSStream> streamLengthCheckSet = new HashSet<COSStream>();
@@ -1065,16 +1065,16 @@ public class PDFParser extends BaseParse
*/
@Override
public void clearResources() {
- super.clearResources();
- if (conflictList != null)
- {
- conflictList.clear();
- conflictList = null;
- }
- if (xrefTrailerResolver != null)
- {
- xrefTrailerResolver.clearResources();
- xrefTrailerResolver = null;
- }
+ super.clearResources();
+ if (conflictList != null)
+ {
+ conflictList.clear();
+ conflictList = null;
+ }
+ if (xrefTrailerResolver != null)
+ {
+ xrefTrailerResolver.clearResources();
+ xrefTrailerResolver = null;
+ }
}
}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java?rev=1634727&r1=1634726&r2=1634727&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java Mon Oct 27 23:00:36 2014
@@ -532,10 +532,10 @@ public class PDPage implements COSObject
for (int i = 0; i < annots.size(); i++)
{
COSBase item = annots.getObject(i);
- if (item == null)
- {
- continue;
- }
+ if (item == null)
+ {
+ continue;
+ }
actuals.add(PDAnnotation.createAnnotation(item));
}
retval = new COSArrayList<PDAnnotation>(actuals, annots);
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDMetadata.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDMetadata.java?rev=1634727&r1=1634726&r2=1634727&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDMetadata.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDMetadata.java Mon Oct 27 23:00:36 2014
@@ -95,8 +95,8 @@ public class PDMetadata extends PDStream
public void importXMPMetadata( byte[] xmp )
throws IOException
{
- OutputStream os = createOutputStream();
- os.write(xmp);
- os.close();
+ OutputStream os = createOutputStream();
+ os.write(xmp);
+ os.close();
}
}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeySecurityHandler.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeySecurityHandler.java?rev=1634727&r1=1634726&r2=1634727&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeySecurityHandler.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/PublicKeySecurityHandler.java Mon Oct 27 23:00:36 2014
@@ -114,7 +114,7 @@ public final class PublicKeySecurityHand
PDEncryption dictionary = doc.getEncryption();
prepareForDecryption( dictionary, doc.getDocument().getDocumentID(),
- decryptionMaterial );
+ decryptionMaterial );
proceedDecryption();
}
@@ -135,103 +135,105 @@ public final class PublicKeySecurityHand
DecryptionMaterial decryptionMaterial)
throws IOException
{
- if(!(decryptionMaterial instanceof PublicKeyDecryptionMaterial))
- {
- throw new IOException(
- "Provided decryption material is not compatible with the document");
- }
-
- decryptMetadata = encryption.isEncryptMetaData();
- if(encryption.getLength() != 0)
- {
- this.keyLength = encryption.getLength();
- }
-
- PublicKeyDecryptionMaterial material = (PublicKeyDecryptionMaterial)decryptionMaterial;
-
- try
- {
- boolean foundRecipient = false;
-
- // the decrypted content of the enveloped data that match
- // the certificate in the decryption material provided
- byte[] envelopedData = null;
-
- // the bytes of each recipient in the recipients array
- byte[][] recipientFieldsBytes = new byte[encryption.getRecipientsLength()][];
-
- int recipientFieldsLength = 0;
-
- for(int i=0; i< encryption.getRecipientsLength(); i++)
- {
- COSString recipientFieldString = encryption.getRecipientStringAt(i);
- byte[] recipientBytes = recipientFieldString.getBytes();
- CMSEnvelopedData data = new CMSEnvelopedData(recipientBytes);
- Iterator<?> recipCertificatesIt = data.getRecipientInfos().getRecipients().iterator();
- while(recipCertificatesIt.hasNext())
- {
- RecipientInformation ri =
- (RecipientInformation)recipCertificatesIt.next();
- // Impl: if a matching certificate was previously found it is an error,
- // here we just don't care about it
- if(ri.getRID().match(material.getCertificate()) && !foundRecipient)
- {
- foundRecipient = true;
- PrivateKey privateKey = (PrivateKey)material.getPrivateKey();
- envelopedData = ri.getContent(new JceKeyTransEnvelopedRecipient(privateKey).setProvider("BC"));
- break;
- }
- }
- recipientFieldsBytes[i] = recipientBytes;
- recipientFieldsLength += recipientBytes.length;
- }
- if(!foundRecipient || envelopedData == null)
- {
- throw new IOException("The certificate matches no recipient entry");
- }
- if(envelopedData.length != 24)
- {
- throw new IOException("The enveloped data does not contain 24 bytes");
- }
- // now envelopedData contains:
- // - the 20 bytes seed
- // - the 4 bytes of permission for the current user
-
- byte[] accessBytes = new byte[4];
- System.arraycopy(envelopedData, 20, accessBytes, 0, 4);
-
- currentAccessPermission = new AccessPermission(accessBytes);
- currentAccessPermission.setReadOnly();
-
- // what we will put in the SHA1 = the seed + each byte contained in the recipients array
- byte[] sha1Input = new byte[recipientFieldsLength + 20];
-
- // put the seed in the sha1 input
- System.arraycopy(envelopedData, 0, sha1Input, 0, 20);
-
- // put each bytes of the recipients array in the sha1 input
- int sha1InputOffset = 20;
- for (byte[] recipientFieldsByte : recipientFieldsBytes)
- {
- System.arraycopy(recipientFieldsByte, 0, sha1Input, sha1InputOffset, recipientFieldsByte.length);
- sha1InputOffset += recipientFieldsByte.length;
- }
-
- MessageDigest md = MessageDigests.getSHA1();
- byte[] mdResult = md.digest(sha1Input);
-
- // we have the encryption key ...
- encryptionKey = new byte[this.keyLength/8];
- System.arraycopy(mdResult, 0, encryptionKey, 0, this.keyLength/8);
- }
- catch(CMSException e)
- {
- throw new IOException(e);
- }
- catch(KeyStoreException e)
- {
- throw new IOException(e);
- }
+ if (!(decryptionMaterial instanceof PublicKeyDecryptionMaterial))
+ {
+ throw new IOException(
+ "Provided decryption material is not compatible with the document");
+ }
+
+ decryptMetadata = encryption.isEncryptMetaData();
+ if (encryption.getLength() != 0)
+ {
+ this.keyLength = encryption.getLength();
+ }
+
+ PublicKeyDecryptionMaterial material = (PublicKeyDecryptionMaterial) decryptionMaterial;
+
+ try
+ {
+ boolean foundRecipient = false;
+
+ // the decrypted content of the enveloped data that match
+ // the certificate in the decryption material provided
+ byte[] envelopedData = null;
+
+ // the bytes of each recipient in the recipients array
+ byte[][] recipientFieldsBytes = new byte[encryption.getRecipientsLength()][];
+
+ int recipientFieldsLength = 0;
+
+ for (int i = 0; i < encryption.getRecipientsLength(); i++)
+ {
+ COSString recipientFieldString = encryption.getRecipientStringAt(i);
+ byte[] recipientBytes = recipientFieldString.getBytes();
+ CMSEnvelopedData data = new CMSEnvelopedData(recipientBytes);
+ Iterator<?> recipCertificatesIt = data.getRecipientInfos().getRecipients()
+ .iterator();
+ while (recipCertificatesIt.hasNext())
+ {
+ RecipientInformation ri = (RecipientInformation) recipCertificatesIt.next();
+ // Impl: if a matching certificate was previously found it is an error,
+ // here we just don't care about it
+ if (ri.getRID().match(material.getCertificate()) && !foundRecipient)
+ {
+ foundRecipient = true;
+ PrivateKey privateKey = (PrivateKey) material.getPrivateKey();
+ envelopedData = ri.getContent(new JceKeyTransEnvelopedRecipient(privateKey)
+ .setProvider("BC"));
+ break;
+ }
+ }
+ recipientFieldsBytes[i] = recipientBytes;
+ recipientFieldsLength += recipientBytes.length;
+ }
+ if (!foundRecipient || envelopedData == null)
+ {
+ throw new IOException("The certificate matches no recipient entry");
+ }
+ if (envelopedData.length != 24)
+ {
+ throw new IOException("The enveloped data does not contain 24 bytes");
+ }
+ // now envelopedData contains:
+ // - the 20 bytes seed
+ // - the 4 bytes of permission for the current user
+
+ byte[] accessBytes = new byte[4];
+ System.arraycopy(envelopedData, 20, accessBytes, 0, 4);
+
+ currentAccessPermission = new AccessPermission(accessBytes);
+ currentAccessPermission.setReadOnly();
+
+ // what we will put in the SHA1 = the seed + each byte contained in the recipients array
+ byte[] sha1Input = new byte[recipientFieldsLength + 20];
+
+ // put the seed in the sha1 input
+ System.arraycopy(envelopedData, 0, sha1Input, 0, 20);
+
+ // put each bytes of the recipients array in the sha1 input
+ int sha1InputOffset = 20;
+ for (byte[] recipientFieldsByte : recipientFieldsBytes)
+ {
+ System.arraycopy(recipientFieldsByte, 0, sha1Input, sha1InputOffset,
+ recipientFieldsByte.length);
+ sha1InputOffset += recipientFieldsByte.length;
+ }
+
+ MessageDigest md = MessageDigests.getSHA1();
+ byte[] mdResult = md.digest(sha1Input);
+
+ // we have the encryption key ...
+ encryptionKey = new byte[this.keyLength / 8];
+ System.arraycopy(mdResult, 0, encryptionKey, 0, this.keyLength / 8);
+ }
+ catch (CMSException e)
+ {
+ throw new IOException(e);
+ }
+ catch (KeyStoreException e)
+ {
+ throw new IOException(e);
+ }
}
/**
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType3Font.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType3Font.java?rev=1634727&r1=1634726&r2=1634727&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType3Font.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType3Font.java Mon Oct 27 23:00:36 2014
@@ -41,7 +41,7 @@ public class PDType3Font extends PDSimpl
{
private static final Log LOG = LogFactory.getLog(PDType3Font.class);
- private PDResources resources;
+ private PDResources resources;
private COSDictionary charProcs;
private Matrix fontMatrix;
@@ -197,7 +197,7 @@ public class PDType3Font extends PDSimpl
COSDictionary resources = (COSDictionary) dict.getDictionaryObject(COSName.RESOURCES);
if (resources != null)
{
- this.resources = new PDResources(resources);
+ this.resources = new PDResources(resources);
}
}
return resources;
@@ -236,7 +236,7 @@ public class PDType3Font extends PDSimpl
{
if (charProcs == null)
{
- charProcs = (COSDictionary) dict.getDictionaryObject(COSName.CHAR_PROCS);
+ charProcs = (COSDictionary) dict.getDictionaryObject(COSName.CHAR_PROCS);
}
return charProcs;
}
@@ -253,7 +253,7 @@ public class PDType3Font extends PDSimpl
if (name != null)
{
COSStream stream;
- stream = (COSStream)getCharProcs().getDictionaryObject(COSName.getPDFName(name));
+ stream = (COSStream)getCharProcs().getDictionaryObject(COSName.getPDFName(name));
return new PDType3CharProc(this, stream);
}
return null;
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/digitalsignature/visible/PDFTemplateBuilder.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/digitalsignature/visible/PDFTemplateBuilder.java?rev=1634727&r1=1634726&r2=1634727&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/digitalsignature/visible/PDFTemplateBuilder.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/digitalsignature/visible/PDFTemplateBuilder.java Mon Oct 27 23:00:36 2014
@@ -39,222 +39,234 @@ import org.apache.pdfbox.pdmodel.interac
public interface PDFTemplateBuilder
{
/**
- * In order to create Affine Transform, using parameters
+ * In order to create Affine Transform, using parameters.
* @param params
*/
- public void createAffineTransform(byte [] params);
-
- /**
- * Creates specified size page
- * @param properties
- */
- public void createPage(PDVisibleSignDesigner properties);
-
- /**
- * Creates template using page
- * @param page
- * @throws IOException
- */
- public void createTemplate(PDPage page) throws IOException;
-
- /**
- * Creates Acro forms in the template
- * @param template
- */
- public void createAcroForm(PDDocument template);
-
- /**
- * Creates signature fields
- * @param acroForm
- * @throws IOException
- */
- public void createSignatureField(PDAcroForm acroForm) throws IOException;
-
- /**
- * Creates PDSignatureField
- * @param pdSignatureField
- * @param page
- * @param signatureName
- * @throws IOException
- */
- public void createSignature(PDSignatureField pdSignatureField, PDPage page,
- String signatureName) throws IOException;
-
- /**
- * Create AcroForm Dictionary
- * @param acroForm
- * @param signatureField
- * @throws IOException
- */
- public void createAcroFormDictionary(PDAcroForm acroForm,
- PDSignatureField signatureField) throws IOException;
-
- /**
- * Creates SingatureRectangle
- * @param signatureField
- * @param properties
- * @throws IOException
- */
- public void createSignatureRectangle(PDSignatureField signatureField,
- PDVisibleSignDesigner properties) throws IOException;
-
- /**
- * Creates procSetArray of PDF,Text,ImageB,ImageC,ImageI
- */
- public void createProcSetArray();
-
+ public void createAffineTransform(byte[] params);
+
/**
- * Creates signature image
+ * Creates specified size page.
+ *
+ * @param properties
+ */
+ public void createPage(PDVisibleSignDesigner properties);
+
+ /**
+ * Creates template using page.
+ *
+ * @param page
+ * @throws IOException
+ */
+ public void createTemplate(PDPage page) throws IOException;
+
+ /**
+ * Creates Acro forms in the template.
+ *
+ * @param template
+ */
+ public void createAcroForm(PDDocument template);
+
+ /**
+ * Creates signature fields.
+ *
+ * @param acroForm
+ * @throws IOException
+ */
+ public void createSignatureField(PDAcroForm acroForm) throws IOException;
+
+ /**
+ * Creates PDSignatureField.
+ *
+ * @param pdSignatureField
+ * @param page
+ * @param signatureName
+ * @throws IOException
+ */
+ public void createSignature(PDSignatureField pdSignatureField, PDPage page, String signatureName)
+ throws IOException;
+
+ /**
+ * Create AcroForm Dictionary.
+ *
+ * @param acroForm
+ * @param signatureField
+ * @throws IOException
+ */
+ public void createAcroFormDictionary(PDAcroForm acroForm, PDSignatureField signatureField)
+ throws IOException;
+
+ /**
+ * Creates SingatureRectangle.
+ *
+ * @param signatureField
+ * @param properties
+ * @throws IOException
+ */
+ public void createSignatureRectangle(PDSignatureField signatureField,
+ PDVisibleSignDesigner properties) throws IOException;
+
+ /**
+ * Creates procSetArray of PDF,Text,ImageB,ImageC,ImageI.
+ */
+ public void createProcSetArray();
+
+ /**
+ * Creates signature image.
* @param template
* @param image
* @throws IOException
*/
- public void createSignatureImage(PDDocument template, BufferedImage image) throws IOException;
-
- /**
- *
- * @param params
- */
- public void createFormaterRectangle(byte [] params);
-
- /**
- *
- * @param template
- */
- public void createHolderFormStream(PDDocument template);
-
- /**
- * Creates resources of form
- */
- public void createHolderFormResources();
-
- /**
- * Creates Form
- * @param holderFormResources
- * @param holderFormStream
- * @param formrect
- */
- public void createHolderForm(PDResources holderFormResources, PDStream holderFormStream,
- PDRectangle formrect);
-
- /**
- * Creates appearance dictionary
- * @param holderForml
- * @param signatureField
- * @throws IOException
- */
- public void createAppearanceDictionary(PDFormXObject holderForml,
- PDSignatureField signatureField) throws IOException;
-
- /**
- *
- * @param template
- */
- public void createInnerFormStream(PDDocument template);
-
-
- /**
- * Creates InnerForm
- */
- public void createInnerFormResource();
-
- /**
- *
- * @param innerFormResources
- * @param innerFormStream
- * @param formrect
- */
- public void createInnerForm(PDResources innerFormResources, PDStream innerFormStream,
- PDRectangle formrect);
-
- /**
- *
- * @param innerForm
- * @param holderFormResources
- */
- public void insertInnerFormToHolerResources(PDFormXObject innerForm,
- PDResources holderFormResources);
-
- /**
- *
- * @param template
- */
- public void createImageFormStream(PDDocument template);
-
- /**
- * Create resource of image form
- */
- public void createImageFormResources();
-
- /**
- * Creates Image form
- * @param imageFormResources
- * @param innerFormResource
- * @param imageFormStream
- * @param formrect
- * @param affineTransform
- * @param img
- * @throws IOException
- */
- public void createImageForm(PDResources imageFormResources, PDResources innerFormResource,
- PDStream imageFormStream, PDRectangle formrect,
- AffineTransform affineTransform, PDImageXObject img)
- throws IOException;
-
- /**
- * Inject procSetArray
- * @param innerForm
- * @param page
- * @param innerFormResources
- * @param imageFormResources
- * @param holderFormResources
- * @param procSet
- */
- public void injectProcSetArray(PDFormXObject innerForm, PDPage page,
- PDResources innerFormResources, PDResources imageFormResources,
- PDResources holderFormResources, COSArray procSet);
-
- /**
- * injects appearance streams
- * @param holderFormStream
- * @param innterFormStream
- * @param imageFormStream
- * @param imageObjectName
- * @param imageName
- * @param innerFormName
- * @param properties
- * @throws IOException
- */
- public void injectAppearanceStreams(PDStream holderFormStream, PDStream innterFormStream,
- PDStream imageFormStream, COSName imageObjectName,
- COSName imageName, COSName innerFormName,
- PDVisibleSignDesigner properties) throws IOException;
-
- /**
- * just to create visible signature
- * @param template
- */
- public void createVisualSignature(PDDocument template);
-
- /**
- * adds Widget Dictionary
- * @param signatureField
- * @param holderFormResources
- * @throws IOException
- */
- public void createWidgetDictionary(PDSignatureField signatureField,
- PDResources holderFormResources) throws IOException;
-
- /**
- *
- * @return - PDF template Structure
- */
- public PDFTemplateStructure getStructure();
-
- /**
- * Closes template
- * @param template
- * @throws IOException
- */
- public void closeTemplate(PDDocument template) throws IOException;
+ public void createSignatureImage(PDDocument template, BufferedImage image) throws IOException;
+
+ /**
+ *
+ * @param params
+ */
+ public void createFormaterRectangle(byte[] params);
+
+ /**
+ *
+ * @param template
+ */
+ public void createHolderFormStream(PDDocument template);
+
+ /**
+ * Creates resources of form
+ */
+ public void createHolderFormResources();
+
+ /**
+ * Creates Form
+ *
+ * @param holderFormResources
+ * @param holderFormStream
+ * @param formrect
+ */
+ public void createHolderForm(PDResources holderFormResources, PDStream holderFormStream,
+ PDRectangle formrect);
+
+ /**
+ * Creates appearance dictionary
+ *
+ * @param holderForml
+ * @param signatureField
+ * @throws IOException
+ */
+ public void createAppearanceDictionary(PDFormXObject holderForml,
+ PDSignatureField signatureField) throws IOException;
+
+ /**
+ *
+ * @param template
+ */
+ public void createInnerFormStream(PDDocument template);
+
+ /**
+ * Creates InnerForm
+ */
+ public void createInnerFormResource();
+
+ /**
+ *
+ * @param innerFormResources
+ * @param innerFormStream
+ * @param formrect
+ */
+ public void createInnerForm(PDResources innerFormResources, PDStream innerFormStream,
+ PDRectangle formrect);
+
+ /**
+ *
+ * @param innerForm
+ * @param holderFormResources
+ */
+ public void insertInnerFormToHolerResources(PDFormXObject innerForm,
+ PDResources holderFormResources);
+
+ /**
+ *
+ * @param template
+ */
+ public void createImageFormStream(PDDocument template);
+
+ /**
+ * Create resource of image form
+ */
+ public void createImageFormResources();
+
+ /**
+ * Creates Image form
+ *
+ * @param imageFormResources
+ * @param innerFormResource
+ * @param imageFormStream
+ * @param formrect
+ * @param affineTransform
+ * @param img
+ * @throws IOException
+ */
+ public void createImageForm(PDResources imageFormResources, PDResources innerFormResource,
+ PDStream imageFormStream, PDRectangle formrect, AffineTransform affineTransform,
+ PDImageXObject img) throws IOException;
+
+ /**
+ * Inject procSetArray
+ *
+ * @param innerForm
+ * @param page
+ * @param innerFormResources
+ * @param imageFormResources
+ * @param holderFormResources
+ * @param procSet
+ */
+ public void injectProcSetArray(PDFormXObject innerForm, PDPage page,
+ PDResources innerFormResources, PDResources imageFormResources,
+ PDResources holderFormResources, COSArray procSet);
+
+ /**
+ * injects appearance streams
+ *
+ * @param holderFormStream
+ * @param innterFormStream
+ * @param imageFormStream
+ * @param imageObjectName
+ * @param imageName
+ * @param innerFormName
+ * @param properties
+ * @throws IOException
+ */
+ public void injectAppearanceStreams(PDStream holderFormStream, PDStream innterFormStream,
+ PDStream imageFormStream, COSName imageObjectName, COSName imageName,
+ COSName innerFormName, PDVisibleSignDesigner properties) throws IOException;
+
+ /**
+ * just to create visible signature
+ *
+ * @param template
+ */
+ public void createVisualSignature(PDDocument template);
+
+ /**
+ * adds Widget Dictionary
+ *
+ * @param signatureField
+ * @param holderFormResources
+ * @throws IOException
+ */
+ public void createWidgetDictionary(PDSignatureField signatureField,
+ PDResources holderFormResources) throws IOException;
+
+ /**
+ *
+ * @return - PDF template Structure
+ */
+ public PDFTemplateStructure getStructure();
+
+ /**
+ * Closes template
+ *
+ * @param template
+ * @throws IOException
+ */
+ public void closeTemplate(PDDocument template) throws IOException;
}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAppearanceString.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAppearanceString.java?rev=1634727&r1=1634726&r2=1634727&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAppearanceString.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAppearanceString.java Mon Oct 27 23:00:36 2014
@@ -50,7 +50,7 @@ import org.apache.pdfbox.contentstream.o
/**
* A default appearance string contains any graphics state or text state operators needed to establish the graphics
- * state parameters, such as text size and colour, for displaying the field?s variable text. Only operators that are
+ * state parameters, such as text size and colour, for displaying the field's variable text. Only operators that are
* allowed within text objects shall occur in this string.
*
* @author Stephan Gerhard
@@ -415,18 +415,18 @@ public final class PDAppearanceString
printWriter.println(leftOffset + " " + verticalOffset + " Td");
// add the value as hex string to deal with non ISO-8859-1 data values
- if (!isMultiLineValue(value))
+ if (!isMultiLineValue(value) || stringWidth > borderEdge.getWidth() - paddingLeft - paddingRight)
{
printWriter.println("<" + new COSString(value).getHexString() + "> Tj");
}
else
{
- String[] lines = value.split("\n");
- for (int i = 0; i < lines.length; i++)
+ String[] paragraphs = value.split("\n");
+ for (int i = 0; i < paragraphs.length; i++)
{
- boolean lastLine = i == (lines.length - 1);
+ boolean lastLine = i == (paragraphs.length - 1);
String endingTag = lastLine ? "> Tj\n" : "> Tj 0 -13 Td";
- printWriter.print("<" + new COSString(lines[i]).getHexString() + endingTag);
+ printWriter.print("<" + new COSString(paragraphs[i]).getHexString() + endingTag);
}
}
printWriter.println("ET");
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFTextStripper.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFTextStripper.java?rev=1634727&r1=1634726&r2=1634727&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFTextStripper.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFTextStripper.java Mon Oct 27 23:00:36 2014
@@ -220,11 +220,11 @@ public class PDFTextStripper extends PDF
document = null;
if (charactersByArticle != null)
{
- charactersByArticle.clear();
+ charactersByArticle.clear();
}
if (characterListMapping != null)
{
- characterListMapping.clear();
+ characterListMapping.clear();
}
startBookmark = null;
endBookmark = null;
@@ -476,17 +476,17 @@ public class PDFTextStripper extends PDF
if (getSortByPosition())
{
TextPositionComparator comparator = new TextPositionComparator();
-
+
// because the TextPositionComparator is not transitive, but
// JDK7+ enforces transitivity on comparators, we need to use
// a custom quicksort implementation (which is slower, unfortunately).
if(useCustomQuicksort)
{
- QuickSort.sort( textList, comparator );
+ QuickSort.sort( textList, comparator );
}
else
{
- Collections.sort( textList, comparator );
+ Collections.sort( textList, comparator );
}
}
Iterator<TextPosition> textIter = textList.iterator();
@@ -1663,7 +1663,7 @@ public class PDFTextStripper extends PDF
*/
protected void setListItemPatterns(List<Pattern> patterns)
{
- listOfPatterns = patterns;
+ listOfPatterns = patterns;
}
/**
Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/TestNonSequentialPDFParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/TestNonSequentialPDFParser.java?rev=1634727&r1=1634726&r2=1634727&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/TestNonSequentialPDFParser.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/TestNonSequentialPDFParser.java Mon Oct 27 23:00:36 2014
@@ -32,82 +32,99 @@ import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
-public class TestNonSequentialPDFParser {
+public class TestNonSequentialPDFParser
+{
- private static final String PATH_OF_PDF = "src/test/resources/org/apache/pdfbox/pdfparser/gdb-refcard.pdf";
- private static File tmpDirectory = new File(System.getProperty("java.io.tmpdir"));
+ private static final String PATH_OF_PDF = "src/test/resources/org/apache/pdfbox/pdfparser/gdb-refcard.pdf";
+ private static File tmpDirectory = new File(System.getProperty("java.io.tmpdir"));
- private int numberOfTmpFiles = 0;
+ private int numberOfTmpFiles = 0;
- /**
- * Initialize the number of tmp file before the test
- * @throws Exception
- */
- @Before
- public void setUp() throws Exception {
- numberOfTmpFiles = getNumberOfTempFile();
- }
-
- /**
- * Count the number of temporary files
- * @return
- */
- private int getNumberOfTempFile() {
- int result = 0;
- File[] tmpPdfs = tmpDirectory.listFiles(new FilenameFilter() {
- public boolean accept(File dir, String name) {
- return name.startsWith(NonSequentialPDFParser.TMP_FILE_PREFIX) && name.endsWith("pdf");
- }
- });
-
- if (tmpPdfs != null) {
- result = tmpPdfs.length;
- }
-
- return result;
- }
-
- @Test
- public void testNonSequentialPDFParserString() throws Exception {
- NonSequentialPDFParser nsp = new NonSequentialPDFParser(PATH_OF_PDF);
- executeParserTest(nsp);
- }
-
- @Test
- public void testNonSequentialPDFParserFile() throws IOException {
- NonSequentialPDFParser nsp = new NonSequentialPDFParser(new File(PATH_OF_PDF));
- executeParserTest(nsp);
- }
-
- @Test
- public void testNonSequentialPDFParserInputStream() throws IOException {
- NonSequentialPDFParser nsp = new NonSequentialPDFParser(new FileInputStream(PATH_OF_PDF));
- executeParserTest(nsp);
- }
+ /**
+ * Initialize the number of tmp file before the test
+ *
+ * @throws Exception
+ */
+ @Before
+ public void setUp() throws Exception
+ {
+ numberOfTmpFiles = getNumberOfTempFile();
+ }
+
+ /**
+ * Count the number of temporary files
+ *
+ * @return
+ */
+ private int getNumberOfTempFile()
+ {
+ int result = 0;
+ File[] tmpPdfs = tmpDirectory.listFiles(new FilenameFilter()
+ {
+ public boolean accept(File dir, String name)
+ {
+ return name.startsWith(NonSequentialPDFParser.TMP_FILE_PREFIX)
+ && name.endsWith("pdf");
+ }
+ });
+
+ if (tmpPdfs != null)
+ {
+ result = tmpPdfs.length;
+ }
+
+ return result;
+ }
+
+ @Test
+ public void testNonSequentialPDFParserString() throws Exception
+ {
+ NonSequentialPDFParser nsp = new NonSequentialPDFParser(PATH_OF_PDF);
+ executeParserTest(nsp);
+ }
+
+ @Test
+ public void testNonSequentialPDFParserFile() throws IOException
+ {
+ NonSequentialPDFParser nsp = new NonSequentialPDFParser(new File(PATH_OF_PDF));
+ executeParserTest(nsp);
+ }
@Test
- public void testNonSequentialPDFParserStringScratchFile() throws Exception {
+ public void testNonSequentialPDFParserInputStream() throws IOException
+ {
+ NonSequentialPDFParser nsp = new NonSequentialPDFParser(new FileInputStream(PATH_OF_PDF));
+ executeParserTest(nsp);
+ }
+
+ @Test
+ public void testNonSequentialPDFParserStringScratchFile() throws Exception
+ {
NonSequentialPDFParser nsp = new NonSequentialPDFParser(PATH_OF_PDF, true);
executeParserTest(nsp);
}
@Test
- public void testNonSequentialPDFParserFileScratchFile() throws IOException {
+ public void testNonSequentialPDFParserFileScratchFile() throws IOException
+ {
NonSequentialPDFParser nsp = new NonSequentialPDFParser(new File(PATH_OF_PDF), true);
executeParserTest(nsp);
}
@Test
- public void testNonSequentialPDFParserInputStreamScratchFile() throws IOException {
- NonSequentialPDFParser nsp = new NonSequentialPDFParser(new FileInputStream(PATH_OF_PDF), true);
+ public void testNonSequentialPDFParserInputStreamScratchFile() throws IOException
+ {
+ NonSequentialPDFParser nsp = new NonSequentialPDFParser(new FileInputStream(PATH_OF_PDF),
+ true);
executeParserTest(nsp);
}
-
- private void executeParserTest(NonSequentialPDFParser nsp) throws IOException {
- nsp.parse();
- assertNotNull(nsp.getDocument());
- // number tmp file must be the same
- assertEquals(numberOfTmpFiles, getNumberOfTempFile());
- }
+
+ private void executeParserTest(NonSequentialPDFParser nsp) throws IOException
+ {
+ nsp.parse();
+ assertNotNull(nsp.getDocument());
+ // number tmp file must be the same
+ assertEquals(numberOfTmpFiles, getNumberOfTempFile());
+ }
}