use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.
the class CatalogValidationProcess method validateICCProfile.
/**
* This method checks the destOutputProfile which must be a valid ICCProfile.
*
* If an other ICCProfile exists in the mapDestOutputProfile, a ValdiationError
* (ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_MULTIPLE) is returned because of only one profile is authorized. If the
* ICCProfile already exist in the mapDestOutputProfile, the method returns null. If the destOutputProfile contains
* an invalid ICCProfile, a ValidationError (ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_INVALID) is returned If the
* destOutputProfile is an empty stream, a ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY) is returned.
*
* If the destOutputFile is valid, mapDestOutputProfile is updated, the ICCProfile is added to the document ctx and
* null is returned.
*
* @param destOutputProfile
* @param mapDestOutputProfile
* @param ctx the preflight context.
* @throws ValidationException
*/
protected void validateICCProfile(COSBase destOutputProfile, Map<COSObjectKey, Boolean> mapDestOutputProfile, PreflightContext ctx) throws ValidationException {
try {
if (destOutputProfile == null) {
return;
}
// destOutputProfile should be an instance of COSObject because of this is a object reference
if (destOutputProfile instanceof COSObject) {
if (mapDestOutputProfile.containsKey(new COSObjectKey((COSObject) destOutputProfile))) {
// the profile is already checked. continue
return;
} else if (!mapDestOutputProfile.isEmpty()) {
// A DestOutputProfile exits but it isn't the same, error
addValidationError(ctx, new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_MULTIPLE, "More than one ICCProfile is defined"));
return;
}
// else the profile will be kept in the tmpDestOutputProfile if it is valid
}
// keep reference to avoid multiple profile definition
mapDestOutputProfile.put(new COSObjectKey((COSObject) destOutputProfile), true);
COSDocument cosDocument = ctx.getDocument().getDocument();
COSStream stream = COSUtils.getAsStream(destOutputProfile, cosDocument);
if (stream == null) {
addValidationError(ctx, new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY, "OutputIntent object uses a NULL Object"));
return;
}
InputStream is = stream.createInputStream();
ICC_Profile iccp = null;
try {
iccp = ICC_Profile.getInstance(is);
} finally {
is.close();
}
if (!validateICCProfileNEntry(stream, ctx, iccp)) {
return;
}
if (!validateICCProfileVersion(iccp, ctx)) {
return;
}
if (ctx.getIccProfileWrapper() == null) {
ctx.setIccProfileWrapper(new ICCProfileWrapper(iccp));
}
} catch (IllegalArgumentException e) {
// this is not a ICC_Profile
addValidationError(ctx, new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_INVALID, "DestOutputProfile isn't a valid ICCProfile: " + e.getMessage(), e));
} catch (IOException e) {
throw new ValidationException("Unable to parse the ICC Profile.", e);
}
}
use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.
the class ExtGStateValidationProcess method checkLowerCA.
/**
* This method checks the "ca" value of the ExtGState dictionary. It is optional but must be 1.0
* if present.
*
* @param context the preflight context.
* @param egs the graphic state to check
*/
private void checkLowerCA(PreflightContext context, COSDictionary egs) {
COSBase lCA = egs.getItem(TRANSPARENCY_DICTIONARY_KEY_LOWER_CA);
if (lCA != null) {
// ---- If ca is present only the value 1.0 is authorized
COSDocument cosDocument = context.getDocument().getDocument();
Float fca = COSUtils.getAsFloat(lCA, cosDocument);
Integer ica = COSUtils.getAsInteger(lCA, cosDocument);
if (!(fca != null && Float.compare(fca, 1.0f) == 0) && !(ica != null && ica == 1)) {
context.addValidationError(new ValidationError(ERROR_TRANSPARENCY_EXT_GS_CA, "ca entry in a ExtGState is invalid"));
}
}
}
use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.
the class ExtGStateValidationProcess method checkFont.
/**
* This method checks a Font array in the ExtGState dictionary.
*
* @param context the preflight context.
* @param egs the Graphic state to check
* @throws ValidationException
*/
private void checkFont(PreflightContext context, COSDictionary egs) throws ValidationException {
COSBase base = egs.getItem(COSName.FONT);
if (base == null) {
return;
}
if (!(base instanceof COSArray) || ((COSArray) base).size() != 2) {
context.addValidationError(new ValidationError(ERROR_SYNTAX_COMMON, "/Font entry in /ExtGState must be an array with 2 elements"));
return;
}
COSArray ar = (COSArray) base;
COSBase base0 = ar.get(0);
if (!(base0 instanceof COSObject)) {
context.addValidationError(new ValidationError(ERROR_SYNTAX_COMMON, "1st element in /Font entry in /ExtGState must be an indirect object"));
return;
}
COSBase base1 = ar.getObject(1);
if (!(base1 instanceof COSNumber)) {
context.addValidationError(new ValidationError(ERROR_SYNTAX_COMMON, "2nd element in /Font entry in /ExtGState must be a number"));
return;
}
COSNumber fontSize = (COSNumber) ar.getObject(1);
if (fontSize.floatValue() > MAX_POSITIVE_FLOAT || fontSize.floatValue() < MAX_NEGATIVE_FLOAT) {
context.addValidationError(new ValidationError(ERROR_SYNTAX_NUMERIC_RANGE, "invalid float range in 2nd element in /Font entry in /ExtGState"));
}
if (ar.getObject(0) instanceof COSDictionary) {
COSDictionary fontDict = (COSDictionary) ar.getObject(0);
try {
PDFont newFont = PDFontFactory.createFont(fontDict);
ContextHelper.validateElement(context, newFont, FONT_PROCESS);
} catch (IOException e) {
addFontError(fontDict, context, e);
}
}
}
use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.
the class GraphicObjectPageValidationProcess method validate.
@Override
public void validate(PreflightContext context) throws ValidationException {
PreflightPath vPath = context.getValidationPath();
XObjectValidator validator = null;
if (!vPath.isEmpty() && vPath.isExpectedType(PDImageXObject.class)) {
validator = new XObjImageValidator(context, (PDImageXObject) vPath.peek());
} else if (!vPath.isEmpty() && vPath.isExpectedType(PDFormXObject.class)) {
validator = new XObjFormValidator(context, (PDFormXObject) vPath.peek());
} else if (!vPath.isEmpty() && vPath.isExpectedType(PDPostScriptXObject.class)) {
validator = new XObjPostscriptValidator(context, (PDPostScriptXObject) vPath.peek());
} else if (!vPath.isEmpty() && vPath.isExpectedType(COSStream.class)) {
context.addValidationError(new ValidationError(PreflightConstants.ERROR_GRAPHIC_XOBJECT_INVALID_TYPE, "Invalid XObject subtype"));
} else {
context.addValidationError(new ValidationError(PreflightConstants.ERROR_GRAPHIC_MISSING_OBJECT, "Graphic validation process needs at least one PDXObject"));
}
if (validator != null) {
validator.validate();
}
}
use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.
the class ResourcesValidationProcess method validateXObjects.
protected void validateXObjects(PreflightContext context, PDResources resources) throws ValidationException {
COSDocument cosDocument = context.getDocument().getDocument();
COSDictionary mapOfXObj = COSUtils.getAsDictionary(resources.getCOSObject().getItem(COSName.XOBJECT), cosDocument);
if (mapOfXObj != null) {
for (Entry<COSName, COSBase> entry : mapOfXObj.entrySet()) {
COSBase xobj = entry.getValue();
if (xobj != null && COSUtils.isStream(xobj, cosDocument)) {
try {
COSStream stream = COSUtils.getAsStream(xobj, cosDocument);
PDXObject pdXObject = PDXObject.createXObject(stream, resources);
if (pdXObject != null) {
ContextHelper.validateElement(context, pdXObject, GRAPHIC_PROCESS);
} else {
ContextHelper.validateElement(context, stream, GRAPHIC_PROCESS);
}
} catch (IOException e) {
context.addValidationError(new ValidationError(ERROR_GRAPHIC_MAIN, e.getMessage() + " for entry '" + entry.getKey().getName() + "'", e));
}
}
}
}
}
Aggregations