Skip to content

Commit

Permalink
close #434: Cannot update metadata: Namespace for prefix 'rdf' has no…
Browse files Browse the repository at this point in the history
…t been declared
  • Loading branch information
ediweissmann committed Jun 28, 2024
1 parent b3c3339 commit d1ab11c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,10 @@ public void execute(SetMetadataParameters parameters) throws TaskException {
try {
updateXmpMetadata(catalog, actualMeta);
} catch (RuntimeException ex) {
if(exceptionStackContains("Namespace for prefix 'xmp' has not been declared", ex)) {
if(exceptionStackContains("Namespace for prefix 'xmp' has not been declared", ex) ||
exceptionStackContains("Namespace for prefix 'rdf' has not been declared", ex)) {
// try to fix the metadata and retry
fixMissingXmpNamespace(catalog);
fixMissingXmpOrRdfNamespace(catalog);
updateXmpMetadata(catalog, actualMeta);
} else {
throw ex;
Expand Down Expand Up @@ -229,18 +230,27 @@ private void deleteAttr(String path, String attrName, Document document) throws
}
}

private void fixMissingXmpNamespace(PDDocumentCatalog catalog) throws IOException {
private void fixMissingXmpOrRdfNamespace(PDDocumentCatalog catalog) throws IOException {
try(InputStream is = catalog.getMetadata().createInputStream()) {
String metadataAsString = new String(is.readAllBytes(), StandardCharsets.UTF_8);
String metadataAsStringOriginal = metadataAsString;

if (!metadataAsString.contains("xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\"")) {
LOG.warn("Metadata seems to be missing xmlns:xmp namespace definition, adding it");
metadataAsString = metadataAsString.replaceAll("<rdf:Description", "<rdf:Description xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\"");

}

if (!metadataAsString.contains("xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"")) {
LOG.warn("Metadata seems to be missing xmlns:rdf namespace definition, adding it");
metadataAsString = metadataAsString.replaceAll("<rdf:RDF", "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">");
}

if(!metadataAsStringOriginal.equals(metadataAsString)){
catalog.setMetadata(new PDMetadata(new ByteArrayInputStream(metadataAsString.getBytes(StandardCharsets.UTF_8))));
}
}
}
}

private void updateXmpMetadata(PDDocumentCatalog catalog, PDDocumentInformation metadata) {
try {
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public void invalidXmlChars_updatedXmpMetadata() throws IOException {

@Test
public void xmpMetadataMissingNamespaceAndWithAttributesInsteadofNodes() throws IOException {
setUpParams(customInput("pdf/xmp_metadata_missing_namespace.pdf"));
setUpParams(customInput("pdf/xmp_metadata_missing_xmp_namespace.pdf"));
parameters.put(PdfMetadataFields.CREATOR, "test_creator");
parameters.put("Producer", "test_producer");

Expand Down Expand Up @@ -306,6 +306,36 @@ public void xmpMetadataMissingNamespaceAndWithAttributesInsteadofNodes() throws
});
}

@Test
public void xmpMetadataMissingNamespace_rdf() throws IOException {
setUpParams(customInput("pdf/xmp_metadata_missing_rdf_namespace.pdf"));
String random = Long.toString(System.currentTimeMillis());
String producer = "test_producer_" + random;
String creator = "test_creator_" + random;

parameters.put(PdfMetadataFields.CREATOR, creator);
parameters.put("Producer", producer);

parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE);
testContext.directoryOutputTo(parameters);

execute(parameters);

testContext.assertTaskCompleted();
testContext.forEachPdfOutput(document -> {
try {
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DocumentBuilder b = f.newDocumentBuilder();
Document xmlDoc = b.parse(document.getDocumentCatalog().getMetadata().createInputStream());

assertEquals(producer, getNodeValue(xmlDoc, "//*[name()='pdf:Producer']"));

} catch (Exception e) {
throw new RuntimeException(e);
}
});
}

@Test
public void updatedXmpMetadataRemoveFields() throws IOException {
setUpParams(stronglyEncryptedInput());
Expand Down
Binary file not shown.

0 comments on commit d1ab11c

Please sign in to comment.