Class PdfArlingtonValidator

java.lang.Object
org.jpedal.arlington.validate.PdfArlingtonValidator

public final class PdfArlingtonValidator extends Object
Validates a PDF file against the Arlington Model.
See Also:
  • Field Details

    • VALID_DATE

      public static final Pattern VALID_DATE
      Regex to validate PDF date strings. See 7.9.4 Dates.

      Each field after the year may only be present if all of the fields before it are. In the time zone, the apostrophe after the hour offset and the minute offset are optional, and so is the apostrophe after the minute offset (it was required before PDF 2.0).

      Note that this does not support language escape sequences!

  • Constructor Details

    • PdfArlingtonValidator

      public PdfArlingtonValidator(Map<String,org.jpedal.arlington.model.ArlingtonObject> model, org.jpedal.io.writer.PdfFile info, org.jpedal.io.writer.PdfBuffer buffer, float pdfVersion)
      Create a new instance of the validator.
      Parameters:
      model - the Arlington Model to validate against
      info - the currently loaded PDF
      buffer - the buffer object for the currently loaded PDF
      pdfVersion - the currently loaded PDF version
  • Method Details

    • withExtensions

      public PdfArlingtonValidator withExtensions(Set<String> extensionNames)
      Specify which extensions should be assumed to be implemented by the PDF being validated.
      Parameters:
      extensionNames - the names of the extensions to assume are implemented or null
      Returns:
      itself
    • getExtensions

      public Set<String> getExtensions()
      Returns the extensions currently assumed to be implemented by the PDF being validated.
      Returns:
      the extensions currently assumed to be implemented by the PDF being validated
      See Also:
    • withFixes

      public PdfArlingtonValidator withFixes(boolean fix)
      Specify whether violations should be fixed in the loaded document instead of being reported.

      When enabled:

      • a missing required key or array element is added with its Arlington default value, if it has a plain literal default;
      • an optional dictionary key with an invalid value (wrong type, not a permitted value or failing its special case) is removed;
      • an optional dictionary key whose value is an empty array, where the array must have at least one element (e.g. an empty /AF), is removed;
      • a direct object which must be indirect is moved to a new indirect object.
      Everything else is still reported. Changes are made to the PdfFile passed to the constructor and are listed by getFixes().

      Name and number trees with entries and the keys which say how a stream is encoded are never removed, as removing them would lose content. A dictionary whose /Type, /Subtype, /S or /FT is not permitted by the definition it is being checked against, but is by another definition it fits at least as well, is not fixed. Changes are kept by updating the indirect objects holding them, so when using validateObject(PdfObjectInt, String, String) with fixing, the object should be the trailer or a direct object: changes to an indirect object passed to it are made to that copy only.

      Parameters:
      fix - true to fix violations
      Returns:
      itself
      See Also:
    • setMemoryCacheThreshold

      public static void setMemoryCacheThreshold(int memoryCacheThreshold)
      Set the threshold at which fixed documents created by fixFile(File, byte[], Map, Set, File) which exceed this size will be written to the disk (encrypted with CryptoIDR) rather than stored in memory.

      Default: 58MB

      Parameters:
      memoryCacheThreshold - the threshold
      See Also:
    • getMemoryCacheThreshold

      public static int getMemoryCacheThreshold()
      Returns the current threshold for which fixed documents are written to disk rather than being stored in memory.
      Returns:
      the current threshold for which fixed documents are written to disk rather than being stored in memory
      See Also:
    • getIssues

      public List<ValidationIssue> getIssues()
      Returns all issues collected so far.
      Returns:
      all issues collected so far
    • getFixes

      public List<org.jpedal.arlington.validate.AppliedFix> getFixes()
      Returns all changes made to the document so far.
      Returns:
      all changes made to the document so far
      See Also:
    • validateFile

      public void validateFile(org.jpedal.io.writer.PdfFile pdf) throws IOException
      Validate an entire file (starting from the trailer) against a TSV definition.
      Parameters:
      pdf - the file to validate
      Throws:
      IOException - if the underlying PDF cannot be read
    • validateObject

      public void validateObject(org.jpedal.io.writer.PdfObjectInt obj, String arlingtonObjectName, String path) throws IOException
      Validate an arbitrary resolved object against a TSV definition.
      Parameters:
      obj - the resolved object to check
      arlingtonObjectName - the Arlington TSV name to validate against
      path - a human readable path label
      Throws:
      IOException - if the underlying PDF cannot be read
    • main

      public static void main(String[] args) throws IOException
      Entry point that validates one or more PDF files against the Arlington Model.

      Usage:

      
       java org.jpedal.arlington.validate.PdfArlingtonValidator --tsv <path-to-arlington-tsv-dir> [--level LEVEL] [--fix OUTPUT_DIR] file1.pdf [file2.pdf ...]
       
      Throws:
      IOException
    • validateFile

      public static PdfArlingtonValidator.Result validateFile(File pdfFile, Map<String,org.jpedal.arlington.model.ArlingtonObject> model) throws IOException
      Validate a single PDF file against the Arlington model.
      Parameters:
      pdfFile - the PDF file on disk
      model - the loaded Arlington model
      Returns:
      the validation result (PDF version detected plus all issues found)
      Throws:
      IOException - if the PDF cannot be opened/parsed at all
    • validateFile

      public static PdfArlingtonValidator.Result validateFile(File pdfFile, byte[] password, Map<String,org.jpedal.arlington.model.ArlingtonObject> model) throws IOException
      Validate a single PDF file against the Arlington model.
      Parameters:
      pdfFile - the PDF file on disk
      password - the password for the file, or null if it does not have one
      model - the loaded Arlington model
      Returns:
      the validation result (PDF version detected plus all issues found)
      Throws:
      IOException - if the PDF cannot be opened/parsed at all
    • validateFile

      public static PdfArlingtonValidator.Result validateFile(File pdfFile, Map<String,org.jpedal.arlington.model.ArlingtonObject> model, Set<String> extensions) throws IOException
      Validate a single PDF file against the Arlington model, assuming a given set of Arlington extensions are implemented.
      Parameters:
      pdfFile - the PDF file on disk
      model - the loaded Arlington model
      extensions - the Arlington extensions to assume are implemented or null
      Returns:
      the validation result (PDF version detected plus all issues found)
      Throws:
      IOException - if the PDF cannot be opened/parsed at all
      See Also:
    • validateFile

      public static PdfArlingtonValidator.Result validateFile(File pdfFile, byte[] password, Map<String,org.jpedal.arlington.model.ArlingtonObject> model, Set<String> extensions) throws IOException
      Validate a single PDF file against the Arlington model, assuming a given set of Arlington extensions are implemented.
      Parameters:
      pdfFile - the PDF file on disk
      password - the password for the file, or null if it does not have one
      model - the loaded Arlington model
      extensions - the Arlington extensions to assume are implemented or null
      Returns:
      the validation result (PDF version detected plus all issues found)
      Throws:
      IOException - if the PDF cannot be opened/parsed at all
      See Also:
    • validateFile

      public static PdfArlingtonValidator.Result validateFile(byte[] pdfFile, Map<String,org.jpedal.arlington.model.ArlingtonObject> model) throws IOException
      Validate a single PDF file against the Arlington model.
      Parameters:
      pdfFile - the PDF file as a byte array
      model - the loaded Arlington model
      Returns:
      the validation result (PDF version detected plus all issues found)
      Throws:
      IOException - if the PDF cannot be opened/parsed at all
    • validateFile

      public static PdfArlingtonValidator.Result validateFile(byte[] pdfFile, byte[] password, Map<String,org.jpedal.arlington.model.ArlingtonObject> model) throws IOException
      Validate a single PDF file against the Arlington model.
      Parameters:
      pdfFile - the PDF file as a byte array
      password - the password for the file, or null if it does not have one
      model - the loaded Arlington model
      Returns:
      the validation result (PDF version detected plus all issues found)
      Throws:
      IOException - if the PDF cannot be opened/parsed at all
    • validateFile

      public static PdfArlingtonValidator.Result validateFile(byte[] pdfFile, Map<String,org.jpedal.arlington.model.ArlingtonObject> model, Set<String> extensions) throws IOException
      Validate a single PDF file against the Arlington model.
      Parameters:
      pdfFile - the PDF file as a byte array
      model - the loaded Arlington model
      extensions - the Arlington extensions to assume are implemented or null
      Returns:
      the validation result (PDF version detected plus all issues found)
      Throws:
      IOException - if the PDF cannot be opened/parsed at all
    • validateFile

      public static PdfArlingtonValidator.Result validateFile(byte[] pdfFile, byte[] password, Map<String,org.jpedal.arlington.model.ArlingtonObject> model, Set<String> extensions) throws IOException
      Validate a single PDF file against the Arlington model.
      Parameters:
      pdfFile - the PDF file as a byte array
      password - the password for the file, or null if it does not have one
      model - the loaded Arlington model
      extensions - the Arlington extensions to assume are implemented or null
      Returns:
      the validation result (PDF version detected plus all issues found)
      Throws:
      IOException - if the PDF cannot be opened/parsed at all
    • validateFile

      public static PdfArlingtonValidator.Result validateFile(InputStream pdfFile, Map<String,org.jpedal.arlington.model.ArlingtonObject> model) throws IOException
      Validate a single PDF file against the Arlington model.
      Parameters:
      pdfFile - the PDF file on disk
      model - the loaded Arlington model
      Returns:
      the validation result (PDF version detected plus all issues found)
      Throws:
      IOException - if the PDF cannot be opened/parsed at all
      API Note:
      PDF files must allow for random access, so the InputStream is read fully then cached to disk. The file is encrypted at rest and deleted once close is called.
    • validateFile

      public static PdfArlingtonValidator.Result validateFile(InputStream pdfFile, byte[] password, Map<String,org.jpedal.arlington.model.ArlingtonObject> model) throws IOException
      Validate a single PDF file against the Arlington model.
      Parameters:
      pdfFile - the PDF file from an InputStream
      password - the password for the file, or null if it does not have one
      model - the loaded Arlington model
      Returns:
      the validation result (PDF version detected plus all issues found)
      Throws:
      IOException - if the PDF cannot be opened/parsed at all
      API Note:
      PDF files must allow for random access, so the InputStream is read fully then cached to disk. The file is encrypted at rest and deleted once close is called.
    • validateFile

      public static PdfArlingtonValidator.Result validateFile(InputStream pdfFile, Map<String,org.jpedal.arlington.model.ArlingtonObject> model, Set<String> extensions) throws IOException
      Validate a single PDF file against the Arlington model.
      Parameters:
      pdfFile - the PDF file on disk
      model - the loaded Arlington model
      extensions - the Arlington extensions to assume are implemented or null
      Returns:
      the validation result (PDF version detected plus all issues found)
      Throws:
      IOException - if the PDF cannot be opened/parsed at all
      API Note:
      PDF files must allow for random access, so the InputStream is read fully then cached to disk. The file is encrypted at rest and deleted once close is called.
    • validateFile

      public static PdfArlingtonValidator.Result validateFile(InputStream pdfFile, byte[] password, Map<String,org.jpedal.arlington.model.ArlingtonObject> model, Set<String> extensions) throws IOException
      Validate a single PDF file against the Arlington model.
      Parameters:
      pdfFile - the PDF file from an InputStream
      password - the password for the file, or null if it does not have one
      model - the loaded Arlington model
      extensions - the Arlington extensions to assume are implemented or null
      Returns:
      the validation result (PDF version detected plus all issues found)
      Throws:
      IOException - if the PDF cannot be opened/parsed at all
      API Note:
      PDF files must allow for random access, so the InputStream is read fully then cached to disk. The file is encrypted at rest and deleted once close is called.
    • fixFile

      public static PdfArlingtonValidator.FixResult fixFile(File pdfFile, Map<String,org.jpedal.arlington.model.ArlingtonObject> model, File outputFile) throws IOException
      Fix a single PDF file against the Arlington model and write the fixed file if no errors remain.
      Parameters:
      pdfFile - the PDF file on disk
      model - the loaded Arlington model
      outputFile - where to write the fixed file
      Returns:
      the changes made, the issues found in the fixed file and whether it was written
      Throws:
      IOException - if the PDF cannot be opened/parsed/written, or it is encrypted
      See Also:
    • fixFile

      public static PdfArlingtonValidator.FixResult fixFile(File pdfFile, Map<String,org.jpedal.arlington.model.ArlingtonObject> model, Set<String> extensions, File outputFile) throws IOException
      Fix a single PDF file against the Arlington model and write the fixed file if no errors remain, assuming a given set of Arlington extensions are implemented.
      Parameters:
      pdfFile - the PDF file on disk
      model - the loaded Arlington model
      extensions - the Arlington extensions to assume are implemented or null
      outputFile - where to write the fixed file
      Returns:
      the changes made, the issues found in the fixed file and whether it was written
      Throws:
      IOException - if the PDF cannot be opened/parsed/written, or it is encrypted
      See Also:
    • fixFile

      public static PdfArlingtonValidator.FixResult fixFile(File pdfFile, byte[] password, Map<String,org.jpedal.arlington.model.ArlingtonObject> model, File outputFile) throws IOException
      Fix a single PDF file against the Arlington model and write the fixed file if no errors remain.
      Parameters:
      pdfFile - the PDF file on disk
      password - the password for the file, or null if it does not have one
      model - the loaded Arlington model
      outputFile - where to write the fixed file
      Returns:
      the changes made, the issues found in the fixed file and whether it was written
      Throws:
      IOException - if the PDF cannot be opened/parsed/written, it is encrypted, or outputFile is pdfFile
      See Also:
    • fixFile

      public static PdfArlingtonValidator.FixResult fixFile(File pdfFile, byte[] password, Map<String,org.jpedal.arlington.model.ArlingtonObject> model, Set<String> extensions, File outputFile) throws IOException
      Fix a single PDF file against the Arlington model and write the fixed file if no errors remain, assuming a given set of Arlington extensions are implemented.

      See withFixes(boolean) for what is fixed. The fixed document is written and validated again, and is only written to outputFile when that finds no ValidationIssue.Severity.ERRORs. The file is rewritten even if nothing needed fixing.

      The fixed document is held in memory, unless it is larger than getMemoryCacheThreshold(), when it is written to a temporary file in the directory of outputFile encrypted with CryptoIDR.

      Parameters:
      pdfFile - the PDF file on disk
      password - the password for the file, or null if it does not have one
      model - the loaded Arlington model
      extensions - the Arlington extensions to assume are implemented or null
      outputFile - where to write the fixed file
      Returns:
      the changes made, the issues found in the fixed file and whether it was written
      Throws:
      IOException - if the PDF cannot be opened/parsed/written, it is encrypted, or outputFile is pdfFile
    • fixFile

      public static PdfArlingtonValidator.FixResult fixFile(byte[] pdfFile, Map<String,org.jpedal.arlington.model.ArlingtonObject> model, File outputFile) throws IOException
      Fix a single PDF file against the Arlington model and write the fixed file if no errors remain.
      Parameters:
      pdfFile - the PDF file as a byte array
      model - the loaded Arlington model
      outputFile - where to write the fixed file
      Returns:
      the changes made, the issues found in the fixed file and whether it was written
      Throws:
      IOException - if the PDF cannot be opened/parsed/written, or it is encrypted
      See Also:
    • fixFile

      public static PdfArlingtonValidator.FixResult fixFile(byte[] pdfFile, Map<String,org.jpedal.arlington.model.ArlingtonObject> model, Set<String> extensions, File outputFile) throws IOException
      Fix a single PDF file against the Arlington model and write the fixed file if no errors remain, assuming a given set of Arlington extensions are implemented.
      Parameters:
      pdfFile - the PDF file as a byte array
      model - the loaded Arlington model
      extensions - the Arlington extensions to assume are implemented or null
      outputFile - where to write the fixed file
      Returns:
      the changes made, the issues found in the fixed file and whether it was written
      Throws:
      IOException - if the PDF cannot be opened/parsed/written, or it is encrypted
      See Also:
    • fixFile

      public static PdfArlingtonValidator.FixResult fixFile(byte[] pdfFile, byte[] password, Map<String,org.jpedal.arlington.model.ArlingtonObject> model, File outputFile) throws IOException
      Fix a single PDF file against the Arlington model and write the fixed file if no errors remain.
      Parameters:
      pdfFile - the PDF file as a byte array
      password - the password for the file, or null if it does not have one
      model - the loaded Arlington model
      outputFile - where to write the fixed file
      Returns:
      the changes made, the issues found in the fixed file and whether it was written
      Throws:
      IOException - if the PDF cannot be opened/parsed/written, or it is encrypted
      See Also:
    • fixFile

      public static PdfArlingtonValidator.FixResult fixFile(byte[] pdfFile, byte[] password, Map<String,org.jpedal.arlington.model.ArlingtonObject> model, Set<String> extensions, File outputFile) throws IOException
      Fix a single PDF file against the Arlington model and write the fixed file if no errors remain, assuming a given set of Arlington extensions are implemented.
      Parameters:
      pdfFile - the PDF file as a byte array
      password - the password for the file, or null if it does not have one
      model - the loaded Arlington model
      extensions - the Arlington extensions to assume are implemented or null
      outputFile - where to write the fixed file
      Returns:
      the changes made, the issues found in the fixed file and whether it was written
      Throws:
      IOException - if the PDF cannot be opened/parsed/written, or it is encrypted
      See Also:
    • fixFile

      public static PdfArlingtonValidator.FixResult fixFile(InputStream pdfFile, Map<String,org.jpedal.arlington.model.ArlingtonObject> model, File outputFile) throws IOException
      Fix a single PDF file against the Arlington model and write the fixed file if no errors remain.
      Parameters:
      pdfFile - the PDF file from an InputStream
      model - the loaded Arlington model
      outputFile - where to write the fixed file
      Returns:
      the changes made, the issues found in the fixed file and whether it was written
      Throws:
      IOException - if the PDF cannot be opened/parsed/written, or it is encrypted
      See Also:
      API Note:
      PDF files must allow for random access, so the InputStream is read fully then cached to disk. The file is encrypted at rest and deleted once close is called.
    • fixFile

      public static PdfArlingtonValidator.FixResult fixFile(InputStream pdfFile, Map<String,org.jpedal.arlington.model.ArlingtonObject> model, Set<String> extensions, File outputFile) throws IOException
      Fix a single PDF file against the Arlington model and write the fixed file if no errors remain, assuming a given set of Arlington extensions are implemented.
      Parameters:
      pdfFile - the PDF file from an InputStream
      model - the loaded Arlington model
      extensions - the Arlington extensions to assume are implemented or null
      outputFile - where to write the fixed file
      Returns:
      the changes made, the issues found in the fixed file and whether it was written
      Throws:
      IOException - if the PDF cannot be opened/parsed/written, or it is encrypted
      See Also:
      API Note:
      PDF files must allow for random access, so the InputStream is read fully then cached to disk. The file is encrypted at rest and deleted once close is called.
    • fixFile

      public static PdfArlingtonValidator.FixResult fixFile(InputStream pdfFile, byte[] password, Map<String,org.jpedal.arlington.model.ArlingtonObject> model, File outputFile) throws IOException
      Fix a single PDF file against the Arlington model and write the fixed file if no errors remain.
      Parameters:
      pdfFile - the PDF file from an InputStream
      password - the password for the file, or null if it does not have one
      model - the loaded Arlington model
      outputFile - where to write the fixed file
      Returns:
      the changes made, the issues found in the fixed file and whether it was written
      Throws:
      IOException - if the PDF cannot be opened/parsed/written, or it is encrypted
      See Also:
      API Note:
      PDF files must allow for random access, so the InputStream is read fully then cached to disk. The file is encrypted at rest and deleted once close is called.
    • fixFile

      public static PdfArlingtonValidator.FixResult fixFile(InputStream pdfFile, byte[] password, Map<String,org.jpedal.arlington.model.ArlingtonObject> model, Set<String> extensions, File outputFile) throws IOException
      Fix a single PDF file against the Arlington model and write the fixed file if no errors remain, assuming a given set of Arlington extensions are implemented.
      Parameters:
      pdfFile - the PDF file from an InputStream
      password - the password for the file, or null if it does not have one
      model - the loaded Arlington model
      extensions - the Arlington extensions to assume are implemented or null
      outputFile - where to write the fixed file
      Returns:
      the changes made, the issues found in the fixed file and whether it was written
      Throws:
      IOException - if the PDF cannot be opened/parsed/written, or it is encrypted
      See Also:
      API Note:
      PDF files must allow for random access, so the InputStream is read fully then cached to disk. The file is encrypted at rest and deleted once close is called.