Skip to content

Commit

Permalink
Support several flavours
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximPlusov committed Jul 30, 2024
1 parent 7bda92d commit 0bb908e
Show file tree
Hide file tree
Showing 24 changed files with 748 additions and 335 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@

import org.verapdf.pdfa.flavours.PDFAFlavour;

import java.util.Collections;
import java.util.List;

public class StaticCoreContainers {

private static final ThreadLocal<PDFAFlavour> flavour = new ThreadLocal<>();
private static final ThreadLocal<List<PDFAFlavour>> flavour = new ThreadLocal<>();

public static void clearAllContainers() {
flavour.set(null);
}

public static PDFAFlavour getFlavour() {
public static List<PDFAFlavour> getFlavour() {
return flavour.get();
}

public static void setFlavour(PDFAFlavour flavour) {
public static void setFlavour(List<PDFAFlavour> flavour) {
StaticCoreContainers.flavour.set(flavour);
}

public static void setFlavour(PDFAFlavour flavour) {
StaticCoreContainers.flavour.set(Collections.singletonList(flavour));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package org.verapdf.model.impl.axl;

import org.verapdf.containers.StaticCoreContainers;
import org.verapdf.pdfa.flavours.PDFFlavours;
import org.verapdf.xmp.XMPConst;
import org.verapdf.xmp.impl.VeraPDFXMPNode;
import org.verapdf.model.tools.xmp.ValidatorsContainer;
Expand Down Expand Up @@ -99,7 +100,7 @@ public Boolean getisValueTypeValidText() {

@Override
public Boolean getisValueTypeDefined() {
if (StaticCoreContainers.getFlavour() != null && StaticCoreContainers.getFlavour().getPart() == PDFAFlavour.Specification.ISO_19005_1) {
if (PDFFlavours.isFlavourPart(StaticCoreContainers.getFlavour(), PDFAFlavour.Specification.ISO_19005_1)) {
return isValueTypeValidForPDFA_1();
}
return isValueTypeValidForPDFA_2_3();
Expand Down
30 changes: 30 additions & 0 deletions core/src/main/java/org/verapdf/pdfa/AbstractFoundry.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.verapdf.pdfa.validation.validators.ValidatorConfig;
import org.verapdf.pdfa.validation.validators.ValidatorFactory;

import java.util.List;
/**
* @author <a href="mailto:carl@openpreservation.org">Carl Wilson</a>
* <a href="https://github.com/carlwilson">carlwilson AT github</a>
Expand Down Expand Up @@ -57,6 +58,16 @@ public PDFAValidator createValidator(ValidatorConfig config, PDFAFlavour flavour
config.isRecordPasses(), config.showErrorMessages(), config.getShowProgress());
}

@Override
public PDFAValidator createValidator(ValidatorConfig config, List<PDFAFlavour> flavours) {
if (config.getMaxFails() > 0) {
return createFailFastValidator(flavours, config.getMaxFails(), config.getMaxNumberOfDisplayedFailedChecks(),
config.isRecordPasses(), config.showErrorMessages(), config.getShowProgress());
}
return createValidator(flavours, config.getMaxNumberOfDisplayedFailedChecks(),
config.isRecordPasses(), config.showErrorMessages(), config.getShowProgress());
}

@Override
public PDFAValidator createValidator(ValidatorConfig config, ValidationProfile profile) {
if (config.getMaxFails() > 0) {
Expand All @@ -67,6 +78,11 @@ public PDFAValidator createValidator(ValidatorConfig config, ValidationProfile p
config.isRecordPasses(), config.showErrorMessages(), config.getShowProgress());
}

@Override
public PDFAValidator createValidator(List<PDFAFlavour> flavours) {
return ValidatorFactory.createValidator(flavours);
}

@Override
public PDFAValidator createValidator(PDFAFlavour flavour, boolean logSuccess) {
return ValidatorFactory.createValidator(flavour, logSuccess);
Expand All @@ -84,6 +100,13 @@ public PDFAValidator createValidator(PDFAFlavour flavour, int maxNumberOfDisplay
showProgress);
}

@Override
public PDFAValidator createValidator(List<PDFAFlavour> flavours, int maxNumberOfDisplayedFailedChecks,
boolean logSuccess, boolean showErrorMessages, boolean showProgress) {
return ValidatorFactory.createValidatorByFlavours(flavours, maxNumberOfDisplayedFailedChecks, logSuccess, showErrorMessages,
showProgress);
}

@Override
public PDFAValidator createValidator(ValidationProfile profile, int maxNumberOfDisplayedFailedChecks,
boolean logSuccess, boolean showErrorMessages, boolean showProgress) {
Expand All @@ -98,6 +121,13 @@ public PDFAValidator createFailFastValidator(PDFAFlavour flavour, int maxFailure
showErrorMessages, showProgress);
}

@Override
public PDFAValidator createFailFastValidator(List<PDFAFlavour> flavours, int maxFailures, int maxNumberOfDisplayedFailedChecks,
boolean logSuccess, boolean showErrorMessages, boolean showProgress) {
return ValidatorFactory.createValidator(flavours, logSuccess, maxFailures, maxNumberOfDisplayedFailedChecks,
showErrorMessages, showProgress);
}

@Override
public PDFAValidator createFailFastValidator(ValidationProfile profile, int maxFailures, int maxNumberOfDisplayedFailedChecks,
boolean logSuccess, boolean showErrorMessages, boolean showProgress) {
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/org/verapdf/pdfa/PDFAParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public interface PDFAParser extends Component {
*/
public PDFAFlavour getFlavour();

public List<PDFAFlavour> getFlavours();

public void setFlavours(List<PDFAFlavour> flavours);

/**
* @return the {@link org.verapdf.metadata.fixer.entity.PDFDocument} parsed.
*/
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/org/verapdf/pdfa/PDFAValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.verapdf.pdfa.validation.profiles.ValidationProfile;
import org.verapdf.processor.reports.enums.JobEndStatus;

import java.util.List;

/**
* A PDFAValidator performs a series of checks on PDF/A documents to verify that
* the document conforms to a specific PDF/A flavour.
Expand Down Expand Up @@ -65,6 +67,8 @@ public interface PDFAValidator extends Component {
*/
public ValidationResult validate(PDFAParser toValidate) throws ValidationException;

public List<ValidationResult> validateAll(PDFAParser toValidate) throws ValidationException;

public String getValidationProgressString();

public void cancelValidation(JobEndStatus endStatus);
Expand Down
38 changes: 38 additions & 0 deletions core/src/main/java/org/verapdf/pdfa/VeraPDFFoundry.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.io.File;
import java.io.InputStream;
import java.util.List;

/**
* The veraPDFFoundry interface provides methods for creating implementations of
Expand Down Expand Up @@ -167,6 +168,20 @@ public PDFAParser createParser(File pdfFile)
*/
public PDFAValidator createValidator(ValidatorConfig config, PDFAFlavour flavour);

/**
* Obtain a new {@link PDFAValidator} instance that uses the list of a custom
* {@link org.verapdf.pdfa.flavours.PDFAFlavour}s.
*
* @param config
* a {@link ValidatorConfig} instance used to configure the
* {@link PDFAValidator}
* @param flavours
* the list of particular {@link org.verapdf.pdfa.flavours.PDFAFlavour}s
* to validated against.
* @return an appropriately configured {@link PDFAValidator} instance.
*/
public PDFAValidator createValidator(ValidatorConfig config, List<PDFAFlavour> flavours);

/**
* Creates a new {@link PDFAValidator} instance that uses one of the
* {@link ValidationProfile}s packaged as a core library resource. While
Expand All @@ -188,6 +203,23 @@ public PDFAParser createParser(File pdfFile)
*/
public PDFAValidator createValidator(PDFAFlavour flavour, boolean logSuccess);

/**
* Creates a new {@link PDFAValidator} instance that uses
* {@link ValidationProfile}s packaged as a core library resource. While
* these profiles are not guaranteed to be up to date, they are available
* when offline. A {@link org.verapdf.pdfa.validation.profiles.ProfileDirectory} populated with the pre-loaded
* profiles can be obtained by calling
* {@link org.verapdf.pdfa.validation.profiles.Profiles#getVeraProfileDirectory()}.
*
* @param flavours
* list of the {@link PDFAFlavour} that are associated with the
* {@code ValidationProfile} to used to initialise the
* {@code PDFAValidator}.
* @return a {@link PDFAValidator} instance initialised from the passed
* parameters
*/
public PDFAValidator createValidator(List<PDFAFlavour> flavours);

/**
* Creates a new {@link PDFAValidator} initialised with the passed profile
* and chosen passed test logging.
Expand All @@ -207,6 +239,9 @@ public PDFAParser createParser(File pdfFile)
public PDFAValidator createValidator(PDFAFlavour flavour, int maxNumberOfDisplayedFailedChecks,
boolean logSuccess, boolean showErrorMessages, boolean showProgress);

public PDFAValidator createValidator(List<PDFAFlavour> flavour, int maxNumberOfDisplayedFailedChecks,
boolean logSuccess, boolean showErrorMessages, boolean showProgress);

public PDFAValidator createValidator(ValidationProfile profile, int maxNumberOfDisplayedFailedChecks,
boolean logSuccess, boolean showErrorMessages, boolean showProgress);

Expand All @@ -230,6 +265,9 @@ public PDFAValidator createValidator(ValidationProfile profile, int maxNumberOfD
public PDFAValidator createFailFastValidator(PDFAFlavour flavour, int maxFailures, int maxNumberOfDisplayedFailedChecks,
boolean logSuccess, boolean showErrorMessages, boolean showProgress);

public PDFAValidator createFailFastValidator(List<PDFAFlavour> flavours, int maxFailures, int maxNumberOfDisplayedFailedChecks,
boolean logSuccess, boolean showErrorMessages, boolean showProgress);

/**
* Creates a new {@link PDFAValidator} initialised with the passed profile,
* requested fast failing behaviour and configured NOT to log passed checks.
Expand Down
68 changes: 67 additions & 1 deletion core/src/main/java/org/verapdf/pdfa/flavours/PDFFlavours.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
package org.verapdf.pdfa.flavours;

import java.util.List;

public class PDFFlavours {

public static boolean isPDFUARelatedFlavour(List<PDFAFlavour> flavours) {
for (PDFAFlavour flavour : flavours) {
if (isPDFUARelatedFlavour(flavour)) {
return true;
}
}
return false;
}

public static boolean isPDFUARelatedFlavour(PDFAFlavour flavour) {
return isFlavourFamily(flavour, PDFAFlavour.SpecificationFamily.PDF_UA) || isWTPDFFlavour(flavour) || isWCAGFlavour(flavour);
Expand All @@ -10,17 +21,54 @@ public static boolean isPDFUA1RelatedFlavour(PDFAFlavour flavour) {
return isFlavour(flavour, PDFAFlavour.PDFUA_1) || isWCAGFlavour(flavour);
}

public static boolean isPDFUA2RelatedFlavour(List<PDFAFlavour> flavours) {
for (PDFAFlavour flavour : flavours) {
if (isPDFUA2RelatedFlavour(flavour)) {
return true;
}
}
return false;
}

public static boolean isPDFUA2RelatedFlavour(PDFAFlavour flavour) {
return isFlavour(flavour, PDFAFlavour.PDFUA_2) || isFlavourPart(flavour, PDFAFlavour.Specification.WTPDF_1_0);
}

public static boolean isWCAGFlavour(List<PDFAFlavour> flavours) {
for (PDFAFlavour flavour : flavours) {
if (isWCAGFlavour(flavour)) {
return true;
}
}
return false;
}


public static boolean isWCAGFlavour(PDFAFlavour flavour) {
return isFlavourFamily(flavour, PDFAFlavour.SpecificationFamily.WCAG);
}


public static boolean isWTPDFFlavour(List<PDFAFlavour> flavours) {
for (PDFAFlavour flavour : flavours) {
if (isWTPDFFlavour(flavour)) {
return true;
}
}
return false;
}

public static boolean isWTPDFFlavour(PDFAFlavour flavour) {
return isFlavourFamily(flavour, PDFAFlavour.SpecificationFamily.WTPDF);
}

public static boolean isFlavour(List<PDFAFlavour> flavours, PDFAFlavour checkedFlavour) {
for (PDFAFlavour flavour : flavours) {
if (isFlavour(flavour, checkedFlavour)) {
return true;
}
}
return false;
}

public static boolean isFlavour(PDFAFlavour currentFlavour, PDFAFlavour checkedFlavour) {
return currentFlavour == checkedFlavour;
Expand All @@ -29,11 +77,29 @@ public static boolean isFlavour(PDFAFlavour currentFlavour, PDFAFlavour checkedF
public static boolean isFlavourFamily(PDFAFlavour flavour, PDFAFlavour.SpecificationFamily family) {
return flavour != null && flavour.getPart().getFamily() == family;
}

public static boolean isFlavourPart(List<PDFAFlavour> flavours, PDFAFlavour.Specification part) {
for (PDFAFlavour flavour : flavours) {
if (isFlavourPart(flavour, part)) {
return true;
}
}
return false;
}

public static boolean isFlavourPart(PDFAFlavour flavour, PDFAFlavour.Specification part) {
return flavour != null && flavour.getPart() == part;
}

public static boolean isPDFSpecification(List<PDFAFlavour> flavours, PDFAFlavour.PDFSpecification pdfSpecification) {
for (PDFAFlavour flavour : flavours) {
if (isPDFSpecification(flavour, pdfSpecification)) {
return true;
}
}
return false;
}

public static boolean isPDFSpecification(PDFAFlavour flavour, PDFAFlavour.PDFSpecification pdfSpecification) {
return flavour != null && flavour.getPart().getPdfSpecification() == pdfSpecification;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
package org.verapdf.pdfa.validation.profiles;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;

Expand Down Expand Up @@ -72,6 +73,8 @@ public interface ProfileDirectory {
* if flavour is null
*/
public ValidationProfile getValidationProfileByFlavour(PDFAFlavour flavour);

public List<ValidationProfile> getValidationProfilesByFlavours(List<PDFAFlavour> flavours);

/**
* @return the full set of {@link ValidationProfile}s held in the directory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;

import java.util.List;
import java.util.LinkedList;
import javax.xml.bind.JAXBException;

import org.verapdf.core.Directory;
Expand Down Expand Up @@ -107,6 +108,15 @@ public ValidationProfile getValidationProfileByFlavour(PDFAFlavour flavour) {
return profile;
}

@Override
public List<ValidationProfile> getValidationProfilesByFlavours(List<PDFAFlavour> flavours) {
List<ValidationProfile> profiles = new LinkedList<>();
for (PDFAFlavour flavour : flavours) {
profiles.add(getValidationProfileByFlavour(flavour));
}
return profiles;
}

/**
* { @inheritDoc }
*/
Expand Down
Loading

0 comments on commit 0bb908e

Please sign in to comment.