Search in sources :

Example 1 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class ActionManagerFactory method callCreateAction.

/**
 * Call the create action to add the ActionManager to the result list. If the aDict parameter isn't an instance of
 * COSDictionary, this method throws a ValdiationException. If the aDict parameter is a reference to a
 * COSDicitonary, the action manager is create only if the linked COSObjectKey is missing from the "alreadyCreated"
 * map, in this case the action is added to the map. If the aDict parameter is an instance of COSDIctionary, it is
 * impossible to check if the ActionManager already exists in the "alreadyCreated" map.
 *
 * @param aDict
 *            a COSBase object (COSObject or COSDictionary) which represent the action dictionary.
 * @param ctx
 *            the preflight validation context.
 * @param result
 *            the list of ActionManager to updated if the aDict parameter is valid.
 * @param additionActionKey
 *            the Action identifier if it is an additional action
 * @param alreadyCreated
 *            This map is used to know if an Action has already been validated. It is useful to avoid infinite loop
 *            in an action which has a Next entry.
 * @throws ValidationException
 */
private void callCreateAction(COSBase aDict, PreflightContext ctx, List<AbstractActionManager> result, String additionActionKey, Map<COSObjectKey, Boolean> alreadyCreated) throws ValidationException {
    COSDocument cosDocument = ctx.getDocument().getDocument();
    if (COSUtils.isDictionary(aDict, cosDocument)) {
        if (aDict instanceof COSObject) {
            COSObjectKey cok = new COSObjectKey((COSObject) aDict);
            if (!alreadyCreated.containsKey(cok)) {
                result.add(createActionManager(ctx, COSUtils.getAsDictionary(aDict, cosDocument), additionActionKey));
                alreadyCreated.put(cok, true);
            }
        } else {
            result.add(createActionManager(ctx, COSUtils.getAsDictionary(aDict, cosDocument), additionActionKey));
        }
    } else {
        ctx.addValidationError(new ValidationError(PreflightConstants.ERROR_ACTION_INVALID_TYPE, "Action entry isn't an instance of COSDictionary"));
    }
}
Also used : COSObjectKey(org.apache.pdfbox.cos.COSObjectKey) COSObject(org.apache.pdfbox.cos.COSObject) COSDocument(org.apache.pdfbox.cos.COSDocument) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 2 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class GoToRemoteAction method innerValid.

/*
     * (non-Javadoc)
     * 
     * @see AbstractActionManager#valid(java.util.List)
     */
@Override
protected boolean innerValid() throws ValidationException {
    COSBase dest = this.actionDictionnary.getItem(COSName.D);
    // ---- D entry is mandatory
    if (dest == null) {
        context.addValidationError(new ValidationError(ERROR_ACTION_MISING_KEY, "/D entry is mandatory for the GoToActions"));
        return false;
    }
    COSDocument cosDocument = this.context.getDocument().getDocument();
    if (!(dest instanceof COSName || COSUtils.isString(dest, cosDocument) || COSUtils.isArray(dest, cosDocument))) {
        context.addValidationError(new ValidationError(ERROR_ACTION_INVALID_TYPE, "Type " + dest.getClass().getSimpleName() + " of /D entry is invalid"));
        return false;
    }
    COSBase f = this.actionDictionnary.getItem(COSName.F);
    if (f == null) {
        context.addValidationError(new ValidationError(ERROR_ACTION_MISING_KEY, "/F entry is mandatory for the GoToRemoteActions"));
        return false;
    }
    if (dest instanceof COSArray) {
        COSArray ar = (COSArray) dest;
        if (ar.size() < 2) {
            context.addValidationError(new ValidationResult.ValidationError(ERROR_SYNTAX_DICT_INVALID, "Destination array must have at least 2 elements"));
            return false;
        }
        if (!(ar.get(1) instanceof COSName)) {
            context.addValidationError(new ValidationResult.ValidationError(ERROR_SYNTAX_DICT_INVALID, "Second element of destination array must be a name"));
            return false;
        }
        validateExplicitDestination(ar);
    }
    try {
        PDDestination.create(dest);
    } catch (IOException e) {
        context.addValidationError(new ValidationResult.ValidationError(PreflightConstants.ERROR_SYNTAX_DICT_INVALID, e.getMessage(), e));
        return false;
    }
    return true;
}
Also used : COSName(org.apache.pdfbox.cos.COSName) COSArray(org.apache.pdfbox.cos.COSArray) COSBase(org.apache.pdfbox.cos.COSBase) COSDocument(org.apache.pdfbox.cos.COSDocument) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) IOException(java.io.IOException) ValidationResult(org.apache.pdfbox.preflight.ValidationResult) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 3 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class ThreadAction method innerValid.

