Skip to content

Commit 31d1265

Browse files
committed
PDFBOX-3882: avoid NPE, move block to the correct location, correct existing NPE handling, improve test coverage
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1930594 13f79535-47bb-0310-9956-ffa450edef68
1 parent 2f68017 commit 31d1265

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,7 @@ private void parseDescriptionRootAttr(XMPMetadata xmp, Element description, Attr
310310
ComplexPropertyContainer container = schema.getContainer();
311311
PropertyType type = checkPropertyDefinition(xmp,
312312
new QName(attr.getNamespaceURI(), attr.getLocalName()));
313-
314-
// PDFBOX-2318, PDFBOX-6106: Default to text if no type is found
313+
315314
if (type == null)
316315
{
317316
if (strictParsing)
@@ -321,6 +320,7 @@ private void parseDescriptionRootAttr(XMPMetadata xmp, Element description, Attr
321320
}
322321
else
323322
{
323+
// PDFBOX-2318, PDFBOX-6106: Default to text if no type is found
324324
type = TypeMapping.createPropertyType(Types.Text, Cardinality.Simple);
325325
}
326326
}
@@ -1058,18 +1058,11 @@ else if (XmpConstants.DEFAULT_RDF_PREFIX.equals(attr.getPrefix()))
10581058
// Instantiate abstract structured type with hint from first element
10591059
QName attrQName = new QName(attr.getNamespaceURI(), attr.getLocalName(), attr.getPrefix());
10601060
PropertyType ctype = checkPropertyDefinition(xmp, attrQName);
1061-
// PDFBOX-2318, PDFBOX-6106: Default to text if no type is found
1061+
// this is the type of the AbstractStructuredType, not of the element(s)
10621062
if (ctype == null)
10631063
{
1064-
if (strictParsing)
1065-
{
1066-
throw new XmpParsingException(ErrorType.InvalidType, "No type defined for {" + attr.getNamespaceURI() + "}"
1067-
+ attr.getLocalName());
1068-
}
1069-
else
1070-
{
1071-
ctype = TypeMapping.createPropertyType(Types.Text, Cardinality.Simple);
1072-
}
1064+
throw new XmpParsingException(ErrorType.NoType,
1065+
"Property '" + attrQName.getLocalPart() + "' not defined in " + attrQName.getNamespaceURI());
10731066
}
10741067
Types tt = ctype.type();
10751068
ast = instanciateStructured(tm, tt, qName.getLocalPart(), attr.getNamespaceURI());
@@ -1085,6 +1078,19 @@ else if (XmpConstants.DEFAULT_RDF_PREFIX.equals(attr.getPrefix()))
10851078
if (ast != null && pm != null && attr.getNamespaceURI() != null)
10861079
{
10871080
PropertyType type = pm.getPropertyType(attr.getLocalName());
1081+
if (type == null)
1082+
{
1083+
if (strictParsing)
1084+
{
1085+
throw new XmpParsingException(ErrorType.InvalidType, "No type defined for {" + attr.getNamespaceURI() + "}"
1086+
+ attr.getLocalName());
1087+
}
1088+
else
1089+
{
1090+
// PDFBOX-2318, PDFBOX-6106: Default to text if no type is found
1091+
type = TypeMapping.createPropertyType(Types.Text, Cardinality.Simple);
1092+
}
1093+
}
10881094
AbstractSimpleProperty asp = tm.instanciateSimpleProperty(
10891095
attr.getNamespaceURI(), attr.getPrefix(), attr.getLocalName(),
10901096
attr.getValue(), type.type());

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,37 @@ void testLenientBagSeqMixup() throws XmpParsingException
556556
assertEquals("Important subject", subjects.get(0));
557557
assertEquals("Unimportant subject", subjects.get(1));
558558
}
559+
560+
@Test
561+
void testBadAttr() throws XmpParsingException
562+
{
563+
String s = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
564+
"<?xpacket begin=\"\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n" +
565+
"<x:xmpmeta xmlns:x=\"adobe:ns:meta/\">\n" +
566+
" <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" +
567+
" <rdf:Description xmlns:xmpTPg=\"http://ns.adobe.com/xap/1.0/t/pg/\"" +
568+
" xmlns:stDim=\"http://ns.adobe.com/xap/1.0/sType/Dimensions#\"" +
569+
" rdf:about=\"\">\n" +
570+
" <xmpTPg:MaxPageSize>\n" +
571+
" <rdf:Description stDim:X=\"4\" stDim:Y=\"3\" stDim:Z=\"inch\"/>\n" +
572+
" </xmpTPg:MaxPageSize>\n" +
573+
" </rdf:Description>\n" +
574+
" </rdf:RDF>\n" +
575+
"</x:xmpmeta><?xpacket end=\"r\"?>";
576+
XmpParsingException ex = assertThrows(
577+
XmpParsingException.class,
578+
() -> new DomXmpParser().parse(s.getBytes(StandardCharsets.UTF_8)));
579+
assertEquals("No type defined for {http://ns.adobe.com/xap/1.0/sType/Dimensions#}X", ex.getMessage());
580+
DomXmpParser xmpParser = new DomXmpParser();
581+
xmpParser.setStrictParsing(false);
582+
XMPMetadata xmp = xmpParser.parse(s.getBytes(StandardCharsets.UTF_8));
583+
XMPPageTextSchema pageTextSchema = xmp.getPageTextSchema();
584+
DimensionsType dim = (DimensionsType) pageTextSchema.getProperty(XMPPageTextSchema.MAX_PAGE_SIZE);
585+
assertEquals("DimensionsType{null x null null}", dim.toString());
586+
assertEquals("[X=TextType:4]", dim.getProperty("X").toString());
587+
assertEquals("[Y=TextType:3]", dim.getProperty("Y").toString());
588+
assertEquals("[Z=TextType:inch]", dim.getProperty("Z").toString());
589+
}
559590

560591
@Test
561592
void testBadType() throws XmpParsingException

0 commit comments

Comments
 (0)