Class BaseParser

java.lang.Object
org.apache.pdfbox.pdfparser.BaseParser
Direct Known Subclasses:
COSParser, PDFObjectStreamParser, PDFStreamParser, PDFXrefStreamParser

public abstract class BaseParser extends Object
This class is used to contain parsing logic that will be used by both the PDFParser and the COSStreamParser.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static final int
     
    protected static final byte
    ASCII code for carriage return.
    protected static final byte
    ASCII code for line feed.
    private static final byte
     
    private static final byte
     
    private static final byte
     
    protected static final int
     
    protected static final int
     
    static final String
    This is a string constant that will be used for comparisons.
    protected COSDocument
    This is the document that will be parsed.
    protected static final int
     
    protected static final String
    This is a string constant that will be used for comparisons.
    protected static final String
    This is a string constant that will be used for comparisons.
    private static final String
    This is a string constant that will be used for comparisons.
    private static final long
     
    protected static final int
     
    private static final org.apache.commons.logging.Log
    Log instance.
    protected static final int
     
    (package private) static final int
     
    protected static final int
     
    private static final String
    This is a string constant that will be used for comparisons.
    protected static final int
     
    private static final long
     
    protected static final int
     
    protected static final int
     
    (package private) final SequentialSource
    This is the stream that will be read from.
    protected static final String
    This is a string constant that will be used for comparisons.
    protected static final int
     
    private static final String
    This is a string constant that will be used for comparisons.
    private final CharsetDecoder
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    private int
    checkForEndOfString(int bracesParameter)
    This is really a bug in the Document creators code, but it caused a crash in PDFBox, the first bug was in this format: /Title ( (5) /Creator which was patched in 1 place.
    private COSBase
     
    protected boolean
    This will tell if the next character is a closing brace( close of PDF array ).
    protected boolean
    isClosing(int c)
    This will tell if the next character is a closing brace( close of PDF array ).
    private boolean
    isCR(int c)
     
    protected boolean
    This will tell if the next byte is a digit or not.
    protected static boolean
    isDigit(int c)
    This will tell if the given value is a digit or not.
    protected boolean
    isEndOfName(int ch)
    Determine if a character terminates a PDF name.
    protected boolean
    This will tell if the next byte to be read is an end of line byte.
    protected boolean
    isEOL(int c)
    This will tell if the next byte to be read is an end of line byte.
    private static boolean
    isHexDigit(char ch)
     
    private boolean
    isLF(int c)
     
    protected boolean
    This will tell if the next byte is a space or not.
    protected boolean
    isSpace(int c)
    This will tell if the given value is a space or not.
    private boolean
    isValidUTF8(byte[] input)
    Returns true if a byte sequence is valid UTF-8.
    protected boolean
    This will tell if the next byte is whitespace or not.
    protected boolean
    isWhitespace(int c)
    This will tell if a character is whitespace or not.
    protected COSBoolean
    This will parse a boolean object from the stream.
    protected COSArray
    This will parse a PDF array object.
    protected COSDictionary
    This will parse a PDF dictionary.
    private boolean
     
    private COSBase
    This will parse a PDF dictionary value.
    private COSString
    This will parse a PDF HEX string with fail fast semantic meaning that we stop if a not allowed character is found.
    protected COSName
    This will parse a PDF name from the stream.
    private COSNumber
     
    protected COSString
    This will parse a PDF string.
    protected COSBase
    This will parse a directory object from the stream.
    protected void
    Read one char and throw an exception if it is not the expected value.
    protected final void
    readExpectedString(char[] expectedString, boolean skipSpaces)
    Reads given pattern from seqSource.
    protected void
    readExpectedString(String expectedString)
    Read one String and throw an exception if it is not the expected value.
    protected int
    This will read a integer from the Stream and throw an IllegalArgumentException if the integer value has more than the maximum object revision (i.e.
    protected int
    This will read an integer from the stream.
    protected String
    This will read bytes until the first end of line marker occurs.
    protected long
    This will read an long from the stream.
    protected long
    This will read a long from the Stream and throw an IOException if the long value is negative or has more than 10 digits (i.e.
    protected String
    This will read the next string from the stream.
    protected String
    readString(int length)
    This will read the next string from the stream up to a certain length.
    protected final StringBuilder
    This method is used to read a token by the readInt() and the readLong() method.
    private boolean
    Keep reading until the end of the dictionary object or the file has been hit, or until a '/' has been found.
    protected void
    This will skip all spaces and comments that are present.
    protected void
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

  • Method Details

    • isHexDigit

      private static boolean isHexDigit(char ch)
    • parseCOSDictionaryValue

      private COSBase parseCOSDictionaryValue() throws IOException
      This will parse a PDF dictionary value.
      Returns:
      The parsed Dictionary object.
      Throws:
      IOException - If there is an error parsing the dictionary object.
    • getObjectFromPool

      private COSBase getObjectFromPool(COSObjectKey key) throws IOException
      Throws:
      IOException
    • parseCOSDictionary

      protected COSDictionary parseCOSDictionary() throws IOException
      This will parse a PDF dictionary.
      Returns:
      The parsed dictionary, never null.
      Throws:
      IOException - If there is an error reading the stream.
    • readUntilEndOfCOSDictionary

      private boolean readUntilEndOfCOSDictionary() throws IOException
      Keep reading until the end of the dictionary object or the file has been hit, or until a '/' has been found.
      Returns:
      true if the end of the object or the file has been found, false if not, i.e. that the caller can continue to parse the dictionary at the current position.
      Throws:
      IOException - if there is a reading error.
    • parseCOSDictionaryNameValuePair

      private boolean parseCOSDictionaryNameValuePair(COSDictionary obj) throws IOException
      Throws:
      IOException
    • skipWhiteSpaces

      protected void skipWhiteSpaces() throws IOException
      Throws:
      IOException
    • checkForEndOfString

      private int checkForEndOfString(int bracesParameter) throws IOException
      This is really a bug in the Document creators code, but it caused a crash in PDFBox, the first bug was in this format: /Title ( (5) /Creator which was patched in 1 place. However it missed the case where the number of opening and closing parenthesis isn't balanced The second bug was in this format /Title (c:\) /Producer
      Parameters:
      bracesParameter - the number of braces currently open.
      Returns:
      the corrected value of the brace counter
      Throws:
      IOException
    • parseCOSString

      protected COSString parseCOSString() throws IOException
      This will parse a PDF string.
      Returns:
      The parsed PDF string.
      Throws:
      IOException - If there is an error reading from the stream.
    • parseCOSHexString

      private COSString parseCOSHexString() throws IOException
      This will parse a PDF HEX string with fail fast semantic meaning that we stop if a not allowed character is found. This is necessary in order to detect malformed input and be able to skip to next object start. We assume starting '<' was already read.
      Returns:
      The parsed PDF string.
      Throws:
      IOException - If there is an error reading from the stream.
    • parseCOSArray

      protected COSArray parseCOSArray() throws IOException
      This will parse a PDF array object.
      Returns:
      The parsed PDF array.
      Throws:
      IOException - If there is an error parsing the stream.
    • isEndOfName

      protected boolean isEndOfName(int ch)
      Determine if a character terminates a PDF name.
      Parameters:
      ch - The character
      Returns:
      true if the character terminates a PDF name, otherwise false.
    • parseCOSName

      protected COSName parseCOSName() throws IOException
      This will parse a PDF name from the stream.
      Returns:
      The parsed PDF name.
      Throws:
      IOException - If there is an error reading from the stream.
    • isValidUTF8

      private boolean isValidUTF8(byte[] input)
      Returns true if a byte sequence is valid UTF-8.
    • parseBoolean

      protected COSBoolean parseBoolean() throws IOException
      This will parse a boolean object from the stream.
      Returns:
      The parsed boolean object.
      Throws:
      IOException - If an IO error occurs during parsing.
    • parseDirObject

      protected COSBase parseDirObject() throws IOException
      This will parse a directory object from the stream.
      Returns:
      The parsed object.
      Throws:
      IOException - If there is an error during parsing.
    • parseCOSNumber

      private COSNumber parseCOSNumber() throws IOException
      Throws:
      IOException
    • readString

      protected String readString() throws IOException
      This will read the next string from the stream.
      Returns:
      The string that was read from the stream, never null.
      Throws:
      IOException - If there is an error reading from the stream.
    • readExpectedString

      protected void readExpectedString(String expectedString) throws IOException
      Read one String and throw an exception if it is not the expected value.
      Parameters:
      expectedString - the String value that is expected.
      Throws:
      IOException - if the String char is not the expected value or if an I/O error occurs.
    • readExpectedString

      protected final void readExpectedString(char[] expectedString, boolean skipSpaces) throws IOException
      Reads given pattern from seqSource. Skipping whitespace at start and end if wanted.
      Parameters:
      expectedString - pattern to be skipped
      skipSpaces - if set to true spaces before and after the string will be skipped
      Throws:
      IOException - if pattern could not be read
    • readExpectedChar

      protected void readExpectedChar(char ec) throws IOException
      Read one char and throw an exception if it is not the expected value.
      Parameters:
      ec - the char value that is expected.
      Throws:
      IOException - if the read char is not the expected value or if an I/O error occurs.
    • readString

      protected String readString(int length) throws IOException
      This will read the next string from the stream up to a certain length.
      Parameters:
      length - The length to stop reading at.
      Returns:
      The string that was read from the stream of length 0 to length.
      Throws:
      IOException - If there is an error reading from the stream.
    • isClosing

      protected boolean isClosing() throws IOException
      This will tell if the next character is a closing brace( close of PDF array ).
      Returns:
      true if the next byte is ']', false otherwise.
      Throws:
      IOException - If an IO error occurs.
    • isClosing

      protected boolean isClosing(int c)
      This will tell if the next character is a closing brace( close of PDF array ).
      Parameters:
      c - The character to check against end of line
      Returns:
      true if the next byte is ']', false otherwise.
    • readLine

      protected String readLine() throws IOException
      This will read bytes until the first end of line marker occurs. NOTE: The EOL marker may consists of 1 (CR or LF) or 2 (CR and CL) bytes which is an important detail if one wants to unread the line.
      Returns:
      The characters between the current position and the end of the line.
      Throws:
      IOException - If there is an error reading from the stream.
    • isEOL

      protected boolean isEOL() throws IOException
      This will tell if the next byte to be read is an end of line byte.
      Returns:
      true if the next byte is 0x0A or 0x0D.
      Throws:
      IOException - If there is an error reading from the stream.
    • isEOL

      protected boolean isEOL(int c)
      This will tell if the next byte to be read is an end of line byte.
      Parameters:
      c - The character to check against end of line
      Returns:
      true if the next byte is 0x0A or 0x0D.
    • isLF

      private boolean isLF(int c)
    • isCR

      private boolean isCR(int c)
    • isWhitespace

      protected boolean isWhitespace() throws IOException
      This will tell if the next byte is whitespace or not.
      Returns:
      true if the next byte in the stream is a whitespace character.
      Throws:
      IOException - If there is an error reading from the stream.
    • isWhitespace

      protected boolean isWhitespace(int c)
      This will tell if a character is whitespace or not. These values are specified in table 1 (page 12) of ISO 32000-1:2008.
      Parameters:
      c - The character to check against whitespace
      Returns:
      true if the character is a whitespace character.
    • isSpace

      protected boolean isSpace() throws IOException
      This will tell if the next byte is a space or not.
      Returns:
      true if the next byte in the stream is a space character.
      Throws:
      IOException - If there is an error reading from the stream.
    • isSpace

      protected boolean isSpace(int c)
      This will tell if the given value is a space or not.
      Parameters:
      c - The character to check against space
      Returns:
      true if the next byte in the stream is a space character.
    • isDigit

      protected boolean isDigit() throws IOException
      This will tell if the next byte is a digit or not.
      Returns:
      true if the next byte in the stream is a digit.
      Throws:
      IOException - If there is an error reading from the stream.
    • isDigit

      protected static boolean isDigit(int c)
      This will tell if the given value is a digit or not.
      Parameters:
      c - The character to be checked
      Returns:
      true if the next byte in the stream is a digit.
    • skipSpaces

      protected void skipSpaces() throws IOException
      This will skip all spaces and comments that are present.
      Throws:
      IOException - If there is an error reading from the stream.
    • readObjectNumber

      protected long readObjectNumber() throws IOException
      This will read a long from the Stream and throw an IOException if the long value is negative or has more than 10 digits (i.e. : bigger than OBJECT_NUMBER_THRESHOLD)
      Returns:
      the object number being read.
      Throws:
      IOException - if an I/O error occurs
    • readGenerationNumber

      protected int readGenerationNumber() throws IOException
      This will read a integer from the Stream and throw an IllegalArgumentException if the integer value has more than the maximum object revision (i.e. : bigger than GENERATION_NUMBER_THRESHOLD)
      Returns:
      the generation number being read.
      Throws:
      IOException - if an I/O error occurs
    • readInt

      protected int readInt() throws IOException
      This will read an integer from the stream.
      Returns:
      The integer that was read from the stream.
      Throws:
      IOException - If there is an error reading from the stream.
    • readLong

      protected long readLong() throws IOException
      This will read an long from the stream.
      Returns:
      The long that was read from the stream.
      Throws:
      IOException - If there is an error reading from the stream.
    • readStringNumber

      protected final StringBuilder readStringNumber() throws IOException
      This method is used to read a token by the readInt() and the readLong() method. Valid delimiters are any non digit values.
      Returns:
      the token to parse as integer or long by the calling method.
      Throws:
      IOException - throws by the seqSource methods.