/*
     * (non-Javadoc)
     * 
     * @see net.awl.edoc.pdfa.validation.actions.AbstractActionManager#valid(java.util .List)
     */
@Override
protected boolean innerValid() {
    COSBase d = this.actionDictionnary.getItem(COSName.D);
    // ---- D entry is mandatory
    if (d == null) {
        context.addValidationError(new ValidationError(ERROR_ACTION_MISING_KEY, "D entry is mandatory for the ThreadAction"));
        return false;
    }
    COSDocument cosDocument = this.context.getDocument().getDocument();
    if (!(COSUtils.isInteger(d, cosDocument) || COSUtils.isString(d, cosDocument) || COSUtils.isDictionary(d, cosDocument))) {
        context.addValidationError(new ValidationError(ERROR_ACTION_INVALID_TYPE, "D entry type is invalid"));
        return false;
    }
    return true;
}
Also used : COSBase(org.apache.pdfbox.cos.COSBase) COSDocument(org.apache.pdfbox.cos.COSDocument) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 4 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class AnnotationValidator method checkPopup.

/**
 * This method validates the Popup entry. This entry shall contain an other Annotation. This annotation is validated
 * with the right AnnotationValidator.
 *
 * @return true if the popup entry is valid, false if not.
 * @throws ValidationException
 */
protected boolean checkPopup() throws ValidationException {
    COSBase cosPopup = this.annotDictionary.getItem(ANNOT_DICTIONARY_VALUE_SUBTYPE_POPUP);
    if (cosPopup != null) {
        COSDictionary popupDict = COSUtils.getAsDictionary(cosPopup, cosDocument);
        if (popupDict == null) {
            ctx.addValidationError(new ValidationError(ERROR_SYNTAX_DICT_INVALID, "An Annotation has a Popup entry, but the value is missing or isn't a dictionary"));
            return false;
        }
        AnnotationValidator popupVal = this.annotFact.getAnnotationValidator(ctx, popupDict);
        return popupVal.validate();
    }
    return true;
}
Also used : COSDictionary(org.apache.pdfbox.cos.COSDictionary) COSBase(org.apache.pdfbox.cos.COSBase) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 5 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class AnnotationValidator method checkFlags.

/**
 * Checks if flags of the annotation are authorized.
 * <UL>
 * <li>Print flag must be 1
 * <li>NoView flag must be 0
 * <li>Hidden flag must be 0
 * <li>Invisible flag must be 0
 * </UL>
 * If one of these flags is invalid, the errors list is updated with the ERROR_ANNOT_FORBIDDEN_FLAG ValidationError
 * code.
 *
 * @return false if a flag is invalid, true otherwise.
 */
protected boolean checkFlags() {
    boolean result = this.pdAnnot.isPrinted();
    result = result && !this.pdAnnot.isHidden();
    result = result && !this.pdAnnot.isInvisible();
    result = result && !this.pdAnnot.isNoView();
    if (!result) {
        ctx.addValidationError(new ValidationError(ERROR_ANNOT_FORBIDDEN_FLAG, "Flags of " + pdAnnot.getSubtype() + " annotation are invalid"));
    }
    return result;
}
Also used : ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Aggregations

ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)108 COSBase (org.apache.pdfbox.cos.COSBase)36 COSDictionary (org.apache.pdfbox.cos.COSDictionary)28 IOException (java.io.IOException)27 COSDocument (org.apache.pdfbox.cos.COSDocument)13 COSObject (org.apache.pdfbox.cos.COSObject)13 COSArray (org.apache.pdfbox.cos.COSArray)10 COSStream (org.apache.pdfbox.cos.COSStream)10 COSString (org.apache.pdfbox.cos.COSString)8 PDColorSpace (org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)8 ValidationException (org.apache.pdfbox.preflight.exception.ValidationException)8 COSName (org.apache.pdfbox.cos.COSName)7 COSObjectKey (org.apache.pdfbox.cos.COSObjectKey)7 PreflightPath (org.apache.pdfbox.preflight.PreflightPath)6 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 PDStream (org.apache.pdfbox.pdmodel.common.PDStream)5 PreflightParser (org.apache.pdfbox.preflight.parser.PreflightParser)5 DublinCoreSchema (org.apache.xmpbox.schema.DublinCoreSchema)5 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)4