use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.
the class PreflightParser method checkEndstreamKeyWord.
/**
* 'endstream' must be preceded by an EOL
*
* @throws IOException
*/
protected void checkEndstreamKeyWord() throws IOException {
source.seek(source.getPosition() - 10);
if (!nextIsEOL()) {
addValidationError(new ValidationError(ERROR_SYNTAX_STREAM_DELIMITER, "Expected 'EOL' before the endstream keyword at offset " + source.getPosition() + " but found '" + source.peek() + "'"));
}
String endstreamV = readString();
if (!endstreamV.equals("endstream")) {
addValidationError(new ValidationError(ERROR_SYNTAX_STREAM_DELIMITER, "Expected 'endstream' keyword at offset " + source.getPosition() + " but found '" + endstreamV + "'"));
}
}
use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.
the class PreflightParser method parseCOSString.
/**
* Check that the hexa string contains only an even number of
* Hexadecimal characters. Once it is done, reset the offset at the beginning of the string and
* call {@link PDFParser#parseCOSString()}
*
* @return The parsed PDF string.
*
* @throws IOException If there is an error reading from the stream.
*/
@Override
protected COSString parseCOSString() throws IOException {
// offset reminder
long offset = source.getPosition();
char nextChar = (char) source.read();
int count = 0;
if (nextChar == '<') {
do {
nextChar = (char) source.read();
if (nextChar != '>') {
if (isWhitespace(nextChar)) {
// ignore space characters
continue;
}
if (Character.digit(nextChar, 16) >= 0) {
count++;
} else {
addValidationError(new ValidationError(ERROR_SYNTAX_HEXA_STRING_INVALID, "Hexa String must have only Hexadecimal Characters (found '" + nextChar + "') at offset " + source.getPosition()));
break;
}
}
} while (nextChar != '>');
}
if (count % 2 != 0) {
addValidationError(new ValidationError(ERROR_SYNTAX_HEXA_STRING_EVEN_NUMBER, "Hexa string shall contain even number of non white space char at offset " + source.getPosition()));
}
// reset the offset to parse the COSString
source.seek(offset);
COSString result = super.parseCOSString();
if (result.getString().length() > MAX_STRING_LENGTH) {
addValidationError(new ValidationError(ERROR_SYNTAX_HEXA_STRING_TOO_LONG, "Hexa string is too long at offset " + source.getPosition()));
}
return result;
}
use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.
the class PreflightParser method checkPdfHeader.
/**
* Check that the PDF header match rules of the PDF/A specification. First line (offset 0) must
* be a comment with the PDF version (version 1.0 isn't conform to the PDF/A specification)
* Second line is a comment with at least 4 bytes greater than 0x80
*/
protected void checkPdfHeader() {
try {
source.seek(0);
String firstLine = readLine();
if (firstLine == null || !firstLine.matches("%PDF-1\\.[1-9]")) {
addValidationError(new ValidationError(PreflightConstants.ERROR_SYNTAX_HEADER, "First line must match %PDF-1.\\d"));
}
String secondLine = readLine();
if (secondLine != null) {
byte[] secondLineAsBytes = secondLine.getBytes(encoding.name());
if (secondLineAsBytes.length >= 5) {
if (secondLineAsBytes[0] != '%') {
addValidationError(new ValidationError(PreflightConstants.ERROR_SYNTAX_HEADER, "Second line must begin with '%' followed by at least 4 bytes greater than 127"));
} else {
for (int i = 1; i < 5; ++i) {
byte b = secondLineAsBytes[i];
if ((b & 0xFF) < 0x80) {
addValidationError(new ValidationError(PreflightConstants.ERROR_SYNTAX_HEADER, "Second line must begin with '%' followed by at least 4 bytes greater than 127"));
break;
}
}
}
} else {
addValidationError(new ValidationError(PreflightConstants.ERROR_SYNTAX_HEADER, "Second line must begin with '%' followed by at least 4 bytes greater than 127"));
}
}
source.seek(0);
} catch (IOException e) {
addValidationError(new ValidationError(PreflightConstants.ERROR_SYNTAX_HEADER, "Unable to read the PDF file : " + e.getMessage(), e));
}
}
use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.
the class XmlResultParser method createResponseWithError.
protected void createResponseWithError(Document rdocument, String pdfType, ValidationResult result, Element preflight) {
Element valid = rdocument.createElement("isValid");
valid.setAttribute("type", pdfType);
valid.setTextContent("false");
preflight.appendChild(valid);
// errors list
Element errors = rdocument.createElement("errors");
Map<ValidationError, Integer> cleaned = cleanErrorList(result.getErrorsList());
preflight.appendChild(errors);
int totalCount = 0;
for (Map.Entry<ValidationError, Integer> entry : cleaned.entrySet()) {
Element error = rdocument.createElement("error");
int count = entry.getValue();
error.setAttribute("count", String.format("%d", count));
totalCount += count;
Element code = rdocument.createElement("code");
ValidationError ve = entry.getKey();
code.setTextContent(ve.getErrorCode());
error.appendChild(code);
Element detail = rdocument.createElement("details");
detail.setTextContent(ve.getDetails());
error.appendChild(detail);
if (ve.getPageNumber() != null) {
Element page = rdocument.createElement("page");
page.setTextContent(ve.getPageNumber().toString());
error.appendChild(page);
}
errors.appendChild(error);
}
errors.setAttribute("count", String.format("%d", totalCount));
}
use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.
the class AbstractProcess method addFontError.
/**
* PDFont loads embedded fonts in its constructor so we have to handle IOExceptions
* from PDFont and translate them into validation errors.
*/
protected void addFontError(COSDictionary dictionary, PreflightContext context, IOException e) {
COSName type = dictionary.getCOSName(COSName.TYPE, COSName.FONT);
if (!COSName.FONT.equals(type)) {
addValidationError(context, new ValidationError(PreflightConstants.ERROR_FONTS_UNKNOWN_FONT_TYPE, "Expected 'Font' dictionary but found '" + type.getName() + "'"));
}
String fontName = "Unknown";
if (dictionary.containsKey(COSName.BASE_FONT)) {
fontName = dictionary.getNameAsString(COSName.BASE_FONT);
}
COSName subType = dictionary.getCOSName(COSName.SUBTYPE);
if (COSName.TYPE1.equals(subType)) {
addValidationError(context, new ValidationError(PreflightConstants.ERROR_FONTS_TYPE1_DAMAGED, "The FontFile can't be read for " + fontName + ": " + e.getMessage()));
} else if (COSName.MM_TYPE1.equals(subType)) {
addValidationError(context, new ValidationError(PreflightConstants.ERROR_FONTS_TYPE1_DAMAGED, "The FontFile can't be read for " + fontName + ": " + e.getMessage()));
} else if (COSName.TRUE_TYPE.equals(subType)) {
addValidationError(context, new ValidationError(PreflightConstants.ERROR_FONTS_TRUETYPE_DAMAGED, "The FontFile can't be read for " + fontName + ": " + e.getMessage()));
} else if (COSName.TYPE3.equals(subType)) {
addValidationError(context, new ValidationError(PreflightConstants.ERROR_FONTS_TYPE3_DAMAGED, "The FontFile can't be read for " + fontName + ": " + e.getMessage()));
} else if (COSName.TYPE0.equals(subType)) {
addValidationError(context, new ValidationError(PreflightConstants.ERROR_FONTS_CID_DAMAGED, "The FontFile can't be read for " + fontName + ": " + e.getMessage()));
} else if (COSName.CID_FONT_TYPE0.equals(subType)) {
addValidationError(context, new ValidationError(PreflightConstants.ERROR_FONTS_UNKNOWN_FONT_TYPE, "Unexpected CIDFontType0 descendant font for " + fontName + ": " + e.getMessage()));
} else if (COSName.CID_FONT_TYPE2.equals(subType)) {
addValidationError(context, new ValidationError(PreflightConstants.ERROR_FONTS_UNKNOWN_FONT_TYPE, "Unexpected CIDFontType2 descendant font for " + fontName + ": " + e.getMessage()));
} else {
addValidationError(context, new ValidationError(PreflightConstants.ERROR_FONTS_UNKNOWN_FONT_TYPE, "Unknown font type for " + fontName));
}
}
Aggregations