Try Catch while parsing XMP-Metadata

This commit is contained in:
Jochen Bohn
2025-12-15 09:16:59 +01:00
parent d2948c63ac
commit 9db9689095
3 changed files with 18 additions and 1 deletions

View File

@@ -150,7 +150,14 @@ public class PDFValidator extends Validator {
final DocumentBuilder builder = factory.newDocumentBuilder(); final DocumentBuilder builder = factory.newDocumentBuilder();
final InputSource is = new InputSource(new StringReader(xmp)); final InputSource is = new InputSource(new StringReader(xmp));
try {
docXMP = builder.parse(is); docXMP = builder.parse(is);
} catch (Exception e) {
context.addResultItem(
new ValidationResultItem(ESeverity.error, "XMP Metadata: Could not parse XMP metadata")
.setSection(10).setPart(EPart.pdf));
throw e;
}
final XPathFactory xpathFactory = XPathFactory.newInstance(); final XPathFactory xpathFactory = XPathFactory.newInstance();

View File

@@ -169,6 +169,16 @@ public class PDFValidatorTest extends ResourceCase {
assertEquals(false, actual.contains("<error"));// issue 18: "ConformanceLevel not found" should not be assertEquals(false, actual.contains("<error"));// issue 18: "ConformanceLevel not found" should not be
// reported since it's actually there // reported since it's actually there
contents = getResourceAsByteArray("invalidXMP-ParseError.pdf");
pv.setFilenameAndContents("invalidXMP-ParseError.pdf", contents);
vc.clear();
pv.validate();
actual = pv.getXMLResult();
assertEquals(true, actual
.contains("<error type=\"10\">XMP Metadata: Could not parse XMP metadata</error>"));
} catch (final IrrecoverableValidationError e) { } catch (final IrrecoverableValidationError e) {
// ignore, will be in XML output anyway // ignore, will be in XML output anyway
} }

Binary file not shown.