You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2025/12/15 16:08:06 UTC

svn commit: r1930593 - in pdfbox/branches/2.0/xmpbox/src: main/java/org/apache/xmpbox/xml test/java/org/apache/xmpbox/xml

Author: tilman
Date: Mon Dec 15 16:08:06 2025
New Revision: 1930593

Log:
PDFBOX-3882: avoid NPE, move block to the correct location, correct existing NPE handling, improve test coverage

Modified:
   pdfbox/branches/2.0/xmpbox/src/main/java/org/apache/xmpbox/xml/DomXmpParser.java
   pdfbox/branches/2.0/xmpbox/src/test/java/org/apache/xmpbox/xml/DomXmpParserTest.java

Modified: pdfbox/branches/2.0/xmpbox/src/main/java/org/apache/xmpbox/xml/DomXmpParser.java
==============================================================================
--- pdfbox/branches/2.0/xmpbox/src/main/java/org/apache/xmpbox/xml/DomXmpParser.java	Mon Dec 15 15:44:03 2025	(r1930592)
+++ pdfbox/branches/2.0/xmpbox/src/main/java/org/apache/xmpbox/xml/DomXmpParser.java	Mon Dec 15 16:08:06 2025	(r1930593)
@@ -310,7 +310,6 @@ public class DomXmpParser
             PropertyType type = checkPropertyDefinition(xmp,
                     new QName(attr.getNamespaceURI(), attr.getLocalName()));
 
-            // PDFBOX-2318, PDFBOX-6106: Default to text if no type is found
             if (type == null)
             {
                 if (strictParsing)
@@ -320,6 +319,7 @@ public class DomXmpParser
                 }
                 else
                 {
+                    // PDFBOX-2318, PDFBOX-6106: Default to text if no type is found
                     type = TypeMapping.createPropertyType(Types.Text, Cardinality.Simple);
                 }
             }
@@ -1063,18 +1063,11 @@ public class DomXmpParser
                     // Instantiate abstract structured type with hint from first element
                     QName attrQName = new QName(attr.getNamespaceURI(), attr.getLocalName(), attr.getPrefix());
                     PropertyType ctype = checkPropertyDefinition(xmp, attrQName);
-                    // PDFBOX-2318, PDFBOX-6106: Default to text if no type is found
+                    // this is the type of the AbstractStructuredType, not of the element(s)
                     if (ctype == null)
                     {
-                        if (strictParsing)
-                        {
-                            throw new XmpParsingException(ErrorType.InvalidType, "No type defined for {" + attr.getNamespaceURI() + "}"
-                                    + attr.getLocalName());
-                        }
-                        else
-                        {
-                            ctype = TypeMapping.createPropertyType(Types.Text, Cardinality.Simple);
-                        }
+                        throw new XmpParsingException(ErrorType.NoType,
+                            "Property '" + attrQName.getLocalPart() + "' not defined in " + attrQName.getNamespaceURI());
                     }
                     Types tt = ctype.type();
                     ast = instanciateStructured(tm, tt, qName.getLocalPart(), attr.getNamespaceURI());
@@ -1090,6 +1083,19 @@ public class DomXmpParser
                 if (ast != null && pm != null && attr.getNamespaceURI() != null)
                 {
                     PropertyType type = pm.getPropertyType(attr.getLocalName());
+                    if (type == null)
+                    {
+                        if (strictParsing)
+                        {
+                            throw new XmpParsingException(ErrorType.InvalidType, "No type defined for {" + attr.getNamespaceURI() + "}"
+                                    + attr.getLocalName());
+                        }
+                        else
+                        {
+                            // PDFBOX-2318, PDFBOX-6106: Default to text if no type is found
+                            type = TypeMapping.createPropertyType(Types.Text, Cardinality.Simple);
+                        }
+                    }
                     AbstractSimpleProperty asp = tm.instanciateSimpleProperty(
                             attr.getNamespaceURI(), attr.getPrefix(), attr.getLocalName(),
                             attr.getValue(), type.type());

Modified: pdfbox/branches/2.0/xmpbox/src/test/java/org/apache/xmpbox/xml/DomXmpParserTest.java
==============================================================================
--- pdfbox/branches/2.0/xmpbox/src/test/java/org/apache/xmpbox/xml/DomXmpParserTest.java	Mon Dec 15 15:44:03 2025	(r1930592)
+++ pdfbox/branches/2.0/xmpbox/src/test/java/org/apache/xmpbox/xml/DomXmpParserTest.java	Mon Dec 15 16:08:06 2025	(r1930593)
@@ -555,6 +555,42 @@ public class DomXmpParserTest
         assertEquals("Important subject", subjects.get(0));
         assertEquals("Unimportant subject", subjects.get(1));
     }
+    
+    @Test
+    public void testBadAttr() throws XmpParsingException, UnsupportedEncodingException
+    {
+        String s = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
+                    "<?xpacket begin=\"\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n" +
+                    "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\">\n" +
+                    "	<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" +
+                    "           <rdf:Description xmlns:xmpTPg=\"http://ns.adobe.com/xap/1.0/t/pg/\"" +
+                    "                            xmlns:stDim=\"http://ns.adobe.com/xap/1.0/sType/Dimensions#\"" +
+                    "		                 rdf:about=\"\">\n" +
+                    "			<xmpTPg:MaxPageSize>\n" +
+                    "				<rdf:Description stDim:X=\"4\" stDim:Y=\"3\" stDim:Z=\"inch\"/>\n" +
+                    "			</xmpTPg:MaxPageSize>\n" +
+                    "		</rdf:Description>\n" +
+                    "	</rdf:RDF>\n" +
+                    "</x:xmpmeta><?xpacket end=\"r\"?>";
+        try
+        {
+            new DomXmpParser().parse(s.getBytes("utf-8"));
+            fail("XmpParsingException expected");
+        }
+        catch (XmpParsingException ex)
+        {
+            assertEquals("No type defined for {http://ns.adobe.com/xap/1.0/sType/Dimensions#}X", ex.getMessage());
+        }        
+        DomXmpParser xmpParser = new DomXmpParser();
+        xmpParser.setStrictParsing(false);
+        XMPMetadata xmp = xmpParser.parse(s.getBytes("utf-8"));
+        XMPageTextSchema pageTextSchema = xmp.getPageTextSchema();
+        DimensionsType dim = (DimensionsType) pageTextSchema.getProperty(XMPageTextSchema.MAX_PAGE_SIZE);
+        assertEquals("DimensionsType{null x null null}", dim.toString());
+        assertEquals("[X=TextType:4]", dim.getProperty("X").toString());
+        assertEquals("[Y=TextType:3]", dim.getProperty("Y").toString());
+        assertEquals("[Z=TextType:inch]", dim.getProperty("Z").toString());
+    }
 
     @Test
     public void testBadType() throws XmpParsingException, UnsupportedEncodingException