Author: tilman
Date: Wed Nov 26 19:10:43 2025
New Revision: 1930017
Log:
PDFBOX-2378: avoid rdf namespace declarations getting lost in serialization
Modified:
pdfbox/trunk/xmpbox/src/main/java/org/apache/xmpbox/XMPMetadata.java
pdfbox/trunk/xmpbox/src/main/java/org/apache/xmpbox/xml/DomXmpParser.java
pdfbox/trunk/xmpbox/src/main/java/org/apache/xmpbox/xml/XmpSerializer.java
pdfbox/trunk/xmpbox/src/test/java/org/apache/xmpbox/parser/DeserializationTest.java
Modified: pdfbox/trunk/xmpbox/src/main/java/org/apache/xmpbox/XMPMetadata.java
==============================================================================
--- pdfbox/trunk/xmpbox/src/main/java/org/apache/xmpbox/XMPMetadata.java
Wed Nov 26 12:45:02 2025 (r1930016)
+++ pdfbox/trunk/xmpbox/src/main/java/org/apache/xmpbox/XMPMetadata.java
Wed Nov 26 19:10:43 2025 (r1930017)
@@ -21,6 +21,7 @@
package org.apache.xmpbox;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -63,6 +64,8 @@ public class XMPMetadata
private final List<XMPSchema> schemas;
+ private Map<String, String> rdfAttributeMap = Collections.emptyMap();
+
private final TypeMapping typeMapping;
/**
@@ -576,4 +579,23 @@ public class XMPMetadata
schemas.clear();
}
+ /**
+ * Get the rdf attribute map (namespace declarations). This is used in
serialization.
+ *
+ * @return the rdf attribute map.
+ */
+ public Map<String, String> getRdfAttributeMap()
+ {
+ return Collections.unmodifiableMap(rdfAttributeMap);
+ }
+
+ /**
+ * Set the rdf attribute map (namespace declarations). This is used in
serialization.
+ *
+ * @param rdfAttributeMap an rdf attribute map.
+ */
+ public void setRdfAttributeMap(Map<String, String> rdfAttributeMap)
+ {
+ this.rdfAttributeMap = rdfAttributeMap;
+ }
}
Modified:
pdfbox/trunk/xmpbox/src/main/java/org/apache/xmpbox/xml/DomXmpParser.java
==============================================================================
--- pdfbox/trunk/xmpbox/src/main/java/org/apache/xmpbox/xml/DomXmpParser.java
Wed Nov 26 12:45:02 2025 (r1930016)
+++ pdfbox/trunk/xmpbox/src/main/java/org/apache/xmpbox/xml/DomXmpParser.java
Wed Nov 26 19:10:43 2025 (r1930017)
@@ -194,6 +194,22 @@ public class DomXmpParser
dataDescriptions.add(description);
}
}
+
+ // PDFBOX-2378: keep rdf namespace declarations for later serialization
+ NamedNodeMap attributes = rdfRdf.getAttributes();
+ if (attributes != null)
+ {
+ Map<String, String> rdfAttributeMap = new HashMap<>();
+ for (int i = 0; i < attributes.getLength(); ++i)
+ {
+ Node item = attributes.item(i);
+ if
(XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(item.getNamespaceURI()))
+ {
+ rdfAttributeMap.put(item.getNodeName(),
item.getNodeValue());
+ }
+ }
+ xmp.setRdfAttributeMap(rdfAttributeMap);
+ }
// find schema description
PdfaExtensionHelper.populateSchemaMapping(xmp);
// parse data description
Modified:
pdfbox/trunk/xmpbox/src/main/java/org/apache/xmpbox/xml/XmpSerializer.java
==============================================================================
--- pdfbox/trunk/xmpbox/src/main/java/org/apache/xmpbox/xml/XmpSerializer.java
Wed Nov 26 12:45:02 2025 (r1930016)
+++ pdfbox/trunk/xmpbox/src/main/java/org/apache/xmpbox/xml/XmpSerializer.java
Wed Nov 26 19:10:43 2025 (r1930017)
@@ -23,6 +23,7 @@ package org.apache.xmpbox.xml;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
@@ -96,6 +97,12 @@ public class XmpSerializer
{
rdf.appendChild(serializeSchema(doc, schema));
}
+ // PDFBOX-2378: avoid rdf namespace declarations getting lost in
serialization
+ Map<String, String> rdfAttributeMap = metadata.getRdfAttributeMap();
+ for (Map.Entry<String, String> entry : rdfAttributeMap.entrySet())
+ {
+ rdf.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
entry.getKey(), entry.getValue());
+ }
// save
save(doc, os, "UTF-8");
}
Modified:
pdfbox/trunk/xmpbox/src/test/java/org/apache/xmpbox/parser/DeserializationTest.java
==============================================================================
---
pdfbox/trunk/xmpbox/src/test/java/org/apache/xmpbox/parser/DeserializationTest.java
Wed Nov 26 12:45:02 2025 (r1930016)
+++
pdfbox/trunk/xmpbox/src/test/java/org/apache/xmpbox/parser/DeserializationTest.java
Wed Nov 26 19:10:43 2025 (r1930017)
@@ -327,8 +327,9 @@ class DeserializationTest
}
@Test
- void testWihtAttributesAsProperties() throws XmpParsingException,
TransformerException, NoSuchAlgorithmException, IOException
+ void testWithAttributesAsProperties() throws XmpParsingException,
TransformerException, NoSuchAlgorithmException, IOException
{
+ // also serves as a test for the changes in PDFBOX-2378
try (InputStream is =
DomXmpParser.class.getResourceAsStream("/validxmp/attr_as_props.xml"))
{
XMPMetadata metadata = xdb.parse(is);
@@ -342,7 +343,7 @@ class DeserializationTest
XMPBasicSchema basic = metadata.getXMPBasicSchema();
assertNotNull(basic.getCreateDate());
- checkTransform(metadata,
"91466370449938102905842936306160100538543510664071400903097987792216034311743");
+ checkTransform(metadata,
"18065297971979344549773207273794555094175502580946345976611821901439849242965");
}
}