Top Fields Constructors Methods
org.apache.pdfbox.preflight.parser

public Class PreflightParser

extends PDFParser
Class Inheritance
Imports
java.io.File, .IOException, java.nio.charset.Charset, .StandardCharsets, java.util.Arrays, .Set, java.util.regex.Matcher, .Pattern, org.apache.pdfbox.cos.COSArray, .COSBase, .COSDictionary, .COSFloat, .COSName, .COSNull, .COSNumber, .COSObject, .COSObjectKey, .COSStream, .COSString, org.apache.pdfbox.io.RandomAccessRead, .RandomAccessReadBufferedFile, org.apache.pdfbox.pdfparser.PDFParser, .XrefTrailerResolver.XRefType, org.apache.pdfbox.pdmodel.PDDocument, org.apache.pdfbox.preflight.Format, .PreflightConfiguration, .PreflightConstants, .PreflightContext, .PreflightDocument, .ValidationResult, .ValidationResult.ValidationError, org.apache.pdfbox.preflight.exception.SyntaxValidationException

Field Summary

Modifier and TypeField and Description
private PreflightConfiguration
private static final Charset
ENCODING

Define a one byte encoding that hasn't specific encoding in UTF-8 charset.

private Format
private PreflightDocument
private ValidationResult

Constructor Summary

AccessConstructor and Description
public
PreflightParser(File file)

Constructor.

public
PreflightParser(RandomAccessRead
input source
rar
)

Constructor.

public
PreflightParser(String filename)

Constructor.

Method Summary

Modifier and TypeMethod and Description
private void
addValidationError(ValidationResult.ValidationError
the validation error to be added
error
)

Add a validation error to the ValidationResult.

private void
checkEndstreamKeyWord(COSDictionary dic, long startOffset)

'endstream' must be preceded by an EOL

private void
checkPdfHeader()

Check that the PDF header match rules of the PDF/A specification.

private long
checkStreamKeyWord()

'stream' must be followed by <CR><LF> or only <LF>

protected PDDocument
createDocument()

Overrides org.apache.pdfbox.pdfparser.PDFParser.createDocument.

Create the resulting document.

protected void
initialParse()

Overrides org.apache.pdfbox.pdfparser.PDFParser.initialParse.

The initial parse will first parse only the trailer, the xrefstart and all xref tables to have a pointer (offset) to all the pdf's objects.

protected int
lastIndexOf(final char[]
pattern to search for
pattern
,
final byte[]
buffer to search pattern in
buf
,
final int
offset (exclusive) where lookup starts at
endOff
)

Overrides org.apache.pdfbox.pdfparser.COSParser.lastIndexOf.

Searches last appearance of pattern within buffer.

private boolean
public PDDocument
parse()

Overrides org.apache.pdfbox.pdfparser.PDFParser.parse.

This will parse the stream and populate the PDDocument object.

