Skip to content

Commit b6c88b8

Browse files
committed
PDFBOX-6125: skip empty property if wrong type when in lenient mode + test
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1930682 13f79535-47bb-0310-9956-ffa450edef68
1 parent 08c6c7a commit b6c88b8

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -573,18 +573,26 @@ private void manageArray(XMPMetadata xmp, Element property, PropertyType type, C
573573
if (bagOrSeq == null)
574574
{
575575
// not an array
576-
String whatFound = "nothing";
577576
Node firstChild = property.getFirstChild();
578-
if (firstChild != null)
577+
if (!strictParsing)
579578
{
580-
whatFound = firstChild instanceof Text ? "Text" : firstChild.getClass().getName();
579+
if (firstChild == null)
580+
{
581+
// PDFBOX-6125: ignore
582+
return;
583+
}
584+
if (firstChild instanceof Text)
585+
{
586+
// PDFBOX-6125: Default to text in lenient mode
587+
// Improvement idea in the future: create an array and add the text item.
588+
manageSimpleType(xmp, property, Types.Text, container);
589+
return;
590+
}
581591
}
582-
if (!strictParsing && firstChild instanceof Text)
592+
String whatFound = "nothing";
593+
if (firstChild != null)
583594
{
584-
// Default to text in lenient mode
585-
// Improvement idea in the future: create an array and add the text item.
586-
manageSimpleType(xmp, property, Types.Text, container);
587-
return;
595+
whatFound = firstChild instanceof Text ? "Text" : firstChild.getClass().getName();
588596
}
589597
throw new XmpParsingException(ErrorType.Format, "Invalid array definition, expecting " + type.card()
590598
+ " and found "

xmpbox/src/test/java/org/apache/xmpbox/xml/DomXmpParserTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,4 +1022,44 @@ void testNonStandardURIinRDF() throws XmpParsingException, TransformerException
10221022
XMPSchema schema4 = xmp4.getSchema("http://ns.adobe.com/pdfx/1.3/");
10231023
assertEquals("[XPressPrivate=TextType:private]", schema4.getProperty("XPressPrivate").toString());
10241024
}
1025+
1026+
/**
1027+
* Test empty property where an LangAlt is expected. The property is skipped in lenient mode.
1028+
*
1029+
* @throws XmpParsingException
1030+
* @throws TransformerException
1031+
* @throws BadFieldValueException
1032+
*/
1033+
@Test
1034+
void testBadProp() throws XmpParsingException, TransformerException, BadFieldValueException
1035+
{
1036+
String s = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
1037+
"<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d' bytes='1506'?><rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:iX=\"http://ns.adobe.com/iX/1.0/\">\n" +
1038+
" <rdf:Description xmlns=\"http://purl.org/dc/elements/1.1/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" about=\"\">\n" +
1039+
" <dc:creator/>\n" +
1040+
" <dc:coverage>Cover</dc:coverage>\n" +
1041+
" </rdf:Description>\n" +
1042+
"</rdf:RDF><?xpacket end='r'?>";
1043+
final DomXmpParser xmpParser1 = new DomXmpParser();
1044+
XmpParsingException ex = assertThrows(XmpParsingException.class,
1045+
() -> xmpParser1.parse(s.getBytes(StandardCharsets.UTF_8)));
1046+
assertEquals("Invalid array definition, expecting Seq and found nothing [prefix=dc; name=creator]", ex.getMessage());
1047+
DomXmpParser xmpParser2 = new DomXmpParser();
1048+
xmpParser2.setStrictParsing(false);
1049+
XMPMetadata xmp2 = xmpParser2.parse(s.getBytes(StandardCharsets.UTF_8));
1050+
DublinCoreSchema dublinCoreSchema2 = xmp2.getDublinCoreSchema();
1051+
assertNull(dublinCoreSchema2.getCreators());
1052+
assertNull(dublinCoreSchema2.getProperty(DublinCoreSchema.CREATOR));
1053+
assertEquals("Cover", dublinCoreSchema2.getCoverage());
1054+
XmpSerializer serializer = new XmpSerializer();
1055+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
1056+
serializer.serialize(xmp2, baos, true);
1057+
DomXmpParser xmpParser3 = new DomXmpParser();
1058+
xmpParser3.setStrictParsing(false);
1059+
XMPMetadata xmp3 = xmpParser3.parse(baos.toByteArray());
1060+
DublinCoreSchema dublinCoreSchema3 = xmp3.getDublinCoreSchema();
1061+
assertNull(dublinCoreSchema3.getCreators());
1062+
assertNull(dublinCoreSchema3.getProperty(DublinCoreSchema.CREATOR));
1063+
assertEquals("Cover", dublinCoreSchema3.getCoverage());
1064+
}
10251065
}

0 commit comments

Comments
 (0)