Skip to content

Commit eeca6f4

Browse files
committed
PDFBOX-2378: avoid rdf namespace declarations getting lost in serialization
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1930017 13f79535-47bb-0310-9956-ffa450edef68
1 parent 6e4e45b commit eeca6f4

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

xmpbox/src/main/java/org/apache/xmpbox/XMPMetadata.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package org.apache.xmpbox;
2222

2323
import java.util.ArrayList;
24+
import java.util.Collections;
2425
import java.util.Iterator;
2526
import java.util.List;
2627
import java.util.Map;
@@ -63,6 +64,8 @@ public class XMPMetadata
6364

6465
private final List<XMPSchema> schemas;
6566

67+
private Map<String, String> rdfAttributeMap = Collections.emptyMap();
68+
6669
private final TypeMapping typeMapping;
6770

6871
/**
@@ -576,4 +579,23 @@ public void clearSchemas()
576579
schemas.clear();
577580
}
578581

582+
/**
583+
* Get the rdf attribute map (namespace declarations). This is used in serialization.
584+
*
585+
* @return the rdf attribute map.
586+
*/
587+
public Map<String, String> getRdfAttributeMap()
588+
{
589+
return Collections.unmodifiableMap(rdfAttributeMap);
590+
}
591+
592+
/**
593+
* Set the rdf attribute map (namespace declarations). This is used in serialization.
594+
*
595+
* @param rdfAttributeMap an rdf attribute map.
596+
*/
597+
public void setRdfAttributeMap(Map<String, String> rdfAttributeMap)
598+
{
599+
this.rdfAttributeMap = rdfAttributeMap;
600+
}
579601
}

xmpbox/src/main/java/org/apache/xmpbox/xml/DomXmpParser.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,22 @@ public XMPMetadata parse(InputStream input) throws XmpParsingException
194194
dataDescriptions.add(description);
195195
}
196196
}
197+
198+
// PDFBOX-2378: keep rdf namespace declarations for later serialization
199+
NamedNodeMap attributes = rdfRdf.getAttributes();
200+
if (attributes != null)
201+
{
202+
Map<String, String> rdfAttributeMap = new HashMap<>();
203+
for (int i = 0; i < attributes.getLength(); ++i)
204+
{
205+
Node item = attributes.item(i);
206+
if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(item.getNamespaceURI()))
207+
{
208+
rdfAttributeMap.put(item.getNodeName(), item.getNodeValue());
209+
}
210+
}
211+
xmp.setRdfAttributeMap(rdfAttributeMap);
212+
}
197213
// find schema description
198214
PdfaExtensionHelper.populateSchemaMapping(xmp);
199215
// parse data description

xmpbox/src/main/java/org/apache/xmpbox/xml/XmpSerializer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.OutputStream;
2424
import java.util.ArrayList;
2525
import java.util.List;
26+
import java.util.Map;
2627

2728
import javax.xml.XMLConstants;
2829
import javax.xml.parsers.DocumentBuilder;
@@ -96,6 +97,12 @@ public void serialize(XMPMetadata metadata, OutputStream os, boolean withXpacket
9697
{
9798
rdf.appendChild(serializeSchema(doc, schema));
9899
}
100+
// PDFBOX-2378: avoid rdf namespace declarations getting lost in serialization
101+
Map<String, String> rdfAttributeMap = metadata.getRdfAttributeMap();
102+
for (Map.Entry<String, String> entry : rdfAttributeMap.entrySet())
103+
{
104+
rdf.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, entry.getKey(), entry.getValue());
105+
}
99106
// save
100107
save(doc, os, "UTF-8");
101108
}

xmpbox/src/test/java/org/apache/xmpbox/parser/DeserializationTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,9 @@ void testRdfAboutFound() throws XmpParsingException, IOException
327327
}
328328

329329
@Test
330-
void testWihtAttributesAsProperties() throws XmpParsingException, TransformerException, NoSuchAlgorithmException, IOException
330+
void testWithAttributesAsProperties() throws XmpParsingException, TransformerException, NoSuchAlgorithmException, IOException
331331
{
332+
// also serves as a test for the changes in PDFBOX-2378
332333
try (InputStream is = DomXmpParser.class.getResourceAsStream("/validxmp/attr_as_props.xml"))
333334
{
334335
XMPMetadata metadata = xdb.parse(is);
@@ -342,7 +343,7 @@ void testWihtAttributesAsProperties() throws XmpParsingException, TransformerExc
342343
XMPBasicSchema basic = metadata.getXMPBasicSchema();
343344
assertNotNull(basic.getCreateDate());
344345

345-
checkTransform(metadata, "91466370449938102905842936306160100538543510664071400903097987792216034311743");
346+
checkTransform(metadata, "18065297971979344549773207273794555094175502580946345976611821901439849242965");
346347
}
347348
}
348349

0 commit comments

Comments
 (0)