public PDDocument
parse(Format
format that the document should follow (default Format#PDF_A1B)
format
)

Parse the given file and check if it is a confirming file according to the given format.

public PDDocument
parse(Format
format that the document should follow (default Format#PDF_A1B)
format
,
PreflightConfiguration
Configuration bean that will be used by the PreflightDocument. If null the format is used to determine the default configuration.
config
)

Parse the given file and check if it is a confirming file according to the given format.

protected COSArray
parseCOSArray()

Overrides org.apache.pdfbox.pdfparser.BaseParser.parseCOSArray.

This will parse a PDF array object.

protected COSName
parseCOSName()

Overrides org.apache.pdfbox.pdfparser.BaseParser.parseCOSName.

This will parse a PDF name from the stream.

protected COSStream

Returns:

parsed pdf stream.
parseCOSStream
(COSDictionary
dictionary that goes with this stream.
dic
)

Overrides org.apache.pdfbox.pdfparser.COSParser.parseCOSStream.

Wraps the PDFParser#parseCOSStream to check rules on 'stream' and 'endstream' keywords.

protected COSString

Returns:

The parsed PDF string.
parseCOSString
()

Overrides org.apache.pdfbox.pdfparser.BaseParser.parseCOSString.

Check that the hexa string contains only an even number of Hexadecimal characters.

protected COSBase

Returns:

The parsed object.
parseDirObject
()

Overrides org.apache.pdfbox.pdfparser.BaseParser.parseDirObject.

Call PDFParser#parseDirObject() check limit range for Float, Integer and number of Dictionary entries.

private COSBase
protected synchronized COSBase
parseObjectDynamically(COSObjectKey
key of object to be parsed
objKey
,
boolean
if true the object to be parsed must be defined in xref (comment: null objects may be missing from xref) and it must not be a compressed object within object stream (this is used to circumvent being stuck in a loop in a malicious PDF)
requireExistingNotCompressedObj
)

Overrides org.apache.pdfbox.pdfparser.COSParser.parseObjectDynamically.

Parse the object for the given object key.

protected boolean

Returns:

false on parsing error
parseXrefTable
(long
the offset to start at
startByteOffset
)

Overrides org.apache.pdfbox.pdfparser.COSParser.parseXrefTable.

Same method than the PDFParser#parseXrefTable(long) with additional controls : - EOL mandatory after the 'xref' keyword - Cross reference subsection header uses single white space as separator - and so on

protected boolean
resetTrailerResolver()

Overrides org.apache.pdfbox.pdfparser.COSParser.resetTrailerResolver.

Indicates whether the xref trailer resolver should be reset or not.

public static ValidationResult

Returns:

the validation result
validate
(File
thew file to be read and validated
file
)

Load and validate the given file.

Inherited from org.apache.pdfbox.pdfparser.PDFParser:
loadloadparse

Field Detail

configback to summary
private PreflightConfiguration config
ENCODINGback to summary
private static final Charset ENCODING

Define a one byte encoding that hasn't specific encoding in UTF-8 charset. Avoid unexpected error when the encoding is Cp5816

formatback to summary
private Format format
preflightDocumentback to summary
private PreflightDocument preflightDocument
validationResultback to summary
private ValidationResult validationResult

Constructor Detail

PreflightParserback to summary
public PreflightParser(File file) throws IOException

Constructor.

Exceptions
IOException:
if there is a reading error.
PreflightParserback to summary
public PreflightParser(RandomAccessRead rar) throws IOException

Constructor.

Parameters
rar:RandomAccessRead

input source

Exceptions
IOException:
if there is a reading error.
PreflightParserback to summary
public PreflightParser(String filename) throws IOException

Constructor.

Exceptions
IOException:
if there is a reading error.

Method Detail

addValidationErrorback to summary
private void addValidationError(ValidationResult.ValidationError error)

Add a validation error to the ValidationResult.

Parameters
error:ValidationResult.ValidationError

the validation error to be added

checkEndstreamKeyWordback to summary
private void checkEndstreamKeyWord(COSDictionary dic, long startOffset) throws IOException

'endstream' must be preceded by an EOL

checkPdfHeaderback to summary
private void checkPdfHeader()

Check that the PDF header match rules of the PDF/A specification. First line (offset 0) must be a comment with the PDF version (version 1.0 isn't conform to the PDF/A specification) Second line is a comment with at least 4 bytes greater than 0x80

checkStreamKeyWordback to summary
private long checkStreamKeyWord() throws IOException

'stream' must be followed by <CR><LF> or only <LF>

createDocumentback to summary
protected PDDocument createDocument() throws IOException

Overrides org.apache.pdfbox.pdfparser.PDFParser.createDocument.

Doc from org.apache.pdfbox.pdfparser.PDFParser.createDocument.

Create the resulting document. Maybe overwritten if the parser uses another class as document.

Returns:PDDocument

the resulting document

Annotations
@Override
Exceptions
IOException:
if the method is called before parsing the document
initialParseback to summary
protected void initialParse() throws IOException

Overrides org.apache.pdfbox.pdfparser.PDFParser.initialParse.

Doc from org.apache.pdfbox.pdfparser.PDFParser.initialParse.

The initial parse will first parse only the trailer, the xrefstart and all xref tables to have a pointer (offset) to all the pdf's objects. It can handle linearized pdfs, which will have an xref at the end pointing to an xref at the beginning of the file. Last the root object is parsed.

Annotations
@Override
Exceptions
IOException:
If something went wrong.
lastIndexOfback to summary
protected int lastIndexOf(final char[] pattern, final byte[] buf, final int endOff)

Overrides org.apache.pdfbox.pdfparser.COSParser.lastIndexOf.

Doc from org.apache.pdfbox.pdfparser.COSParser.lastIndexOf.

Searches last appearance of pattern within buffer. Lookup before _lastOff and goes back until 0.

Parameters
pattern:char[]

pattern to search for

buf:byte[]

buffer to search pattern in

endOff:int

offset (exclusive) where lookup starts at

Returns:int

start offset of pattern within buffer or -1 if pattern could not be found

Annotations
@Override
nextIsEOLback to summary
private boolean nextIsEOL() throws IOException
parseback to summary
public PDDocument parse() throws IOException

Overrides org.apache.pdfbox.pdfparser.PDFParser.parse.

Doc from org.apache.pdfbox.pdfparser.PDFParser.parse.

This will parse the stream and populate the PDDocument object. This will close the keystore stream when it is done parsing. Lenient mode is active by default.

Returns:PDDocument

the populated PDDocument

Annotations
@Override
Exceptions
IOException:
If there is an error reading from the stream or corrupt data is found.
parseback to summary
public PDDocument parse(Format format) throws IOException

Parse the given file and check if it is a confirming file according to the given format.

Parameters
format:Format

format that the document should follow (default Format#PDF_A1B)

parseback to summary
public PDDocument parse(Format format, PreflightConfiguration config) throws IOException

Parse the given file and check if it is a confirming file according to the given format.

Parameters
format:Format

format that the document should follow (default Format#PDF_A1B)

config:PreflightConfiguration

Configuration bean that will be used by the PreflightDocument. If null the format is used to determine the default configuration.

parseCOSArrayback to summary
protected COSArray parseCOSArray() throws IOException

Overrides org.apache.pdfbox.pdfparser.BaseParser.parseCOSArray.

Doc from org.apache.pdfbox.pdfparser.BaseParser.parseCOSArray.

This will parse a PDF array object.

Returns:COSArray

The parsed PDF array.

Annotations
@Override
Exceptions
IOException:
If there is an error parsing the stream.
parseCOSNameback to summary
protected COSName parseCOSName() throws IOException

Overrides org.apache.pdfbox.pdfparser.BaseParser.parseCOSName.

Doc from org.apache.pdfbox.pdfparser.BaseParser.parseCOSName.

This will parse a PDF name from the stream.

Returns:COSName

The parsed PDF name.

Annotations
@Override
Exceptions
IOException:
If there is an error reading from the stream.
parseCOSStreamback to summary
protected COSStream parseCOSStream(COSDictionary dic) throws IOException

Overrides org.apache.pdfbox.pdfparser.COSParser.parseCOSStream.

Wraps the PDFParser#parseCOSStream to check rules on 'stream' and 'endstream' keywords. checkStreamKeyWord() and checkEndstreamKeyWord(org.apache.pdfbox.cos.COSDictionary, long)

Parameters
dic:COSDictionary

dictionary that goes with this stream.

Returns:COSStream

parsed pdf stream.

Annotations
@Override
Exceptions
IOException:
if an error occurred reading the stream, like problems with reading length attribute, stream does not end with 'endstream' after data read, stream too short etc.
parseCOSStringback to summary
protected COSString parseCOSString() throws IOException

Overrides org.apache.pdfbox.pdfparser.BaseParser.parseCOSString.

Check that the hexa string contains only an even number of Hexadecimal characters. Once it is done, reset the offset at the beginning of the string and call PDFParser#parseCOSString()

Returns:COSString

The parsed PDF string.

Annotations
@Override
Exceptions
IOException:
If there is an error reading from the stream.
parseDirObjectback to summary
protected COSBase parseDirObject() throws IOException

Overrides org.apache.pdfbox.pdfparser.BaseParser.parseDirObject.

Call PDFParser#parseDirObject() check limit range for Float, Integer and number of Dictionary entries.

Returns:COSBase

The parsed object.

Annotations
@Override
Exceptions
IOException:
if there is an error during parsing.
parseFileObjectback to summary
private COSBase parseFileObject(Long offsetOrObjstmObNr, final COSObjectKey objKey) throws IOException

Hides org.apache.pdfbox.pdfparser.COSParser.parseFileObject.

parseObjectDynamicallyback to summary
protected synchronized COSBase parseObjectDynamically(COSObjectKey objKey, boolean requireExistingNotCompressedObj) throws IOException

Overrides org.apache.pdfbox.pdfparser.COSParser.parseObjectDynamically.

Doc from org.apache.pdfbox.pdfparser.COSParser.parseObjectDynamically.

Parse the object for the given object key.

Parameters
objKey:COSObjectKey

key of object to be parsed

requireExistingNotCompressedObj:boolean

if true the object to be parsed must be defined in xref (comment: null objects may be missing from xref) and it must not be a compressed object within object stream (this is used to circumvent being stuck in a loop in a malicious PDF)

Returns:COSBase

the parsed object (which is also added to document object)

Annotations
@Override
Exceptions
IOException:
If an IO error occurs.
parseXrefTableback to summary
protected boolean parseXrefTable(long startByteOffset) throws IOException

Overrides org.apache.pdfbox.pdfparser.COSParser.parseXrefTable.

Same method than the PDFParser#parseXrefTable(long) with additional controls : - EOL mandatory after the 'xref' keyword - Cross reference subsection header uses single white space as separator - and so on

Parameters
startByteOffset:long

the offset to start at

Returns:boolean

false on parsing error

Annotations
@Override
Exceptions
IOException:
If an IO error occurs.
resetTrailerResolverback to summary
protected boolean resetTrailerResolver()

Overrides org.apache.pdfbox.pdfparser.COSParser.resetTrailerResolver.

Doc from org.apache.pdfbox.pdfparser.COSParser.resetTrailerResolver.

Indicates whether the xref trailer resolver should be reset or not. Should be overwritten if the xref trailer resolver is needed after the initial parsing.

Returns:boolean

true if the xref trailer resolver should be reset

Annotations
@Override
validateback to summary
public static ValidationResult validate(File file) throws IOException

Load and validate the given file. Returns the validation result and closes the read pdf.

Parameters
file:File

thew file to be read and validated

Returns:ValidationResult

the validation result

Exceptions
IOException:
in case of a file reading or parsing error