Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/pdfbox 3.0 #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<commons-compress.version>1.22</commons-compress.version>
<opennlp-tools.version>1.9.4</opennlp-tools.version>

<pdfbox.version>2.0.27</pdfbox.version>
<pdfbox.version>3.0.1</pdfbox.version>

<jaxb-api.version>2.3.1</jaxb-api.version>
<jaxb-runtime.version>2.3.6</jaxb-runtime.version>
Expand Down
59 changes: 21 additions & 38 deletions src/main/java/org/jadice/filetype/matchers/PDFMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.XMLConstants;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
Expand All @@ -15,6 +19,8 @@
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.pdfbox.Loader;
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
Expand All @@ -41,7 +47,7 @@
* A {@link Matcher} for PDF documents .
* <p>
* Caveat: for performance reasons, this should only be called from a context where the stream has
* already be identified as a PDF file/stream.
* already been identified as a PDF file/stream.
*/
public class PDFMatcher extends Matcher {
private static final Logger LOGGER = LoggerFactory.getLogger(PDFMatcher.class);
Expand Down Expand Up @@ -82,13 +88,13 @@ public boolean matches(final Context context) {
SeekableInputStream sis = context.getStream();
try {
sis.seek(0);
try (PDDocument document = PDDocument.load(sis)) {
try (PDDocument document = Loader.loadPDF(IOUtils.toByteArray(sis))) {
context.setProperty(MimeTypeAction.KEY, PDF_MIME_TYPE);

Map<String, Object> pdfDetails = new HashMap<String, Object>();
Map<String, Object> pdfDetails = new HashMap<>();
context.setProperty(DETAILS_KEY, pdfDetails);

pdfDetails.put(NUMBER_OF_PAGES_KEY, Integer.valueOf(document.getNumberOfPages()));
pdfDetails.put(NUMBER_OF_PAGES_KEY, document.getNumberOfPages());

PDDocumentInformation info = document.getDocumentInformation();
if (null != info) {
Expand All @@ -112,7 +118,7 @@ public boolean matches(final Context context) {
pdfDetails.put(IS_ENCRYPTED_KEY, false);


final List<String> filenames = new ArrayList<String>();
final List<String> filenames = new ArrayList<>();

PDDocumentNameDictionary namesDictionary = new PDDocumentNameDictionary(document.getDocumentCatalog());
PDEmbeddedFilesNameTreeNode efTree = namesDictionary.getEmbeddedFiles();
Expand Down Expand Up @@ -164,7 +170,10 @@ private void provideXMPMetadata(final Map<String, Object> pdfDetails, final PDMe
StreamResult xmlOutput = new StreamResult(new StringWriter());

// Configure transformer
Transformer transformer = TransformerFactory.newInstance().newTransformer();
TransformerFactory tf = TransformerFactory.newInstance();
tf.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
tf.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(xmlInput, xmlOutput);

Expand All @@ -183,7 +192,7 @@ private static void extractFilesFromPage(final PDPage page, final List<String> f
PDComplexFileSpecification complexFileSpec = (PDComplexFileSpecification) fileSpec;
PDEmbeddedFile embeddedFile = getEmbeddedFile(complexFileSpec);
if (embeddedFile != null) {
extractFile(filenames, complexFileSpec.getFilename(), embeddedFile);
extractFile(filenames, complexFileSpec.getFilename());
}
}
}
Expand All @@ -204,19 +213,17 @@ private static void extractFilesFromEFTree(final PDEmbeddedFilesNameTreeNode efT
}
}

private static void extractFiles(final Map<String, PDComplexFileSpecification> names, final List<String> filenames)
throws IOException {
private static void extractFiles(final Map<String, PDComplexFileSpecification> names, final List<String> filenames) {
for (Entry<String, PDComplexFileSpecification> entry : names.entrySet()) {
PDComplexFileSpecification fileSpec = entry.getValue();
PDEmbeddedFile embeddedFile = getEmbeddedFile(fileSpec);
if (embeddedFile != null) {
extractFile(filenames, fileSpec.getFilename(), embeddedFile);
extractFile(filenames, fileSpec.getFilename());
}
}
}

private static void extractFile(final List<String> filenames, final String filename,
final PDEmbeddedFile embeddedFile) throws IOException {
private static void extractFile(final List<String> filenames, final String filename) {
filenames.add(filename);
}

Expand Down Expand Up @@ -261,7 +268,7 @@ private static void addTextInfo(final Map<String, Object> pdfDetails, final PDDo
reader.setEndPage(i);
final String pdfText = reader.getText(doc).replaceAll("([\\r\\n])", "");
textLengthPerPages.add(pdfText.length());
if (pdfText.length() > 0) {
if (!pdfText.isEmpty()) {
containsText = true;
}
}
Expand All @@ -273,28 +280,4 @@ private static void addTextInfo(final Map<String, Object> pdfDetails, final PDDo
}
}

/**
* Reads the whole stream to determine the length of it.
*
* @param sis stream
* @return length of given stream or -1 if any error occurred
*/
private static long getFileLength(final SeekableInputStream sis) {
try {
sis.seek(0);
int read = 0;
final byte[] buffer = new byte[4096];
do {
synchronized (sis) { // perform synchronization inside while loop! See DOCPV-932
read = sis.read(buffer);
}
} while (read != -1);

// whole sis is read now
return sis.length();
} catch (Exception e) {
LOGGER.warn("Failed to determine file length.", e);
return -1;
}
}
}